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.Gear;
28 import fr.ifremer.tutti.persistence.entities.referential.Gears;
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.GearModel;
34 import fr.ifremer.tutti.service.referential.csv.GearRow;
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 CsvConsumerForTemporaryGear extends CsvComsumer<GearRow, GearModel> {
53
54
55 private static final Log log = LogFactory.getLog(CsvConsumerForTemporaryGear.class);
56
57 public CsvConsumerForTemporaryGear(Path file, char separator, boolean failFast) {
58 super(file, GearModel.forImport(separator), failFast);
59 }
60
61 public void checkRow(ImportRow<GearRow> row,
62 PersistenceService persistenceService,
63 DecoratorService decoratorService,
64 ReferentialImportRequest<Gear, Integer> requestResult) {
65
66 if (row.isValid()) {
67
68 GearRow 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 Gear gear = requestResult.getExistingEntityById(id);
83
84 if (gear == null) {
85 throw new ApplicationBusinessException(t("tutti.service.referential.import.gear.error.notExistingId", id));
86 }
87
88 if (delete) {
89
90
91 checkDelete(bean, gear, persistenceService, decoratorService, requestResult);
92
93 } else {
94
95
96 checkUpdate(bean, gear, requestResult);
97
98 }
99 }
100
101 }
102
103 reportError(row);
104
105 }
106
107 public void checkRowForGenericFormatImport(ImportRow<GearRow> row, ReferentialImportRequest<Gear, Integer> requestResult) {
108
109 GearRow bean = row.getBean();
110 String name = bean.getName();
111
112 if (row.isValid()) {
113
114 Integer id = bean.getIdAsInt();
115
116 if (id == null) {
117
118
119 addCheckError(row, new ApplicationBusinessException(t("tutti.service.referential.import.gear.error.noId")));
120
121 } else if (!Gears.isTemporaryId(id)) {
122
123 addCheckError(row, new ApplicationBusinessException(t("tutti.service.referential.import.gear.error.idNotTemporary", id)));
124
125 }else if (requestResult.isIdAlreadyAdded(id)) {
126
127 addCheckError(row, new ApplicationBusinessException(t("tutti.service.referential.import.gear.error.id.alreaydAdded", id)));
128
129 }
130
131 if (StringUtils.isBlank(name)) {
132
133 addCheckError(row, new ApplicationBusinessException(t("tutti.service.referential.import.gear.error.noName")));
134
135 } else if (requestResult.isNaturalIdAlreadyAdded(name)) {
136
137 addCheckError(row, new ApplicationBusinessException(t("tutti.service.referential.import.gear.error.name.alreaydAdded", name)));
138
139 }
140
141 }
142
143 reportError(row);
144
145 if (row.isValid()) {
146
147 Gear entity = bean.toEntity();
148 boolean toAdd = requestResult.addExistingNaturalId(name);
149 if (toAdd) {
150
151 if (log.isInfoEnabled()) {
152 log.info("Will add gear with name: " + name);
153 }
154 requestResult.addEntityToAdd(entity);
155
156 } else {
157
158 if (log.isInfoEnabled()) {
159 log.info("Will link gear with name: " + name);
160 }
161 requestResult.addEntityToLink(entity);
162
163 }
164
165 }
166
167 }
168
169 protected void checkAdd(GearRow bean, ReferentialImportRequest<Gear, Integer> requestResult) {
170
171 String name = bean.getName();
172 boolean delete = BooleanUtils.isTrue(bean.getToDelete());
173
174 if (delete) {
175 throw new ApplicationBusinessException(t("tutti.service.referential.import.gear.error.cannotDeleteWithoutId"));
176 }
177
178 if (StringUtils.isBlank(name)) {
179 throw new ApplicationBusinessException(t("tutti.service.referential.import.gear.error.noName"));
180 }
181
182 if (!requestResult.addExistingNaturalId(name)) {
183 throw new ApplicationBusinessException(t("tutti.service.referential.import.gear.error.existingName", name));
184 }
185
186 if (log.isInfoEnabled()) {
187 log.info("Will add gear with name: " + name);
188 }
189
190 requestResult.addEntityToAdd(bean.toEntity());
191
192 }
193
194 protected void checkDelete(GearRow bean, Gear gear, PersistenceService persistenceService,
195 DecoratorService decoratorService,
196 ReferentialImportRequest<Gear, Integer> requestResult) {
197
198 Integer id = bean.getIdAsInt();
199 String name = bean.getName();
200
201 if (persistenceService.isTemporaryGearUsed(id)) {
202
203 String gearRef = id + " : " + decoratorService.getDecoratorByType(Gear.class).toString(gear);
204 throw new ApplicationBusinessException(t("tutti.service.referential.import.gear.error.used", gearRef));
205 }
206
207 requestResult.addIdToDelete(id);
208 requestResult.removeExistingNaturalId(name);
209
210 if (log.isInfoEnabled()) {
211 log.info("Will delete gear with name: " + name);
212 }
213
214 requestResult.addEntityToAdd(bean.toEntity());
215
216 }
217
218 protected void checkUpdate(GearRow bean, Gear gear, ReferentialImportRequest<Gear, Integer> requestResult) {
219
220 Integer id = bean.getIdAsInt();
221 String name = bean.getName();
222
223 if (StringUtils.isBlank(name)) {
224 throw new ApplicationBusinessException(t("tutti.service.referential.import.gear.error.noName", id));
225 }
226
227 if (!name.equals(gear.getName()) && !requestResult.addExistingNaturalId(name)) {
228 throw new ApplicationBusinessException(t("tutti.service.referential.import.gear.error.existingName", name));
229 }
230
231 if (log.isInfoEnabled()) {
232 log.info("Will update gear with name: " + name);
233 }
234
235 requestResult.addEntityToUpdate(bean.toEntity());
236
237 }
238 }