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 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.ParameterDTO;
34  import fr.ifremer.dali.dto.referential.pmfm.ParameterGroupDTO;
35  import fr.ifremer.quadrige3.core.dao.referential.pmfm.ParameterDaoImpl;
36  import fr.ifremer.quadrige3.core.dao.technical.Assert;
37  import fr.ifremer.quadrige3.core.service.technical.CacheService;
38  import org.hibernate.Query;
39  import org.hibernate.SessionFactory;
40  import org.hibernate.type.IntegerType;
41  import org.hibernate.type.StringType;
42  import org.springframework.beans.factory.annotation.Autowired;
43  import org.springframework.cache.Cache;
44  import org.springframework.dao.DataRetrievalFailureException;
45  import org.springframework.stereotype.Repository;
46  
47  import javax.annotation.Resource;
48  import java.util.Arrays;
49  import java.util.Iterator;
50  import java.util.List;
51  import java.util.Map;
52  
53  /**
54   * Parameter DAO
55   * Created by Ludovic on 29/07/2015.
56   */
57  @Repository("daliParameterDao")
58  public class DaliParameterDaoImpl extends ParameterDaoImpl implements DaliParameterDao {
59  
60      @Resource
61      protected CacheService cacheService;
62  
63      @Resource
64      protected DaliConfiguration config;
65  
66      @Resource(name = "daliTranscribingItemDao")
67      private DaliTranscribingItemDao transcribingItemDao;
68  
69      @Resource(name = "daliPmfmDao")
70      protected DaliPmfmDao pmfmDao;
71  
72      @Resource(name = "daliQualitativeValueDao")
73      private DaliQualitativeValueDao qualitativeValueDao;
74  
75      @Resource(name = "daliParameterDao")
76      private DaliParameterDao loopbackDao;
77  
78      @Resource(name = "daliReferentialDao")
79      protected DaliReferentialDao referentialDao;
80  
81      /**
82       * Constructor used by Spring
83       *
84       * @param sessionFactory a {@link org.hibernate.SessionFactory} object.
85       */
86      @Autowired
87      public DaliParameterDaoImpl(SessionFactory sessionFactory) {
88          super(sessionFactory);
89      }
90  
91      /** {@inheritDoc} */
92      @Override
93      public List<ParameterGroupDTO> getAllParameterGroups(List<String> statusCodes) {
94  
95          Cache cacheById = cacheService.getCache(PARAMETER_GROUP_BY_ID_CACHE);
96  
97          Iterator<Object[]> it = Daos.queryIteratorWithStatus(createQuery("allParameterGroups"), statusCodes);
98  
99          List<ParameterGroupDTO> result = Lists.newArrayList();
100         while (it.hasNext()) {
101             Object[] source = it.next();
102             ParameterGroupDTO parameter = toParameterGroupDTO(Arrays.asList(source).iterator());
103             result.add(parameter);
104             cacheById.put(parameter.getId(), parameter);
105         }
106 
107         return ImmutableList.copyOf(result);
108     }
109 
110     /** {@inheritDoc} */
111     @Override
112     public ParameterGroupDTO getParameterGroupById(int parameterGroupId) {
113         Object[] source = queryUnique("parameterGroupById", "parameterGroupId", IntegerType.INSTANCE, parameterGroupId);
114 
115         if (source == null) {
116             throw new DataRetrievalFailureException("can't load parameter group with id = " + parameterGroupId);
117         }
118 
119         return toParameterGroupDTO(Arrays.asList(source).iterator());
120     }
121 
122     /** {@inheritDoc} */
123     @Override
124     public List<ParameterDTO> getAllParameters(List<String> statusCodes) {
125 
126         Cache cacheByCode = cacheService.getCache(PARAMETER_BY_CODE_CACHE);
127         List<ParameterDTO> result = Lists.newArrayList();
128         Map<String, String> transcribingNamesByCode = getTranscribingNames();
129         Map<String, String> transcribingCodesByCode = getTranscribingCodes();
130 
131         Iterator<Object[]> it = Daos.queryIteratorWithStatus(createQuery("allParameters"), statusCodes);
132 
133         while (it.hasNext()) {
134             Object[] source = it.next();
135             ParameterDTO parameter = toParameterDTO(Arrays.asList(source).iterator(), transcribingNamesByCode, transcribingCodesByCode);
136             // without qualitative values
137             result.add(parameter);
138             cacheByCode.put(parameter.getCode(), parameter);
139         }
140 
141         return ImmutableList.copyOf(result);
142     }
143 
144     /** {@inheritDoc} */
145     @Override
146     public ParameterDTO getParameterByCode(String parameterCode) {
147         Assert.notBlank(parameterCode);
148 
149         Object[] source = queryUnique("parameterByCode", "parameterCode", StringType.INSTANCE, parameterCode);
150 
151         if (source == null) {
152             throw new DataRetrievalFailureException("can't load parameter with code = " + parameterCode);
153         }
154 
155         ParameterDTO result = toParameterDTO(Arrays.asList(source).iterator(), getTranscribingNames(), getTranscribingCodes());
156         // add all associated Qualitative values
157         result.setQualitativeValues(qualitativeValueDao.getQualitativeValuesByParameterCode(result.getCode()));
158         return result;
159     }
160 
161     /** {@inheritDoc} */
162     @Override
163     public List<ParameterDTO> findParameters(String parameterCode, Integer parameterGroupId, List<String> statusCodes) {
164         List<ParameterDTO> result = Lists.newArrayList();
165         Map<String, String> transcribingNamesByCode = getTranscribingNames();
166         Map<String, String> transcribingCodesByCode = getTranscribingCodes();
167 
168         Query query = createQuery("parametersByCriteria",
169                 "parameterCode", StringType.INSTANCE, parameterCode,
170                 "parameterGroupId", IntegerType.INSTANCE, parameterGroupId);
171 
172         Iterator<Object[]> it = Daos.queryIteratorWithStatus(query, statusCodes);
173 
174         // other solution: use criteria API and findByExample
175         while (it.hasNext()) {
176             Object[] source = it.next();
177             ParameterDTO parameter = toParameterDTO(Arrays.asList(source).iterator(), transcribingNamesByCode, transcribingCodesByCode);
178             // add all associated Qualitative values
179             parameter.setQualitativeValues(qualitativeValueDao.getQualitativeValuesByParameterCode(parameter.getCode()));
180             result.add(parameter);
181         }
182 
183         return ImmutableList.copyOf(result);
184     }
185 
186     private ParameterGroupDTO toParameterGroupDTO(Iterator<Object> source) {
187         ParameterGroupDTO parameterGroup = DaliBeanFactory.newParameterGroupDTO();
188         parameterGroup.setId((Integer) source.next());
189         parameterGroup.setName((String) source.next());
190         parameterGroup.setDescription((String) source.next());
191         parameterGroup.setStatus(referentialDao.getStatusByCode((String) source.next()));
192         Integer parentId = (Integer) source.next();
193         if (parentId != null) {
194             parameterGroup.setParentParameterGroup(loopbackDao.getParameterGroupById(parentId));
195         }
196         return parameterGroup;
197     }
198 
199     private Map<String, String> getTranscribingNames() {
200         return transcribingItemDao.getAllTranscribingItemsByCode(config.getTranscribingItemTypeLbForParameterNm());
201     }
202 
203     private Map<String, String> getTranscribingCodes() {
204         return transcribingItemDao.getAllTranscribingItemsByCode(config.getTranscribingItemTypeLbForParameterCd());
205     }
206 
207     private ParameterDTO toParameterDTO(Iterator<Object> source, Map<String, String> transcribingNamesByCode, Map<String, String> transcribingCodesByCode) {
208         ParameterDTO result = DaliBeanFactory.newParameterDTO();
209         result.setCode((String) source.next());
210         result.setName((String) source.next());
211         result.setDescription((String) source.next());
212         result.setQualitative(Daos.safeConvertToBoolean(source.next()));
213         result.setCalculated(Daos.safeConvertToBoolean(source.next()));
214         result.setTaxonomic(Daos.safeConvertToBoolean(source.next()));
215         result.setStatus(referentialDao.getStatusByCode((String) source.next()));
216         Integer groupId = (Integer) source.next();
217         if (groupId != null) {
218             result.setParameterGroup(loopbackDao.getParameterGroupById(groupId));
219         }
220         result.setComment((String) source.next());
221         result.setCreationDate(Daos.convertToDate(source.next()));
222         result.setUpdateDate(Daos.convertToDate(source.next()));
223 
224         // transcribed name
225         result.setName(transcribingNamesByCode.getOrDefault(result.getCode(), result.getName()));
226         // transcribed code (Mantis #47330)
227         result.setTranscribedCode(transcribingCodesByCode.getOrDefault(result.getCode(), result.getCode()));
228 
229         return result;
230     }
231 
232 }