View Javadoc

1   package uk.ac.cam.spectra.spectrasub;
2   
3   import java.io.File;
4   import java.io.IOException;
5   import java.io.Serializable;
6   
7   import org.apache.log4j.Logger;
8   
9   import uk.ac.cam.spectra.Combine;
10  import uk.ac.cam.spectra.DataFileType;
11  import uk.ac.cam.spectra.Validation;
12  
13  import com.Ostermiller.util.MD5;
14  
15  /**
16   * Represents an uploaded data file. Mainly a data object, has trivial methods 
17   * that calculate the file digest and validate the file. 
18   * 
19   * @author ojd20
20   */
21  public class DataFile implements Serializable {
22      private static final Logger LOG = Logger.getLogger(DataFile.class);
23  
24      private static final long serialVersionUID = -1598966150024544621L;
25  
26      private Combine combine = Combine.combine;
27  
28      private boolean convert;
29  
30      private DataFileType dataFileType;
31  
32      private String description;
33  
34      private String digest;
35  
36      private String error;
37  
38      private boolean extractMetadata;
39  
40      private boolean convertOnWarning;
41  
42      private boolean createInChIOnWarning;
43  
44      private transient File file;
45  
46      /**
47       * The original name of the file.
48       */
49      private String fileName;
50  
51      private String filePath;
52  
53      private String metadataCombination;
54  
55      private String type;
56  
57      private Validation validation;
58  
59      private String warning;
60  
61      public Combine getCombine() {
62          return combine;
63      }
64  
65      public boolean getConvert() {
66          return convert;
67      }
68  
69      public DataFileType getDataFileType() {
70          return dataFileType;
71      }
72  
73      public String getDescription() {
74          return description;
75      }
76  
77      public String getError() {
78          return error;
79      }
80  
81      public boolean getExtractMetadata() {
82          return extractMetadata;
83      }
84  
85      public File getFile() {
86          if (file == null && filePath != null) {
87              file = new File(filePath);
88          }
89          return file;
90      }
91  
92      public String getFileName() {
93          return fileName;
94      }
95  
96      public String getMD5Sum() {
97          if (file == null) {
98              throw new IllegalStateException("No file has been set");
99          }
100         return digest;
101     }
102 
103     public String getMetadataCombination() {
104         return metadataCombination;
105     }
106 
107     public String getType() {
108         return type;
109     }
110 
111     public Validation getValidation() {
112         return validation;
113     }
114 
115     public String getWarning() {
116         return warning;
117     }
118 
119     public void setConvert(boolean convert) {
120         this.convert = convert;
121     }
122 
123     public void setDataFileType(DataFileType type) {
124         this.dataFileType = type;
125     }
126 
127     public void setDescription(String description) {
128         this.description = description;
129     }
130 
131     public void setError(String error) {
132         this.error = error;
133     }
134 
135     public void setExtractMetadata(boolean extractMetadata) {
136         this.extractMetadata = extractMetadata;
137     }
138 
139     public void setFile(File file) {
140         this.file = file;
141         filePath = file.getAbsolutePath();
142         try {
143             digest = MD5.getHashString(file);
144         } catch (IOException e) {
145             LOG.warn("Problem digesting " + file + ": " + e.getMessage(), e);
146             throw new RuntimeException("Problem digesting " + file + ": "
147                     + e.getMessage(), e);
148         }
149     }
150 
151     public void setFileName(String fileName) {
152         this.fileName = fileName;
153     }
154 
155     public void setMetadataCombination(String metadataCombination) {
156         this.metadataCombination = metadataCombination;
157         combine = Combine.valueOf(metadataCombination);
158     }
159 
160     public void setType(String type) {
161         this.type = type;
162     }
163 
164     public void setValidation(Validation validation) {
165         this.validation = validation;
166     }
167 
168     public void setWarning(String warning) {
169         this.warning = warning;
170     }
171 
172     public boolean getConvertOnWarning() {
173         return convertOnWarning;
174     }
175 
176     public void setConvertOnWarning(boolean convertOnWarning) {
177         this.convertOnWarning = convertOnWarning;
178     }
179 
180     public boolean getCreateInChIOnWarning() {
181         return createInChIOnWarning;
182     }
183 
184     public void setCreateInChIOnWarning(boolean createInChIOnWarning) {
185         this.createInChIOnWarning = createInChIOnWarning;
186     }
187 
188     @Override
189     public int hashCode() {
190         final int PRIME = 31;
191         int result = 1;
192         result = PRIME * result + ((combine == null) ? 0 : combine.hashCode());
193         result = PRIME * result + (convert ? 1231 : 1237);
194         result = PRIME * result + (convertOnWarning ? 1231 : 1237);
195         result = PRIME * result + (createInChIOnWarning ? 1231 : 1237);
196         result = PRIME * result
197                 + ((dataFileType == null) ? 0 : dataFileType.hashCode());
198         result = PRIME * result
199                 + ((description == null) ? 0 : description.hashCode());
200         result = PRIME * result + ((digest == null) ? 0 : digest.hashCode());
201         result = PRIME * result + ((error == null) ? 0 : error.hashCode());
202         result = PRIME * result + (extractMetadata ? 1231 : 1237);
203         result = PRIME * result
204                 + ((fileName == null) ? 0 : fileName.hashCode());
205         result = PRIME * result
206                 + ((filePath == null) ? 0 : filePath.hashCode());
207         result = PRIME
208                 * result
209                 + ((metadataCombination == null) ? 0 : metadataCombination
210                         .hashCode());
211         result = PRIME * result + ((type == null) ? 0 : type.hashCode());
212         result = PRIME * result
213                 + ((validation == null) ? 0 : validation.hashCode());
214         result = PRIME * result + ((warning == null) ? 0 : warning.hashCode());
215         return result;
216     }
217 
218     @Override
219     public boolean equals(Object obj) {
220         if (this == obj)
221             return true;
222         if (obj == null)
223             return false;
224         if (getClass() != obj.getClass())
225             return false;
226         final DataFile other = (DataFile) obj;
227         if (combine == null) {
228             if (other.combine != null)
229                 return false;
230         } else if (!combine.equals(other.combine))
231             return false;
232         if (convert != other.convert)
233             return false;
234         if (convertOnWarning != other.convertOnWarning)
235             return false;
236         if (createInChIOnWarning != other.createInChIOnWarning)
237             return false;
238         if (dataFileType == null) {
239             if (other.dataFileType != null)
240                 return false;
241         } else if (!dataFileType.equals(other.dataFileType))
242             return false;
243         if (description == null) {
244             if (other.description != null)
245                 return false;
246         } else if (!description.equals(other.description))
247             return false;
248         if (digest == null) {
249             if (other.digest != null)
250                 return false;
251         } else if (!digest.equals(other.digest))
252             return false;
253         if (error == null) {
254             if (other.error != null)
255                 return false;
256         } else if (!error.equals(other.error))
257             return false;
258         if (extractMetadata != other.extractMetadata)
259             return false;
260         if (fileName == null) {
261             if (other.fileName != null)
262                 return false;
263         } else if (!fileName.equals(other.fileName))
264             return false;
265         if (filePath == null) {
266             if (other.filePath != null)
267                 return false;
268         } else if (!filePath.equals(other.filePath))
269             return false;
270         if (metadataCombination == null) {
271             if (other.metadataCombination != null)
272                 return false;
273         } else if (!metadataCombination.equals(other.metadataCombination))
274             return false;
275         if (type == null) {
276             if (other.type != null)
277                 return false;
278         } else if (!type.equals(other.type))
279             return false;
280         if (validation == null) {
281             if (other.validation != null)
282                 return false;
283         } else if (!validation.equals(other.validation))
284             return false;
285         if (warning == null) {
286             if (other.warning != null)
287                 return false;
288         } else if (!warning.equals(other.warning))
289             return false;
290         return true;
291     }
292 
293     public String toString() {
294         return new StringBuilder().append(getType()).append(": [").append(
295                 getFile()).append("]").toString();
296     }
297 
298 }