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.referential.pmfm.FractionImpl;
32 import fr.ifremer.quadrige2.core.dao.referential.pmfm.MatrixImpl;
33 import fr.ifremer.quadrige2.core.dao.referential.pmfm.MethodImpl;
34 import fr.ifremer.quadrige2.core.dao.referential.pmfm.ParameterImpl;
35 import fr.ifremer.quadrige2.core.dao.technical.Dates;
36 import fr.ifremer.quadrige2.core.exception.BadUpdateDtException;
37 import fr.ifremer.quadrige2.core.vo.system.rule.RulePmfmVO;
38 import org.hibernate.SessionFactory;
39 import org.nuiton.i18n.I18n;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.context.annotation.Lazy;
42 import org.springframework.stereotype.Repository;
43
44 import java.sql.Timestamp;
45 import java.util.Collection;
46 import java.util.Objects;
47
48
49
50
51 @Repository("rulePmfmDao")
52 @Lazy
53 public class RulePmfmDaoImpl
54 extends RulePmfmDaoBase
55 {
56
57
58
59 @Autowired
60 public RulePmfmDaoImpl(SessionFactory sessionFactory) {
61 super();
62 setSessionFactory(sessionFactory);
63 }
64
65
66
67
68 protected RulePmfmVO handleSave(RulePmfmVO source, Timestamp updateDt)
69 {
70 Preconditions.checkNotNull(source);
71 Preconditions.checkNotNull(source.getRuleCd());
72
73
74 Rule parent = get(RuleImpl.class, source.getRuleCd());
75
76
77 RulePmfm entity = null;
78 boolean isNew = false;
79 if (source.getRulePmfmId() != null) {
80 entity = get(source.getRulePmfmId());
81 }
82 if (entity == null) {
83 entity = RulePmfm.Factory.newInstance();
84 parent.addRulePmfms(entity);
85 entity.setRule(parent);
86 isNew = true;
87 }
88
89
90 if (!isNew) {
91 if (entity.getUpdateDt() != null) {
92 Timestamp serverUpdateDtNoMillisecond = Dates.resetMillisecond(entity.getUpdateDt());
93 if (source.getUpdateDt() == null) {
94 throw new BadUpdateDtException(I18n.t("quadrige2.dao.rule.badUpdateDt", source.getRuleCd(),
95 serverUpdateDtNoMillisecond,
96 "null"));
97 }
98 Timestamp sourceUpdateDtNoMillisecond = Dates.resetMillisecond(source.getUpdateDt());
99 if (!Objects.equals(sourceUpdateDtNoMillisecond, serverUpdateDtNoMillisecond)) {
100 throw new BadUpdateDtException(I18n.t("quadrige2.dao.rule.badUpdateDt", source.getRuleCd(), serverUpdateDtNoMillisecond,
101 sourceUpdateDtNoMillisecond));
102 }
103 }
104 }
105
106
107 Timestamp newUpdateDt = updateDt;
108 if (newUpdateDt == null) {
109 newUpdateDt = getDatabaseCurrentTimestamp();
110 }
111 source.setUpdateDt(newUpdateDt);
112
113
114 rulePmfmVOToEntity(source, entity, true);
115
116
117 if (isNew) {
118 getSession().save(entity);
119
120 source.setRulePmfmId(entity.getRulePmfmId());
121 } else {
122 getSession().update(entity);
123 }
124
125 return source;
126 }
127
128
129
130
131 protected void handleRemoveByIds(Collection<Integer> rulePmfmIds) {
132 for (Integer rulePmfmId: rulePmfmIds) {
133 remove(rulePmfmId);
134 }
135 }
136
137
138
139
140 public void toRulePmfmVO(
141 RulePmfm source,
142 RulePmfmVO target)
143 {
144 super.toRulePmfmVO(source, target);
145
146
147 if (source.getRule() == null) {
148 target.setRuleCd(null);
149 }
150 else {
151 target.setRuleCd(source.getRule().getRuleCd());
152 }
153
154
155 if (source.getParameter() == null) {
156 target.setParCd(null);
157 }
158 else {
159 target.setParCd(source.getParameter().getParCd());
160 }
161
162
163 if (source.getMatrix() == null) {
164 target.setMatrixId(null);
165 }
166 else {
167 target.setMatrixId(source.getMatrix().getMatrixId());
168 }
169
170
171
172 if (source.getFraction() == null) {
173 target.setFractionId(null);
174 }
175 else {
176 target.setFractionId(source.getFraction().getFractionId());
177 }
178
179
180 if (source.getMethod() == null) {
181 target.setMethodId(null);
182 }
183 else {
184 target.setMethodId(source.getMethod().getMethodId());
185 }
186 }
187
188
189
190
191
192
193
194 private RulePmfm loadRulePmfmFromRulePmfmVO(RulePmfmVO rulePmfmVO)
195 {
196 RulePmfm rulePmfm = null;
197 if (rulePmfmVO.getRulePmfmId() != null)
198 {
199 rulePmfm = this.get(rulePmfmVO.getRulePmfmId());
200 }
201 if (rulePmfm == null)
202 {
203 rulePmfm = RulePmfm.Factory.newInstance();
204 }
205 return rulePmfm;
206 }
207
208
209
210
211 public RulePmfm rulePmfmVOToEntity(RulePmfmVO rulePmfmVO)
212 {
213 RulePmfm entity = this.loadRulePmfmFromRulePmfmVO(rulePmfmVO);
214 this.rulePmfmVOToEntity(rulePmfmVO, entity, true);
215 return entity;
216 }
217
218
219
220
221 @Override
222 public void rulePmfmVOToEntity(
223 RulePmfmVO source,
224 RulePmfm target,
225 boolean copyIfNull)
226 {
227 super.rulePmfmVOToEntity(source, target, copyIfNull);
228
229
230 if (copyIfNull || source.getRulePmfmId() != null) {
231 target.setRulePmfmId(source.getRulePmfmId());
232 }
233
234
235 if (copyIfNull || source.getRuleCd() != null) {
236 if (source.getRuleCd() == null) {
237 target.setRule(null);
238 }
239 else {
240 target.setRule(load(RuleImpl.class, source.getRuleCd()));
241 }
242 }
243
244
245 if (copyIfNull || source.getParCd() != null) {
246 if (source.getParCd() == null) {
247 target.setParameter(null);
248 }
249 else {
250 target.setParameter(load(ParameterImpl.class, source.getParCd()));
251 }
252 }
253
254
255 if (copyIfNull || source.getMatrixId() != null) {
256 if (source.getMatrixId() == null) {
257 target.setMatrix(null);
258 }
259 else {
260 target.setMatrix(load(MatrixImpl.class, source.getMatrixId()));
261 }
262 }
263
264
265 if (copyIfNull || source.getFractionId() != null) {
266 if (source.getFractionId() == null) {
267 target.setFraction(null);
268 }
269 else {
270 target.setFraction(load(FractionImpl.class, source.getFractionId()));
271 }
272 }
273
274
275 if (copyIfNull || source.getMethodId() != null) {
276 if (source.getMethodId() == null) {
277 target.setMethod(null);
278 }
279 else {
280 target.setMethod(load(MethodImpl.class, source.getMethodId()));
281 }
282 }
283 }
284 }