1 package fr.ifremer.tutti.service.referential.consumer;
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 fr.ifremer.tutti.persistence.entities.referential.Person;
28 import fr.ifremer.tutti.persistence.entities.referential.Persons;
29 import fr.ifremer.tutti.service.DecoratorService;
30 import fr.ifremer.tutti.service.PersistenceService;
31 import fr.ifremer.tutti.service.csv.CsvComsumer;
32 import fr.ifremer.tutti.service.referential.ReferentialImportRequest;
33 import fr.ifremer.tutti.service.referential.csv.PersonModel;
34 import fr.ifremer.tutti.service.referential.csv.PersonRow;
35 import org.apache.commons.lang3.BooleanUtils;
36 import org.apache.commons.lang3.StringUtils;
37 import org.apache.commons.logging.Log;
38 import org.apache.commons.logging.LogFactory;
39 import org.nuiton.csv.ImportRow;
40 import org.nuiton.jaxx.application.ApplicationBusinessException;
41
42 import java.nio.file.Path;
43
44 import static org.nuiton.i18n.I18n.t;
45
46
47
48
49
50
51
52 public class CsvConsumerForTemporaryPerson extends CsvComsumer<PersonRow, PersonModel> {
53
54
55 private static final Log log = LogFactory.getLog(CsvConsumerForTemporaryPerson.class);
56
57 public CsvConsumerForTemporaryPerson(Path file, char separator, boolean reportError) {
58 super(file, PersonModel.forImport(separator), reportError);
59 }
60
61 public void checkRow(ImportRow<PersonRow> row,
62 PersistenceService persistenceService,
63 DecoratorService decoratorService,
64 ReferentialImportRequest<Person, Integer> requestResult) {
65
66 if (row.isValid()) {
67
68 PersonRow bean = row.getBean();
69
70 Integer id = bean.getIdAsInt();
71 boolean delete = BooleanUtils.isTrue(bean.getToDelete());
72
73 if (id == null) {
74
75
76 checkAdd(bean, requestResult);
77
78 } else {
79
80
81
82 Person person = requestResult.getExistingEntityById(id);
83
84 if (person == null) {
85 throw new ApplicationBusinessException(t("tutti.service.referential.import.person.error.notExistingId", id));
86 }
87
88 if (delete) {
89
90
91 checkDelete(bean, person, persistenceService, decoratorService, requestResult);
92
93 } else {
94
95
96 checkUpdate(bean, person, requestResult);
97
98 }
99 }
100
101 }
102
103 reportError(row);
104
105 }
106
107 public void checkRowForGenericFormatImport(ImportRow<PersonRow> row, ReferentialImportRequest<Person, Integer> requestResult) {
108
109 PersonRow bean = row.getBean();
110 String name = bean.getFullName();
111
112 if (row.isValid()) {
113
114 Integer id = bean.getIdAsInt();
115
116 if (id == null) {
117
118 addCheckError(row, new ApplicationBusinessException(t("tutti.service.referential.import.person.error.noId")));
119
120 } else if (!Persons.isTemporaryId(id)) {
121
122 addCheckError(row, new ApplicationBusinessException(t("tutti.service.referential.import.person.error.idNotTemporary", id)));
123
124 } else if (requestResult.isIdAlreadyAdded(id)) {
125
126 addCheckError(row, new ApplicationBusinessException(t("tutti.service.referential.import.person.error.id.alreaydAdded", id)));
127
128 }
129
130 if (StringUtils.isBlank(name)) {
131
132 addCheckError(row, new ApplicationBusinessException(t("tutti.service.referential.import.person.error.noName")));
133
134 } else if (requestResult.isNaturalIdAlreadyAdded(name)) {
135
136 addCheckError(row, new ApplicationBusinessException(t("tutti.service.referential.import.person.error.name.alreaydAdded", name)));
137
138 }
139
140 }
141
142 reportError(row);
143
144 if (row.isValid()) {
145
146 Person entity = bean.toEntity();
147 boolean toAdd = requestResult.addExistingNaturalId(name);
148 if (toAdd) {
149
150 if (log.isInfoEnabled()) {
151 log.info("Will add person with name: " + name);
152 }
153 requestResult.addEntityToAdd(entity);
154
155 } else {
156
157 if (log.isInfoEnabled()) {
158 log.info("Will link person with name: " + name);
159 }
160 requestResult.addEntityToLink(entity);
161 }
162
163 }
164
165 }
166
167 protected void checkAdd(PersonRow bean, ReferentialImportRequest<Person, Integer> requestResult) {
168
169 String name = bean.getFullName();
170 boolean delete = BooleanUtils.isTrue(bean.getToDelete());
171
172 if (delete) {
173 throw new ApplicationBusinessException(t("tutti.service.referential.import.person.error.cannotDeleteWithoutId"));
174 }
175
176 if (StringUtils.isBlank(name)) {
177 throw new ApplicationBusinessException(t("tutti.service.referential.import.person.error.noName"));
178 }
179
180 if (!requestResult.addExistingNaturalId(name)) {
181 throw new ApplicationBusinessException(t("tutti.service.referential.import.person.error.existingName", name));
182 }
183
184 if (log.isInfoEnabled()) {
185 log.info("Will add person with name: " + name);
186 }
187
188 requestResult.addEntityToAdd(bean.toEntity());
189
190 }
191
192 protected void checkDelete(PersonRow bean, Person person, PersistenceService persistenceService,
193 DecoratorService decoratorService,
194 ReferentialImportRequest<Person, Integer> requestResult) {
195
196 Integer id = bean.getIdAsInt();
197 String name = bean.getFullName();
198
199 if (persistenceService.isTemporaryPersonUsed(id)) {
200
201 String personRef = id + " : " + decoratorService.getDecoratorByType(Person.class).toString(person);
202 throw new ApplicationBusinessException(t("tutti.service.referential.import.person.error.used", personRef));
203 }
204
205 if (log.isInfoEnabled()) {
206 log.info("Will delete person with name: " + name);
207 }
208
209 requestResult.addIdToDelete(id);
210 requestResult.removeExistingNaturalId(name);
211
212 }
213
214 protected void checkUpdate(PersonRow bean, Person person, ReferentialImportRequest<Person, Integer> requestResult) {
215
216 Integer id = bean.getIdAsInt();
217 String name = bean.getFullName();
218
219 if (StringUtils.isBlank(name)) {
220 throw new ApplicationBusinessException(t("tutti.service.referential.import.person.error.noName", id));
221 }
222
223 String previousFullName = Persons.getFullName(person);
224 if (!previousFullName.equals(name) && !requestResult.addExistingNaturalId(name)) {
225 throw new ApplicationBusinessException(t("tutti.service.referential.import.person.error.existingName", name));
226 }
227
228 if (log.isInfoEnabled()) {
229 log.info("Will update person with name: " + name);
230 }
231
232 requestResult.addEntityToUpdate(bean.toEntity());
233
234 }
235
236 }