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.technical.Dates;
32 import fr.ifremer.quadrige2.core.exception.BadUpdateDtException;
33 import fr.ifremer.quadrige2.core.vo.system.rule.RuleParameterVO;
34 import org.hibernate.SessionFactory;
35 import org.nuiton.i18n.I18n;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.context.annotation.Lazy;
38 import org.springframework.stereotype.Repository;
39
40 import java.sql.Timestamp;
41 import java.util.Collection;
42 import java.util.Objects;
43
44
45
46
47 @Repository("ruleParameterDao")
48 @Lazy
49 public class RuleParameterDaoImpl
50 extends RuleParameterDaoBase
51 {
52
53
54
55 @Autowired
56 public RuleParameterDaoImpl(SessionFactory sessionFactory) {
57 super();
58 setSessionFactory(sessionFactory);
59 }
60
61
62
63
64 protected RuleParameterVO handleSave(RuleParameterVO source, Timestamp updateDt) {
65 Preconditions.checkNotNull(source);
66 Preconditions.checkNotNull(source.getRuleCd());
67
68
69 Rule parent = get(RuleImpl.class, source.getRuleCd());
70
71
72 RuleParameter entity = null;
73 boolean isNew = false;
74 if (source.getRuleParId() != null) {
75 entity = get(source.getRuleParId());
76 }
77 if (entity == null) {
78 entity = RuleParameter.Factory.newInstance();
79 parent.addRuleParameters(entity);
80 entity.setRule(parent);
81 isNew = true;
82 }
83
84
85 if (!isNew) {
86 if (entity.getUpdateDt() != null) {
87 Timestamp serverUpdateDtNoMillisecond = Dates.resetMillisecond(entity.getUpdateDt());
88 if (source.getUpdateDt() == null) {
89 throw new BadUpdateDtException(I18n.t("quadrige2.dao.rule.badUpdateDt", source.getRuleCd(),
90 serverUpdateDtNoMillisecond,
91 "null"));
92 }
93 Timestamp sourceUpdateDtNoMillisecond = Dates.resetMillisecond(source.getUpdateDt());
94 if (!Objects.equals(sourceUpdateDtNoMillisecond, serverUpdateDtNoMillisecond)) {
95 throw new BadUpdateDtException(I18n.t("quadrige2.dao.rule.badUpdateDt", source.getRuleCd(), serverUpdateDtNoMillisecond,
96 sourceUpdateDtNoMillisecond));
97 }
98 }
99 }
100
101
102 Timestamp newUpdateDt = updateDt;
103 if (newUpdateDt == null) {
104 newUpdateDt = getDatabaseCurrentTimestamp();
105 }
106 source.setUpdateDt(newUpdateDt);
107
108
109 ruleParameterVOToEntity(source, entity, true);
110
111
112 if (isNew) {
113 getSession().save(entity);
114
115 source.setRuleParId(entity.getRuleParId());
116 } else {
117 getSession().update(entity);
118 }
119
120 return source;
121 }
122
123
124
125
126 protected void handleRemoveByIds(Collection<Integer> ruleParIds) {
127 for (Integer ruleParId: ruleParIds) {
128 remove(ruleParId);
129 }
130 }
131
132
133
134
135 public void toRuleParameterVO(
136 RuleParameter source,
137 RuleParameterVO target)
138 {
139 super.toRuleParameterVO(source, target);
140
141
142 if (source.getRule() == null) {
143 target.setRuleCd(null);
144 }
145 else {
146 target.setRuleCd(source.getRule().getRuleCd());
147 }
148
149
150 if (source.getFunctionParameter() == null) {
151 target.setFunctionParId(null);
152 }
153 else {
154 target.setFunctionParId(source.getFunctionParameter().getFunctionParId());
155 }
156
157 }
158
159
160
161
162
163
164 private RuleParameter loadRuleParameterFromRuleParameterVO(RuleParameterVO ruleParameterVO)
165 {
166 RuleParameter ruleParameter = null;
167 if (ruleParameterVO.getRuleParId() != null) {
168 ruleParameter = this.get(ruleParameterVO.getRuleParId());
169 }
170 if (ruleParameter == null) {
171 ruleParameter = RuleParameter.Factory.newInstance();
172 }
173 return ruleParameter;
174 }
175
176
177
178
179 public RuleParameter ruleParameterVOToEntity(RuleParameterVO ruleParameterVO)
180 {
181 RuleParameter entity = this.loadRuleParameterFromRuleParameterVO(ruleParameterVO);
182 this.ruleParameterVOToEntity(ruleParameterVO, entity, true);
183 return entity;
184 }
185
186
187
188
189 @Override
190 public void ruleParameterVOToEntity(
191 RuleParameterVO source,
192 RuleParameter target,
193 boolean copyIfNull)
194 {
195 super.ruleParameterVOToEntity(source, target, copyIfNull);
196
197
198 if (copyIfNull || source.getRuleParId() != null) {
199 target.setRuleParId(source.getRuleParId());
200 }
201
202
203 if (copyIfNull || source.getRuleCd() != null) {
204 if (source.getRuleCd() == null) {
205 target.setRule(null);
206 }
207 else {
208 target.setRule(load(RuleImpl.class, source.getRuleCd()));
209 }
210 }
211
212
213 if (copyIfNull || source.getFunctionParId() != null) {
214 if (source.getFunctionParId() == null) {
215 target.setFunctionParameter(null);
216 }
217 else {
218 target.setFunctionParameter(load(FunctionParameterImpl.class, source.getFunctionParId()));
219 }
220 }
221 }
222 }