1 package fr.ifremer.dali.dao.referential.pmfm;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 import com.google.common.collect.ImmutableList;
27 import com.google.common.collect.Lists;
28 import fr.ifremer.dali.config.DaliConfiguration;
29 import fr.ifremer.dali.dao.referential.DaliReferentialDao;
30 import fr.ifremer.dali.dao.referential.transcribing.DaliTranscribingItemDao;
31 import fr.ifremer.dali.dao.technical.Daos;
32 import fr.ifremer.dali.dto.DaliBeanFactory;
33 import fr.ifremer.dali.dto.referential.pmfm.MatrixDTO;
34 import fr.ifremer.quadrige3.core.dao.referential.pmfm.MatrixDaoImpl;
35 import fr.ifremer.quadrige3.core.service.technical.CacheService;
36 import org.hibernate.Query;
37 import org.hibernate.SessionFactory;
38 import org.hibernate.type.IntegerType;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.dao.DataRetrievalFailureException;
41 import org.springframework.stereotype.Repository;
42
43 import javax.annotation.Resource;
44 import java.util.Arrays;
45 import java.util.Iterator;
46 import java.util.List;
47 import java.util.Map;
48
49
50
51
52
53 @Repository("daliMatrixDao")
54 public class DaliMatrixDaoImpl extends MatrixDaoImpl implements DaliMatrixDao {
55
56 @Resource
57 protected CacheService cacheService;
58
59 @Resource
60 protected DaliConfiguration config;
61
62 @Resource(name = "daliTranscribingItemDao")
63 private DaliTranscribingItemDao transcribingItemDao;
64
65 @Resource(name = "daliFractionDao")
66 private DaliFractionDao fractionDao;
67
68 @Resource(name = "daliReferentialDao")
69 protected DaliReferentialDao referentialDao;
70
71
72
73
74
75
76 @Autowired
77 public DaliMatrixDaoImpl(SessionFactory sessionFactory) {
78 super(sessionFactory);
79 }
80
81
82 @Override
83 public List<MatrixDTO> getAllMatrices(List<String> statusCodes) {
84
85 List<MatrixDTO> result = Lists.newArrayList();
86 Map<Integer, String> transcribingNamesById = getTranscribingNames();
87
88 Iterator<Object[]> it = Daos.queryIteratorWithStatus(createQuery("allMatrices"), statusCodes);
89
90 while (it.hasNext()) {
91 Object[] source = it.next();
92 MatrixDTO matrix = toMatrixDTO(Arrays.asList(source).iterator(), transcribingNamesById);
93
94 matrix.addAllFractions(fractionDao.getFractionsByMatrixId(matrix.getId()));
95 result.add(matrix);
96 }
97
98 return ImmutableList.copyOf(result);
99 }
100
101
102 @Override
103 public MatrixDTO getMatrixById(int matrixId) {
104
105 Object[] source = queryUnique("matrixById", "matrixId", IntegerType.INSTANCE, matrixId);
106
107 if (source == null) {
108 throw new DataRetrievalFailureException("can't load matrix with id = " + matrixId);
109 }
110
111 MatrixDTO result = toMatrixDTO(Arrays.asList(source).iterator(), getTranscribingNames());
112
113
114 result.addAllFractions(fractionDao.getFractionsByMatrixId(result.getId()));
115
116 return result;
117 }
118
119
120 @Override
121 public List<MatrixDTO> findMatrices(Integer matrixId, List<String> statusCodes) {
122 List<MatrixDTO> result = Lists.newArrayList();
123 Map<Integer, String> transcribingNamesById = getTranscribingNames();
124
125 Query query = createQuery("matrixByCriteria", "matrixId", IntegerType.INSTANCE, matrixId);
126 Iterator<Object[]> it = Daos.queryIteratorWithStatus(query, statusCodes);
127
128 while (it.hasNext()) {
129 Object[] source = it.next();
130 MatrixDTO matrix = toMatrixDTO(Arrays.asList(source).iterator(), transcribingNamesById);
131
132 matrix.addAllFractions(fractionDao.getFractionsByMatrixId(matrix.getId()));
133
134 result.add(matrix);
135 }
136
137 return ImmutableList.copyOf(result);
138
139 }
140
141 private Map<Integer, String> getTranscribingNames() {
142 return transcribingItemDao.getAllTranscribingItemsById(config.getTranscribingItemTypeLbForMatrixNm());
143 }
144
145 private MatrixDTO toMatrixDTO(Iterator<Object> source, Map<Integer, String> transcribingNamesById) {
146 MatrixDTO result = DaliBeanFactory.newMatrixDTO();
147 result.setId((Integer) source.next());
148 result.setName((String) source.next());
149 result.setDescription((String) source.next());
150 result.setStatus(referentialDao.getStatusByCode((String) source.next()));
151 result.setComment((String) source.next());
152 result.setCreationDate(Daos.convertToDate(source.next()));
153 result.setUpdateDate(Daos.convertToDate(source.next()));
154
155
156 result.setName(transcribingNamesById.getOrDefault(result.getId(), result.getName()));
157
158 return result;
159 }
160
161 }