LanguageContentDataDAOImpl.java

  1. /*
  2.  * Copyright 2010 James Pether Sörling
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *   http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  *
  16.  *  $Id$
  17.  *  $HeadURL$
  18. */
  19. package com.hack23.cia.service.data.impl;

  20. import java.util.List;

  21. import javax.persistence.TypedQuery;
  22. import javax.persistence.criteria.CriteriaQuery;
  23. import javax.persistence.criteria.Predicate;
  24. import javax.persistence.criteria.Root;

  25. import org.springframework.stereotype.Repository;

  26. import com.hack23.cia.model.internal.application.system.impl.LanguageContentData;
  27. import com.hack23.cia.model.internal.application.system.impl.LanguageContentData_;
  28. import com.hack23.cia.service.data.api.LanguageContentDataDAO;

  29. /**
  30.  * The Class LanguageContentDataDAOImpl.
  31.  */
  32. @Repository("LanguageContentDataDAO")
  33. final class LanguageContentDataDAOImpl extends
  34. AbstractGenericDAOImpl<LanguageContentData, Long> implements
  35. LanguageContentDataDAO {

  36.     /**
  37.      * Instantiates a new language content data dao impl.
  38.      */
  39.     public LanguageContentDataDAOImpl() {
  40.         super(LanguageContentData.class);
  41.     }

  42.     @Override
  43.     public LanguageContentData findTranslation(final String key,
  44.             final String fromLanguage, final String toLanguage) {

  45.         final CriteriaQuery<LanguageContentData> criteriaQuery = getCriteriaBuilder()
  46.                 .createQuery(getPersistentClass());
  47.         final Root<LanguageContentData> root = criteriaQuery
  48.                 .from(getPersistentClass());
  49.         criteriaQuery.select(root);
  50.         final Predicate keyCondition = getCriteriaBuilder().equal(root.get(LanguageContentData_.refKey),
  51.                 key);
  52.         final Predicate fromCondition = getCriteriaBuilder().equal(
  53.                 root.get(LanguageContentData_.fromLanguage),
  54.                 fromLanguage);
  55.         final Predicate toCondition = getCriteriaBuilder().equal(root.get(LanguageContentData_.toLanguage),
  56.                 toLanguage);

  57.         final Predicate and = getCriteriaBuilder().and(keyCondition, fromCondition, toCondition);

  58.         criteriaQuery.where(and);

  59.         final TypedQuery<LanguageContentData> typedQuery = getEntityManager()
  60.                 .createQuery(criteriaQuery);
  61.         addCacheHints(typedQuery, "findTranslation");

  62.         final List<LanguageContentData> resultList = typedQuery.getResultList();

  63.         if (resultList.isEmpty()) {
  64.             return null;
  65.         } else {
  66.             return resultList.get(0);
  67.         }

  68.     }

  69.     /**
  70.      * Gets the id list.
  71.      *
  72.      * @return the id list
  73.      */
  74.     private List<Long> getIdList() {
  75.         final CriteriaQuery<Long> criteria = getCriteriaBuilder().createQuery(
  76.                 Long.class);
  77.         final Root<LanguageContentData> root = criteria
  78.                 .from(LanguageContentData.class);
  79.         criteria.select(root.get(LanguageContentData_.hjid));
  80.         return getEntityManager().createQuery(criteria).getResultList();
  81.     }

  82. }