-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataset.java
690 lines (578 loc) · 23.2 KB
/
Dataset.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
package edu.harvard.iq.dataverse;
import edu.harvard.iq.dataverse.harvest.client.HarvestingClient;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Index;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.UniqueConstraint;
import org.hibernate.validator.constraints.NotBlank;
/**
*
* @author skraffmiller
*/
@NamedQueries(
@NamedQuery(name = "Dataset.findByIdentifier",
query = "SELECT d FROM Dataset d WHERE d.identifier=:identifier")
)
@Entity
@Table(indexes = {
@Index(columnList = "guestbook_id"),
@Index(columnList = "thumbnailfile_id")},
uniqueConstraints = @UniqueConstraint(columnNames = {"authority,protocol,identifier,doiseparator"}))
public class Dataset extends DvObjectContainer {
// public static final String REDIRECT_URL = "/dataset.xhtml?persistentId=";
public static final String TARGET_URL = "/citation?persistentId=";
private static final long serialVersionUID = 1L;
@OneToMany(mappedBy = "owner", cascade = CascadeType.MERGE)
private List<DataFile> files = new ArrayList();
public enum FileUploadMechanism { GUI, SWORD, DROPBOX, RSYNC, GLOBUS, ASPERA };
private String protocol;
private String authority;
private String doiSeparator;
@Column(columnDefinition = "TEXT", nullable = true)
private String dcmType;
@Column(columnDefinition = "TEXT", nullable = true)
private String dcmValue;
@Temporal(value = TemporalType.TIMESTAMP)
private Date globalIdCreateTime;
@Temporal(value = TemporalType.TIMESTAMP)
private Date lastExportTime;
@NotBlank(message = "Please enter an identifier for your dataset.")
@Column(nullable = false)
private String identifier;
@OneToMany(mappedBy = "dataset", orphanRemoval = true, cascade = {CascadeType.REMOVE, CascadeType.MERGE, CascadeType.PERSIST})
@OrderBy("versionNumber DESC, minorVersionNumber DESC")
private List<DatasetVersion> versions = new ArrayList();
@OneToOne(mappedBy = "dataset", cascade = {CascadeType.REMOVE, CascadeType.MERGE, CascadeType.PERSIST})
private DatasetLock datasetLock;
@OneToOne(cascade = {CascadeType.MERGE, CascadeType.PERSIST})
@JoinColumn(name = "thumbnailfile_id")
private DataFile thumbnailFile;
@OneToOne(cascade = {CascadeType.MERGE, CascadeType.PERSIST})
@JoinColumn(name = "guestbook_id", unique = false, nullable = true, insertable = true, updatable = true)
private Guestbook guestbook;
@OneToMany(mappedBy="dataset", cascade={CascadeType.REMOVE, CascadeType.MERGE, CascadeType.PERSIST})
private List<DatasetLinkingDataverse> datasetLinkingDataverses;
public List<DatasetLinkingDataverse> getDatasetLinkingDataverses() {
return datasetLinkingDataverses;
}
public void setDatasetLinkingDataverses(List<DatasetLinkingDataverse> datasetLinkingDataverses) {
this.datasetLinkingDataverses = datasetLinkingDataverses;
}
private boolean fileAccessRequest;
@OneToMany(mappedBy = "dataset", orphanRemoval = true, cascade = {CascadeType.REMOVE, CascadeType.MERGE, CascadeType.PERSIST})
private List<DataFileCategory> dataFileCategories = null;
@ManyToOne
@JoinColumn(name = "citationDateDatasetFieldType_id")
private DatasetFieldType citationDateDatasetFieldType;
public DatasetFieldType getCitationDateDatasetFieldType() {
return citationDateDatasetFieldType;
}
public void setCitationDateDatasetFieldType(DatasetFieldType citationDateDatasetFieldType) {
this.citationDateDatasetFieldType = citationDateDatasetFieldType;
}
public Dataset() {
//this.versions = new ArrayList();
DatasetVersion datasetVersion = new DatasetVersion();
datasetVersion.setDataset(this);
datasetVersion.setVersionState(DatasetVersion.VersionState.DRAFT);
datasetVersion.setFileMetadatas(new ArrayList());
datasetVersion.setVersionNumber(new Long(1));
datasetVersion.setMinorVersionNumber(new Long(0));
versions.add(datasetVersion);
}
public String getProtocol() {
return protocol;
}
public void setProtocol(String protocol) {
this.protocol = protocol;
}
public String getAuthority() {
return authority;
}
public void setAuthority(String authority) {
this.authority = authority;
}
public String getIdentifier() {
return identifier;
}
public void setIdentifier(String identifier) {
this.identifier = identifier;
}
public String getDoiSeparator() {
return doiSeparator;
}
public void setDoiSeparator(String doiSeparator) {
this.doiSeparator = doiSeparator;
}
public Date getGlobalIdCreateTime() {
return globalIdCreateTime;
}
public void setGlobalIdCreateTime(Date globalIdCreateTime) {
this.globalIdCreateTime = globalIdCreateTime;
}
public Date getLastExportTime() {
return lastExportTime;
}
public void setLastExportTime(Date lastExportTime) {
this.lastExportTime = lastExportTime;
}
public Guestbook getGuestbook() {
return guestbook;
}
public void setGuestbook(Guestbook guestbook) {
this.guestbook = guestbook;
}
public boolean isFileAccessRequest() {
return fileAccessRequest;
}
public void setFileAccessRequest(boolean fileAccessRequest) {
this.fileAccessRequest = fileAccessRequest;
}
public String getPersistentURL() {
return new GlobalId(this).toURL().toString();
}
public String getGlobalId() {
return new GlobalId(this).toString();
}
public List<DataFile> getFiles() {
return files;
}
public void setFiles(List<DataFile> files) {
this.files = files;
}
public DatasetLock getDatasetLock() {
return datasetLock;
}
public void setDatasetLock(DatasetLock datasetLock) {
this.datasetLock = datasetLock;
}
public boolean isLocked() {
if (datasetLock != null) {
return true;
}
return false;
}
public boolean isDeaccessioned() {
// return true, if all published versions were deaccessioned
boolean hasDeaccessionedVersions = false;
for (DatasetVersion testDsv : getVersions()) {
if (testDsv.isReleased()) {
return false;
}
//Also check for draft version
if (testDsv.isDraft()) {
return false;
}
if (testDsv.isDeaccessioned()) {
hasDeaccessionedVersions = true;
}
}
return hasDeaccessionedVersions; // since any published version would have already returned
}
public DatasetVersion getLatestVersion() {
return getVersions().get(0);
}
public DatasetVersion getLatestVersionForCopy() {
for (DatasetVersion testDsv : getVersions()) {
if (testDsv.isReleased() || testDsv.isArchived()) {
return testDsv;
}
}
return getVersions().get(0);
}
public List<DatasetVersion> getVersions() {
return versions;
}
public void setVersions(List<DatasetVersion> versions) {
this.versions = versions;
}
private DatasetVersion createNewDatasetVersion(Template template) {
DatasetVersion dsv = new DatasetVersion();
dsv.setVersionState(DatasetVersion.VersionState.DRAFT);
dsv.setFileMetadatas(new ArrayList());
DatasetVersion latestVersion;
//if the latest version has values get them copied over
if (template != null) {
dsv.updateDefaultValuesFromTemplate(template);
} else {
latestVersion = getLatestVersionForCopy();
if (latestVersion.getUNF() != null){
dsv.setUNF(latestVersion.getUNF());
}
if (latestVersion.getDatasetFields() != null && !latestVersion.getDatasetFields().isEmpty()) {
dsv.setDatasetFields(dsv.copyDatasetFields(latestVersion.getDatasetFields()));
}
if (latestVersion.getTermsOfUseAndAccess()!= null){
dsv.setTermsOfUseAndAccess(latestVersion.getTermsOfUseAndAccess().copyTermsOfUseAndAccess());
} else {
TermsOfUseAndAccess terms = new TermsOfUseAndAccess();
terms.setDatasetVersion(dsv);
terms.setLicense(TermsOfUseAndAccess.License.CC0);
dsv.setTermsOfUseAndAccess(terms);
}
for (FileMetadata fm : latestVersion.getFileMetadatas()) {
FileMetadata newFm = new FileMetadata();
// TODO:
// the "category" will be removed, shortly.
// (replaced by multiple, tag-like categories of
// type DataFileCategory) -- L.A. beta 10
//newFm.setCategory(fm.getCategory());
// yep, these are the new categories:
newFm.setCategories(fm.getCategories());
newFm.setDescription(fm.getDescription());
newFm.setLabel(fm.getLabel());
newFm.setDirectoryLabel(fm.getDirectoryLabel());
newFm.setRestricted(fm.isRestricted());
newFm.setDataFile(fm.getDataFile());
newFm.setDatasetVersion(dsv);
dsv.getFileMetadatas().add(newFm);
}
}
// I'm adding the version to the list so it will be persisted when
// the study object is persisted.
if (template == null) {
getVersions().add(0, dsv);
} else {
this.setVersions(new ArrayList());
getVersions().add(0, dsv);
}
dsv.setDataset(this);
return dsv;
}
public DatasetVersion getEditVersion() {
return getEditVersion(null);
}
public DatasetVersion getEditVersion(Template template) {
DatasetVersion latestVersion = this.getLatestVersion();
if (!latestVersion.isWorkingCopy() || template != null) {
// if the latest version is released or archived, create a new version for editing
return createNewDatasetVersion(template);
} else {
// else, edit existing working copy
return latestVersion;
}
}
public DatasetVersion getCreateVersion() {
DatasetVersion dsv = new DatasetVersion();
dsv.setVersionState(DatasetVersion.VersionState.DRAFT);
dsv.setDataset(this);
dsv.initDefaultValues();
this.setVersions(new ArrayList());
getVersions().add(0, dsv);
return dsv;
}
public Date getMostRecentMajorVersionReleaseDate() {
if (this.isHarvested()) {
return getVersions().get(0).getReleaseTime();
} else {
for (DatasetVersion version : this.getVersions()) {
if (version.isReleased() && version.getMinorVersionNumber().equals(new Long(0))) {
return version.getReleaseTime();
}
}
return null;
}
}
public DatasetVersion getReleasedVersion() {
for (DatasetVersion version : this.getVersions()) {
if (version.isReleased()) {
return version;
}
}
return null;
}
public List<DataFileCategory> getCategories() {
return dataFileCategories;
}
public void setFileCategories(List<DataFileCategory> categories) {
this.dataFileCategories = categories;
}
public void addFileCategory(DataFileCategory category) {
if (dataFileCategories == null) {
dataFileCategories = new ArrayList<>();
}
dataFileCategories.add(category);
}
public Collection<String> getCategoriesByName() {
Collection<String> ret = getCategoryNames();
// "Documentation", "Data" and "Code" are the 3 default categories that we
// present by default:
// (TODO: ? - provide these as constants somewhere? -- L.A. beta15)
if (!ret.contains("Documentation")) {
ret.add("Documentation");
}
if (!ret.contains("Data")) {
ret.add("Data");
}
if (!ret.contains("Code")) {
ret.add("Code");
}
return ret;
}
public void setCategoriesByName(List<String> newCategoryNames) {
if (newCategoryNames != null) {
Collection<String> oldCategoryNames = getCategoryNames();
for (int i = 0; i < newCategoryNames.size(); i++) {
if (!oldCategoryNames.contains(newCategoryNames.get(i))) {
DataFileCategory newCategory = new DataFileCategory();
newCategory.setName(newCategoryNames.get(i));
newCategory.setDataset(this);
this.addFileCategory(newCategory);
}
}
}
}
/*
public void addCategoryByName(String newCategoryName) {
if (newCategoryName != null && !newCategoryName.equals("")) {
Collection<String> oldCategoryNames = getCategoryNames();
if (!oldCategoryNames.contains(newCategoryName)) {
DataFileCategory newCategory = new DataFileCategory();
newCategory.setName(newCategoryName);
newCategory.setDataset(this);
this.addFileCategory(newCategory);
}
}
}*/
public DataFileCategory getCategoryByName(String categoryName) {
if (categoryName != null && !categoryName.equals("")) {
if (dataFileCategories != null) {
for (int i = 0; i < dataFileCategories.size(); i++) {
if (categoryName.equals(dataFileCategories.get(i).getName())) {
return dataFileCategories.get(i);
}
}
}
DataFileCategory newCategory = new DataFileCategory();
newCategory.setName(categoryName);
newCategory.setDataset(this);
this.addFileCategory(newCategory);
return newCategory;
}
return null;
}
private Collection<String> getCategoryNames() {
if (dataFileCategories != null) {
ArrayList<String> ret = new ArrayList<>(dataFileCategories.size());
for ( DataFileCategory dfc : dataFileCategories ) {
ret.add( dfc.getName() );
}
return ret;
} else {
return new ArrayList<>();
}
}
public Path getFileSystemDirectory() {
Path studyDir = null;
String filesRootDirectory = System.getProperty("dataverse.files.directory");
if (filesRootDirectory == null || filesRootDirectory.equals("")) {
filesRootDirectory = "/tmp/files";
}
if (this.getAuthority() != null && this.getIdentifier() != null) {
studyDir = Paths.get(filesRootDirectory, this.getAuthority(), this.getIdentifier());
}
return studyDir;
}
public String getNextMajorVersionString() {
// Never need to get the next major version for harvested studies.
if (isHarvested()) {
throw new IllegalStateException();
}
for (DatasetVersion dv : this.getVersions()) {
if (!dv.isWorkingCopy()) {
return Integer.toString(dv.getVersionNumber().intValue() + 1) + ".0";
}
}
return "1.0";
}
public String getNextMinorVersionString() {
// Never need to get the next minor version for harvested studies.
if (isHarvested()) {
throw new IllegalStateException();
}
for (DatasetVersion dv : this.getVersions()) {
if (!dv.isWorkingCopy()) {
return Integer.toString(dv.getVersionNumber().intValue()) + "."
+ Integer.toString(dv.getMinorVersionNumber().intValue() + 1);
}
}
return "1.0";
}
public Integer getVersionNumber() {
for (DatasetVersion dv : this.getVersions()) {
if (!dv.isWorkingCopy()) {
return dv.getVersionNumber().intValue();
}
}
return 1;
}
public Integer getMinorVersionNumber() {
for (DatasetVersion dv : this.getVersions()) {
if (!dv.isWorkingCopy()) {
return dv.getMinorVersionNumber().intValue();
}
}
return 0;
}
public String getCitation() {
return getCitation(false, getLatestVersion());
}
public String getCitation(DatasetVersion version) {
return version.getCitation();
}
public String getCitation(boolean isOnlineVersion, DatasetVersion version) {
return version.getCitation(isOnlineVersion);
}
public String getPublicationDateFormattedYYYYMMDD() {
if (getPublicationDate() != null){
return new SimpleDateFormat("yyyy-MM-dd").format(getPublicationDate());
}
return null;
}
public DataFile getThumbnailFile() {
return thumbnailFile;
}
public void setThumbnailFile(DataFile thumbnailFile) {
this.thumbnailFile = thumbnailFile;
}
@ManyToOne
@JoinColumn(name="harvestingClient_id")
private HarvestingClient harvestedFrom;
public HarvestingClient getHarvestedFrom() {
return this.harvestedFrom;
}
public void setHarvestedFrom(HarvestingClient harvestingClientConfig) {
this.harvestedFrom = harvestingClientConfig;
}
public boolean isHarvested() {
return this.harvestedFrom != null;
}
private String harvestIdentifier;
public String getHarvestIdentifier() {
return harvestIdentifier;
}
public void setHarvestIdentifier(String harvestIdentifier) {
this.harvestIdentifier = harvestIdentifier;
}
public String getRemoteArchiveURL() {
if (isHarvested()) {
if (HarvestingClient.HARVEST_STYLE_DATAVERSE.equals(this.getHarvestedFrom().getHarvestStyle())) {
return this.getHarvestedFrom().getArchiveUrl() + "/dataset.xhtml?persistentId=" + getGlobalId();
} else if (HarvestingClient.HARVEST_STYLE_VDC.equals(this.getHarvestedFrom().getHarvestStyle())) {
String rootArchiveUrl = this.getHarvestedFrom().getHarvestingUrl();
int c = rootArchiveUrl.indexOf("/OAIHandler");
if (c > 0) {
rootArchiveUrl = rootArchiveUrl.substring(0, c);
return rootArchiveUrl + "/faces/study/StudyPage.xhtml?globalId=" + getGlobalId();
}
} else if (HarvestingClient.HARVEST_STYLE_ICPSR.equals(this.getHarvestedFrom().getHarvestStyle())) {
// For the ICPSR, it turns out that the best thing to do is to
// rely on the DOI to send the user to the right landing page for
// the study:
//String icpsrId = identifier;
//return this.getOwner().getHarvestingClient().getArchiveUrl() + "/icpsrweb/ICPSR/studies/"+icpsrId+"?q="+icpsrId+"&searchSource=icpsr-landing";
return "http://doi.org/" + authority + "/" + identifier;
} else if (HarvestingClient.HARVEST_STYLE_NESSTAR.equals(this.getHarvestedFrom().getHarvestStyle())) {
String nServerURL = this.getHarvestedFrom().getArchiveUrl();
// chop any trailing slashes in the server URL - or they will result
// in multiple slashes in the final URL pointing to the study
// on server of origin; Nesstar doesn't like it, apparently.
nServerURL = nServerURL.replaceAll("/*$", "");
String nServerURLencoded = nServerURL;
nServerURLencoded.replace(":", "%3A");
nServerURLencoded.replace("/", "%2F");
String NesstarWebviewPage = nServerURL
+ "/webview/?mode=documentation&submode=abstract&studydoc="
+ nServerURLencoded + "%2Fobj%2FfStudy%2F"
+ identifier
+ "&top=yes";
return NesstarWebviewPage;
} else if (HarvestingClient.HARVEST_STYLE_ROPER.equals(this.getHarvestedFrom().getHarvestStyle())) {
return this.getHarvestedFrom().getArchiveUrl() + "/CFIDE/cf/action/catalog/abstract.cfm?archno=" + identifier;
} else if (HarvestingClient.HARVEST_STYLE_HGL.equals(this.getHarvestedFrom().getHarvestStyle())) {
// a bit of a hack, true.
// HGL documents, when turned into Dataverse studies/datasets
// all 1 datafile; the location ("storage identifier") of the file
// is the URL pointing back to the HGL GUI viewer. This is what
// we will display for the dataset URL. -- L.A.
// TODO: create a 4.+ ticket for a cleaner solution.
List<DataFile> dataFiles = this.getFiles();
if (dataFiles != null && dataFiles.size() == 1) {
if (dataFiles.get(0) != null) {
String hglUrl = dataFiles.get(0).getStorageIdentifier();
if (hglUrl != null && hglUrl.matches("^http.*")) {
return hglUrl;
}
}
}
return this.getHarvestedFrom().getArchiveUrl();
}else {
return this.getHarvestedFrom().getArchiveUrl();
}
}
return null;
}
public String getHarvestingDescription() {
if (isHarvested()) {
return this.getHarvestedFrom().getArchiveDescription();
}
return null;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Dataset)) {
return false;
}
Dataset other = (Dataset) object;
return Objects.equals(getId(), other.getId());
}
@Override
public <T> T accept(Visitor<T> v) {
return v.visit(this);
}
@Override
public String getDisplayName() {
DatasetVersion dsv = getReleasedVersion();
return dsv != null ? dsv.getTitle() : getLatestVersion().getTitle();
}
@Override
protected boolean isPermissionRoot() {
return false;
}
@Override
public boolean isAncestorOf( DvObject other ) {
return equals(other) || equals(other.getOwner());
}
public String getDcmType() {
return dcmType;
}
public void setDcmType(String dcmType) {
this.dcmType = dcmType;
}
public String getDcmValue() {
return dcmValue;
}
public void setDcmValue(String dcmValue) {
this.dcmValue = dcmValue;
}
}