1 package fr.ifremer.tutti.persistence.service.referential;
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
27 import com.google.common.base.Preconditions;
28 import com.google.common.collect.Lists;
29 import fr.ifremer.adagio.core.dao.administration.user.DepartmentCode;
30 import fr.ifremer.adagio.core.dao.administration.user.DepartmentId;
31 import fr.ifremer.adagio.core.dao.administration.user.PersonExtendDao;
32 import fr.ifremer.adagio.core.dao.administration.user.UserProfilId;
33 import fr.ifremer.adagio.core.dao.referential.StatusCode;
34 import fr.ifremer.adagio.core.vo.administration.user.PersonVO;
35 import fr.ifremer.tutti.persistence.entities.referential.Person;
36 import fr.ifremer.tutti.persistence.entities.referential.Persons;
37 import org.apache.commons.logging.Log;
38 import org.apache.commons.logging.LogFactory;
39 import org.hibernate.type.IntegerType;
40 import org.hibernate.type.StringType;
41 import org.nuiton.jaxx.application.ApplicationBusinessException;
42 import org.springframework.stereotype.Service;
43
44 import javax.annotation.Resource;
45 import java.util.Collection;
46 import java.util.Collections;
47 import java.util.Iterator;
48 import java.util.List;
49
50
51
52
53
54
55
56 @Service("personPersistenceService")
57 public class PersonPersistenceServiceImpl extends ReferentialPersistenceServiceSupport implements PersonPersistenceService {
58
59
60 private static final Log log = LogFactory.getLog(PersonPersistenceServiceImpl.class);
61
62 @Resource(name = "personDao")
63 protected PersonExtendDao personDao;
64
65 @Override
66 public List<Person> getAllPerson() {
67
68 Iterator<Object[]> list = queryListWithStatus(
69 "allPersons",
70 "observerProfilId", IntegerType.INSTANCE, UserProfilId.OBSERVER.getValue(),
71 "projectMemberProfilId", IntegerType.INSTANCE, UserProfilId.PROJECT_MEMBER.getValue(),
72 "userProfilId", IntegerType.INSTANCE, UserProfilId.USER.getValue(),
73 "departementPrefixCode", StringType.INSTANCE, DepartmentCode.INSIDE_PREFIX.getValue()
74 );
75
76 List<Person> result = Lists.newArrayList();
77 loadPersons(list, result);
78 return Collections.unmodifiableList(result);
79
80 }
81
82 @Override
83 public List<Person> getAllPersonWithObsoletes() {
84
85 Iterator<Object[]> list = queryListWithStatus2(
86 "allPersonsWithObsoletes",
87 "observerProfilId", IntegerType.INSTANCE, UserProfilId.OBSERVER.getValue(),
88 "projectMemberProfilId", IntegerType.INSTANCE, UserProfilId.PROJECT_MEMBER.getValue(),
89 "userProfilId", IntegerType.INSTANCE, UserProfilId.USER.getValue(),
90 "departementPrefixCode", StringType.INSTANCE, DepartmentCode.INSIDE_PREFIX.getValue()
91 );
92
93 List<Person> result = Lists.newArrayList();
94 loadPersons(list, result);
95 return Collections.unmodifiableList(result);
96 }
97
98 @Override
99 public Person getPerson(Integer personId) {
100
101 Object[] source = queryUniqueWithStatus2(
102 "personById",
103 "personId", IntegerType.INSTANCE, personId);
104
105 return source == null ? null : loadPerson(source);
106
107 }
108
109 @Override
110 public boolean isTemporaryPersonUsed(Integer id) {
111 Long count = queryUniqueTyped("countManagerPersonInScientificCruise", "id", IntegerType.INSTANCE, id);
112 boolean result = count > 0;
113
114 if (!result) {
115 count = queryUniqueTyped("countRecorderPersonInScientificCruise", "id", IntegerType.INSTANCE, id);
116 result = count > 0;
117 }
118
119 if (!result) {
120 count = queryUniqueTyped("countRecorderPersonInFishingTrip", "id", IntegerType.INSTANCE, id);
121 result = count > 0;
122 }
123
124 if (!result) {
125 count = queryUniqueTyped("countPersonInVesselPersonFeatures", "id", IntegerType.INSTANCE, id);
126 result = count > 0;
127 }
128
129 return result;
130 }
131
132 @Override
133 public List<Person> addTemporaryPersons(List<Person> persons) {
134
135 List<Person> result = Lists.newArrayList();
136 for (Person source : persons) {
137 Person added = addTemporaryPerson(source);
138 result.add(added);
139 }
140 return Collections.unmodifiableList(result);
141
142 }
143
144 @Override
145 public List<Person> updateTemporaryPersons(List<Person> persons) {
146
147 List<Person> result = Lists.newArrayList();
148 for (Person source : persons) {
149 Person updated = updateTemporaryPerson(source);
150 result.add(updated);
151 }
152 return Collections.unmodifiableList(result);
153
154 }
155
156 @Override
157 public List<Person> linkTemporaryPersons(List<Person> persons) {
158
159 List<Person> result = Lists.newArrayList();
160 for (Person source : persons) {
161 Person linked = linkTemporaryPerson(source);
162 result.add(linked);
163 }
164 return Collections.unmodifiableList(result);
165
166 }
167
168 @Override
169 public void replacePerson(Person source, Person target, boolean delete) {
170
171 Preconditions.checkNotNull(source);
172 Preconditions.checkNotNull(target);
173 Preconditions.checkState(Persons.isTemporary(source));
174 Preconditions.checkState(!Persons.isTemporary(target));
175
176 Integer sourceId = source.getIdAsInt();
177 Integer targetId = target.getIdAsInt();
178
179 queryUpdate("replaceManagerPersonInScientificCruise",
180 "sourceId", IntegerType.INSTANCE, sourceId,
181 "targetId", IntegerType.INSTANCE, targetId);
182
183 queryUpdate("replaceRecorderPersonInScientificCruise",
184 "sourceId", IntegerType.INSTANCE, sourceId,
185 "targetId", IntegerType.INSTANCE, targetId);
186
187 queryUpdate("replaceRecorderPersonInFishingTrip",
188 "sourceId", IntegerType.INSTANCE, sourceId,
189 "targetId", IntegerType.INSTANCE, targetId);
190
191 queryUpdate("replacePersonInVesselPersonFeatures",
192 "sourceId", IntegerType.INSTANCE, sourceId,
193 "targetId", IntegerType.INSTANCE, targetId);
194
195 cacheService.clearAllCaches();
196
197
198
199 if (delete) {
200
201 deleteTemporaryPerson(sourceId);
202
203 }
204
205 }
206
207 @Override
208 public void deleteTemporaryPersons(Collection<Integer> ids) {
209
210 for (Integer id : ids) {
211 deleteTemporaryPerson(id);
212 }
213
214 }
215
216 @Override
217 public void deleteTemporaryPerson(Integer id) {
218
219 Preconditions.checkNotNull(id);
220 if (id > 0) {
221 throw new ApplicationBusinessException(String.format("Can't delete a Person with a positive id %d.", id));
222 }
223 Person person = getPerson(id);
224 if (person == null) {
225 throw new ApplicationBusinessException(String.format("Person with id %d does not exists", id));
226 }
227 personDao.remove(id);
228
229 }
230
231 protected Person addTemporaryPerson(Person source) {
232
233 Preconditions.checkNotNull(source);
234 Preconditions.checkNotNull(source.getFirstName());
235 Preconditions.checkNotNull(source.getLastName());
236 Preconditions.checkArgument(source.getIdAsInt() == null || Persons.isTemporaryId(source.getIdAsInt()));
237
238 fr.ifremer.adagio.core.dao.administration.user.Person target =
239 personDao.createAsTemporary(source.getLastName(), source.getFirstName(),
240 DepartmentId.UNKNOWN_RECORDER_DEPARTMENT.getValue());
241 Person result = Persons.newPerson();
242 result.setId(target.getId());
243
244
245 result.setLastName(source.getLastName());
246 result.setFirstName(source.getFirstName());
247 setStatus(StatusCode.TEMPORARY.getValue(), result);
248 return result;
249
250 }
251
252 protected Person updateTemporaryPerson(Person source) {
253
254 Preconditions.checkNotNull(source);
255 Preconditions.checkNotNull(source.getFirstName());
256 Preconditions.checkNotNull(source.getLastName());
257 Preconditions.checkNotNull(source.getId());
258 Preconditions.checkArgument(Persons.isTemporaryId(source.getIdAsInt()));
259
260 Person result = getPerson(source.getIdAsInt());
261
262
263 result.setLastName(source.getLastName());
264 result.setFirstName(source.getFirstName());
265 setStatus(StatusCode.TEMPORARY.getValue(), result);
266
267 PersonVO toUpdate = new PersonVO();
268 toUpdate.setId(source.getIdAsInt());
269 toUpdate.setLastname(source.getLastName());
270 toUpdate.setFirstname(source.getFirstName());
271 toUpdate.setDepartmentId(DepartmentId.UNKNOWN_RECORDER_DEPARTMENT.getValue());
272 toUpdate.setCreationDate(personDao.load(source.getIdAsInt()).getCreationDate());
273 toUpdate.setStatusCode(StatusCode.TEMPORARY.getValue());
274 personDao.save(toUpdate);
275
276 return result;
277
278 }
279
280 protected Person linkTemporaryPerson(Person source) {
281
282 Preconditions.checkNotNull(source);
283 Preconditions.checkNotNull(source.getFirstName());
284 Preconditions.checkNotNull(source.getLastName());
285 Preconditions.checkNotNull(source.getId());
286 Preconditions.checkArgument(Persons.isTemporaryId(source.getIdAsInt()));
287
288 Iterator<Object[]> sources =queryListWithStatus(
289 "personByFullName",
290 "personFirstName", StringType.INSTANCE, source.getFirstName(),
291 "personLastName", StringType.INSTANCE, source.getLastName());
292
293 Object[] row = null;
294 if (sources.hasNext()) {
295 row = sources.next();
296
297 if (sources.hasNext()) {
298 Object[] next = sources.next();
299 Person person = loadPerson(next);
300 if (log.isInfoEnabled()) {
301 log.info("Il existe une autre personne d'identifiant "+person.getId()+" trouvée en pase pour la personne temporaire "+source.getFirstName()+" - "+source.getLastName());
302 }
303 }
304 }
305
306
307
308
309
310
311 return row == null ? null : loadPerson(row);
312
313 }
314
315 protected void loadPersons(Iterator<Object[]> list, List<Person> result) {
316 while (list.hasNext()) {
317 Object[] source = list.next();
318 Person target = loadPerson(source);
319 result.add(target);
320 }
321 }
322
323 protected Person loadPerson(Object... source) {
324
325 Person target = Persons.newPerson();
326 target.setId(String.valueOf(source[0]));
327 target.setLastName((String) source[1]);
328 target.setFirstName((String) source[2]);
329 target.setDepartment((String) source[3]);
330 setStatus((String) source[4], target);
331 return target;
332
333 }
334
335 }