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 fr.ifremer.dali.dao.technical.Daos;
28  import fr.ifremer.dali.dto.DaliBeanFactory;
29  import fr.ifremer.dali.dto.referential.AnalysisInstrumentDTO;
30  import fr.ifremer.quadrige3.core.dao.referential.AnalysisInstrumentDaoImpl;
31  import fr.ifremer.quadrige3.core.service.technical.CacheService;
32  import org.apache.commons.collections4.CollectionUtils;
33  import org.hibernate.Query;
34  import org.hibernate.SessionFactory;
35  import org.hibernate.type.IntegerType;
36  import org.hibernate.type.StringType;
37  import org.springframework.beans.factory.annotation.Autowired;
38  import org.springframework.cache.Cache;
39  import org.springframework.dao.DataRetrievalFailureException;
40  import org.springframework.stereotype.Repository;
41  
42  import javax.annotation.Resource;
43  import java.util.ArrayList;
44  import java.util.Arrays;
45  import java.util.Iterator;
46  import java.util.List;
47  
48  /**
49   * Analysis Instrument DAO
50   * <p/>
51   * Created by Ludovic on 17/07/2015.
52   */
53  @Repository("daliAnalysisInstrumentDao")
54  public class DaliAnalysisInstrumentDaoImpl extends AnalysisInstrumentDaoImpl implements DaliAnalysisInstrumentDao {
55  
56      @Resource
57      protected CacheService cacheService;
58  
59      @Resource(name = "daliReferentialDao")
60      protected DaliReferentialDao referentialDao;
61  
62      /**
63       * Constructor used by Spring
64       *
65       * @param sessionFactory a {@link org.hibernate.SessionFactory} object.
66       */
67      @Autowired
68      public DaliAnalysisInstrumentDaoImpl(SessionFactory sessionFactory) {
69          super(sessionFactory);
70      }
71  
72      /** {@inheritDoc} */
73      @Override
74      public List<AnalysisInstrumentDTO> getAllAnalysisInstruments(List<String> statusCodes) {
75  
76          Cache cacheById = cacheService.getCache(ANALYSIS_INSTRUMENT_BY_ID_CACHE);
77  
78          Iterator<Object[]> it = Daos.queryIteratorWithStatus(createQuery("allAnalysisInstruments"), statusCodes);
79  
80          List<AnalysisInstrumentDTO> result = new ArrayList<>();
81          while (it.hasNext()) {
82              Object[] source = it.next();
83              AnalysisInstrumentDTO analysisInstrument = toAnalysisInstrumentDTO(Arrays.asList(source).iterator());
84              result.add(analysisInstrument);
85  
86              cacheById.put(analysisInstrument.getId(), analysisInstrument);
87          }
88  
89          return ImmutableList.copyOf(result);
90      }
91  
92      /** {@inheritDoc} */
93      @Override
94      public List<AnalysisInstrumentDTO> findAnalysisInstruments(List<String> statusCodes, Integer analysisInstrumentId) {
95  
96          Query query = createQuery("analysisInstrumentsByCriteria",
97                  "analysisInstrumentId", IntegerType.INSTANCE, analysisInstrumentId);
98  
99          Iterator<Object[]> it = Daos.queryIteratorWithStatus(query, statusCodes);
100 
101         List<AnalysisInstrumentDTO> result = new ArrayList<>();
102         while (it.hasNext()) {
103             Object[] source = it.next();
104             result.add(toAnalysisInstrumentDTO(Arrays.asList(source).iterator()));
105         }
106 
107         return ImmutableList.copyOf(result);
108     }
109 
110     /** {@inheritDoc} */
111     @Override
112     public List<AnalysisInstrumentDTO> findAnalysisInstrumentsByName(List<String> statusCodes, String analysisInstrumentName) {
113         Query query = createQuery("analysisInstrumentsByName",
114                 "analysisInstrumentName", StringType.INSTANCE, analysisInstrumentName);
115 
116         Iterator<Object[]> it = Daos.queryIteratorWithStatus(query, statusCodes);
117 
118         List<AnalysisInstrumentDTO> result = new ArrayList<>();
119         while (it.hasNext()) {
120             Object[] source = it.next();
121             result.add(toAnalysisInstrumentDTO(Arrays.asList(source).iterator()));
122         }
123 
124         return ImmutableList.copyOf(result);
125     }
126 
127     /** {@inheritDoc} */
128     @Override
129     public AnalysisInstrumentDTO getAnalysisInstrumentById(int analysisInstrumentId) {
130 
131         Object[] source = queryUnique("analysisInstrumentById", "analysisInstrumentId", IntegerType.INSTANCE, analysisInstrumentId);
132 
133         if (source == null) {
134             throw new DataRetrievalFailureException("can't load analysis instrument with id = " + analysisInstrumentId);
135         }
136 
137         return toAnalysisInstrumentDTO(Arrays.asList(source).iterator());
138     }
139 
140     /** {@inheritDoc} */
141     @SuppressWarnings("unchecked")
142     @Override
143     public List<AnalysisInstrumentDTO> getAnalysisInstrumentsByIds(List<Integer> analysisInstrumentIds) {
144 
145         List<AnalysisInstrumentDTO> result = new ArrayList<>();
146         if (CollectionUtils.isEmpty(analysisInstrumentIds))
147             return result;
148 
149         Iterator<Object[]> it = createQuery("analysisInstrumentsByIds")
150                 .setParameterList("analysisInstrumentIds", analysisInstrumentIds)
151                 .iterate();
152 
153         while (it.hasNext()) {
154             Object[] row = it.next();
155             result.add(toAnalysisInstrumentDTO(Arrays.asList(row).iterator()));
156         }
157 
158         return result;
159 
160     }
161 
162 
163     /* private methods */
164 
165     private AnalysisInstrumentDTO toAnalysisInstrumentDTO(Iterator<Object> source) {
166         AnalysisInstrumentDTO result = DaliBeanFactory.newAnalysisInstrumentDTO();
167         result.setId((Integer) source.next());
168         result.setName((String) source.next());
169         result.setDescription((String) source.next());
170         result.setStatus(referentialDao.getStatusByCode((String) source.next()));
171         result.setComment((String) source.next());
172         result.setCreationDate(Daos.convertToDate(source.next()));
173         result.setUpdateDate(Daos.convertToDate(source.next()));
174         return result;
175     }
176 
177 }