View Javadoc
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  
21  import java.util.List;
22  
23  import javax.persistence.TypedQuery;
24  import javax.persistence.criteria.CriteriaQuery;
25  import javax.persistence.criteria.Predicate;
26  import javax.persistence.criteria.Root;
27  
28  import org.springframework.stereotype.Repository;
29  
30  import com.hack23.cia.model.internal.application.system.impl.LanguageContentData;
31  import com.hack23.cia.model.internal.application.system.impl.LanguageContentData_;
32  import com.hack23.cia.service.data.api.LanguageContentDataDAO;
33  
34  /**
35   * The Class LanguageContentDataDAOImpl.
36   */
37  @Repository("LanguageContentDataDAO")
38  final class LanguageContentDataDAOImpl extends
39  AbstractGenericDAOImpl<LanguageContentData, Long> implements
40  LanguageContentDataDAO {
41  
42  	/**
43  	 * Instantiates a new language content data dao impl.
44  	 */
45  	public LanguageContentDataDAOImpl() {
46  		super(LanguageContentData.class);
47  	}
48  
49  	@Override
50  	public LanguageContentData findTranslation(final String key,
51  			final String fromLanguage, final String toLanguage) {
52  
53  		final CriteriaQuery<LanguageContentData> criteriaQuery = getCriteriaBuilder()
54  				.createQuery(getPersistentClass());
55  		final Root<LanguageContentData> root = criteriaQuery
56  				.from(getPersistentClass());
57  		criteriaQuery.select(root);
58  		final Predicate keyCondition = getCriteriaBuilder().equal(root.get(LanguageContentData_.refKey),
59  				key);
60  		final Predicate fromCondition = getCriteriaBuilder().equal(
61  				root.get(LanguageContentData_.fromLanguage),
62  				fromLanguage);
63  		final Predicate toCondition = getCriteriaBuilder().equal(root.get(LanguageContentData_.toLanguage),
64  				toLanguage);
65  
66  		final Predicate and = getCriteriaBuilder().and(keyCondition, fromCondition, toCondition);
67  
68  		criteriaQuery.where(and);
69  
70  		final TypedQuery<LanguageContentData> typedQuery = getEntityManager()
71  				.createQuery(criteriaQuery);
72  		addCacheHints(typedQuery, "findTranslation");
73  
74  		final List<LanguageContentData> resultList = typedQuery.getResultList();
75  
76  		if (resultList.isEmpty()) {
77  			return null;
78  		} else {
79  			return resultList.get(0);
80  		}
81  
82  	}
83  
84  	/**
85  	 * Gets the id list.
86  	 *
87  	 * @return the id list
88  	 */
89  	private List<Long> getIdList() {
90  		final CriteriaQuery<Long> criteria = getCriteriaBuilder().createQuery(
91  				Long.class);
92  		final Root<LanguageContentData> root = criteria
93  				.from(LanguageContentData.class);
94  		criteria.select(root.get(LanguageContentData_.hjid));
95  		return getEntityManager().createQuery(criteria).getResultList();
96  	}
97  
98  }