View Javadoc
1   package emissary.pickup;
2   
3   import java.io.DataInputStream;
4   import java.io.DataOutputStream;
5   import java.io.IOException;
6   import javax.annotation.Nullable;
7   
8   /**
9    * A WorkUnit is a unit of work a worker will process. The idea is to replace fileNameList. Currently, WorkBundle is set
10   * to only have one file, and so there will only be one WorkUnit.
11   */
12  public final class WorkUnit {
13      private String fileName;
14      private String transactionId;
15      // worker updates this boolean
16      private boolean failedToParse = false;
17      private boolean failedToProcess = false;
18  
19      /**
20       * Constructs WorkUnit with specified filename
21       * 
22       * @param fileName the associated filename
23       */
24      WorkUnit(@Nullable String fileName) {
25          this.fileName = fileName;
26      }
27  
28  
29      /**
30       * Constructs WorkUnit with specified filename, transactionId, failedToParse, failedToProcess
31       *
32       * @param fileName the associated filename
33       * @param transactionId the associated transactionId
34       * @param failedToParse the status of failed to parse
35       * @param failedToProcess the status of failed to process
36       */
37      WorkUnit(String fileName, String transactionId, boolean failedToParse, boolean failedToProcess) {
38          this.fileName = fileName;
39          this.transactionId = transactionId;
40          this.failedToParse = failedToParse;
41          this.failedToProcess = failedToProcess;
42      }
43  
44      public static WorkUnit readFromStream(DataInputStream in) throws IOException {
45          final WorkUnit u = new WorkUnit(null);
46          u.fileName = WorkBundle.readUtfOrNull(in);
47          u.transactionId = WorkBundle.readUtfOrNull(in);
48          u.failedToParse = in.readBoolean();
49          u.failedToProcess = in.readBoolean();
50          return u;
51      }
52  
53      public void writeToStream(DataOutputStream out) throws IOException {
54          WorkBundle.writeUtfOrNull(fileName, out);
55          WorkBundle.writeUtfOrNull(transactionId, out);
56          out.writeBoolean(failedToParse);
57          out.writeBoolean(failedToProcess);
58      }
59  
60      /**
61       * Gets the filename for the WorkUnit
62       * 
63       * @return the filename
64       */
65      public String getFileName() {
66          return fileName;
67      }
68  
69      /**
70       * Sets the filename for the WorkUnit
71       * 
72       * @param fileName the filename
73       */
74      public void setFilename(String fileName) {
75          this.fileName = fileName;
76      }
77  
78      /**
79       * Gets the transaction Id of the WorkUnit
80       * 
81       * @return the transaction Id
82       */
83      public String getTransactionId() {
84          return transactionId;
85      }
86  
87      /**
88       * Sets the transaction id of the WorkUnit
89       * 
90       * @param transactionId the transaction id to set
91       */
92      public void setTransactionId(String transactionId) {
93          this.transactionId = transactionId;
94      }
95  
96      /**
97       * Sets the WorkUnit as a failed to parse WorkUnit
98       */
99      public void setFailedToParse() {
100         this.failedToParse = true;
101     }
102 
103     /**
104      * Gets the status of whether the WorkUnit failed to parse
105      * 
106      * @return the boolean status of failed to parse
107      */
108     public boolean failedToParse() {
109         return failedToParse;
110     }
111 
112     /**
113      * Sets the status of whether the WorkUnit had an error in processing
114      */
115     public void setFailedToProcess() {
116         this.failedToProcess = true;
117     }
118 
119     /**
120      * Gets the status of whether file had an error in processing.
121      * 
122      * @return the boolean status of file process
123      */
124     public boolean failedToProcess() {
125         return failedToProcess;
126     }
127 }