1
2
3
4
5
6
7 package fr.ifremer.quadrige2.core.dao.data.survey;
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.Function;
31 import com.google.common.base.Preconditions;
32 import com.google.common.collect.Lists;
33 import fr.ifremer.quadrige2.core.dao.administration.user.DepartmentImpl;
34 import fr.ifremer.quadrige2.core.dao.administration.user.Quser;
35 import fr.ifremer.quadrige2.core.dao.administration.user.QuserImpl;
36 import fr.ifremer.quadrige2.core.dao.referential.monitoringLocation.PositionningSystemImpl;
37 import fr.ifremer.quadrige2.core.dao.system.synchronization.DeletedItemHistoryExtendDao;
38 import fr.ifremer.quadrige2.core.dao.technical.Daos;
39 import fr.ifremer.quadrige2.core.dao.technical.Dates;
40 import fr.ifremer.quadrige2.core.exception.BadUpdateDtException;
41 import fr.ifremer.quadrige2.core.vo.data.survey.OccasionVO;
42 import org.apache.commons.collections4.CollectionUtils;
43 import org.apache.commons.lang3.ArrayUtils;
44 import org.hibernate.SessionFactory;
45 import org.nuiton.i18n.I18n;
46 import org.springframework.beans.factory.annotation.Autowired;
47 import org.springframework.context.annotation.Lazy;
48 import org.springframework.stereotype.Repository;
49
50 import javax.annotation.Nullable;
51 import javax.annotation.Resource;
52 import java.sql.Timestamp;
53 import java.util.Collection;
54 import java.util.List;
55 import java.util.Objects;
56
57
58
59
60 @Repository("occasionDao")
61 @Lazy
62 public class OccasionDaoImpl
63 extends OccasionDaoBase {
64 @Resource(name = "deletedItemHistoryDao")
65 private DeletedItemHistoryExtendDao deletedItemHistoryDao;
66
67
68
69
70 @Autowired
71 public OccasionDaoImpl(SessionFactory sessionFactory) {
72 super();
73 setSessionFactory(sessionFactory);
74 }
75
76
77
78
79 @Override
80 public void remove(Collection<Occasion> entities) {
81 Preconditions.checkNotNull(entities);
82 Preconditions.checkArgument(entities.size() > 0);
83
84 for (Occasion entity : entities) {
85 remove(entity);
86 }
87 }
88
89
90
91
92 @Override
93 public void remove(Occasion entity) {
94
95
96 if (CollectionUtils.isNotEmpty(entity.getMoratoria())) {
97 entity.getMoratoria().clear();
98 }
99
100
101 if (CollectionUtils.isNotEmpty(entity.getQusers())) {
102 entity.getQusers().clear();
103 }
104
105
106 deletedItemHistoryDao.insertDeletedItem(OccasionImpl.class, entity);
107
108 super.remove(entity);
109 }
110
111
112
113
114 protected OccasionVO handleSave(OccasionVO source, Timestamp updateDt) {
115 Preconditions.checkNotNull(source);
116 Preconditions.checkNotNull(source.getCampaignId());
117
118
119 Campaign parent = get(CampaignImpl.class, source.getCampaignId());
120
121
122 Occasion entity = null;
123 boolean isNew = false;
124 if (source.getOccasId() != null) {
125 entity = get(source.getOccasId());
126 }
127 if (entity == null) {
128 entity = Occasion.Factory.newInstance();
129 parent.addOccasions(entity);
130 entity.setCampaign(parent);
131 isNew = true;
132 }
133
134
135 if (!isNew) {
136 if (entity.getUpdateDt() != null) {
137 Timestamp serverUpdateDtNoMillisecond = Dates.resetMillisecond(entity.getUpdateDt());
138 if (source.getUpdateDt() == null) {
139 throw new BadUpdateDtException(I18n.t("quadrige2.dao.occasion.badUpdateDt", source.getOccasId(),
140 serverUpdateDtNoMillisecond,
141 "null"));
142 }
143 Timestamp sourceUpdateDtNoMillisecond = Dates.resetMillisecond(source.getUpdateDt());
144 if (!Objects.equals(sourceUpdateDtNoMillisecond, serverUpdateDtNoMillisecond)) {
145 throw new BadUpdateDtException(I18n.t("quadrige2.dao.occasion.badUpdateDt", source.getOccasId(), serverUpdateDtNoMillisecond,
146 sourceUpdateDtNoMillisecond));
147 }
148 }
149 }
150
151
152 Timestamp newUpdateDt = updateDt;
153 if (newUpdateDt == null) {
154 newUpdateDt = getDatabaseCurrentTimestamp();
155 }
156 source.setUpdateDt(newUpdateDt);
157
158
159 occasionVOToEntity(source, entity, true);
160
161
162 if (isNew) {
163 Integer occasId = (Integer) getSession().save(entity);
164 source.setOccasId(occasId);
165 } else {
166 getSession().update(entity);
167 }
168
169 return source;
170 }
171
172
173
174
175 protected void handleRemoveByIds(Collection<Integer> occasIds) {
176 for (Integer occasId : occasIds) {
177 remove(occasId);
178 }
179 }
180
181
182
183
184 public void toOccasionVO(
185 Occasion source,
186 OccasionVO target) {
187
188 super.toOccasionVO(source, target);
189
190
191 target.setRecDepId(source.getRecorderDepartment() != null ? source.getRecorderDepartment().getDepId() : null);
192
193
194 target.setPosSystemId(source.getPositionningSystem() != null ? source.getPositionningSystem().getPosSystemId() : null);
195
196
197 target.setShipId(source.getShip() != null ? source.getShip().getShipId() : null);
198
199
200 if (CollectionUtils.isEmpty(source.getQusers())) {
201 target.setQuserIds(null);
202 } else {
203 List<Integer> quserIds = Lists.transform(Lists.newArrayList(source.getQusers()),
204 new Function<Quser, Integer>() {
205 @Nullable
206 @Override
207 public Integer apply(Quser source) {
208 return source.getQuserId();
209 }
210 });
211 target.setQuserIds(quserIds.toArray(new Integer[quserIds.size()]));
212 }
213 }
214
215
216
217
218
219
220 private Occasion loadOccasionFromOccasionVO(OccasionVO occasionVO) {
221 Occasion occasion = null;
222 if (occasionVO.getOccasId() != null) {
223 occasion = this.get(occasionVO.getOccasId());
224 }
225 if (occasion == null) {
226 occasion = Occasion.Factory.newInstance();
227 }
228 return occasion;
229 }
230
231
232
233
234 public Occasion occasionVOToEntity(OccasionVO occasionVO) {
235 Occasion entity = this.loadOccasionFromOccasionVO(occasionVO);
236 this.occasionVOToEntity(occasionVO, entity, true);
237 return entity;
238 }
239
240
241
242
243 @Override
244 public void occasionVOToEntity(
245 OccasionVO source,
246 Occasion target,
247 boolean copyIfNull) {
248 super.occasionVOToEntity(source, target, copyIfNull);
249
250
251 if (copyIfNull || source.getCampaignId() != null) {
252 if (source.getCampaignId() == null) {
253 target.setCampaign(null);
254 } else {
255 target.setCampaign(load(CampaignImpl.class, source.getCampaignId()));
256 }
257 }
258
259
260 if (copyIfNull || source.getRecDepId() != null) {
261 target.setRecorderDepartment(source.getRecDepId() != null ? load(DepartmentImpl.class, source.getRecDepId()) : null);
262 }
263
264
265 if (copyIfNull || source.getPosSystemId() != null) {
266 target.setPositionningSystem(source.getPosSystemId() != null ? load(PositionningSystemImpl.class, source.getPosSystemId()) : null);
267 }
268
269
270 if (copyIfNull || source.getShipId() != null) {
271 target.setShip(source.getShipId() != null ? load(ShipImpl.class, source.getShipId()) : null);
272 }
273
274
275 if (copyIfNull || ArrayUtils.isNotEmpty(source.getQuserIds())) {
276 if (ArrayUtils.isEmpty(source.getQuserIds())) {
277 target.getQusers().clear();
278 } else {
279 Daos.replaceEntities(
280 target.getQusers(),
281 source.getQuserIds(),
282 quserId -> load(QuserImpl.class, quserId));
283 }
284 }
285 }
286
287
288
289 }