Пример #1
0
 /**
  * Adds an <code>Expression</code> to a <code>Criteria</code>.
  *
  * @param criteria
  * @param parameterName
  * @param parameterValue
  * @param comparatorID
  * @param matchMode
  */
 private void addExpression(
     org.hibernate.Criteria criteria,
     String parameterName,
     Object parameterValue,
     int comparatorID,
     org.hibernate.criterion.MatchMode matchMode) {
   if (parameterValue != null) {
     switch (comparatorID) {
       case CriteriaSearchParameter.LIKE_COMPARATOR:
         {
           if ((matchMode != null) && (parameterValue instanceof String)) {
             criteria.add(
                 org.hibernate.criterion.Expression.like(
                     parameterName, (String) parameterValue, matchMode));
           } else {
             criteria.add(org.hibernate.criterion.Expression.like(parameterName, parameterValue));
           }
           break;
         }
       case CriteriaSearchParameter.INSENSITIVE_LIKE_COMPARATOR:
         {
           if ((matchMode != null) && (parameterValue instanceof String)) {
             criteria.add(
                 org.hibernate.criterion.Expression.ilike(
                     parameterName, (String) parameterValue, matchMode));
           } else {
             criteria.add(org.hibernate.criterion.Expression.ilike(parameterName, parameterValue));
           }
           break;
         }
       case CriteriaSearchParameter.EQUAL_COMPARATOR:
         {
           criteria.add(org.hibernate.criterion.Expression.eq(parameterName, parameterValue));
           break;
         }
       case CriteriaSearchParameter.GREATER_THAN_OR_EQUAL_COMPARATOR:
         {
           criteria.add(org.hibernate.criterion.Expression.ge(parameterName, parameterValue));
           break;
         }
       case CriteriaSearchParameter.GREATER_THAN_COMPARATOR:
         {
           criteria.add(org.hibernate.criterion.Expression.gt(parameterName, parameterValue));
           break;
         }
       case CriteriaSearchParameter.LESS_THAN_OR_EQUAL_COMPARATOR:
         {
           criteria.add(org.hibernate.criterion.Expression.le(parameterName, parameterValue));
           break;
         }
       case CriteriaSearchParameter.LESS_THAN_COMPARATOR:
         {
           criteria.add(org.hibernate.criterion.Expression.lt(parameterName, parameterValue));
           break;
         }
     }
   } else {
     criteria.add(org.hibernate.criterion.Expression.isNull(parameterName));
   }
 }
  public static void main(String[] args) throws Exception {
    Criteria c = HibernateUtil.currentSession().createCriteria(GuiaSimples.class);
    c.add(Expression.ge("dataAtendimento", Utils.parse("21/02/2008")));
    c.add(Expression.le("dataAtendimento", Utils.parse("20/07/2008")));
    c.add(Expression.in("situacao.descricao", situacoes));

    alimentarMapa(c.list());

    processarArquivo();
  }
 @SuppressWarnings("unchecked")
 public List<GraffitiDTO> getByWriteDate(Date writeDate) throws DataAccessException {
   Criteria c =
       getCurrentSession()
           .createCriteria(Graffiti.class)
           .createAlias("member", "member")
           .add(Expression.ge("writeDate", writeDate))
           .addOrder(Order.asc("writeDate"))
           .setProjection(
               Projections.projectionList()
                   .add(Projections.property("id").as("id"))
                   .add(Projections.property("member.name").as("writerName"))
                   .add(Projections.property("member.avatar").as("writerAvatar"))
                   .add(Projections.property("contents").as("contents"))
                   .add(Projections.property("writeDate").as("writtenDate")))
           .setResultTransformer(new AliasToBeanResultTransformer(GraffitiDTO.class));
   return c.list();
 }
  public static java.util.List getListOfObjectByDate(
      Class classBean,
      String strKey,
      Object value,
      String strKeyDt,
      Date dt,
      boolean blBiggerOrLess,
      String strDbName) {

    List result = null;
    Session session = null;
    Transaction tx = null;

    try {

      if (strKey != null) {

        session = getSessionDude(strDbName); // sessionFactory.getCurrentSession();
        tx = session.beginTransaction();

        Criteria criteria = session.createCriteria(classBean);
        criteria.add(Restrictions.eq(strKey, value));
        criteria.add(Expression.ge(strKeyDt, dt));
        criteria.addOrder(Order.desc(strKey));
        tx.commit();

        result = criteria.list();
      }

    } finally {
      if (session != null && session.isOpen()) {
        session.close();
      }
    }

    return result;
  }
