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.ArrayList;
22  import java.util.List;
23  
24  import javax.persistence.criteria.CriteriaQuery;
25  import javax.persistence.criteria.Path;
26  import javax.persistence.criteria.Root;
27  
28  import org.springframework.stereotype.Repository;
29  
30  import com.hack23.cia.model.external.worldbank.data.impl.Country;
31  import com.hack23.cia.model.external.worldbank.data.impl.Indicator;
32  import com.hack23.cia.model.external.worldbank.data.impl.WorldBankData;
33  import com.hack23.cia.model.external.worldbank.data.impl.WorldBankData_;
34  import com.hack23.cia.service.data.api.DataDAO;
35  
36  /**
37   * The Class DataDAOImpl.
38   */
39  @Repository("DataDAOImpl")
40  final class DataDAOImpl extends AbstractGenericDAOImpl<WorldBankData, Long> implements DataDAO {
41  
42  	/** The Constant EXPECTED_NR_ELEMENTS. */
43  	private static final int EXPECTED_NR_ELEMENTS = 2;
44  
45  	/**
46  	 * Instantiates a new data dao impl.
47  	 */
48  	public DataDAOImpl() {
49  		super(WorldBankData.class);
50  	}
51  
52  	@Override
53  	public List<String> getIdList() {
54  		final CriteriaQuery<Object[]> criteria = getCriteriaBuilder().createQuery(Object[].class);
55  		final Root<WorldBankData> personRoot = criteria.from(WorldBankData.class);
56  		final Path<Country> countryPath = personRoot.get(WorldBankData_.country);
57  		final Path<Indicator> indicatorPath = personRoot.get(WorldBankData_.indicator);
58  		criteria.select(getCriteriaBuilder().array(countryPath, indicatorPath));
59  		criteria.distinct(true);
60  		final List<Object[]> valueArray = getEntityManager().createQuery(criteria).getResultList();
61  		final List<String> resultList = new ArrayList<>();
62  
63  		for (final Object[] objects : valueArray) {
64  			if (objects.length == EXPECTED_NR_ELEMENTS) {
65  				final StringBuilder stringBuilder = new StringBuilder().append(((Country) objects[0]).getId())
66  						.append('.').append(((Indicator) objects[1]).getId());
67  				resultList.add(stringBuilder.toString());
68  			}
69  		}
70  		return resultList;
71  	}
72  
73  }