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);
  }
  public List<SocialActivity> getActivities(Long userId, int start, int count)
      throws SystemException {
    List<ActivitySubscription> subscriptions = null;
    // for now no activity selection is made, TODO
    subscriptions = activitySubscriptionPersistence.findByreceiverId(userId);

    if (subscriptions.size() == 0) {
      return new ArrayList<SocialActivity>();
    }

    DynamicQuery query = DynamicQueryFactoryUtil.forClass(SocialActivity.class);
    Criterion crit = null;

    for (ActivitySubscription sub : subscriptions) {
      Map<String, Number> criterion = new HashMap<String, Number>();
      criterion.put("classNameId", sub.getClassNameId());
      criterion.put("classPK", sub.getClassPK());

      if (sub.getType() != null) {
        criterion.put("type", sub.getType());
      }
      Criterion subCriterion = RestrictionsFactoryUtil.allEq(criterion);

      if (sub.getExtraData() != null && sub.getExtraData().length() > 0) {
        subCriterion =
            RestrictionsFactoryUtil.and(
                subCriterion, RestrictionsFactoryUtil.ilike("extraData", sub.getExtraData() + "%"));
      }

      if (crit == null) {
        crit = subCriterion;
      } else {
        crit = RestrictionsFactoryUtil.or(crit, subCriterion);
      }
    }
    query.add(crit).addOrder(OrderFactoryUtil.desc("createDate"));

    List<SocialActivity> activities = new ArrayList<SocialActivity>();
    List<Object> queryResults =
        SocialActivityLocalServiceUtil.dynamicQuery(query, start, start + count - 1);

    for (Object activity : queryResults) {
      activities.add((SocialActivity) activity);
    }

    return activities;
  }
  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);
  }