View Javadoc
1   package fr.ifremer.dali.dao.referential.pmfm;
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 fr.ifremer.dali.config.DaliConfiguration;
28  import fr.ifremer.dali.dao.referential.DaliReferentialDao;
29  import fr.ifremer.dali.dao.referential.DaliUnitDao;
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.DaliBeans;
34  import fr.ifremer.dali.dto.referential.pmfm.FractionDTO;
35  import fr.ifremer.dali.dto.referential.pmfm.MatrixDTO;
36  import fr.ifremer.dali.dto.referential.pmfm.PmfmDTO;
37  import fr.ifremer.quadrige3.core.dao.referential.pmfm.PmfmDaoImpl;
38  import fr.ifremer.quadrige3.core.service.technical.CacheService;
39  import org.apache.commons.collections4.CollectionUtils;
40  import org.hibernate.Query;
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  
52  /**
53   * <p>DaliPmfmDaoImpl class.</p>
54   */
55  @Repository("daliPmfmDao")
56  public class DaliPmfmDaoImpl extends PmfmDaoImpl implements DaliPmfmDao {
57  
58      @Resource
59      protected CacheService cacheService;
60  
61      @Resource
62      protected DaliConfiguration config;
63  
64      @Resource(name = "daliParameterDao")
65      private DaliParameterDao parameterDao;
66  
67      @Resource(name = "daliMethodDao")
68      private DaliMethodDao methodDao;
69  
70      @Resource(name = "daliFractionDao")
71      private DaliFractionDao fractionDao;
72  
73      @Resource(name = "daliMatrixDao")
74      private DaliMatrixDao matrixDao;
75  
76      @Resource(name = "daliUnitDao")
77      private DaliUnitDao unitDao;
78  
79      @Resource(name = "daliTranscribingItemDao")
80      private DaliTranscribingItemDao transcribingItemDao;
81  
82      @Resource(name = "daliQualitativeValueDao")
83      private DaliQualitativeValueDao qualitativeValueDao;
84  
85      @Resource(name = "daliReferentialDao")
86      protected DaliReferentialDao referentialDao;
87  
88      /**
89       * <p>Constructor for DaliPmfmDaoImpl.</p>
90       *
91       * @param sessionFactory a {@link org.hibernate.SessionFactory} object.
92       */
93      @Autowired
94      public DaliPmfmDaoImpl(SessionFactory sessionFactory) {
95          super(sessionFactory);
96      }
97  
98      /**
99       * {@inheritDoc}
100      */
101     @Override
102     public List<PmfmDTO> getAllPmfms(List<String> statusCodes) {
103 
104         Cache cacheById = cacheService.getCache(PMFM_BY_ID_CACHE);
105 
106         // Read all transcribing names
107         Map<Integer, String> transcribingNamesById = getTranscribingNames();
108 
109         Iterator<Object[]> it = Daos.queryIteratorWithStatus(createQuery("allPmfms"), statusCodes);
110 
111         List<PmfmDTO> result = new ArrayList<>();
112         while (it.hasNext()) {
113             Object[] source = it.next();
114 
115             // Create the VO
116             PmfmDTO pmfm = toPmfmDTO(Arrays.asList(source).iterator(), transcribingNamesById);
117 
118             result.add(pmfm);
119             cacheById.put(pmfm.getId(), pmfm);
120         }
121 
122         return ImmutableList.copyOf(result);
123     }
124 
125     /**
126      * {@inheritDoc}
127      */
128     @Override
129     public List<PmfmDTO> findPmfms(String parameterCode, Integer matrixId, Integer fractionId, Integer methodId, Integer unitId, String pmfmName, List<String> statusCodes) {
130         // Read all transcribing names
131         Map<Integer, String> transcribingNamesById = getTranscribingNames();
132 
133         Query query = createQuery("pmfmsByCriteria",
134                 "parameterCode", StringType.INSTANCE, parameterCode,
135                 "matrixId", IntegerType.INSTANCE, matrixId,
136                 "fractionId", IntegerType.INSTANCE, fractionId,
137                 "methodId", IntegerType.INSTANCE, methodId,
138                 "unitId", IntegerType.INSTANCE, unitId,
139                 "pmfmName", StringType.INSTANCE, pmfmName); // todo LP 07/02/2020 : the transcribing name is not handled (see Mantis #49923)
140 
141         Iterator<Object[]> it = Daos.queryIteratorWithStatus(query, statusCodes);
142 
143         // other solution: use criteria API and findByExample
144         List<PmfmDTO> result = new ArrayList<>();
145         while (it.hasNext()) {
146             Object[] source = it.next();
147 
148             // Create the VO
149             PmfmDTO pmfm = toPmfmDTO(Arrays.asList(source).iterator(), transcribingNamesById);
150             result.add(pmfm);
151         }
152 
153         return ImmutableList.copyOf(result);
154     }
155 
156     /**
157      * {@inheritDoc}
158      */
159     @Override
160     public PmfmDTO getPmfmById(int pmfmId) {
161         Object[] source = queryUnique("pmfmById",
162                 "pmfmId", IntegerType.INSTANCE, pmfmId);
163 
164         if (source == null) {
165             throw new DataRetrievalFailureException("can't load pmfm with id = " + pmfmId);
166         }
167 
168         return toPmfmDTO(Arrays.asList(source).iterator(), getTranscribingNames());
169     }
170 
171     /**
172      * {@inheritDoc}
173      *
174      * @param pmfmIds
175      */
176     @Override
177     @SuppressWarnings("unchecked")
178     public List<PmfmDTO> getPmfmsByIds(Collection<Integer> pmfmIds) {
179 
180         List<PmfmDTO> result = new ArrayList<>();
181         if (CollectionUtils.isEmpty(pmfmIds))
182             return result;
183 
184         // Read all transcribing names
185         Map<Integer, String> transcribingNamesById = getTranscribingNames();
186 
187         Iterator<Object[]> it = createQuery("pmfmByIds")
188                 .setParameterList("pmfmIds", pmfmIds)
189                 .iterate();
190 
191         while (it.hasNext()) {
192             Object[] source = it.next();
193 
194             // Create the VO
195             result.add(toPmfmDTO(Arrays.asList(source).iterator(), transcribingNamesById));
196         }
197 
198         return result;
199     }
200 
201     private Map<Integer, String> getTranscribingNames() {
202         return transcribingItemDao.getAllTranscribingItemsById(config.getTranscribingItemTypeLbForPmfmNm());
203     }
204 
205     private PmfmDTO toPmfmDTO(Iterator<Object> source, Map<Integer, String> transcribingNamesById) {
206         PmfmDTO result = DaliBeanFactory.newPmfmDTO();
207 
208         result.setId((Integer) source.next());
209 
210         // max nb decimals
211         result.setMaxDecimals((Short) source.next());
212 
213         // nb significant figures
214         result.setFiguresNumber((Short) source.next());
215 
216         // status
217         result.setStatus(referentialDao.getStatusByCode((String) source.next()));
218 
219         // parameter by cache
220         result.setParameter(parameterDao.getParameterByCode((String) source.next()));
221 
222         // matrix by cache
223         MatrixDTO matrix = DaliBeans.clone(matrixDao.getMatrixById((int) source.next()));
224         // remove some redundant data
225         if (matrix != null) matrix.setFractions(null);
226         result.setMatrix(matrix);
227 
228         // fraction by cache
229         FractionDTO fraction = DaliBeans.clone(fractionDao.getFractionById((int) source.next()));
230         // remove some redundant data
231         if (fraction != null) fraction.setMatrixes(null);
232         result.setFraction(fraction);
233 
234         // method by cache
235         result.setMethod(methodDao.getMethodById((int) source.next()));
236 
237         // unit by cache
238         result.setUnit(unitDao.getUnitById((int) source.next()));
239 
240         result.setComment((String) source.next());
241         result.setCreationDate(Daos.convertToDate(source.next()));
242         result.setUpdateDate(Daos.convertToDate(source.next()));
243 
244         // name
245         result.setName(transcribingNamesById.getOrDefault(result.getId(), result.getName()));
246 
247         // QV
248         result.setQualitativeValues(qualitativeValueDao.getQualitativeValuesByPmfmId(result.getId()));
249 
250         return result;
251     }
252 
253 }