Пример #5
0
 /**
  * Adds an <code>Expression</code> to a <code>Criteria</code>. The given <code>parameterValues
  * </code> represents either an array of <code>String</code> or another object. The different
  * values in the array are added to a disjunction which is connected with logical and to the other
  * criteria of the search.
  *
  * @param criteria
  * @param parameterName
  * @param parameterValues
  * @param searchIfNull
  * @param comparatorID
  * @param matchMode
  */
 private void addExpression(
     org.hibernate.Criteria criteria,
     String parameterName,
     Object[] parameterValues,
     int comparatorID,
     org.hibernate.criterion.MatchMode matchMode) {
   if (parameterValues != null) {
     org.hibernate.criterion.Disjunction disjunction =
         org.hibernate.criterion.Expression.disjunction();
     switch (comparatorID) {
       case CriteriaSearchParameter.LIKE_COMPARATOR:
         {
           if ((matchMode != null) && (parameterValues instanceof String[])) {
             String[] stringParameterValues = (String[]) parameterValues;
             for (int index = 0; index < parameterValues.length; index++) {
               if (stringParameterValues[index] != null) {
                 disjunction.add(
                     org.hibernate.criterion.Expression.like(
                         parameterName, stringParameterValues[index], matchMode));
               } else {
                 disjunction.add(org.hibernate.criterion.Expression.isNull(parameterName));
               }
             }
           } else {
             for (int index = 0; index < parameterValues.length; index++) {
               if (parameterValues[index] != null) {
                 disjunction.add(
                     org.hibernate.criterion.Expression.like(
                         parameterName, parameterValues[index]));
               } else {
                 disjunction.add(org.hibernate.criterion.Expression.isNull(parameterName));
               }
             }
           }
           break;
         }
       case CriteriaSearchParameter.INSENSITIVE_LIKE_COMPARATOR:
         {
           if ((matchMode != null) && (parameterValues instanceof String[])) {
             String[] stringParameterValues = (String[]) parameterValues;
             for (int index = 0; index < parameterValues.length; index++) {
               if (stringParameterValues[index] != null) {
                 disjunction.add(
                     org.hibernate.criterion.Expression.ilike(
                         parameterName, stringParameterValues[index], matchMode));
               } else {
                 disjunction.add(org.hibernate.criterion.Expression.isNull(parameterName));
               }
             }
           } else {
             for (int index = 0; index < parameterValues.length; index++) {
               if (parameterValues[index] != null) {
                 disjunction.add(
                     org.hibernate.criterion.Expression.ilike(
                         parameterName, parameterValues[index]));
               } else {
                 disjunction.add(org.hibernate.criterion.Expression.isNull(parameterName));
               }
             }
           }
           break;
         }
       case CriteriaSearchParameter.EQUAL_COMPARATOR:
         {
           for (int index = 0; index < parameterValues.length; index++) {
             if (parameterValues[index] != null) {
               disjunction.add(
                   org.hibernate.criterion.Expression.eq(parameterName, parameterValues[index]));
             } else {
               disjunction.add(org.hibernate.criterion.Expression.isNull(parameterName));
             }
           }
           break;
         }
       case CriteriaSearchParameter.GREATER_THAN_OR_EQUAL_COMPARATOR:
         {
           for (int index = 0; index < parameterValues.length; index++) {
             if (parameterValues[index] != null) {
               disjunction.add(
                   org.hibernate.criterion.Expression.ge(parameterName, parameterValues[index]));
             } else {
               disjunction.add(org.hibernate.criterion.Expression.isNull(parameterName));
             }
           }
           break;
         }
       case CriteriaSearchParameter.GREATER_THAN_COMPARATOR:
         {
           for (int index = 0; index < parameterValues.length; index++) {
             if (parameterValues[index] != null) {
               disjunction.add(
                   org.hibernate.criterion.Expression.gt(parameterName, parameterValues[index]));
             } else {
               disjunction.add(org.hibernate.criterion.Expression.isNull(parameterName));
             }
           }
           break;
         }
       case CriteriaSearchParameter.LESS_THAN_OR_EQUAL_COMPARATOR:
         {
           for (int index = 0; index < parameterValues.length; index++) {
             if (parameterValues[index] != null) {
               disjunction.add(
                   org.hibernate.criterion.Expression.le(parameterName, parameterValues[index]));
             } else {
               disjunction.add(org.hibernate.criterion.Expression.isNull(parameterName));
             }
           }
           break;
         }
       case CriteriaSearchParameter.LESS_THAN_COMPARATOR:
         {
           for (int index = 0; index < parameterValues.length; index++) {
             if (parameterValues[index] != null) {
               disjunction.add(
                   org.hibernate.criterion.Expression.lt(parameterName, parameterValues[index]));
             } else {
               disjunction.add(org.hibernate.criterion.Expression.isNull(parameterName));
             }
           }
           break;
         }
     }
     criteria.add(disjunction);
   } else {
     criteria.add(org.hibernate.criterion.Expression.isNull(parameterName));
   }
 }
  public List getComments(
      WebsiteData website,
      WeblogEntryData entry,
      String searchString,
      Date startDate,
      Date endDate,
      Boolean pending,
      Boolean approved,
      Boolean spam,
      boolean reverseChrono,
      int offset,
      int length)
      throws RollerException {

    try {
      Session session = ((HibernatePersistenceStrategy) this.strategy).getSession();
      Criteria criteria = session.createCriteria(CommentData.class);

      if (entry != null) {
        criteria.add(Expression.eq("weblogEntry", entry));
      } else if (website != null) {
        criteria.createAlias("weblogEntry", "e");
        criteria.add(Expression.eq("e.website", website));
      }

      if (searchString != null) {
        criteria.add(
            Expression.disjunction()
                .add(Expression.like("url", searchString, MatchMode.ANYWHERE))
                .add(Expression.like("content", searchString, MatchMode.ANYWHERE)));
      }

      if (startDate != null) {
        criteria.add(Expression.ge("postTime", startDate));
      }

      if (endDate != null) {
        criteria.add(Expression.le("postTime", endDate));
      }

      if (pending != null) {
        criteria.add(Expression.eq("pending", pending));
      }

      if (approved != null) {
        criteria.add(Expression.eq("approved", approved));
      }

      if (spam != null) {
        criteria.add(Expression.eq("spam", spam));
      }

      if (length != -1) {
        criteria.setMaxResults(offset + length);
      }

      if (reverseChrono) {
        criteria.addOrder(Order.desc("postTime"));
      } else {
        criteria.addOrder(Order.asc("postTime"));
      }

      List comments = criteria.list();
      if (offset == 0 || comments.size() < offset) {
        return comments;
      }
      List range = new ArrayList();
      for (int i = offset; i < comments.size(); i++) {
        range.add(comments.get(i));
      }
      return range;

    } catch (HibernateException e) {
      log.error(e);
      throw new RollerException(e);
    }
  }
  public List getWeblogEntries(
      WebsiteData website,
      UserData user,
      Date startDate,
      Date endDate,
      String catName,
      String status,
      String sortby,
      String locale,
      int offset,
      int length)
      throws RollerException {

    WeblogCategoryData cat = null;
    if (StringUtils.isNotEmpty(catName) && website != null) {
      cat = getWeblogCategoryByPath(website, catName);
      if (cat == null) catName = null;
    }
    if (catName != null && catName.trim().equals("/")) {
      catName = null;
    }

    try {
      Session session = ((HibernatePersistenceStrategy) this.strategy).getSession();
      Criteria criteria = session.createCriteria(WeblogEntryData.class);

      if (website != null) {
        criteria.add(Expression.eq("website", website));
      } else {
        criteria.createAlias("website", "w");
        criteria.add(Expression.eq("w.enabled", Boolean.TRUE));
      }

      if (user != null) {
        criteria.add(Expression.eq("creator", user));
      }

      if (startDate != null) {
        criteria.add(Expression.ge("pubTime", startDate));
      }

      if (endDate != null) {
        criteria.add(Expression.le("pubTime", endDate));
      }

      if (cat != null && website != null) {
        criteria.add(Expression.eq("category", cat));
      }

      if (status != null) {
        criteria.add(Expression.eq("status", status));
      }

      if (locale != null) {
        criteria.add(Expression.ilike("locale", locale, MatchMode.START));
      }

      if (sortby != null && sortby.equals("updateTime")) {
        criteria.addOrder(Order.desc("updateTime"));
      } else {
        criteria.addOrder(Order.desc("pubTime"));
      }

      if (offset != 0) {
        criteria.setFirstResult(offset);
      }
      if (length != -1) {
        criteria.setMaxResults(length);
      }
      return criteria.list();

    } catch (HibernateException e) {
      log.error(e);
      throw new RollerException(e);
    }
  }