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.FractionDTO;
34  import fr.ifremer.dali.dto.referential.pmfm.MatrixDTO;
35  import fr.ifremer.quadrige3.core.dao.referential.pmfm.FractionDaoImpl;
36  import fr.ifremer.quadrige3.core.service.technical.CacheService;
37  import org.hibernate.Query;
38  import org.hibernate.SessionFactory;
39  import org.hibernate.type.IntegerType;
40  import org.springframework.beans.factory.annotation.Autowired;
41  import org.springframework.dao.DataRetrievalFailureException;
42  import org.springframework.stereotype.Repository;
43  
44  import javax.annotation.Resource;
45  import java.util.Arrays;
46  import java.util.Iterator;
47  import java.util.List;
48  import java.util.Map;
49  
50  /**
51   * Fraction DAO
52   * Created by Ludovic on 29/07/2015.
53   */
54  @Repository("daliFractionDao")
55  public class DaliFractionDaoImpl extends FractionDaoImpl implements DaliFractionDao {
56  
57      @Resource
58      protected CacheService cacheService;
59  
60      @Resource
61      protected DaliConfiguration config;
62  
63      @Resource(name = "daliTranscribingItemDao")
64      private DaliTranscribingItemDao transcribingItemDao;
65  
66      @Resource(name = "daliMatrixDao")
67      private DaliMatrixDao matrixDao;
68  
69      @Resource(name = "daliReferentialDao")
70      protected DaliReferentialDao referentialDao;
71  
72      /**
73       * Constructor used by Spring
74       *
75       * @param sessionFactory a {@link org.hibernate.SessionFactory} object.
76       */
77      @Autowired
78      public DaliFractionDaoImpl(SessionFactory sessionFactory) {
79          super(sessionFactory);
80      }
81  
82      /** {@inheritDoc} */
83      @Override
84      public List<FractionDTO> getAllFractions(List<String> statusCodes) {
85  
86          List<FractionDTO> result = Lists.newArrayList();
87          Map<Integer, String> transcribingNamesById = getTranscribingNames();
88  
89          Iterator<Object[]> it = Daos.queryIteratorWithStatus(createQuery("allFractions"), statusCodes);
90  
91          FractionDTO fraction = null;
92          while (it.hasNext()) {
93              Object[] row = it.next();
94              Iterator<Object> rowIt = Arrays.asList(row).iterator();
95  
96              if (fraction == null || !fraction.getId().equals(row[0])) {
97                  fraction = toFractionDTO(rowIt, transcribingNamesById);
98                  result.add(fraction);
99              } else {
100                 // dummy iteration
101                 toFractionDTO(rowIt, transcribingNamesById);
102             }
103 
104             // the last element is the associated matrix
105             Integer matrixId = (Integer) rowIt.next();
106             if (matrixId != null) {
107                 MatrixDTO matrix = matrixDao.getMatrixById(matrixId);
108                 fraction.addMatrixes(matrix);
109                 matrix.addFractions(fraction);
110             }
111         }
112 
113         return ImmutableList.copyOf(result);
114     }
115 
116     /** {@inheritDoc} */
117     @Override
118     public FractionDTO getFractionById(int fractionId) {
119 
120         Iterator<Object[]> it = queryIterator("fractionById", "fractionId", IntegerType.INSTANCE, fractionId);
121         Map<Integer, String> transcribingNamesById = getTranscribingNames();
122 
123         if (!it.hasNext()) {
124             throw new DataRetrievalFailureException("can't load fraction with id = " + fractionId);
125         }
126 
127         FractionDTO fraction = null;
128         while (it.hasNext()) {
129             Object[] row = it.next();
130             Iterator<Object> rowIt = Arrays.asList(row).iterator();
131             if (fraction == null) {
132                 fraction = toFractionDTO(rowIt, transcribingNamesById);
133             } else {
134                 toFractionDTO(rowIt, transcribingNamesById);
135             }
136             // the last element is the associated matrix
137             Integer matrixId = (Integer) rowIt.next();
138             if (matrixId != null) {
139                 MatrixDTO matrix = matrixDao.getMatrixById(matrixId);
140                 fraction.addMatrixes(matrix);
141                 matrix.addFractions(fraction);
142             }
143         }
144         return fraction;
145     }
146 
147     /** {@inheritDoc} */
148     @Override
149     public List<FractionDTO> getFractionsByMatrixId(Integer matrixId) {
150         List<FractionDTO> result = Lists.newArrayList();
151         Map<Integer, String> transcribingNamesById = getTranscribingNames();
152 
153         Iterator<Object[]> it = queryIterator("fractionsByMatrixId", "matrixId", IntegerType.INSTANCE, matrixId);
154 
155         while (it.hasNext()) {
156             Object[] row = it.next();
157             result.add(toFractionDTO(Arrays.asList(row).iterator(), transcribingNamesById));
158         }
159 
160         return ImmutableList.copyOf(result);
161 
162     }
163 
164     /** {@inheritDoc} */
165     @Override
166     public List<FractionDTO> findFractions(Integer fractionId, List<String> statusCodes) {
167         List<FractionDTO> result = Lists.newArrayList();
168         Map<Integer, String> transcribingNamesById = getTranscribingNames();
169 
170         Query query = createQuery("fractionByCriteria", "fractionId", IntegerType.INSTANCE, fractionId);
171         Iterator<Object[]> it = Daos.queryIteratorWithStatus(query, statusCodes);
172 
173         FractionDTO fraction = null;
174         while (it.hasNext()) {
175             Object[] row = it.next();
176             Iterator<Object> rowIt = Arrays.asList(row).iterator();
177 
178             if (fraction == null || !fraction.getId().equals(row[0])) {
179                 fraction = toFractionDTO(rowIt, transcribingNamesById);
180                 result.add(fraction);
181             } else {
182                 // dummy iteration
183                 toFractionDTO(rowIt, transcribingNamesById);
184             }
185 
186             // the last element is the associated matrix
187             Integer matrixId = (Integer) rowIt.next();
188             if (matrixId != null) {
189                 MatrixDTO matrix = matrixDao.getMatrixById(matrixId);
190                 fraction.addMatrixes(matrix);
191                 matrix.addFractions(fraction);
192             }
193         }
194 
195         return ImmutableList.copyOf(result);
196 
197     }
198 
199     private Map<Integer, String> getTranscribingNames() {
200         return transcribingItemDao.getAllTranscribingItemsById(config.getTranscribingItemTypeLbForFractionNm());
201     }
202 
203     private FractionDTO toFractionDTO(Iterator<Object> source, Map<Integer, String> transcribingNamesById) {
204         FractionDTO result = DaliBeanFactory.newFractionDTO();
205         result.setId((Integer) source.next());
206         result.setName((String) source.next());
207         result.setDescription((String) source.next());
208         result.setStatus(referentialDao.getStatusByCode((String) source.next()));
209         result.setComment((String) source.next());
210         result.setCreationDate(Daos.convertToDate(source.next()));
211         result.setUpdateDate(Daos.convertToDate(source.next()));
212 
213         // transcribed name
214         result.setName(transcribingNamesById.getOrDefault(result.getId(), result.getName()));
215 
216         return result;
217     }
218 
219 }