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.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   * Matrix DAO
51   * Created by Ludovic on 29/07/2015.
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       * Constructor used by Spring
73       *
74       * @param sessionFactory a {@link org.hibernate.SessionFactory} object.
75       */
76      @Autowired
77      public DaliMatrixDaoImpl(SessionFactory sessionFactory) {
78          super(sessionFactory);
79      }
80  
81      /** {@inheritDoc} */
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              // add fractions
94              matrix.addAllFractions(fractionDao.getFractionsByMatrixId(matrix.getId()));
95              result.add(matrix);
96          }
97  
98          return ImmutableList.copyOf(result);
99      }
100 
101     /** {@inheritDoc} */
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         // add fractions
114         result.addAllFractions(fractionDao.getFractionsByMatrixId(result.getId()));
115 
116         return result;
117     }
118 
119     /** {@inheritDoc} */
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             // add fractions
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         // transcribed name
156         result.setName(transcribingNamesById.getOrDefault(result.getId(), result.getName()));
157 
158         return result;
159     }
160 
161 }