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.ImmutableList;
27 import com.google.common.collect.ImmutableListMultimap;
28 import com.google.common.collect.Lists;
29 import com.google.common.collect.Multimap;
30 import fr.ifremer.quadrige3.core.dao.referential.StatusCode;
31 import fr.ifremer.quadrige3.core.dao.referential.StatusImpl;
32 import fr.ifremer.quadrige3.core.dao.referential.pmfm.Method;
33 import fr.ifremer.quadrige3.core.dao.referential.pmfm.MethodDaoImpl;
34 import fr.ifremer.quadrige3.core.dao.technical.Assert;
35 import fr.ifremer.quadrige3.core.dao.technical.hibernate.TemporaryDataHelper;
36 import fr.ifremer.quadrige3.core.service.technical.CacheService;
37 import fr.ifremer.reefdb.dao.technical.Daos;
38 import fr.ifremer.reefdb.dto.ReefDbBeanFactory;
39 import fr.ifremer.reefdb.dto.ReefDbBeans;
40 import fr.ifremer.reefdb.dto.referential.pmfm.MethodDTO;
41 import org.apache.commons.collections4.CollectionUtils;
42 import org.hibernate.Query;
43 import org.hibernate.SessionFactory;
44 import org.hibernate.type.IntegerType;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.cache.Cache;
47 import org.springframework.dao.DataRetrievalFailureException;
48 import org.springframework.stereotype.Repository;
49
50 import javax.annotation.Resource;
51 import java.util.Arrays;
52 import java.util.Iterator;
53 import java.util.List;
54 import java.util.Objects;
55
56
57
58
59
60 @Repository("reefDbMethodDao")
61 public class ReefDbMethodDaoImpl extends MethodDaoImpl implements ReefDbMethodDao {
62
63 private static final Multimap<String, String> columnNamesByRulesTableNames = ImmutableListMultimap.<String, String>builder()
64 .put("RULE_PMFM", "METHOD_ID").build();
65
66 private static final Multimap<String, String> columnNamesByReferentialTableNames = ImmutableListMultimap.<String, String>builder()
67 .put("PMFM", "METHOD_ID").build();
68
69 @Resource
70 protected CacheService cacheService;
71
72
73
74
75
76
77 @Autowired
78 public ReefDbMethodDaoImpl(SessionFactory sessionFactory) {
79 super(sessionFactory);
80 }
81
82
83 @Override
84 public List<MethodDTO> getAllMethods(List<String> statusCodes) {
85
86 Cache cacheById = cacheService.getCache(METHOD_BY_ID_CACHE);
87
88 Iterator<Object[]> it = Daos.queryIteratorWithStatus(createQuery("allMethods"), statusCodes);
89
90 List<MethodDTO> result = Lists.newArrayList();
91 while (it.hasNext()) {
92 Object[] source = it.next();
93 MethodDTO method = toMethodDTO(Arrays.asList(source).iterator());
94 result.add(method);
95
96 cacheById.put(method.getId(), method);
97 }
98
99 return ImmutableList.copyOf(result);
100 }
101
102
103 @Override
104 public MethodDTO getMethodById(int methodId) {
105
106 Object[] source = queryUnique("methodById", "methodId", IntegerType.INSTANCE, methodId);
107
108 if (source == null) {
109 throw new DataRetrievalFailureException("can't load method with id = " + methodId);
110 }
111
112 return toMethodDTO(Arrays.asList(source).iterator());
113 }
114
115
116 @Override
117 public List<MethodDTO> findMethods(Integer methodId, List<String> statusCodes) {
118
119 Query query = createQuery("methodsByCriteria", "methodId", IntegerType.INSTANCE, methodId);
120 Iterator<Object[]> it = Daos.queryIteratorWithStatus(query, statusCodes);
121
122 List<MethodDTO> result = Lists.newArrayList();
123 while (it.hasNext()) {
124 Object[] source = it.next();
125 MethodDTO method = toMethodDTO(Arrays.asList(source).iterator());
126 result.add(method);
127 }
128
129 return ImmutableList.copyOf(result);
130 }
131
132
133 @Override
134 public void saveMethods(List<? extends MethodDTO> methods) {
135 if (CollectionUtils.isEmpty(methods)) {
136 return;
137 }
138
139 for (MethodDTO method : methods) {
140 if (method.isDirty()) {
141 saveMethod(method);
142 method.setDirty(false);
143 }
144 }
145 getSession().flush();
146 getSession().clear();
147 }
148
149
150 @Override
151 public void deleteMethods(List<Integer> methodIds) {
152 if (methodIds == null) return;
153 methodIds.stream().filter(Objects::nonNull).distinct().forEach(this::remove);
154 getSession().flush();
155 getSession().clear();
156 }
157
158
159 @Override
160 public void replaceTemporaryMethod(Integer sourceId, Integer targetId, boolean delete) {
161 Assert.notNull(sourceId);
162 Assert.notNull(targetId);
163
164 executeMultipleUpdate(columnNamesByReferentialTableNames, sourceId, targetId);
165
166 if (delete) {
167
168 remove(sourceId);
169 }
170
171 getSession().flush();
172 getSession().clear();
173 }
174
175
176 @Override
177 public boolean isMethodUsedInProgram(int methodId) {
178
179 return queryCount("countPmfmStrategyByMethodId", "methodId", IntegerType.INSTANCE, methodId) > 0;
180 }
181
182
183 @Override
184 public boolean isMethodUsedInRules(int methodId) {
185
186 return executeMultipleCount(columnNamesByRulesTableNames, methodId);
187 }
188
189
190 @Override
191 public boolean isMethodUsedInReferential(int methodId) {
192
193 return executeMultipleCount(columnNamesByReferentialTableNames, methodId) || isMethodUsedInRules(methodId);
194 }
195
196 private void saveMethod(MethodDTO method) {
197 Assert.notNull(method);
198 Assert.notBlank(method.getName());
199
200 if (method.getStatus() == null) {
201 method.setStatus(Daos.getStatus(StatusCode.LOCAL_ENABLE));
202 }
203 Assert.isTrue(ReefDbBeans.isLocalStatus(method.getStatus()), "source must have local status");
204
205 Method target;
206 if (method.getId() == null) {
207 target = Method.Factory.newInstance();
208 target.setMethodId(TemporaryDataHelper.getNewNegativeIdForTemporaryData(getSession(), target.getClass()));
209 } else {
210 target = get(method.getId());
211 Assert.isTrue(ReefDbBeans.isLocalStatus(target.getStatus()), "target must have local status");
212 }
213
214 target.setMethodNm(method.getName());
215 target.setStatus(load(StatusImpl.class, method.getStatus().getCode()));
216 target.setMethodRk("1");
217
218 target.setMethodRef(method.getReference());
219 target.setMethodDc(method.getDescription());
220 target.setMethodCondition(method.getDescriptionPackaging());
221 target.setMethodPrepar(method.getDescriptionPreparation());
222 target.setMethodConserv(method.getDescriptionPreservation());
223
224 if (target.getMethodCreationDt() == null) {
225 target.setMethodCreationDt(newCreateDate());
226 }
227 target.setUpdateDt(newUpdateTimestamp());
228
229 getSession().save(target);
230 method.setId(target.getMethodId());
231
232 }
233
234 private MethodDTO toMethodDTO(Iterator<Object> source) {
235 MethodDTO result = ReefDbBeanFactory.newMethodDTO();
236 result.setId((Integer) source.next());
237 result.setName((String) source.next());
238 result.setDescription((String) source.next());
239 result.setReference((String) source.next());
240 result.setNumber((String) source.next());
241 result.setDescriptionPackaging((String) source.next());
242 result.setDescriptionPreparation((String) source.next());
243 result.setDescriptionPreservation((String) source.next());
244 result.setStatus(Daos.getStatus((String) source.next()));
245 result.setComment((String) source.next());
246 result.setCreationDate(Daos.convertToDate(source.next()));
247 result.setUpdateDate(Daos.convertToDate(source.next()));
248 return result;
249 }
250
251 }