1
2
3
4
5
6
7 package fr.ifremer.quadrige2.core.dao.system.rule;
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 import com.google.common.base.Preconditions;
31 import fr.ifremer.quadrige2.core.dao.system.synchronization.DeletedItemHistoryExtendDao;
32 import fr.ifremer.quadrige2.core.dao.technical.Beans;
33 import fr.ifremer.quadrige2.core.dao.technical.Daos;
34 import fr.ifremer.quadrige2.core.dao.technical.Dates;
35 import fr.ifremer.quadrige2.core.exception.BadUpdateDtException;
36 import fr.ifremer.quadrige2.core.vo.system.rule.RuleParameterVO;
37 import fr.ifremer.quadrige2.core.vo.system.rule.RulePmfmVO;
38 import fr.ifremer.quadrige2.core.vo.system.rule.RuleVO;
39 import org.apache.commons.collections4.CollectionUtils;
40 import org.apache.commons.collections4.MapUtils;
41 import org.apache.commons.lang3.ArrayUtils;
42 import org.hibernate.SessionFactory;
43 import org.nuiton.i18n.I18n;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.context.annotation.Lazy;
46 import org.springframework.stereotype.Repository;
47
48 import javax.annotation.Resource;
49 import java.sql.Timestamp;
50 import java.util.Collection;
51 import java.util.Map;
52 import java.util.Objects;
53
54
55
56
57 @Repository("ruleDao")
58 @Lazy
59 public class RuleDaoImpl
60 extends RuleDaoBase
61 {
62
63 @Resource
64 private RuleParameterDao ruleParameterDao;
65
66 @Resource
67 private RulePmfmDao rulePmfmDao;
68
69 @Resource(name = "deletedItemHistoryDao")
70 private DeletedItemHistoryExtendDao deletedItemHistoryDao;
71
72
73
74
75 @Autowired
76 public RuleDaoImpl(SessionFactory sessionFactory) {
77 super();
78 setSessionFactory(sessionFactory);
79 }
80
81
82 @Override
83 public void remove(Collection<Rule> entities) {
84 Preconditions.checkNotNull(entities);
85 Preconditions.checkArgument(entities.size() > 0);
86
87 for (Rule entity : entities) {
88 remove(entity);
89 }
90 }
91
92
93 @Override
94 public void remove(Rule entity) {
95
96
97 if (CollectionUtils.isNotEmpty(entity.getRuleParameters())) {
98 ruleParameterDao.remove(entity.getRuleParameters());
99 entity.getRuleParameters().clear();
100 }
101
102 getSession().flush();
103
104
105 if (CollectionUtils.isNotEmpty(entity.getRulePmfms())) {
106 rulePmfmDao.remove(entity.getRulePmfms());
107 entity.getRulePmfms().clear();
108 }
109
110 getSession().flush();
111
112
113 deletedItemHistoryDao.insertDeletedItem(RuleImpl.class, entity);
114
115 super.remove(entity);
116 }
117
118
119
120
121 protected RuleVO handleSave(RuleVO source, Timestamp updateDt) {
122 Preconditions.checkNotNull(source);
123 Preconditions.checkNotNull(source.getRuleCd());
124
125
126 RuleList parent = get(RuleListImpl.class, source.getRuleListCd());
127
128
129 Rule entity = get(source.getRuleCd());
130 boolean isNew = false;
131 if (entity == null) {
132 entity = Rule.Factory.newInstance();
133 parent.addRules(entity);
134 entity.setRuleList(parent);
135 isNew = true;
136 }
137
138
139 if (!isNew) {
140 if (entity.getUpdateDt() != null) {
141 Timestamp serverUpdateDtNoMillisecond = Dates.resetMillisecond(entity.getUpdateDt());
142 if (source.getUpdateDt() == null) {
143 throw new BadUpdateDtException(I18n.t("quadrige2.dao.rule.badUpdateDt", source.getRuleListCd(),
144 serverUpdateDtNoMillisecond,
145 "null"));
146 }
147 Timestamp sourceUpdateDtNoMillisecond = Dates.resetMillisecond(source.getUpdateDt());
148 if (!Objects.equals(sourceUpdateDtNoMillisecond, serverUpdateDtNoMillisecond)) {
149 throw new BadUpdateDtException(I18n.t("quadrige2.dao.rule.badUpdateDt", source.getRuleCd(), serverUpdateDtNoMillisecond,
150 sourceUpdateDtNoMillisecond));
151 }
152 }
153 }
154
155
156 Timestamp newUpdateDt = updateDt;
157 if (newUpdateDt == null) {
158 newUpdateDt = getDatabaseCurrentTimestamp();
159 }
160 source.setUpdateDt(newUpdateDt);
161
162
163 ruleVOToEntity(source, entity, true, false );
164
165
166 if (isNew) {
167 getSession().save(entity);
168 } else {
169 getSession().update(entity);
170 }
171
172
173 Map<Integer, RuleParameter> ruleParametersToRemove = Beans.splitByProperty(entity.getRuleParameters(), "ruleParId");
174 Map<Integer, RulePmfm> rulePmfmsToRemove = Beans.splitByProperty(entity.getRulePmfms(), "rulePmfmId");
175
176
177 if (ArrayUtils.isNotEmpty(source.getRuleParameterVOs())) {
178 for (RuleParameterVO ruleParameterVO : source.getRuleParameterVOs()) {
179 ruleParameterVO.setRuleCd(entity.getRuleCd());
180 ruleParameterVO = ruleParameterDao.save(ruleParameterVO, newUpdateDt);
181 ruleParametersToRemove.remove(ruleParameterVO.getRuleParId());
182 }
183 }
184
185
186 if (ArrayUtils.isNotEmpty(source.getRulePmfmVOs())) {
187 for (RulePmfmVO rulePmfmVO : source.getRulePmfmVOs()) {
188 rulePmfmVO.setRuleCd(entity.getRuleCd());
189 rulePmfmVO = rulePmfmDao.save(rulePmfmVO, newUpdateDt);
190 rulePmfmsToRemove.remove(rulePmfmVO.getRulePmfmId());
191 }
192 }
193
194
195 if (MapUtils.isNotEmpty(ruleParametersToRemove)) {
196 ruleParameterDao.remove(ruleParametersToRemove.values());
197 entity.getRuleParameters().removeAll(ruleParametersToRemove.values());
198 }
199
200
201 if (MapUtils.isNotEmpty(rulePmfmsToRemove)) {
202 rulePmfmDao.remove(rulePmfmsToRemove.values());
203 entity.getRulePmfms().removeAll(rulePmfmsToRemove.values());
204 }
205
206 return source;
207 }
208
209
210
211
212 protected void handleRemoveByCds(String[] ruleCds) {
213 for (String ruleCd: ruleCds) {
214 remove(ruleCd);
215 }
216 }
217
218
219
220
221 public void toRuleVO(
222 Rule source,
223 RuleVO target)
224 {
225
226 super.toRuleVO(source, target);
227
228
229 if (source.getRuleList() == null) {
230 target.setRuleListCd(null);
231 }
232 else {
233 target.setRuleListCd(source.getRuleList().getRuleListCd());
234 }
235
236
237 if (source.getFunction() == null) {
238 target.setFunctionId(null);
239 } else {
240 target.setFunctionId(source.getFunction().getFunctionId());
241 }
242
243
244 if (CollectionUtils.isEmpty(source.getRuleParameters())) {
245 target.setRuleParameterVOs(null);
246 }
247 else {
248 target.setRuleParameterVOs(ruleParameterDao.toRuleParameterVOArray(source.getRuleParameters()));
249 }
250
251
252 if (CollectionUtils.isEmpty(source.getRulePmfms())) {
253 target.setRulePmfmVOs(null);
254 }
255 else {
256 target.setRulePmfmVOs(rulePmfmDao.toRulePmfmVOArray(source.getRulePmfms()));
257 }
258 }
259
260
261
262
263
264
265 private Rule loadRuleFromRuleVO(RuleVO ruleVO)
266 {
267 Rule rule = null;
268 if (ruleVO.getRuleCd() != null) {
269 rule = this.get(ruleVO.getRuleCd());
270 }
271 if (rule == null)
272 {
273 rule = Rule.Factory.newInstance();
274 }
275 return rule;
276 }
277
278
279
280
281 public Rule ruleVOToEntity(RuleVO ruleVO)
282 {
283 Rule entity = this.loadRuleFromRuleVO(ruleVO);
284 this.ruleVOToEntity(ruleVO, entity, true);
285 return entity;
286 }
287
288
289
290
291 @Override
292 public void ruleVOToEntity(
293 RuleVO source,
294 Rule target,
295 boolean copyIfNull)
296 {
297 this.ruleVOToEntity(source, target, copyIfNull, false);
298 }
299
300
301
302
303 protected void ruleVOToEntity(
304 RuleVO source,
305 Rule target,
306 boolean copyIfNull,
307 boolean withChildrenEntities)
308 {
309
310 super.ruleVOToEntity(source, target, copyIfNull);
311
312
313 if (copyIfNull || source.getRuleCd() != null)
314 {
315 target.setRuleCd(source.getRuleCd());
316 }
317
318
319 if (copyIfNull || source.getRuleListCd() != null) {
320 if (source.getRuleListCd() == null) {
321 target.setRuleList(null);
322 }
323 else {
324 target.setRuleList(load(RuleListImpl.class, source.getRuleListCd()));
325 }
326 }
327
328
329 if (copyIfNull || source.getFunctionId() != null)
330 {
331 if (source.getFunctionId() == null) {
332 target.setFunction(null);
333 }
334 else {
335 target.setFunction(load(FunctionImpl.class, source.getFunctionId()));
336 }
337 }
338
339 if (withChildrenEntities) {
340
341
342 if (copyIfNull || ArrayUtils.isNotEmpty(source.getRuleParameterVOs())) {
343 if (ArrayUtils.isEmpty(source.getRuleParameterVOs())) {
344 target.getRuleParameters().clear();
345 } else {
346 Daos.replaceEntities(target.getRuleParameters(),
347 source.getRuleParameterVOs(),
348 vo -> ruleParameterDao.ruleParameterVOToEntity(vo));
349 }
350 }
351
352
353 if (copyIfNull || ArrayUtils.isNotEmpty(source.getRulePmfmVOs())) {
354 if (ArrayUtils.isEmpty(source.getRulePmfmVOs())) {
355 target.getRulePmfms().clear();
356 } else {
357 Daos.replaceEntities(target.getRulePmfms(),
358 source.getRulePmfmVOs(),
359 vo -> rulePmfmDao.rulePmfmVOToEntity(vo));
360 }
361 }
362 }
363 }
364 }