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 com.google.common.collect.Lists;
28  import fr.ifremer.dali.config.DaliConfiguration;
29  import fr.ifremer.dali.dao.referential.transcribing.DaliTranscribingItemDao;
30  import fr.ifremer.dali.dao.technical.Daos;
31  import fr.ifremer.dali.dto.DaliBeanFactory;
32  import fr.ifremer.dali.dto.referential.UnitDTO;
33  import fr.ifremer.quadrige3.core.dao.referential.UnitDaoImpl;
34  import fr.ifremer.quadrige3.core.service.technical.CacheService;
35  import org.hibernate.Query;
36  import org.hibernate.SessionFactory;
37  import org.hibernate.type.IntegerType;
38  import org.springframework.beans.factory.annotation.Autowired;
39  import org.springframework.cache.Cache;
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   * Unit DAO
51   * <p/>
52   * Created by Ludovic on 28/07/2015.
53   */
54  @Repository("daliUnitDao")
55  public class DaliUnitDaoImpl extends UnitDaoImpl implements DaliUnitDao {
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       * Constructor used by Spring
71       *
72       * @param sessionFactory a {@link org.hibernate.SessionFactory} object.
73       */
74      @Autowired
75      public DaliUnitDaoImpl(SessionFactory sessionFactory) {
76          super(sessionFactory);
77      }
78  
79      /** {@inheritDoc} */
80      @Override
81      public UnitDTO getUnitById(int unitId) {
82  
83          Object[] source = queryUnique("unitById", "unitId", IntegerType.INSTANCE, unitId);
84  
85          if (source == null) {
86              throw new DataRetrievalFailureException("can't load unit with id = " + unitId);
87          }
88  
89          return toUnitDTO(Arrays.asList(source).iterator(), getTranscribingNames(), getTranscribingSymbols());
90      }
91  
92      /** {@inheritDoc} */
93      @Override
94      public List<UnitDTO> getAllUnits(List<String> statusCodes) {
95  
96          Cache cacheById = cacheService.getCache(UNIT_BY_ID_CACHE);
97          List<UnitDTO> result = Lists.newArrayList();
98          Map<Integer, String> transcribingNamesById = getTranscribingNames();
99          Map<Integer, String> transcribingSymbolsById = getTranscribingSymbols();
100 
101         Iterator<Object[]> it = Daos.queryIteratorWithStatus(createQuery("allUnits"), statusCodes);
102 
103         while (it.hasNext()) {
104             Object[] source = it.next();
105             UnitDTO unit = toUnitDTO(Arrays.asList(source).iterator(), transcribingNamesById, transcribingSymbolsById);
106             if (unit != null) {
107                 result.add(unit);
108 
109                 // update cache
110                 cacheById.put(unit.getId(), unit);
111             }
112         }
113 
114         return ImmutableList.copyOf(result);
115     }
116 
117     /** {@inheritDoc} */
118     @Override
119     public List<UnitDTO> findUnits(Integer unitId, List<String> statusCodes) {
120 
121         List<UnitDTO> result = Lists.newArrayList();
122         Map<Integer, String> transcribingNamesById = getTranscribingNames();
123         Map<Integer, String> transcribingSymbolsById = getTranscribingSymbols();
124 
125         Query query = createQuery("unitsByCriteria", "unitId", IntegerType.INSTANCE, unitId);
126         Iterator<Object[]> it = Daos.queryIteratorWithStatus(query, statusCodes);
127 
128         while (it.hasNext()) {
129             Object[] source = it.next();
130             UnitDTO unit = toUnitDTO(Arrays.asList(source).iterator(), transcribingNamesById, transcribingSymbolsById);
131             if (unit != null) result.add(unit);
132         }
133 
134         return ImmutableList.copyOf(result);
135     }
136 
137     private Map<Integer, String> getTranscribingNames() {
138         return transcribingItemDao.getAllTranscribingItemsById(config.getTranscribingItemTypeLbForUnitNm());
139     }
140 
141     private Map<Integer, String> getTranscribingSymbols() {
142         return transcribingItemDao.getAllTranscribingItemsById(config.getTranscribingItemTypeLbForUnitSymbol());
143     }
144 
145     private UnitDTO toUnitDTO(Iterator<Object> source, Map<Integer, String> transcribingNamesById, Map<Integer, String> transcribingSymbolsById) {
146         UnitDTO result = DaliBeanFactory.newUnitDTO();
147         result.setId((Integer) source.next());
148         if (result.getId() == null) {
149             source.next();
150             source.next();
151             source.next();
152             return null;
153         }
154         result.setName((String) source.next());
155         result.setSymbol((String) source.next());
156         result.setStatus(referentialDao.getStatusByCode((String) source.next()));
157         result.setComment((String) source.next());
158         result.setCreationDate(Daos.convertToDate(source.next()));
159         result.setUpdateDate(Daos.convertToDate(source.next()));
160 
161         // transcribed name
162         result.setName(transcribingNamesById.getOrDefault(result.getId(), result.getName()));
163 
164         // transcribed symbol
165         result.setSymbol(transcribingSymbolsById.getOrDefault(result.getId(), result.getSymbol()));
166 
167         return result;
168     }
169 
170 }