1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
38
39 @Repository("DataDAOImpl")
40 final class DataDAOImpl extends AbstractGenericDAOImpl<WorldBankData, Long> implements DataDAO {
41
42
43 private static final int EXPECTED_NR_ELEMENTS = 2;
44
45
46
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 }