protected List<Object> doFindF_E_ByG_F(
      long groupId, long folderId, QueryDefinition<?> queryDefinition, boolean inlineSQLHelper) {

    Session session = null;

    try {
      session = openSession();

      StringBundler sb = new StringBundler(5);

      sb.append("SELECT * FROM (");

      String sql = null;

      if (queryDefinition.getStatus() == WorkflowConstants.STATUS_ANY) {
        sql = CustomSQLUtil.get(getClass(), FIND_F_BY_G_P);
      } else {
        sql = CustomSQLUtil.get(getClass(), FIND_F_BY_G_P_S);

        sql = replaceExcludeStatus(sql, queryDefinition);
      }

      if (inlineSQLHelper) {
        sql =
            InlineSQLHelperUtil.replacePermissionCheck(
                sql, BookmarksFolder.class.getName(), "BookmarksFolder.folderId", groupId);
      }

      sb.append(sql);
      sb.append(" UNION ALL ");

      if (queryDefinition.getStatus() == WorkflowConstants.STATUS_ANY) {
        sql = CustomSQLUtil.get(getClass(), FIND_E_BY_G_F);
      } else {
        sql = CustomSQLUtil.get(getClass(), FIND_E_BY_G_F_S);

        sql = replaceExcludeStatus(sql, queryDefinition);
      }

      if (inlineSQLHelper) {
        sql =
            InlineSQLHelperUtil.replacePermissionCheck(
                sql, BookmarksEntry.class.getName(), "BookmarksEntry.fileEntryId", groupId);
      }

      sb.append(sql);
      sb.append(") TEMP_TABLE ORDER BY modelName ASC");

      sql = sb.toString();

      SQLQuery q = session.createSynchronizedSQLQuery(sql);

      q.addScalar("modelId", Type.LONG);
      q.addScalar("modelName", Type.STRING);
      q.addScalar("modelFolder", Type.LONG);

      QueryPos qPos = QueryPos.getInstance(q);

      qPos.add(groupId);
      qPos.add(folderId);

      if (queryDefinition.getStatus() != WorkflowConstants.STATUS_ANY) {
        qPos.add(queryDefinition.getStatus());
      }

      qPos.add(groupId);
      qPos.add(folderId);

      if (queryDefinition.getStatus() != WorkflowConstants.STATUS_ANY) {
        qPos.add(queryDefinition.getStatus());
      }

      List<Object> models = new ArrayList<>();

      Iterator<Object[]> itr =
          (Iterator<Object[]>)
              QueryUtil.iterate(
                  q, getDialect(), queryDefinition.getStart(), queryDefinition.getEnd());

      while (itr.hasNext()) {
        Object[] array = itr.next();

        long modelId = (Long) array[0];
        // String name = (String)array[1];
        long modelFolder = (Long) array[2];

        Object obj = null;

        if (modelFolder == 0) {
          obj = BookmarksFolderUtil.findByPrimaryKey(modelId);
        } else {
          obj = BookmarksEntryUtil.findByPrimaryKey(modelId);
        }

        models.add(obj);
      }

      return models;
    } catch (Exception e) {
      throw new SystemException(e);
    } finally {
      closeSession(session);
    }
  }
  protected List<DDMStructure> doFindByC_G_C_N_D_S_T_S(
      long companyId,
      long[] groupIds,
      long classNameId,
      String[] names,
      String[] descriptions,
      String storageType,
      int type,
      int status,
      boolean andOperator,
      int start,
      int end,
      OrderByComparator<DDMStructure> orderByComparator,
      boolean inlineSQLHelper) {

    names = CustomSQLUtil.keywords(names);
    descriptions = CustomSQLUtil.keywords(descriptions, false);

    Session session = null;

    try {
      session = openSession();

      String sql = CustomSQLUtil.get(FIND_BY_C_G_C_N_D_S_T_R);

      if (inlineSQLHelper) {
        sql =
            InlineSQLHelperUtil.replacePermissionCheck(
                sql,
                DDMStructurePermission.getStructureModelResourceName(classNameId),
                "DDMStructure.structureId",
                groupIds);
      }

      sql = StringUtil.replace(sql, "[$GROUP_ID$]", getGroupIds(groupIds));
      sql = StringUtil.replace(sql, "[$STATUS$]", getStatus(status));
      sql =
          CustomSQLUtil.replaceKeywords(
              sql, "lower(CAST_TEXT(DDMStructure.name))", StringPool.LIKE, false, names);
      sql =
          CustomSQLUtil.replaceKeywords(
              sql, "DDMStructure.description", StringPool.LIKE, true, descriptions);
      sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);

      if (orderByComparator != null) {
        sql = CustomSQLUtil.replaceOrderBy(sql, orderByComparator);
      }

      SQLQuery q = session.createSynchronizedSQLQuery(sql);

      q.addEntity("DDMStructure", DDMStructureImpl.class);

      QueryPos qPos = QueryPos.getInstance(q);

      qPos.add(companyId);

      if (groupIds != null) {
        qPos.add(groupIds);
      }

      qPos.add(classNameId);
      qPos.add(names, 2);
      qPos.add(descriptions, 2);
      qPos.add(storageType);
      qPos.add(storageType);
      qPos.add(type);

      if (status != WorkflowConstants.STATUS_ANY) {
        qPos.add(status);
      }

      return (List<DDMStructure>) QueryUtil.list(q, getDialect(), start, end);
    } catch (Exception e) {
      throw new SystemException(e);
    } finally {
      closeSession(session);
    }
  }
  protected int doCountF_E_ByG_F(
      long groupId, long folderId, QueryDefinition<?> queryDefinition, boolean inlineSQLHelper) {

    Session session = null;

    try {
      session = openSession();

      StringBundler sb = new StringBundler(5);

      sb.append(StringPool.OPEN_PARENTHESIS);

      String sql = null;

      if (queryDefinition.getStatus() == WorkflowConstants.STATUS_ANY) {
        sql = CustomSQLUtil.get(getClass(), COUNT_F_BY_G_P);
      } else {
        sql = CustomSQLUtil.get(getClass(), COUNT_F_BY_G_P_S);

        sql = replaceExcludeStatus(sql, queryDefinition);
      }

      if (inlineSQLHelper) {
        sql =
            InlineSQLHelperUtil.replacePermissionCheck(
                sql, BookmarksFolder.class.getName(), "BookmarksFolder.folderId", groupId);
      }

      sb.append(sql);
      sb.append(") UNION ALL (");

      if (queryDefinition.getStatus() == WorkflowConstants.STATUS_ANY) {
        sql = CustomSQLUtil.get(getClass(), COUNT_E_BY_G_F);
      } else {
        sql = CustomSQLUtil.get(getClass(), COUNT_E_BY_G_F_S);

        sql = replaceExcludeStatus(sql, queryDefinition);
      }

      if (inlineSQLHelper) {
        sql =
            InlineSQLHelperUtil.replacePermissionCheck(
                sql, BookmarksEntry.class.getName(), "BookmarksEntry.fileEntryId", groupId);
      }

      sb.append(sql);
      sb.append(StringPool.CLOSE_PARENTHESIS);

      sql = sb.toString();

      SQLQuery q = session.createSynchronizedSQLQuery(sql);

      q.addScalar(COUNT_COLUMN_NAME, Type.LONG);

      QueryPos qPos = QueryPos.getInstance(q);

      qPos.add(groupId);
      qPos.add(folderId);

      if (queryDefinition.getStatus() != WorkflowConstants.STATUS_ANY) {
        qPos.add(queryDefinition.getStatus());
      }

      qPos.add(groupId);
      qPos.add(folderId);

      if (queryDefinition.getStatus() != WorkflowConstants.STATUS_ANY) {
        qPos.add(queryDefinition.getStatus());
      }

      int count = 0;

      Iterator<Long> itr = q.iterate();

      while (itr.hasNext()) {
        Long l = itr.next();

        if (l != null) {
          count += l.intValue();
        }
      }

      return count;
    } catch (Exception e) {
      throw new SystemException(e);
    } finally {
      closeSession(session);
    }
  }
  protected int doCountByC_G_C_N_D_S_T_S(
      long companyId,
      long[] groupIds,
      long classNameId,
      String[] names,
      String[] descriptions,
      String storageType,
      int type,
      int status,
      boolean andOperator,
      boolean inlineSQLHelper) {

    names = CustomSQLUtil.keywords(names);
    descriptions = CustomSQLUtil.keywords(descriptions, false);

    Session session = null;

    try {
      session = openSession();

      String sql = CustomSQLUtil.get(COUNT_BY_C_G_C_N_D_S_T_S);

      if (inlineSQLHelper) {
        sql =
            InlineSQLHelperUtil.replacePermissionCheck(
                sql,
                DDMStructurePermission.getStructureModelResourceName(classNameId),
                "DDMStructure.structureId",
                groupIds);
      }

      sql = StringUtil.replace(sql, "[$GROUP_ID$]", getGroupIds(groupIds));
      sql = StringUtil.replace(sql, "[$STATUS$]", getStatus(status));
      sql =
          CustomSQLUtil.replaceKeywords(
              sql, "lower(CAST_TEXT(DDMStructure.name))", StringPool.LIKE, false, names);

      sql =
          CustomSQLUtil.replaceKeywords(
              sql, "DDMStructure.description", StringPool.LIKE, true, descriptions);

      sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);

      SQLQuery q = session.createSynchronizedSQLQuery(sql);

      q.addScalar(COUNT_COLUMN_NAME, Type.LONG);

      QueryPos qPos = QueryPos.getInstance(q);

      qPos.add(companyId);

      if (groupIds != null) {
        qPos.add(groupIds);
      }

      qPos.add(classNameId);
      qPos.add(names, 2);
      qPos.add(descriptions, 2);
      qPos.add(storageType);
      qPos.add(storageType);
      qPos.add(type);

      if (status != WorkflowConstants.STATUS_ANY) {
        qPos.add(status);
      }

      Iterator<Long> itr = q.iterate();

      if (itr.hasNext()) {
        Long count = itr.next();

        if (count != null) {
          return count.intValue();
        }
      }

      return 0;
    } catch (Exception e) {
      throw new SystemException(e);
    } finally {
      closeSession(session);
    }
  }