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);
  }
  @Override
  public Date getLastPostDateByUserId(long groupId, long userId) {
    DynamicQuery dynamicQuery =
        DynamicQueryFactoryUtil.forClass(
            MBThread.class, MBStatsUserImpl.TABLE_NAME, ClassLoaderUtil.getPortalClassLoader());

    Projection projection = ProjectionFactoryUtil.max("lastPostDate");

    dynamicQuery.setProjection(projection);

    Property property = PropertyFactoryUtil.forName("threadId");

    Disjunction disjunction = RestrictionsFactoryUtil.disjunction();

    QueryDefinition queryDefinition = new QueryDefinition(WorkflowConstants.STATUS_IN_TRASH);

    List<MBThread> threads = mbThreadLocalService.getGroupThreads(groupId, queryDefinition);

    for (MBThread thread : threads) {
      disjunction.add(property.ne(thread.getThreadId()));
    }

    dynamicQuery.add(disjunction);

    List<Date> results = mbStatsUserLocalService.dynamicQuery(dynamicQuery);

    return results.get(0);
  }
  protected Criterion getAssetClassPKs(Long[] assetClassPKs) {
    Disjunction disjunction = RestrictionsFactoryUtil.disjunction();

    for (Long assetClassPK : assetClassPKs) {
      Property classPKProperty = PropertyFactoryUtil.forName("classPK");

      disjunction.add(classPKProperty.eq(assetClassPK));
    }

    return disjunction;
  }
  protected Criterion getAssetClassNames(String[] assetClassNames) {
    Disjunction disjunction = RestrictionsFactoryUtil.disjunction();

    for (String assetClassName : assetClassNames) {
      Property classNameProperty = PropertyFactoryUtil.forName("className");

      disjunction.add(classNameProperty.like(assetClassName));
    }

    return disjunction;
  }
  public int populateMessages(
      List<Message> messages,
      long folderId,
      String keywords,
      int pageNumber,
      int messagesPerPage,
      String orderByField,
      String orderByType)
      throws SystemException {

    DynamicQuery countDynamicQuery =
        DynamicQueryFactoryUtil.forClass(Message.class, getClassLoader());

    countDynamicQuery.add(RestrictionsFactoryUtil.eq("folderId", folderId));

    DynamicQuery messageDynamicQuery =
        DynamicQueryFactoryUtil.forClass(Message.class, getClassLoader());

    messageDynamicQuery.add(RestrictionsFactoryUtil.eq("folderId", folderId));

    if (Validator.isNotNull(keywords)) {
      String value = "%" + keywords + "%";

      Disjunction disjunction = RestrictionsFactoryUtil.disjunction();

      disjunction.add(RestrictionsFactoryUtil.ilike("subject", value));
      disjunction.add(RestrictionsFactoryUtil.ilike("body", value));

      countDynamicQuery.add(disjunction);

      messageDynamicQuery.add(disjunction);
    }

    if (orderByType.equals("desc")) {
      messageDynamicQuery.addOrder(OrderFactoryUtil.desc(orderByField));
    } else {
      messageDynamicQuery.addOrder(OrderFactoryUtil.asc(orderByField));
    }

    int start = messagesPerPage * (pageNumber - 1);
    int end = messagesPerPage * pageNumber;

    messages.addAll(messagePersistence.findWithDynamicQuery(messageDynamicQuery, start, end));

    return (int) dynamicQueryCount(countDynamicQuery);
  }
  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;
  }