View Javadoc
1   /*
2    * This file is part of Waarp Project (named also Waarp or GG).
3    *
4    *  Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
5    *  tags. See the COPYRIGHT.txt in the distribution for a full listing of
6    * individual contributors.
7    *
8    *  All Waarp Project is free software: you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or (at your
11   * option) any later version.
12   *
13   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License along with
18   * Waarp . If not, see <http://www.gnu.org/licenses/>.
19   */
20  
21  package org.waarp.openr66.pojo;
22  
23  import com.fasterxml.jackson.annotation.JsonIgnore;
24  import com.fasterxml.jackson.annotation.JsonProperty;
25  import com.fasterxml.jackson.annotation.JsonValue;
26  import org.waarp.common.database.exception.WaarpDatabaseSqlException;
27  import org.waarp.common.logging.WaarpLogger;
28  import org.waarp.common.logging.WaarpLoggerFactory;
29  import org.waarp.openr66.context.ErrorCode;
30  import org.waarp.openr66.database.DbConstantR66;
31  import org.waarp.openr66.database.data.DbTaskRunner;
32  import org.waarp.openr66.protocol.configuration.Configuration;
33  
34  import javax.xml.bind.annotation.XmlAccessType;
35  import javax.xml.bind.annotation.XmlAccessorType;
36  import javax.xml.bind.annotation.XmlElement;
37  import javax.xml.bind.annotation.XmlTransient;
38  import javax.xml.bind.annotation.XmlType;
39  import java.sql.Timestamp;
40  import java.sql.Types;
41  import java.util.Date;
42  import java.util.HashMap;
43  import java.util.Map;
44  
45  import static org.waarp.common.database.data.AbstractDbData.*;
46  import static org.waarp.openr66.dao.database.DBTransferDAO.*;
47  
48  /**
49   * Transfer data object
50   */
51  @XmlType(name = DbTaskRunner.XMLRUNNER)
52  @XmlAccessorType(XmlAccessType.NONE)
53  public class Transfer {
54    /**
55     * Internal Logger
56     */
57    private static final WaarpLogger logger =
58        WaarpLoggerFactory.getLogger(Transfer.class);
59  
60    public enum TASKSTEP {
61      NOTASK(0), PRETASK(1), TRANSFERTASK(2), POSTTASK(3), ALLDONETASK(4),
62      ERRORTASK(5);
63  
64      private final int taskNo;
65  
66      private static final Map<Integer, TASKSTEP> map =
67          new HashMap<Integer, TASKSTEP>();
68  
69      static {
70        for (final TASKSTEP task : TASKSTEP.values()) {
71          map.put(task.taskNo, task);
72        }
73      }
74  
75      TASKSTEP(final int task) {
76        taskNo = task;
77      }
78  
79      public static TASKSTEP valueOf(final int taskStep) {
80        return map.get(taskStep);
81      }
82  
83      public final DbTaskRunner.TASKSTEP toLegacy() {
84        return DbTaskRunner.TASKSTEP.valueOf(name());
85      }
86  
87      @JsonValue
88      public final int getTaskNo() {
89        return taskNo;
90      }
91    }
92  
93    @XmlElement(name = ID_FIELD)
94    @JsonProperty("SPECIALID")
95    private long id = DbConstantR66.ILLEGALVALUE;
96  
97    /**
98     * True if requester is the sender of the file (SEND MODE) False if
99     * requested is the sender of the file
100    * (RETRIEVE MODE)
101    */
102   @XmlElement(name = RETRIEVE_MODE_FIELD)
103   @JsonProperty("RETRIEVEMODE")
104   private boolean retrieveMode;
105 
106   @XmlElement(name = ID_RULE_FIELD)
107   @JsonProperty("IDRULE")
108   private String rule = "";
109 
110   @XmlElement(name = TRANSFER_MODE_FIELD)
111   @JsonProperty("MODETRANS")
112   private int transferMode = 1;
113 
114   @XmlElement(name = FILENAME_FIELD)
115   @JsonProperty("FILENAME")
116   private String filename = "";
117 
118   @XmlElement(name = ORIGINAL_NAME_FIELD)
119   @JsonProperty("ORIGINALNAME")
120   private String originalName = "";
121 
122   @XmlElement(name = FILE_INFO_FIELD)
123   @JsonProperty("FILEINFO")
124   private String fileInfo = "";
125 
126   @XmlElement(name = IS_MOVED_FIELD)
127   @JsonProperty("ISMOVED")
128   private boolean isMoved;
129 
130   @XmlElement(name = BLOCK_SIZE_FIELD)
131   @JsonProperty("BLOCKSZ")
132   private int blockSize;
133 
134   @XmlElement(name = OWNER_REQUEST_FIELD)
135   @JsonProperty("OWNERREQ")
136   private String ownerRequest = Configuration.configuration.getHostId();
137 
138   @XmlElement(name = REQUESTER_FIELD)
139   @JsonProperty("REQUESTER")
140   private String requester = "";
141 
142   @XmlElement(name = REQUESTED_FIELD)
143   @JsonProperty("REQUESTED")
144   private String requested = "";
145 
146   @XmlElement(name = TRANSFER_INFO_FIELD)
147   @JsonProperty("TRANSFERINFO")
148   private String transferInfo = "";
149 
150   @XmlElement(name = GLOBAL_STEP_FIELD)
151   @JsonProperty("GLOBALSTEP")
152   private TASKSTEP globalStep = TASKSTEP.NOTASK;
153 
154   @XmlElement(name = GLOBAL_LAST_STEP_FIELD)
155   @JsonProperty("GLOBALLASTSTEP")
156   private TASKSTEP lastGlobalStep = TASKSTEP.NOTASK;
157 
158   @XmlElement(name = STEP_FIELD)
159   @JsonProperty("STEP")
160   private int step = -1;
161 
162   @XmlElement(name = STEP_STATUS_FIELD)
163   @JsonProperty("STEPSTATUS")
164   private ErrorCode stepStatus = ErrorCode.Unknown;
165 
166   @XmlElement(name = INFO_STATUS_FIELD)
167   @JsonProperty("INFOSTATUS")
168   private ErrorCode infoStatus = ErrorCode.Unknown;
169 
170   @XmlElement(name = RANK_FIELD)
171   @JsonProperty("RANK")
172   private int rank;
173 
174   @XmlTransient
175   @JsonProperty("STARTTRANS")
176   private Timestamp start = new Timestamp(0);
177 
178   @XmlTransient
179   @JsonProperty("STOPTRANS")
180   private Timestamp stop = new Timestamp(0);
181 
182   @XmlTransient
183   @JsonProperty("UPDATEDINFO")
184   private UpdatedInfo updatedInfo = UpdatedInfo.UNKNOWN;
185 
186   @XmlElement(name = TRANSFER_START_FIELD)
187   @JsonIgnore
188   public final long getXmlStart() {
189     return start.getTime();
190   }
191 
192   public final void setXmlStart(final long xml) {
193     start = new Timestamp(xml);
194   }
195 
196   @XmlElement(name = TRANSFER_STOP_FIELD)
197   @JsonIgnore
198   public final long getXmlStop() {
199     return stop.getTime();
200   }
201 
202   public final void setXmlStop(final long xml) {
203     stop = new Timestamp(xml);
204   }
205 
206   /**
207    * Full Constructor to create Transfer from the database
208    *
209    * @param id
210    * @param rule
211    * @param mode
212    * @param filename
213    * @param originalName
214    * @param fileInfo
215    * @param isMoved
216    * @param blockSize
217    * @param retrieveMode
218    * @param ownerReq
219    * @param requester
220    * @param requested
221    * @param transferInfo
222    * @param globalStep
223    * @param lastGlobalStep
224    * @param step
225    * @param stepStatus
226    * @param infoStatus
227    * @param rank
228    * @param start
229    * @param stop
230    * @param updatedInfo
231    */
232   public Transfer(final long id, final String rule, final int mode,
233                   final String filename, final String originalName,
234                   final String fileInfo, final boolean isMoved,
235                   final int blockSize, final boolean retrieveMode,
236                   final String ownerReq, final String requester,
237                   final String requested, final String transferInfo,
238                   final TASKSTEP globalStep, final TASKSTEP lastGlobalStep,
239                   final int step, final ErrorCode stepStatus,
240                   final ErrorCode infoStatus, final int rank,
241                   final Timestamp start, final Timestamp stop,
242                   final UpdatedInfo updatedInfo)
243       throws WaarpDatabaseSqlException {
244     this(id, rule, mode, filename, originalName, fileInfo, isMoved, blockSize,
245          retrieveMode, ownerReq, requester, requested, transferInfo, globalStep,
246          lastGlobalStep, step, stepStatus, infoStatus, rank, start, stop);
247     this.updatedInfo = updatedInfo;
248   }
249 
250   /**
251    * Constructor to create Transfer from remote requests
252    *
253    * @param id
254    * @param rule
255    * @param mode
256    * @param filename
257    * @param originalName
258    * @param fileInfo
259    * @param isMoved
260    * @param blockSize
261    * @param retrieveMode
262    * @param ownerReq
263    * @param requester
264    * @param requested
265    * @param transferInfo
266    * @param globalStep
267    * @param lastGlobalStep
268    * @param step
269    * @param stepStatus
270    * @param infoStatus
271    * @param rank
272    * @param start
273    * @param stop
274    */
275   public Transfer(final long id, final String rule, final int mode,
276                   final String filename, final String originalName,
277                   final String fileInfo, final boolean isMoved,
278                   final int blockSize, final boolean retrieveMode,
279                   final String ownerReq, final String requester,
280                   final String requested, final String transferInfo,
281                   final TASKSTEP globalStep, final TASKSTEP lastGlobalStep,
282                   final int step, final ErrorCode stepStatus,
283                   final ErrorCode infoStatus, final int rank,
284                   final Timestamp start, final Timestamp stop)
285       throws WaarpDatabaseSqlException {
286     this.id = id;
287     this.rule = rule;
288     transferMode = mode;
289     this.retrieveMode = retrieveMode;
290     this.filename = filename;
291     this.originalName = originalName;
292     this.fileInfo = fileInfo;
293     this.isMoved = isMoved;
294     this.blockSize = blockSize;
295     ownerRequest = ownerReq;
296     this.requester = requester;
297     this.requested = requested;
298     this.transferInfo = transferInfo;
299     this.globalStep = globalStep;
300     this.lastGlobalStep = lastGlobalStep;
301     this.step = step;
302     this.stepStatus = stepStatus;
303     this.infoStatus = infoStatus;
304     this.rank = rank;
305     this.start = start;
306     this.stop = stop;
307     checkValues();
308   }
309 
310   /**
311    * Constructor to create transfer locally with delayed start time
312    *
313    * @param rule
314    * @param retrieveMode
315    * @param file
316    * @param fileInfo
317    * @param blockSize
318    */
319   public Transfer(final String remote, final String rule, final int ruleMode,
320                   final boolean retrieveMode, final String file,
321                   final String fileInfo, final int blockSize,
322                   final Timestamp start) throws WaarpDatabaseSqlException {
323     ownerRequest = Configuration.configuration.getHostId();
324     requester = Configuration.configuration.getHostId();
325     requested = remote;
326     this.rule = rule;
327     transferMode = ruleMode;
328     this.retrieveMode = retrieveMode;
329     filename = file;
330     originalName = file;
331     this.fileInfo = fileInfo;
332     this.blockSize = blockSize;
333     this.start = start;
334     checkValues();
335   }
336 
337   /**
338    * Constructor to create transfer locally
339    *
340    * @param rule
341    * @param retrieveMode
342    * @param file
343    * @param fileInfo
344    * @param blockSize
345    */
346   public Transfer(final String remote, final String rule, final int ruleMode,
347                   final boolean retrieveMode, final String file,
348                   final String fileInfo, final int blockSize)
349       throws WaarpDatabaseSqlException {
350     this(remote, rule, ruleMode, retrieveMode, file, fileInfo, blockSize,
351          new Timestamp(new Date().getTime()));
352   }
353 
354   /**
355    * Empty constructor
356    */
357   public Transfer() {
358     // Nothing
359   }
360 
361   @JsonIgnore
362   public final void checkValues() throws WaarpDatabaseSqlException {
363     validateLength(Types.NVARCHAR, rule, ownerRequest, requested, requester);
364     validateLength(Types.VARCHAR, filename, originalName, fileInfo,
365                    transferInfo);
366   }
367 
368   public final long getId() {
369     return id;
370   }
371 
372   public final void setId(final long id) {
373     this.id = id;
374   }
375 
376   public final boolean getRetrieveMode() {
377     return retrieveMode;
378   }
379 
380   public final void setRetrieveMode(final boolean retrieveMode) {
381     this.retrieveMode = retrieveMode;
382   }
383 
384   public final String getRule() {
385     return rule;
386   }
387 
388   public final void setRule(final String rule) {
389     this.rule = rule;
390   }
391 
392   public final int getTransferMode() {
393     return transferMode;
394   }
395 
396   public final void setTransferMode(final int mode) {
397     transferMode = mode;
398   }
399 
400   public final String getFilename() {
401     return filename;
402   }
403 
404   public final void setFilename(final String filename) {
405     this.filename = filename;
406   }
407 
408   public final String getOriginalName() {
409     return originalName;
410   }
411 
412   public final void setOriginalName(final String originalName) {
413     this.originalName = originalName;
414   }
415 
416   public final String getFileInfo() {
417     return fileInfo;
418   }
419 
420   public final void setFileInfo(final String fileInfo) {
421     this.fileInfo = fileInfo;
422   }
423 
424   public final boolean getIsMoved() {
425     return isMoved;
426   }
427 
428   public final void setIsMoved(final boolean isMoved) {
429     this.isMoved = isMoved;
430   }
431 
432   public final int getBlockSize() {
433     return blockSize;
434   }
435 
436   public final void setBlockSize(final int blockSize) {
437     this.blockSize = blockSize;
438   }
439 
440   public final String getOwnerRequest() {
441     return ownerRequest;
442   }
443 
444   public final void setOwnerRequest(final String ownerRequest) {
445     this.ownerRequest = ownerRequest;
446   }
447 
448   public final String getRequester() {
449     return requester;
450   }
451 
452   public final void setRequester(final String requester) {
453     this.requester = requester;
454   }
455 
456   public final String getRequested() {
457     return requested;
458   }
459 
460   public final void setRequested(final String requested) {
461     this.requested = requested;
462   }
463 
464   public final String getTransferInfo() {
465     return transferInfo;
466   }
467 
468   public final void setTransferInfo(final String transferInfo) {
469     this.transferInfo = transferInfo;
470   }
471 
472   public final TASKSTEP getGlobalStep() {
473     return globalStep;
474   }
475 
476   public final void setGlobalStep(final TASKSTEP globalStep) {
477     this.globalStep = globalStep;
478   }
479 
480   public final TASKSTEP getLastGlobalStep() {
481     return lastGlobalStep;
482   }
483 
484   public final void setLastGlobalStep(final TASKSTEP lastGlobalStep) {
485     this.lastGlobalStep = lastGlobalStep;
486   }
487 
488   public final int getStep() {
489     return step;
490   }
491 
492   public final void setStep(final int step) {
493     this.step = step;
494   }
495 
496   public final ErrorCode getStepStatus() {
497     return stepStatus;
498   }
499 
500   public final void setStepStatus(final ErrorCode stepStatus) {
501     this.stepStatus = stepStatus;
502   }
503 
504   public final ErrorCode getInfoStatus() {
505     return infoStatus;
506   }
507 
508   public final void setInfoStatus(final ErrorCode infoStatus) {
509     this.infoStatus = infoStatus;
510   }
511 
512   public final int getRank() {
513     return rank;
514   }
515 
516   public final void setRank(final int rank) {
517     this.rank = rank;
518   }
519 
520   public final Timestamp getStart() {
521     return start;
522   }
523 
524   public final void setStart(final Timestamp start) {
525     this.start = start;
526   }
527 
528   public final Timestamp getStop() {
529     return stop;
530   }
531 
532   public final void setStop(final Timestamp stop) {
533     this.stop = stop;
534   }
535 
536   public final UpdatedInfo getUpdatedInfo() {
537     return updatedInfo;
538   }
539 
540   public final void setUpdatedInfo(final UpdatedInfo info) {
541     updatedInfo = info;
542   }
543 }