Mercurial > hg > LGDataverses
comparison src/main/java/edu/harvard/iq/dataverse/FileMetadata.java @ 10:a50cf11e5178
Rewrite LGDataverse completely upgrading to dataverse4.0
| author | Zoe Hong <zhong@mpiwg-berlin.mpg.de> |
|---|---|
| date | Tue, 08 Sep 2015 17:00:21 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 9:5926d6419569 | 10:a50cf11e5178 |
|---|---|
| 1 package edu.harvard.iq.dataverse; | |
| 2 | |
| 3 import java.io.Serializable; | |
| 4 import java.util.ArrayList; | |
| 5 import java.util.Collection; | |
| 6 import java.util.Comparator; | |
| 7 import java.util.List; | |
| 8 import java.util.logging.Level; | |
| 9 import java.util.logging.Logger; | |
| 10 import javax.persistence.CascadeType; | |
| 11 import javax.persistence.Column; | |
| 12 import javax.persistence.Entity; | |
| 13 import javax.persistence.GeneratedValue; | |
| 14 import javax.persistence.GenerationType; | |
| 15 import javax.persistence.Id; | |
| 16 import javax.persistence.Index; | |
| 17 import javax.persistence.JoinColumn; | |
| 18 import javax.persistence.JoinTable; | |
| 19 import javax.persistence.ManyToMany; | |
| 20 import javax.persistence.ManyToOne; | |
| 21 import javax.persistence.Table; | |
| 22 import javax.persistence.Version; | |
| 23 import org.hibernate.validator.constraints.NotBlank; | |
| 24 import javax.validation.constraints.Pattern; | |
| 25 | |
| 26 | |
| 27 /** | |
| 28 * | |
| 29 * @author skraffmiller | |
| 30 */ | |
| 31 @Table(indexes = {@Index(columnList="datafile_id"), @Index(columnList="datasetversion_id")} ) | |
| 32 @Entity | |
| 33 public class FileMetadata implements Serializable { | |
| 34 private static final long serialVersionUID = 1L; | |
| 35 | |
| 36 private static final Logger logger = Logger.getLogger(FileMetadata.class.getCanonicalName()); | |
| 37 | |
| 38 @Pattern(regexp="^[^:<>;#/\"\\*\\|\\?\\\\]*$", message = "File Name cannot contain any of the following characters: \\ / : * ? \" < > | ; # .") | |
| 39 @NotBlank(message = "Please specify a file name.") | |
| 40 @Column( nullable=false ) | |
| 41 private String label = ""; | |
| 42 @Column(columnDefinition = "TEXT") | |
| 43 private String description = ""; | |
| 44 | |
| 45 private boolean restricted; | |
| 46 | |
| 47 @ManyToOne | |
| 48 @JoinColumn(nullable=false) | |
| 49 private DatasetVersion datasetVersion; | |
| 50 @ManyToOne | |
| 51 @JoinColumn(nullable=false) | |
| 52 private DataFile dataFile; | |
| 53 | |
| 54 public String getLabel() { | |
| 55 return label; | |
| 56 } | |
| 57 | |
| 58 public void setLabel(String label) { | |
| 59 this.label = label; | |
| 60 } | |
| 61 | |
| 62 | |
| 63 public String getDescription() { | |
| 64 return description; | |
| 65 } | |
| 66 | |
| 67 public void setDescription(String description) { | |
| 68 this.description = description; | |
| 69 } | |
| 70 | |
| 71 // TODO: remove the following 2 methods: -- L.A. beta 10 | |
| 72 //public String getCategory() { | |
| 73 // return category; | |
| 74 //} | |
| 75 | |
| 76 //public void setCategory(String category) { | |
| 77 // this.category = category; | |
| 78 //} | |
| 79 | |
| 80 public boolean isRestricted() { | |
| 81 return restricted; | |
| 82 } | |
| 83 | |
| 84 public void setRestricted(boolean restricted) { | |
| 85 this.restricted = restricted; | |
| 86 } | |
| 87 | |
| 88 | |
| 89 /* | |
| 90 * File Categories to which this version of the DataFile belongs: | |
| 91 */ | |
| 92 @ManyToMany (cascade = {CascadeType.REMOVE, CascadeType.MERGE, CascadeType.PERSIST}) | |
| 93 @JoinTable(indexes = {@Index(columnList="filecategories_id"),@Index(columnList="filemetadatas_id")}) | |
| 94 private List<DataFileCategory> fileCategories; | |
| 95 | |
| 96 public List<DataFileCategory> getCategories() { | |
| 97 return fileCategories; | |
| 98 } | |
| 99 | |
| 100 public void setCategories(List<DataFileCategory> fileCategories) { | |
| 101 this.fileCategories = fileCategories; | |
| 102 } | |
| 103 | |
| 104 public void addCategory(DataFileCategory category) { | |
| 105 if (fileCategories == null) { | |
| 106 fileCategories = new ArrayList<>(); | |
| 107 } | |
| 108 fileCategories.add(category); | |
| 109 } | |
| 110 | |
| 111 public List<String> getCategoriesByName() { | |
| 112 ArrayList<String> ret = new ArrayList<>(); | |
| 113 if (fileCategories != null) { | |
| 114 for (int i = 0; i < fileCategories.size(); i++) { | |
| 115 ret.add(fileCategories.get(i).getName()); | |
| 116 } | |
| 117 } | |
| 118 return ret; | |
| 119 } | |
| 120 | |
| 121 // alternative, experimental method: | |
| 122 | |
| 123 public void setCategoriesByName(List<String> newCategoryNames) { | |
| 124 setCategories(null); // ?? TODO: investigate! | |
| 125 | |
| 126 if (newCategoryNames != null) { | |
| 127 | |
| 128 for (int i = 0; i < newCategoryNames.size(); i++) { | |
| 129 // Dataset.getCategoryByName() will check if such a category | |
| 130 // already exists for the parent dataset; it will be created | |
| 131 // if not. The method will return null if the supplied | |
| 132 // category name is null or empty. -- L.A. 4.0 beta 10 | |
| 133 DataFileCategory fileCategory = null; | |
| 134 try { | |
| 135 // Using "try {}" to catch any null pointer exceptions, | |
| 136 // just in case: | |
| 137 fileCategory = this.getDatasetVersion().getDataset().getCategoryByName(newCategoryNames.get(i)); | |
| 138 } catch (Exception ex) { | |
| 139 fileCategory = null; | |
| 140 } | |
| 141 if (fileCategory != null) { | |
| 142 this.addCategory(fileCategory); | |
| 143 fileCategory.addFileMetadata(this); | |
| 144 } | |
| 145 } | |
| 146 } | |
| 147 } | |
| 148 | |
| 149 /* | |
| 150 note that this version only *adds* new categories, but does not | |
| 151 remove the ones that has been unchecked! | |
| 152 public void setCategoriesByName(List<String> newCategoryNames) { | |
| 153 if (newCategoryNames != null) { | |
| 154 Collection<String> oldCategoryNames = getCategoriesByName(); | |
| 155 | |
| 156 | |
| 157 for (int i = 0; i < newCategoryNames.size(); i++) { | |
| 158 if (!oldCategoryNames.contains(newCategoryNames.get(i))) { | |
| 159 // Dataset.getCategoryByName() will check if such a category | |
| 160 // already exists for the parent dataset; it will be created | |
| 161 // if not. The method will return null if the supplied | |
| 162 // category name is null or empty. -- L.A. 4.0 beta 10 | |
| 163 DataFileCategory fileCategory = null; | |
| 164 try { | |
| 165 // Using "try {}" to catch any null pointer exceptions, | |
| 166 // just in case: | |
| 167 fileCategory = this.getDatasetVersion().getDataset().getCategoryByName(newCategoryNames.get(i)); | |
| 168 } catch (Exception ex) { | |
| 169 fileCategory = null; | |
| 170 } | |
| 171 if (fileCategory != null) { | |
| 172 this.addCategory(fileCategory); | |
| 173 fileCategory.addFileMetadata(this); | |
| 174 } | |
| 175 } | |
| 176 } | |
| 177 } | |
| 178 } | |
| 179 */ | |
| 180 | |
| 181 public void addCategoryByName(String newCategoryName) { | |
| 182 if (newCategoryName != null && !newCategoryName.equals("")) { | |
| 183 Collection<String> oldCategoryNames = getCategoriesByName(); | |
| 184 if (!oldCategoryNames.contains(newCategoryName)) { | |
| 185 DataFileCategory fileCategory = null; | |
| 186 // Dataset.getCategoryByName() will check if such a category | |
| 187 // already exists for the parent dataset; it will be created | |
| 188 // if not. The method will return null if the supplied | |
| 189 // category name is null or empty. -- L.A. 4.0 beta 10 | |
| 190 try { | |
| 191 // Using "try {}" to catch any null pointer exceptions, | |
| 192 // just in case: | |
| 193 fileCategory = this.getDatasetVersion().getDataset().getCategoryByName(newCategoryName); | |
| 194 } catch (Exception ex) { | |
| 195 fileCategory = null; | |
| 196 } | |
| 197 | |
| 198 | |
| 199 if (fileCategory != null) { | |
| 200 logger.log(Level.FINE, "Found file category for {0}", newCategoryName); | |
| 201 | |
| 202 this.addCategory(fileCategory); | |
| 203 fileCategory.addFileMetadata(this); | |
| 204 } else { | |
| 205 logger.log(Level.INFO, "Could not find file category for {0}", newCategoryName); | |
| 206 } | |
| 207 } else { | |
| 208 // don't do anything - this file metadata already belongs to | |
| 209 // this category. | |
| 210 } | |
| 211 } | |
| 212 } | |
| 213 | |
| 214 | |
| 215 | |
| 216 public DatasetVersion getDatasetVersion() { | |
| 217 return datasetVersion; | |
| 218 } | |
| 219 | |
| 220 public void setDatasetVersion(DatasetVersion datasetVersion) { | |
| 221 this.datasetVersion = datasetVersion; | |
| 222 } | |
| 223 | |
| 224 | |
| 225 | |
| 226 public DataFile getDataFile() { | |
| 227 return dataFile; | |
| 228 } | |
| 229 | |
| 230 public void setDataFile(DataFile dataFile) { | |
| 231 this.dataFile = dataFile; | |
| 232 } | |
| 233 | |
| 234 | |
| 235 @Id | |
| 236 @GeneratedValue(strategy = GenerationType.IDENTITY) | |
| 237 private Long id; | |
| 238 | |
| 239 /** | |
| 240 * Getter for property id. | |
| 241 * @return Value of property id. | |
| 242 */ | |
| 243 public Long getId() { | |
| 244 return this.id; | |
| 245 } | |
| 246 | |
| 247 /** | |
| 248 * Setter for property id. | |
| 249 * @param id New value of property id. | |
| 250 */ | |
| 251 public void setId(Long id) { | |
| 252 this.id = id; | |
| 253 } | |
| 254 | |
| 255 | |
| 256 @Version | |
| 257 private Long version; | |
| 258 | |
| 259 /** | |
| 260 * Getter for property version. | |
| 261 * @return Value of property version. | |
| 262 */ | |
| 263 public Long getVersion() { | |
| 264 return this.version; | |
| 265 } | |
| 266 | |
| 267 /** | |
| 268 * Setter for property version. | |
| 269 * @param version New value of property version. | |
| 270 */ | |
| 271 public void setVersion(Long version) { | |
| 272 this.version = version; | |
| 273 } | |
| 274 | |
| 275 | |
| 276 @Override | |
| 277 public int hashCode() { | |
| 278 int hash = 0; | |
| 279 hash += (id != null ? id.hashCode() : 0); | |
| 280 return hash; | |
| 281 } | |
| 282 | |
| 283 @Override | |
| 284 public boolean equals(Object object) { | |
| 285 if (!(object instanceof FileMetadata)) { | |
| 286 return false; | |
| 287 } | |
| 288 FileMetadata other = (FileMetadata) object; | |
| 289 | |
| 290 return !((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))); | |
| 291 } | |
| 292 | |
| 293 /* | |
| 294 * An experimental method for comparing 2 file metadatas *by content*; i.e., | |
| 295 * this would be for checking 2 metadatas from 2 different versions, to | |
| 296 * determine if any of the actual metadata fields have changed between | |
| 297 * versions. | |
| 298 */ | |
| 299 public boolean contentEquals(FileMetadata other) { | |
| 300 if (other == null) { | |
| 301 return false; | |
| 302 } | |
| 303 | |
| 304 if (this.getLabel() != null) { | |
| 305 if (!this.getLabel().equals(other.getLabel())) { | |
| 306 return false; | |
| 307 } | |
| 308 } else if (other.getLabel() != null) { | |
| 309 return false; | |
| 310 } | |
| 311 | |
| 312 if (this.getDescription() != null) { | |
| 313 if (!this.getDescription().equals(other.getDescription())) { | |
| 314 return false; | |
| 315 } | |
| 316 } else if (other.getDescription() != null) { | |
| 317 return false; | |
| 318 } | |
| 319 | |
| 320 /* | |
| 321 * we could also compare the sets of file categories; but since this | |
| 322 * functionality is for deciding whether to index an extra filemetadata, | |
| 323 * we're not doing it, as of now; because the categories are not indexed | |
| 324 * and not displayed on the search cards. | |
| 325 * -- L.A. 4.0 beta12 | |
| 326 */ | |
| 327 | |
| 328 return true; | |
| 329 } | |
| 330 | |
| 331 | |
| 332 @Override | |
| 333 public String toString() { | |
| 334 return "edu.harvard.iq.dvn.core.study.FileMetadata[id=" + id + "]"; | |
| 335 } | |
| 336 | |
| 337 public static final Comparator<FileMetadata> compareByLabel = new Comparator<FileMetadata>() { | |
| 338 @Override | |
| 339 public int compare(FileMetadata o1, FileMetadata o2) { | |
| 340 return o1.getLabel().toUpperCase().compareTo(o2.getLabel().toUpperCase()); | |
| 341 } | |
| 342 }; | |
| 343 } |
