View Javadoc
1   package fr.ifremer.dali.dao.referential;
2   
3   /*
4    * #%L
5    * Dali :: Core
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2014 - 2015 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
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.pmfm.DaliPmfmDao;
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.*;
34  import fr.ifremer.dali.service.StatusFilter;
35  import fr.ifremer.quadrige3.core.dao.referential.StatusCode;
36  import fr.ifremer.quadrige3.core.dao.technical.Assert;
37  import fr.ifremer.quadrige3.core.dao.technical.hibernate.HibernateDaoSupport;
38  import fr.ifremer.quadrige3.core.service.technical.CacheService;
39  import fr.ifremer.quadrige3.ui.core.dto.QuadrigeBeanFactory;
40  import fr.ifremer.quadrige3.ui.core.dto.referential.StatusDTO;
41  import org.hibernate.SessionFactory;
42  import org.hibernate.type.IntegerType;
43  import org.hibernate.type.StringType;
44  import org.springframework.beans.factory.annotation.Autowired;
45  import org.springframework.cache.Cache;
46  import org.springframework.dao.DataRetrievalFailureException;
47  import org.springframework.stereotype.Repository;
48  
49  import javax.annotation.Resource;
50  import java.util.*;
51  import java.util.stream.Collectors;
52  
53  /**
54   * <p>DaliReferentialDaoImpl class.</p>
55   *
56   */
57  @Repository("daliReferentialDao")
58  public class DaliReferentialDaoImpl extends HibernateDaoSupport implements DaliReferentialDao {
59  
60      /**
61       * Logger.
62       */
63  //    private static final Log log = LogFactory.getLog(DaliReferentialDaoImpl.class);
64  
65      @Resource
66      protected CacheService cacheService;
67  
68      @Resource
69      protected DaliConfiguration config;
70  
71      @Resource(name = "daliTranscribingItemDao")
72      private DaliTranscribingItemDao transcribingItemDao;
73  
74      @Resource(name = "daliPmfmDao")
75      protected DaliPmfmDao pmfmDao;
76  
77      @Resource(name = "daliReferentialDao")
78      protected DaliReferentialDao loopbackDao;
79  
80      /**
81       * <p>Constructor for DaliReferentialDaoImpl.</p>
82       *
83       * @param sessionFactory a {@link org.hibernate.SessionFactory} object.
84       */
85      @Autowired
86      public DaliReferentialDaoImpl(SessionFactory sessionFactory) {
87          super();
88          setSessionFactory(sessionFactory);
89      }
90  
91      @Override
92      public List<StatusDTO> getAllStatus() {
93          Cache cacheByCode = cacheService.getCache(STATUS_BY_CODE_CACHE);
94          List<StatusDTO> result = new ArrayList<>();
95          Iterator<Object[]> it = queryIterator("allStatus");
96          Map<String, String> transcribingNames = getTranscribingStatusNamesByCode();
97          while (it.hasNext()) {
98              Object[] source = it.next();
99              StatusDTO status = toStatusDTO(Arrays.asList(source).iterator(), transcribingNames);
100             result.add(status);
101 
102             cacheByCode.put(status.getCode(), status);
103         }
104         return result;
105     }
106 
107     @Override
108     public StatusDTO getStatusByCode(String statusCode) {
109         Object[] source = queryUnique("statusByCode", "statusCd", StringType.INSTANCE, statusCode);
110         if (source == null) {
111             throw new DataRetrievalFailureException("can't load status with code = " + statusCode);
112         }
113         return toStatusDTO(Arrays.asList(source).iterator(), getTranscribingStatusNamesByCode());
114     }
115 
116     /** {@inheritDoc} */
117     @Override
118     public List<StatusDTO> getStatusByCodes(List<String> statusCodes) {
119         Assert.notNull(statusCodes);
120         return statusCodes.stream().map(statusCode -> loopbackDao.getStatusByCode(statusCode)).collect(Collectors.toList());
121     }
122 
123     /** {@inheritDoc} */
124     @Override
125     public List<LevelDTO> getAllDepthLevels() {
126 
127         // only enable (=national) depth levels
128         String statusCode = StatusCode.ENABLE.getValue();
129 
130         Cache cacheById = cacheService.getCache(DEPTH_LEVEL_BY_ID_CACHE);
131 
132         Iterator<Object[]> it = queryIterator("allDepthLevels",
133                 "statusCode", StringType.INSTANCE, statusCode);
134 
135         List<LevelDTO> result = Lists.newArrayList();
136         while (it.hasNext()) {
137             Object[] source = it.next();
138             LevelDTO depthLevel = toDepthLevelDTO(Arrays.asList(source).iterator(), statusCode);
139             result.add(depthLevel);
140 
141             // update cache
142             cacheById.put(depthLevel.getId(), depthLevel);
143         }
144 
145         return ImmutableList.copyOf(result);
146     }
147 
148     /** {@inheritDoc} */
149     @Override
150     public LevelDTO getDepthLevelById(int depthLevelId) {
151 
152         // only enable (=national) depth levels
153         String statusCode = StatusCode.ENABLE.getValue();
154 
155         Object[] source = queryUnique("depthLevelById",
156                 "statusCode", StringType.INSTANCE, statusCode,
157                 "depthLevelId", IntegerType.INSTANCE, depthLevelId);
158 
159         if (source == null) {
160             throw new DataRetrievalFailureException("can't load depth level with id = " + depthLevelId);
161         }
162 
163         return toDepthLevelDTO(Arrays.asList(source).iterator(), statusCode);
164     }
165 
166     /** {@inheritDoc} */
167     @Override
168     public List<QualityLevelDTO> getAllQualityFlags(List<String> statusCodes) {
169 
170         Cache cacheByCode = cacheService.getCache(QUALITY_FLAG_BY_CODE_CACHE);
171 
172         Iterator<Object[]> it = Daos.queryIteratorWithStatus(createQuery("allQualityFlags"), statusCodes);
173         Map<String, String> transcribingNamesByCode = getTranscribingQualityLevelNamesByCode();
174 
175         List<QualityLevelDTO> result = Lists.newArrayList();
176         while (it.hasNext()) {
177             Object[] source = it.next();
178             QualityLevelDTO qualityFlag = toQualityLevelDTO(Arrays.asList(source).iterator(), transcribingNamesByCode);
179             result.add(qualityFlag);
180 
181             cacheByCode.put(qualityFlag.getCode(), qualityFlag);
182         }
183 
184         return ImmutableList.copyOf(result);
185     }
186 
187     /** {@inheritDoc} */
188     @Override
189     public QualityLevelDTO getQualityFlagByCode(String qualityFlagCode) {
190 
191         Object[] source = queryUnique("qualityFlagByCode", "qualityFlagCode", StringType.INSTANCE, qualityFlagCode);
192 
193         if (source == null) {
194             throw new DataRetrievalFailureException("can't load quality flag with code = " + qualityFlagCode);
195         }
196 
197         return toQualityLevelDTO(Arrays.asList(source).iterator(), getTranscribingQualityLevelNamesByCode());
198     }
199 
200     /** {@inheritDoc} */
201     @Override
202     public List<PositioningSystemDTO> getAllPositioningSystems() {
203 
204         Cache cacheById = cacheService.getCache(POSITIONING_SYSTEM_BY_ID_CACHE);
205 
206         Iterator<Object[]> it = queryIterator("allPositioningSystems");
207 
208         List<PositioningSystemDTO> result = Lists.newArrayList();
209         while (it.hasNext()) {
210             Object[] source = it.next();
211             PositioningSystemDTO positioningSystem = toPositioningSystemDTO(Arrays.asList(source).iterator());
212             result.add(positioningSystem);
213 
214             cacheById.put(positioningSystem.getId(), positioningSystem);
215         }
216 
217         return ImmutableList.copyOf(result);
218     }
219 
220     /** {@inheritDoc} */
221     @Override
222     public PositioningSystemDTO getPositioningSystemById(int posSystemId) {
223         Object[] source = queryUnique("positioningSystemById", "posSystemId", IntegerType.INSTANCE, posSystemId);
224 
225         if (source == null) {
226             throw new DataRetrievalFailureException("can't load positioning system with id = " + posSystemId);
227         }
228 
229         return toPositioningSystemDTO(Arrays.asList(source).iterator());
230     }
231 
232     /** {@inheritDoc} */
233     @Override
234     public List<GroupingTypeDTO> getAllGroupingTypes() {
235 
236         Iterator<Object[]> it = Daos.queryIteratorWithStatus(createQuery("allOrderItemTypes"), StatusFilter.ACTIVE.toStatusCodes());
237 
238         List<GroupingTypeDTO> result = Lists.newArrayList();
239         while (it.hasNext()) {
240             Object[] source = it.next();
241             GroupingTypeDTO groupingType = toGroupingTypeDTO(Arrays.asList(source).iterator());
242 
243             groupingType.setGrouping(getGroupingsByType(groupingType.getCode()));
244 
245             result.add(groupingType);
246         }
247 
248         return ImmutableList.copyOf(result);
249     }
250 
251     /** {@inheritDoc} */
252     @Override
253     public List<GroupingDTO> getGroupingsByType(String groupingType) {
254         Iterator<Object[]> it = queryIterator("allOrderItemsByTypeCode", "typeCode", StringType.INSTANCE, groupingType);
255 
256         List<GroupingDTO> result = Lists.newArrayList();
257         while (it.hasNext()) {
258             Object[] source = it.next();
259             result.add(toGroupingDTO(Arrays.asList(source).iterator()));
260         }
261 
262         return result;
263     }
264 
265     /** {@inheritDoc} */
266     @Override
267     public List<CitationDTO> getAllCitations() {
268         Iterator<Object[]> it = queryIterator("allCitations");
269 
270         List<CitationDTO> result = Lists.newArrayList();
271         while (it.hasNext()) {
272             Object[] source = it.next();
273             result.add(toCitationDTO(Arrays.asList(source).iterator()));
274         }
275 
276         return ImmutableList.copyOf(result);
277     }
278 
279     /** {@inheritDoc} */
280     @Override
281     public List<PhotoTypeDTO> getAllPhotoTypes() {
282 
283         Iterator<Object[]> it = Daos.queryIteratorWithStatus(createQuery("allPhotoTypes"), StatusFilter.ACTIVE.toStatusCodes());
284 
285         List<PhotoTypeDTO> result = Lists.newArrayList();
286         while (it.hasNext()) {
287             Object[] source = it.next();
288             result.add(toPhotoTypeDTO(Arrays.asList(source).iterator()));
289         }
290 
291         return ImmutableList.copyOf(result);
292     }
293 
294     /** {@inheritDoc} */
295     @Override
296     public PhotoTypeDTO getPhotoTypeByCode(String photoTypeCode) {
297         Object[] source = queryUnique("photoTypeByCode", "photoTypeCode", StringType.INSTANCE, photoTypeCode);
298 
299         if (source == null) {
300             throw new DataRetrievalFailureException("can't load photo type with id = " + photoTypeCode);
301         }
302 
303         return toPhotoTypeDTO(Arrays.asList(source).iterator());
304     }
305 
306     @Override
307     public List<TaxonomicLevelDTO> getAllTaxonomicLevels() {
308         Cache cacheByCode = cacheService.getCache(TAXONOMIC_LEVEL_BY_CODE_CACHE);
309 
310         Iterator<Object[]> it = queryIterator("allTaxonomicLevels");
311 
312         List<TaxonomicLevelDTO> result = Lists.newArrayList();
313         Map<String, String> transcribingNamesByCode = getTranscribingTaxonomicLevelNamesByCode();
314 
315         while (it.hasNext()) {
316             Object[] source = it.next();
317             TaxonomicLevelDTO taxonomicLevel = toTaxonomicLevelDTO(Arrays.asList(source).iterator(), transcribingNamesByCode);
318             result.add(taxonomicLevel);
319 
320             cacheByCode.put(taxonomicLevel.getCode(), taxonomicLevel);
321         }
322         return ImmutableList.copyOf(result);
323     }
324 
325     @Override
326     public TaxonomicLevelDTO getTaxonomicLevelByCode(String code) {
327         Object[] source = queryUnique("taxonomicLevelByCode", "taxLevelCd", StringType.INSTANCE, code);
328 
329         if (source == null) {
330             throw new DataRetrievalFailureException("can't load taxonomic level with code = " + code);
331         }
332 
333         return toTaxonomicLevelDTO(Arrays.asList(source).iterator(), getTranscribingTaxonomicLevelNamesByCode());
334     }
335 
336     // INTERNAL METHODS
337 
338     private Map<String, String> getTranscribingStatusNamesByCode() {
339         return transcribingItemDao.getAllTranscribingItemsByCode(config.getTranscribingItemTypeLbForStatusNm());
340     }
341 
342     private StatusDTO toStatusDTO(Iterator<Object> source, Map<String, String> transcribingNamesByCode) {
343         StatusDTO result = QuadrigeBeanFactory.newStatusDTO();
344         result.setCode((String) source.next());
345         result.setName((String) source.next());
346         // transcribing name (Mantis #47626)
347         result.setName(transcribingNamesByCode.getOrDefault(result.getCode(), result.getName()));
348         return result;
349     }
350 
351     private Map<String, String> getTranscribingTaxonomicLevelNamesByCode() {
352         return transcribingItemDao.getAllTranscribingItemsByCode(config.getTranscribingItemTypeLbForTaxLevelNm());
353     }
354 
355     private TaxonomicLevelDTO toTaxonomicLevelDTO(Iterator<Object> source, Map<String, String> transcribingNamesByCode) {
356         TaxonomicLevelDTO result = DaliBeanFactory.newTaxonomicLevelDTO();
357         result.setCode((String) source.next());
358         result.setLabel((String) source.next());
359         result.setName((String) source.next());
360         result.setNumber((Integer) source.next());
361         // transcribed name (Mantis #47626)
362         result.setName(transcribingNamesByCode.getOrDefault(result.getCode(), result.getName()));
363         return result;
364     }
365 
366     private LevelDTO toDepthLevelDTO(Iterator<Object> source, String statusCode) {
367         LevelDTO result = DaliBeanFactory.newLevelDTO();
368         result.setId((Integer) source.next());
369         result.setName((String) source.next());
370         result.setDescription((String) source.next());
371         result.setStatus(loopbackDao.getStatusByCode(statusCode));
372         return result;
373     }
374 
375     private PhotoTypeDTO toPhotoTypeDTO(Iterator<Object> source) {
376         PhotoTypeDTO result = DaliBeanFactory.newPhotoTypeDTO();
377         result.setCode((String) source.next());
378         result.setName((String) source.next());
379         result.setDescription((String) source.next());
380         result.setStatus(loopbackDao.getStatusByCode((String) source.next()));
381         return result;
382     }
383 
384     private Map<String, String> getTranscribingQualityLevelNamesByCode() {
385         return transcribingItemDao.getAllTranscribingItemsByCode(config.getTranscribingItemTypeLbForQualFlagNm());
386     }
387 
388     private QualityLevelDTO toQualityLevelDTO(Iterator<Object> source, Map<String, String> transcribingNamesByCode) {
389         QualityLevelDTO result = DaliBeanFactory.newQualityLevelDTO();
390         result.setCode((String) source.next());
391         result.setName((String) source.next());
392         result.setDescription((String) source.next());
393         result.setStatus(loopbackDao.getStatusByCode((String) source.next()));
394         // transcribed name (Mantis #47330)
395         result.setName(transcribingNamesByCode.getOrDefault(result.getCode(), result.getName()));
396         return result;
397     }
398 
399     private PositioningSystemDTO toPositioningSystemDTO(Iterator<Object> source) {
400         PositioningSystemDTO result = DaliBeanFactory.newPositioningSystemDTO();
401         result.setId((Integer) source.next());
402         result.setName((String) source.next());
403         result.setPrecision((String) source.next());
404         // a positioning system is always enable (=national)
405         result.setStatus(loopbackDao.getStatusByCode(StatusCode.ENABLE.getValue()));
406         return result;
407     }
408 
409     private GroupingTypeDTO toGroupingTypeDTO(Iterator<Object> source) {
410         GroupingTypeDTO result = DaliBeanFactory.newGroupingTypeDTO();
411         result.setCode((String) source.next());
412         result.setName((String) source.next());
413         return result;
414     }
415 
416     private GroupingDTO toGroupingDTO(Iterator<Object> source) {
417         GroupingDTO result = DaliBeanFactory.newGroupingDTO();
418         result.setId((Integer) source.next());
419         result.setName((String) source.next());
420         return result;
421     }
422 
423     private CitationDTO toCitationDTO(Iterator<Object> source) {
424         CitationDTO result = DaliBeanFactory.newCitationDTO();
425         result.setId((Integer) source.next());
426         result.setName((String) source.next());
427         return result;
428     }
429 }