protected List<ResourceTypePermission> getResourceTypePermissions(
      PortletDataContext portletDataContext, Role importedRole) throws SystemException {

    DynamicQuery dynamicQuery = ResourceTypePermissionLocalServiceUtil.dynamicQuery();

    Property companyIdProperty = PropertyFactoryUtil.forName("companyId");

    dynamicQuery.add(companyIdProperty.eq(portletDataContext.getCompanyId()));

    Junction junction = RestrictionsFactoryUtil.disjunction();

    long[] permissibleGroupIds = {
      GroupConstants.DEFAULT_PARENT_GROUP_ID,
      portletDataContext.getCompanyId(),
      portletDataContext.getCompanyGroupId(),
      portletDataContext.getUserPersonalSiteGroupId()
    };

    for (long permissibleGroupId : permissibleGroupIds) {
      Property property = PropertyFactoryUtil.forName("groupId");

      junction.add(property.eq(permissibleGroupId));
    }

    dynamicQuery.add(junction);

    Property roleIdProperty = PropertyFactoryUtil.forName("roleId");

    dynamicQuery.add(roleIdProperty.eq(importedRole.getRoleId()));

    return ResourceTypePermissionLocalServiceUtil.dynamicQuery(dynamicQuery);
  }
  protected DynamicQuery buildDynamicQuery(
      long groupId,
      String title,
      String content,
      int status,
      Date startDate,
      Date endDate,
      boolean andOperator) {

    Junction junction = null;

    if (andOperator) {
      junction = RestrictionsFactoryUtil.conjunction();
    } else {
      junction = RestrictionsFactoryUtil.disjunction();
    }

    Map<String, String> terms = new HashMap<String, String>();

    if (Validator.isNotNull(title)) {
      terms.put("title", title);
    }

    if (Validator.isNotNull(content)) {
      terms.put("content", content);
    }

    for (Map.Entry<String, String> entry : terms.entrySet()) {
      String key = entry.getKey();
      String value = entry.getValue();

      Disjunction disjunction = RestrictionsFactoryUtil.disjunction();

      for (String keyword : KnowledgeBaseUtil.parseKeywords(value)) {
        Criterion criterion =
            RestrictionsFactoryUtil.ilike(key, StringUtil.quote(keyword, StringPool.PERCENT));

        disjunction.add(criterion);
      }

      junction.add(disjunction);
    }

    if (status != WorkflowConstants.STATUS_ANY) {
      Property property = PropertyFactoryUtil.forName("status");

      junction.add(property.eq(status));
    }

    if ((endDate != null) && (startDate != null)) {
      Disjunction disjunction = RestrictionsFactoryUtil.disjunction();

      String[] propertyNames = {"createDate", "modifiedDate"};

      for (String propertyName : propertyNames) {
        Property property = PropertyFactoryUtil.forName(propertyName);

        Conjunction conjunction = RestrictionsFactoryUtil.conjunction();

        conjunction.add(property.gt(startDate));
        conjunction.add(property.lt(endDate));

        disjunction.add(conjunction);
      }

      junction.add(disjunction);
    }

    DynamicQuery dynamicQuery =
        DynamicQueryFactoryUtil.forClass(KBArticle.class, getClass().getClassLoader());

    if (status == WorkflowConstants.STATUS_ANY) {
      Property property = PropertyFactoryUtil.forName("latest");

      dynamicQuery.add(property.eq(Boolean.TRUE));
    } else if (status == WorkflowConstants.STATUS_APPROVED) {
      Property property = PropertyFactoryUtil.forName("main");

      dynamicQuery.add(property.eq(Boolean.TRUE));
    }

    if (groupId > 0) {
      Property property = PropertyFactoryUtil.forName("groupId");

      dynamicQuery.add(property.eq(groupId));
    }

    return dynamicQuery.add(junction);
  }
  protected DynamicQuery buildDynamicQuery(
      Long userId,
      String assetType,
      String nodeName,
      String kaleoDefinitionName,
      Boolean completed,
      ServiceContext serviceContext) {

    DynamicQuery dynamicQuery =
        DynamicQueryFactoryUtil.forClass(KaleoInstance.class, getClassLoader());

    Property companyIdProperty = PropertyFactoryUtil.forName("companyId");

    dynamicQuery.add(companyIdProperty.eq(serviceContext.getCompanyId()));

    if (Validator.isNotNull(userId)) {
      Property userIdProperty = PropertyFactoryUtil.forName("userId");

      dynamicQuery.add(userIdProperty.eq(userId));
    }

    if (completed != null) {
      if (completed) {
        Property completionDateProperty = PropertyFactoryUtil.forName("completionDate");

        dynamicQuery.add(completionDateProperty.isNotNull());
      } else {
        Property completionDateProperty = PropertyFactoryUtil.forName("completionDate");

        dynamicQuery.add(completionDateProperty.isNull());
      }
    }

    Junction junction = RestrictionsFactoryUtil.disjunction();

    if (Validator.isNotNull(assetType)) {
      Property classNameProperty = PropertyFactoryUtil.forName("className");

      junction.add(classNameProperty.like(assetType));
    }

    if (Validator.isNotNull(kaleoDefinitionName)) {
      Property kaleoDefinitionNameProperty = PropertyFactoryUtil.forName("kaleoDefinitionName");

      junction.add(kaleoDefinitionNameProperty.eq(kaleoDefinitionName));
    }

    if (Validator.isNotNull(nodeName)) {
      Property kaleoInstanceIdProperty = PropertyFactoryUtil.forName("kaleoInstanceId");

      DynamicQuery subdynamicQuery =
          DynamicQueryFactoryUtil.forClass(KaleoInstanceToken.class, getClassLoader())
              .setProjection(kaleoInstanceIdProperty);

      Property currentKaleoNodeNameProperty = PropertyFactoryUtil.forName("currentKaleoNodeName");

      subdynamicQuery.add(currentKaleoNodeNameProperty.like(nodeName));

      junction.add(kaleoInstanceIdProperty.in(subdynamicQuery));
    }

    dynamicQuery.add(junction);

    return dynamicQuery;
  }