View Javadoc
1   package fr.ifremer.reefdb.dao.referential.pmfm;
2   
3   /*
4    * #%L
5    * Reef DB :: 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.Lists;
27  import fr.ifremer.quadrige3.core.dao.referential.StatusCode;
28  import fr.ifremer.quadrige3.core.dao.referential.StatusImpl;
29  import fr.ifremer.quadrige3.core.dao.referential.pmfm.ParameterImpl;
30  import fr.ifremer.quadrige3.core.dao.referential.pmfm.QualitativeValue;
31  import fr.ifremer.quadrige3.core.dao.referential.pmfm.QualitativeValueDaoImpl;
32  import fr.ifremer.quadrige3.core.dao.technical.Assert;
33  import fr.ifremer.quadrige3.core.dao.technical.hibernate.TemporaryDataHelper;
34  import fr.ifremer.quadrige3.core.service.technical.CacheService;
35  import fr.ifremer.reefdb.dao.technical.Daos;
36  import fr.ifremer.reefdb.dto.ReefDbBeanFactory;
37  import fr.ifremer.reefdb.dto.ReefDbBeans;
38  import fr.ifremer.reefdb.dto.referential.pmfm.QualitativeValueDTO;
39  import fr.ifremer.reefdb.service.StatusFilter;
40  import org.hibernate.SessionFactory;
41  import org.hibernate.type.IntegerType;
42  import org.hibernate.type.StringType;
43  import org.springframework.beans.factory.annotation.Autowired;
44  import org.springframework.cache.Cache;
45  import org.springframework.dao.DataRetrievalFailureException;
46  import org.springframework.stereotype.Repository;
47  
48  import javax.annotation.Resource;
49  import java.util.Arrays;
50  import java.util.Iterator;
51  import java.util.List;
52  
53  /**
54   * Dao for QualitativeValue
55   * Created by Ludovic on 22/09/2015.
56   */
57  @Repository("reefDbQualitativeValueDao")
58  public class ReefDbQualitativeValueDaoImpl extends QualitativeValueDaoImpl implements ReefDbQualitativeValueDao {
59  
60      @Resource
61      protected CacheService cacheService;
62  
63      /**
64       * Constructor used by Spring
65       *
66       * @param sessionFactory a {@link org.hibernate.SessionFactory} object.
67       */
68      @Autowired
69      public ReefDbQualitativeValueDaoImpl(SessionFactory sessionFactory) {
70          super(sessionFactory);
71      }
72  
73      /** {@inheritDoc} */
74      @Override
75      public List<QualitativeValueDTO> getQualitativeValuesByParameterCode(String parameterCode) {
76  
77          Cache cacheById = cacheService.getCache(QUALITATIVE_VALUE_BY_ID_CACHE);
78  
79          Iterator<Object[]> it = Daos.queryIteratorWithStatus(
80                  createQuery("allQualitativeValuesByParameterCode", "parameterCode", StringType.INSTANCE, parameterCode),
81                  StatusFilter.ALL.toStatusCodes());
82          List<QualitativeValueDTO> result = Lists.newArrayList();
83          while (it.hasNext()) {
84              Object[] source = it.next();
85              QualitativeValueDTO qualitativeValue = toQualitativeValueDTO(Arrays.asList(source).iterator());
86              result.add(qualitativeValue);
87              cacheById.put(qualitativeValue.getId(), qualitativeValue);
88          }
89  
90          return result;
91      }
92  
93      /** {@inheritDoc} */
94      @Override
95      public List<QualitativeValueDTO> getQualitativeValuesByPmfmId(Integer pmfmId) {
96          Iterator<Object[]> it = Daos.queryIteratorWithStatus(
97                  createQuery("allQualitativeValuesByPmfmId", "pmfmId", IntegerType.INSTANCE, pmfmId),
98                  StatusFilter.ACTIVE.toStatusCodes());
99          List<QualitativeValueDTO> result = Lists.newArrayList();
100         while (it.hasNext()) {
101             Object[] source = it.next();
102             QualitativeValueDTO qualitativeValue = toQualitativeValueDTO(Arrays.asList(source).iterator());
103             result.add(qualitativeValue);
104         }
105 
106         return result;
107     }
108 
109     /** {@inheritDoc} */
110     @Override
111     public QualitativeValueDTO getQualitativeValueById(int qualitativeValueId) {
112         Object[] source = queryUnique("qualitativeValueById", "qualitativeValueId", IntegerType.INSTANCE, qualitativeValueId);
113 
114         if (source == null) {
115             throw new DataRetrievalFailureException("can't load qualitative value with id = " + qualitativeValueId);
116         }
117 
118         return toQualitativeValueDTO(Arrays.asList(source).iterator());
119     }
120 
121     /** {@inheritDoc} */
122     @Override
123     public void saveQualitativeValue(String parameterCode, QualitativeValueDTO qualitativeValue) {
124         Assert.notBlank(parameterCode);
125         Assert.notNull(qualitativeValue);
126         Assert.notBlank(qualitativeValue.getName());
127         if (qualitativeValue.getStatus() == null) {
128             qualitativeValue.setStatus(Daos.getStatus(StatusCode.LOCAL_ENABLE));
129         }
130         Assert.isTrue(ReefDbBeans.isLocalStatus(qualitativeValue.getStatus()), "source must have local status");
131 
132         QualitativeValue target;
133         if (qualitativeValue.getId() == null) {
134             target = QualitativeValue.Factory.newInstance();
135             target.setQualValueId(TemporaryDataHelper.getNewNegativeIdForTemporaryData(getSession(), target.getClass()));
136             target.setParameter(load(ParameterImpl.class, parameterCode));
137         } else {
138             target = get(qualitativeValue.getId());
139             Assert.isTrue(ReefDbBeans.isLocalStatus(target.getStatus()), "target must have local status");
140             Assert.equals(target.getParameter().getParCd(), parameterCode, "existing qualitative value for another parameter");
141         }
142 
143         target.setQualValueNm(qualitativeValue.getName());
144         target.setQualValueDc(qualitativeValue.getDescription());
145         target.setStatus(load(StatusImpl.class, qualitativeValue.getStatus().getCode()));
146 //        target.setUpdateDt(newUpdateTimestamp());
147 
148         getSession().save(target);
149         getSession().flush();
150         qualitativeValue.setId(target.getQualValueId());
151     }
152 
153     private QualitativeValueDTO toQualitativeValueDTO(Iterator<Object> source) {
154         QualitativeValueDTO result = ReefDbBeanFactory.newQualitativeValueDTO();
155         result.setId((Integer) source.next());
156         result.setName((String) source.next());
157         result.setDescription((String) source.next());
158         result.setStatus(Daos.getStatus((String) source.next()));
159         return result;
160     }
161 
162 }