1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.waarp.vitam.ingest;
22
23 import com.fasterxml.jackson.annotation.JsonGetter;
24 import com.fasterxml.jackson.annotation.JsonIgnore;
25 import com.fasterxml.jackson.annotation.JsonProperty;
26 import com.fasterxml.jackson.annotation.JsonSetter;
27 import fr.gouv.vitam.common.GlobalDataRest;
28 import fr.gouv.vitam.common.StringUtils;
29 import fr.gouv.vitam.common.exception.InvalidParseOperationException;
30 import fr.gouv.vitam.common.json.JsonHandler;
31 import fr.gouv.vitam.common.model.LocalFile;
32 import fr.gouv.vitam.common.model.RequestResponseOK;
33 import org.waarp.common.exception.IllegalFiniteStateException;
34 import org.waarp.common.exception.InvalidArgumentException;
35 import org.waarp.common.logging.WaarpLogger;
36 import org.waarp.common.logging.WaarpLoggerFactory;
37 import org.waarp.common.state.MachineState;
38 import org.waarp.common.state.Transition;
39 import org.waarp.common.utility.ParametersChecker;
40 import org.waarp.vitam.common.AbstractVitamRequest;
41 import org.waarp.vitam.common.WaarpCommon.TaskOption;
42
43 import java.io.File;
44 import java.util.EnumSet;
45 import java.util.concurrent.ConcurrentHashMap;
46
47
48
49
50
51 public class IngestRequest extends AbstractVitamRequest {
52
53
54
55 static final String RESUME = "RESUME";
56
57
58
59 private static final WaarpLogger logger =
60 WaarpLoggerFactory.getLogger(IngestRequest.class);
61 @JsonIgnore
62 MachineState<IngestStep> step = IngestStep.newSessionMachineState();
63
64
65
66
67 @JsonProperty("contextId")
68 private String contextId;
69 @JsonProperty("action")
70 private String action = RESUME;
71 @JsonProperty("checkAtr")
72 private boolean checkAtr;
73
74 public IngestRequest() {
75
76 }
77
78
79
80
81
82
83
84
85
86
87
88
89 public IngestRequest(final TaskOption taskOption, final String contextId,
90 final String action, final boolean checkAtr,
91 final IngestRequestFactory factory)
92 throws InvalidParseOperationException {
93 super(taskOption);
94 try {
95 ParametersChecker
96 .checkParameterDefault(getCheckMessage(), contextId, action);
97 ParametersChecker.checkSanityString(contextId, action);
98 } catch (IllegalArgumentException | InvalidArgumentException e) {
99 logger.error(e);
100 throw new InvalidParseOperationException(e);
101 }
102 this.contextId = contextId;
103 this.action = action;
104 this.checkAtr = checkAtr;
105 this.status = this.step.getCurrent().getStatusMonitor();
106 try {
107 factory.saveNewIngestRequest(this);
108 } catch (InvalidParseOperationException e) {
109 logger.error("Will not be able to save: {}", this, e);
110 throw e;
111 }
112 }
113
114 @Override
115 public String toString() {
116 return "Ingest = Step: " + (step != null? step.getCurrent() : "noStep") +
117 " " + JsonHandler.unprettyPrint(this);
118 }
119
120 @JsonGetter("contextId")
121 public String getContextId() {
122 return contextId;
123 }
124
125
126
127
128
129
130
131 @JsonSetter("contextId")
132 public IngestRequest setContextId(final String contextId) {
133 try {
134 ParametersChecker.checkParameterDefault(getCheckMessage(), contextId);
135 StringUtils.checkSanityString(contextId);
136 } catch (InvalidParseOperationException | IllegalArgumentException e) {
137 logger.error(e);
138 throw new IllegalArgumentException(e.getMessage(), e);
139 }
140 this.contextId = contextId;
141 return this;
142 }
143
144 @JsonGetter("action")
145 public String getAction() {
146 return action;
147 }
148
149
150
151
152
153
154 @JsonSetter("action")
155 public IngestRequest setAction(final String action) {
156 try {
157 ParametersChecker.checkParameterDefault(getCheckMessage(), action);
158 StringUtils.checkSanityString(action);
159 } catch (InvalidParseOperationException | IllegalArgumentException e) {
160 logger.error(e);
161 throw new IllegalArgumentException(e.getMessage(), e);
162 }
163 this.action = action;
164 return this;
165 }
166
167
168
169
170
171
172
173
174
175
176
177
178 @JsonIgnore
179 public IngestRequest setStep(final IngestStep step, final int status,
180 IngestRequestFactory factory)
181 throws InvalidParseOperationException {
182 if (this.step == null) {
183 if (!step.equals(IngestStep.END)) {
184 logger.debug("Step {} could not be set since IngestRequest done", step);
185 }
186
187 return this;
188 }
189 if (!step.equals(IngestStep.ERROR) && this.step.getCurrent().equals(step)) {
190
191 return this;
192 }
193 try {
194 this.step.setCurrent(step);
195 } catch (IllegalFiniteStateException e) {
196 logger.error(e);
197 this.step.setDryCurrent(step);
198 }
199 setStatus(step != IngestStep.ERROR? step.getStatusMonitor() : status)
200 .setLastTryTime(System.currentTimeMillis());
201 return save(factory);
202 }
203
204
205
206
207
208
209
210
211
212
213 @JsonSetter("status")
214 public IngestRequest setStatus(final int status) {
215 this.status = status;
216 if (step != null) {
217 step.setDryCurrent(IngestStep.getFromInt(status));
218 }
219 return this;
220 }
221
222
223
224
225
226
227
228
229
230
231 @JsonIgnore
232 public IngestRequest save(IngestRequestFactory factory)
233 throws InvalidParseOperationException {
234 factory.saveIngestRequest(this);
235 return this;
236 }
237
238 @JsonIgnore
239 public IngestStep getStep() {
240 if (step == null) {
241 return null;
242 }
243 return step.getCurrent();
244 }
245
246 @JsonGetter("checkAtr")
247 public boolean isCheckAtr() {
248 return checkAtr;
249 }
250
251 @JsonSetter("checkAtr")
252 public IngestRequest setCheckAtr(final boolean checkAtr) {
253 this.checkAtr = checkAtr;
254 return this;
255 }
256
257
258
259
260
261
262
263
264 @JsonIgnore
265 public IngestRequest setFromRequestResponse(
266 RequestResponseOK requestResponse) {
267 String requestIdNew =
268 requestResponse.getHeaderString(GlobalDataRest.X_REQUEST_ID);
269 String globalExecutionStateNew = requestResponse
270 .getHeaderString(GlobalDataRest.X_GLOBAL_EXECUTION_STATE);
271 String globalExecutionStatusNew = requestResponse
272 .getHeaderString(GlobalDataRest.X_GLOBAL_EXECUTION_STATUS);
273 try {
274 ParametersChecker.checkParameterDefault(getCheckMessage(), requestIdNew,
275 globalExecutionStateNew,
276 globalExecutionStatusNew);
277 ParametersChecker.checkSanityString(requestIdNew, globalExecutionStateNew,
278 globalExecutionStatusNew);
279 } catch (IllegalArgumentException | InvalidArgumentException e) {
280 logger.error(e);
281 throw new IllegalArgumentException(e.getMessage(), e);
282 }
283 setGlobalExecutionState(globalExecutionStateNew)
284 .setGlobalExecutionStatus(globalExecutionStatusNew)
285 .setRequestId(requestIdNew);
286 return this;
287 }
288
289
290
291
292 @JsonIgnore
293 public LocalFile getLocalFile() {
294 return new LocalFile(getPath());
295 }
296
297
298
299
300 @JsonIgnore
301 public File getAtrFile(IngestRequestFactory factory) {
302 return factory.getXmlAtrFile(this);
303 }
304
305
306
307
308 enum CONTEXT {
309
310
311
312 DEFAULT_WORKFLOW,
313
314
315
316 HOLDING_SCHEME,
317
318
319
320 FILING_SCHEME;
321
322 public static boolean checkCorrectness(String arg) {
323 try {
324 CONTEXT.valueOf(arg);
325 return true;
326 } catch (IllegalArgumentException ignore) {
327 return false;
328 }
329 }
330 }
331
332
333
334
335 enum IngestStep {
336
337
338
339 STARTUP(-1),
340
341
342
343 RETRY_INGEST(-2),
344
345
346
347 RETRY_INGEST_ID(-3),
348
349
350
351 RETRY_ATR(-4),
352
353
354
355 RETRY_ATR_FORWARD(-5),
356
357
358
359 ERROR(-7),
360
361
362
363 END(-10);
364
365 private static final ConcurrentHashMap<IngestStep, EnumSet<IngestStep>>
366 stateMap = new ConcurrentHashMap<>();
367
368 static {
369 initR66FiniteStates();
370 }
371
372 private final int statusMonitor;
373
374 IngestStep(int status) {
375 this.statusMonitor = status;
376 }
377
378
379
380
381
382 private static void initR66FiniteStates() {
383 for (final IngestTransition trans : IngestTransition.values()) {
384 stateMap.put(trans.elt.getState(), trans.elt.getSet());
385 }
386 }
387
388
389
390
391 private static MachineState<IngestStep> newSessionMachineState() {
392 return new MachineState<>(STARTUP, stateMap);
393 }
394
395
396
397
398 static void endSessionMachineSate(MachineState<IngestStep> machine) {
399 if (machine != null) {
400 machine.release();
401 }
402 }
403
404 static IngestStep getFromInt(int status) {
405 switch (status) {
406 case -1:
407 return STARTUP;
408 case -2:
409 return RETRY_INGEST;
410 case -3:
411 return RETRY_INGEST_ID;
412 case -4:
413 return RETRY_ATR;
414 case -5:
415 return RETRY_ATR_FORWARD;
416 case -10:
417 return END;
418 case -7:
419 default:
420 return ERROR;
421 }
422 }
423
424 int getStatusMonitor() {
425 return statusMonitor;
426 }
427
428 private enum IngestTransition {
429 T_STARTUP(STARTUP, EnumSet.of(RETRY_INGEST, ERROR)),
430 T_RETRY_INGEST(RETRY_INGEST, EnumSet.of(RETRY_INGEST_ID, ERROR)),
431 T_RETRY_INGEST_ID(RETRY_INGEST_ID, EnumSet.of(RETRY_ATR, ERROR, END)),
432 T_RETRY_ATR(RETRY_ATR, EnumSet.of(RETRY_ATR_FORWARD, ERROR)),
433 T_RETRY_ATR_FORWARD(RETRY_ATR_FORWARD, EnumSet.of(ERROR, END)),
434 T_ERROR(ERROR, EnumSet.of(ERROR, END)), T_END(END, EnumSet.of(END));
435
436 private final Transition<IngestStep> elt;
437
438 IngestTransition(IngestStep state, EnumSet<IngestStep> set) {
439 elt = new Transition<>(state, set);
440 }
441
442 }
443 }
444
445 }