1 package fr.ifremer.reefdb.dao.referential.pmfm;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
55
56
57 @Repository("reefDbQualitativeValueDao")
58 public class ReefDbQualitativeValueDaoImpl extends QualitativeValueDaoImpl implements ReefDbQualitativeValueDao {
59
60 @Resource
61 protected CacheService cacheService;
62
63
64
65
66
67
68 @Autowired
69 public ReefDbQualitativeValueDaoImpl(SessionFactory sessionFactory) {
70 super(sessionFactory);
71 }
72
73
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
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
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
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
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 }