/**
   * Método utilizado para criar o objeto Example. Este objeto é utilizado para realizar a busca por
   * exemplo.
   *
   * @param objeto sobre o qual o Example será criado
   * @return em objeto do tipo Example
   */
  public final Example criarExemplo(T objeto) {

    Example example = Example.create(objeto);
    example.enableLike(MatchMode.ANYWHERE);
    example.excludeZeroes();
    example.ignoreCase();

    return example;
  }
Beispiel #2
0
 @SuppressWarnings("unchecked")
 public List<T> findByExample(T exampleInstance, String[] excludeProperty) {
   String[] exclude = (excludeProperty != null) ? excludeProperty : excludeDefault;
   Criteria crit = getSession().createCriteria(persistentClass);
   Example example = Example.create(exampleInstance);
   example.enableLike();
   example.ignoreCase();
   for (String ex : exclude) {
     example.excludeProperty(ex);
   }
   crit.add(example);
   return crit.list();
 }
  public PaginatedList<Keyword> findKeywords(
      Keyword keyword,
      final boolean ignoreCase,
      final Order order,
      final Integer firstResult,
      final Integer maxResults)
      throws IllegalArgumentException {
    if (keyword == null) {
      throw new IllegalArgumentException("keyword cannot be null");
    }
    final MatchMode matchMode = MatchMode.ANYWHERE;
    Example criterionKeyword = Example.create(keyword);
    criterionKeyword.enableLike(matchMode);
    if (ignoreCase) {
      criterionKeyword.ignoreCase();
    }

    // Normally, Hibernate performs a left-outer join, when searching for
    // an object with collections using Criteria. This returns a ResultSet
    // that contains duplicate objects. In order to get a unique list of
    // Keywords with paginated support, we need to a nested query to find
    // distinct matching ids, then get the Keywords. The end result is a
    // subselect in the main query, but only one query is sent.
    DetachedCriteria dc = DetachedCriteria.forClass(Keyword.class);
    dc.add(criterionKeyword);
    dc.setResultTransformer(DISTINCT_ROOT_ENTITY);

    Conjunction conjunction =
        createTranslationConditions(keyword.getTranslations(), ignoreCase, matchMode);
    if (conjunction != null) dc.createCriteria(FIELD_TRANSLATIONS).add(conjunction);
    dc.setProjection(Projections.id());

    Criteria criteria = getSession().createCriteria(Keyword.class);
    criteria.add(Subqueries.propertyIn(FIELD_ID, dc));
    if (Order.desc == order) criteria.addOrder(desc(FIELD_KEYWORD));
    else criteria.addOrder(asc(FIELD_KEYWORD));
    if (firstResult != null) criteria.setFirstResult(firstResult);
    if (maxResults != null) criteria.setMaxResults(maxResults);

    final List<Keyword> criteriaList = criteria.list();
    int maxListSize = 0;
    if (criteriaList.size() > 0) {
      maxListSize = calculateMaxListSize(criterionKeyword, conjunction);
    }

    return new PaginatedList<Keyword>(criteriaList, maxListSize);
  }
  /**
   * Create the search criteria for a {@link Translation}.
   *
   * @param translations the translation to add to the criteria
   * @param ignoreCase flag indicating if case should be ignored during search
   * @param matchMode flag indicating the type of string pattern matching
   * @return the additional search parameters for the {@link Translation} fields
   */
  private Conjunction createTranslationConditions(
      SortedSet<Translation> translations, final boolean ignoreCase, final MatchMode matchMode) {
    Conjunction conjunction = null;
    if (translations != null && !translations.isEmpty()) {
      final Translation translation = translations.first();

      Example criterionTranslation = Example.create(translation);
      criterionTranslation.enableLike(matchMode);
      if (ignoreCase) {
        criterionTranslation.ignoreCase();
      }

      conjunction = conjunction();
      conjunction.add(criterionTranslation);

      addBundleCriteria(conjunction, translation.getBundle());
      addCountryCriteria(conjunction, translation.getCountry());
      addLanguageCriteria(conjunction, translation.getLanguage());
    }

    return conjunction;
  }