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