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.Expression;
26  import javax.persistence.criteria.Predicate;
27  import javax.persistence.criteria.Root;
28  
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  import org.springframework.stereotype.Repository;
32  
33  import com.hack23.cia.model.external.riksdagen.dokumentlista.impl.DocumentElement;
34  import com.hack23.cia.model.external.riksdagen.dokumentlista.impl.DocumentElement_;
35  import com.hack23.cia.service.data.api.DocumentElementDAO;
36  
37  /**
38   * The Class DocumentElementDAOImpl.
39   */
40  @Repository("DocumentElementDAO")
41  final class DocumentElementDAOImpl extends
42  AbstractGenericDAOImpl<DocumentElement, String> implements
43  DocumentElementDAO {
44  
45  	/** The Constant LOGGER. */
46  	private static final Logger LOGGER = LoggerFactory
47  			.getLogger(DocumentElementDAOImpl.class);
48  
49  
50  	/**
51  	 * Instantiates a new document element dao impl.
52  	 */
53  	public DocumentElementDAOImpl() {
54  		super(DocumentElement.class);
55  	}
56  
57  	@Override
58  	public boolean checkDocumentElement(final String documentId) {
59  		final CriteriaQuery<DocumentElement> criteriaQuery = getCriteriaBuilder()
60  				.createQuery(DocumentElement.class);
61  		final Root<DocumentElement> root = criteriaQuery
62  				.from(DocumentElement.class);
63  		criteriaQuery.select(root);
64  		final Predicate condition = getCriteriaBuilder().equal(
65  				root.get(DocumentElement_.id), documentId);
66  		criteriaQuery.where(condition);
67  		final TypedQuery<DocumentElement> typedQuery = getEntityManager()
68  				.createQuery(criteriaQuery);
69  		addCacheHints(typedQuery, "checkDocumentElement");
70  
71  		final List<DocumentElement> resultList = typedQuery.getResultList();
72  
73  		return !resultList.isEmpty();
74  	}
75  
76  	@Override
77  	public List<String> getAvaibleDocumentContent() {
78  		final CriteriaQuery<String> criteria = getCriteriaBuilder()
79  				.createQuery(String.class);
80  		final Root<DocumentElement> root = criteria.from(DocumentElement.class);
81  		criteria.select(root.get(DocumentElement_.id));
82  		criteria.where(getCriteriaBuilder().isNotNull(
83  				root.get(DocumentElement_.documentUrlText)));
84  		return getEntityManager().createQuery(criteria).getResultList();
85  	}
86  
87  	@Override
88  	public List<String> getAvaibleDocumentStatus() {
89  		final CriteriaQuery<String> criteria = getCriteriaBuilder()
90  				.createQuery(String.class);
91  		final Root<DocumentElement> root = criteria.from(DocumentElement.class);
92  		criteria.select(root.get(DocumentElement_.id));
93  		criteria.where(getCriteriaBuilder().isNotNull(
94  				root.get(DocumentElement_.documentStatusUrlXml)));
95  		return getEntityManager().createQuery(criteria).getResultList();
96  	}
97  
98  	@Override
99  	public List<String> getIdList() {
100 		final CriteriaQuery<String> criteria = getCriteriaBuilder()
101 				.createQuery(String.class);
102 		final Root<DocumentElement> root = criteria.from(DocumentElement.class);
103 		criteria.select(root.get(DocumentElement_.id));
104 		return getEntityManager().createQuery(criteria).getResultList();
105 	}
106 
107 	@Override
108 	public int getMissingDocumentStartFromYear(final int startCheckYear) {
109 
110 		final CriteriaQuery<String> criteria = getCriteriaBuilder()
111 				.createQuery(String.class);
112 		final Root<DocumentElement> root = criteria.from(DocumentElement.class);
113 		final Expression<String> createdYear = getCriteriaBuilder().substring(root.get(DocumentElement_.createdDate), 0,5);
114 		criteria.select(createdYear).groupBy(createdYear).orderBy(getCriteriaBuilder().asc(createdYear));
115 		final List<String> resultList = getEntityManager().createQuery(criteria).getResultList();
116 
117 		LOGGER.info("getMissingDocumentStartFromYear current years contain documents:{}",resultList);
118 
119 		if (resultList.isEmpty() || Integer.parseInt(resultList.get(0)) < startCheckYear) {
120 			return startCheckYear;
121 		} else {
122 			return Integer.parseInt(resultList.get(resultList.size()-1));
123 		}
124 	}
125 
126 }