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.MethodDTO;
34  import fr.ifremer.quadrige3.core.dao.referential.pmfm.MethodDaoImpl;
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.cache.Cache;
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   * Method DAO
52   * Created by Ludovic on 29/07/2015.
53   */
54  @Repository("daliMethodDao")
55  public class DaliMethodDaoImpl extends MethodDaoImpl implements DaliMethodDao {
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 = "daliReferentialDao")
67      protected DaliReferentialDao referentialDao;
68  
69      /**
70       * <p>Constructor for DaliMethodDaoImpl.</p>
71       *
72       * @param sessionFactory a {@link org.hibernate.SessionFactory} object.
73       */
74      @Autowired
75      public DaliMethodDaoImpl(SessionFactory sessionFactory) {
76          super(sessionFactory);
77      }
78  
79      /** {@inheritDoc} */
80      @Override
81      public List<MethodDTO> getAllMethods(List<String> statusCodes) {
82  
83          Cache cacheById = cacheService.getCache(METHOD_BY_ID_CACHE);
84          List<MethodDTO> result = Lists.newArrayList();
85          Map<Integer, String> transcribingNamesById = getTranscribingNames();
86  
87          Iterator<Object[]> it = Daos.queryIteratorWithStatus(createQuery("allMethods"), statusCodes);
88  
89          while (it.hasNext()) {
90              Object[] source = it.next();
91              MethodDTO method = toMethodDTO(Arrays.asList(source).iterator(), transcribingNamesById);
92              result.add(method);
93  
94              cacheById.put(method.getId(), method);
95          }
96  
97          return ImmutableList.copyOf(result);
98      }
99  
100     /** {@inheritDoc} */
101     @Override
102     public MethodDTO getMethodById(int methodId) {
103 
104         Object[] source = queryUnique("methodById", "methodId", IntegerType.INSTANCE, methodId);
105 
106         if (source == null) {
107             throw new DataRetrievalFailureException("can't load method with id = " + methodId);
108         }
109 
110         return toMethodDTO(Arrays.asList(source).iterator(), getTranscribingNames());
111     }
112 
113     /** {@inheritDoc} */
114     @Override
115     public List<MethodDTO> findMethods(Integer methodId, List<String> statusCodes) {
116         List<MethodDTO> result = Lists.newArrayList();
117         Map<Integer, String> transcribingNamesById = getTranscribingNames();
118 
119         Query query = createQuery("methodsByCriteria", "methodId", IntegerType.INSTANCE, methodId);
120         Iterator<Object[]> it = Daos.queryIteratorWithStatus(query, statusCodes);
121 
122         while (it.hasNext()) {
123             Object[] source = it.next();
124             MethodDTO method = toMethodDTO(Arrays.asList(source).iterator(), transcribingNamesById);
125             result.add(method);
126         }
127 
128         return ImmutableList.copyOf(result);
129     }
130 
131     private Map<Integer, String> getTranscribingNames() {
132         return transcribingItemDao.getAllTranscribingItemsById(config.getTranscribingItemTypeLbForMethodNm());
133     }
134 
135     private MethodDTO toMethodDTO(Iterator<Object> source, Map<Integer, String> transcribingNamesById) {
136         MethodDTO result = DaliBeanFactory.newMethodDTO();
137         result.setId((Integer) source.next());
138         result.setName((String) source.next());
139         result.setDescription((String) source.next());
140         result.setReference((String) source.next());
141         result.setNumber((String) source.next());
142         result.setDescriptionPackaging((String) source.next());
143         result.setDescriptionPreparation((String) source.next());
144         result.setDescriptionPreservation((String) source.next());
145         result.setStatus(referentialDao.getStatusByCode((String) source.next()));
146         result.setComment((String) source.next());
147         result.setCreationDate(Daos.convertToDate(source.next()));
148         result.setUpdateDate(Daos.convertToDate(source.next()));
149 
150         // transcribed name
151         result.setName(transcribingNamesById.getOrDefault(result.getId(), result.getName()));
152 
153         return result;
154     }
155 
156 }