comparison src/main/java/edu/harvard/iq/dataverse/DatasetVersionUI.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 /*
2 * To change this license header, choose License Headers in Project Properties.
3 * To change this template file, choose Tools | Templates
4 * and open the template in the editor.
5 */
6 package edu.harvard.iq.dataverse;
7
8 import edu.harvard.iq.dataverse.util.MarkupChecker;
9 import edu.harvard.iq.dataverse.util.StringUtil;
10 import java.io.Serializable;
11 import java.sql.Timestamp;
12 import java.text.SimpleDateFormat;
13 import java.util.ArrayList;
14 import java.util.Calendar;
15 import java.util.Collections;
16 import java.util.Comparator;
17 import java.util.Date;
18 import java.util.HashMap;
19 import java.util.List;
20 import java.util.Map;
21 import javax.ejb.EJB;
22 import javax.faces.view.ViewScoped;
23 import javax.inject.Named;
24 import javax.persistence.EntityManager;
25 import javax.persistence.PersistenceContext;
26
27 /**
28 *
29 * @author skraffmiller
30 */
31 @ViewScoped
32 public class DatasetVersionUI implements Serializable {
33
34 @EJB
35 DataverseServiceBean dataverseService;
36 @PersistenceContext(unitName = "VDCNet-ejbPU")
37 private EntityManager em;
38
39 public DatasetVersionUI() {
40 }
41
42 private Map<MetadataBlock, List<DatasetField>> metadataBlocksForView = new HashMap();
43 private Map<MetadataBlock, List<DatasetField>> metadataBlocksForEdit = new HashMap();
44
45 public Map<MetadataBlock, List<DatasetField>> getMetadataBlocksForView() {
46 return metadataBlocksForView;
47 }
48
49 public void setMetadataBlocksForView(Map<MetadataBlock, List<DatasetField>> metadataBlocksForView) {
50 this.metadataBlocksForView = metadataBlocksForView;
51 }
52
53 public Map<MetadataBlock, List<DatasetField>> getMetadataBlocksForEdit() {
54 return metadataBlocksForEdit;
55 }
56
57 public void setMetadataBlocksForEdit(Map<MetadataBlock, List<DatasetField>> metadataBlocksForEdit) {
58 this.metadataBlocksForEdit = metadataBlocksForEdit;
59 }
60
61 public DatasetVersionUI initDatasetVersionUI(DatasetVersion datasetVersion, boolean createBlanks) {
62 /*takes in the values of a dataset version
63 and apportions them into lists for
64 viewing and editng in the dataset page.
65 */
66
67 setDatasetVersion(datasetVersion);
68 //this.setDatasetAuthors(new ArrayList());
69 this.setDatasetRelPublications(new ArrayList());
70
71 // loop through vaues to get fields for view mode
72 for (DatasetField dsf : datasetVersion.getDatasetFields()) {
73 //Special Handling for various fields displayed above tabs in dataset page view.
74 if (dsf.getDatasetFieldType().getName().equals(DatasetFieldConstant.title)) {
75 setTitle(dsf);
76 } else if (dsf.getDatasetFieldType().getName().equals(DatasetFieldConstant.description)) {
77 setDescription(dsf);
78 String descriptionString = "";
79 if(dsf.getDatasetFieldCompoundValues() != null && dsf.getDatasetFieldCompoundValues().get(0) != null){
80 DatasetFieldCompoundValue descriptionValue = dsf.getDatasetFieldCompoundValues().get(0);
81 for (DatasetField subField : descriptionValue.getChildDatasetFields()) {
82 if (subField.getDatasetFieldType().getName().equals(DatasetFieldConstant.descriptionText) && !subField.isEmptyForDisplay()) {
83 descriptionString = subField.getValue();
84 }
85 }
86 }
87 setDescriptionDisplay(MarkupChecker.sanitizeBasicHTML(descriptionString) );
88 } else if (dsf.getDatasetFieldType().getName().equals(DatasetFieldConstant.keyword)) {
89 setKeyword(dsf);
90 String keywordString = "";
91 for (DatasetFieldCompoundValue keywordValue : dsf.getDatasetFieldCompoundValues()) {
92 for (DatasetField subField : keywordValue.getChildDatasetFields()) {
93 if (subField.getDatasetFieldType().getName().equals(DatasetFieldConstant.keywordValue) && !subField.isEmptyForDisplay()) {
94 if (keywordString.isEmpty()){
95 keywordString = subField.getValue();
96 } else {
97 keywordString += ", " + subField.getValue();
98 }
99 }
100 }
101 }
102 setKeywordDisplay(keywordString);
103 } else if (dsf.getDatasetFieldType().getName().equals(DatasetFieldConstant.subject) && !dsf.isEmptyForDisplay()) {
104 setSubject(dsf);
105 } else if (dsf.getDatasetFieldType().getName().equals(DatasetFieldConstant.notesText) && !dsf.isEmptyForDisplay()) {
106 this.setNotes(dsf);
107 } else if (dsf.getDatasetFieldType().getName().equals(DatasetFieldConstant.publication)) {
108 //Special handling for Related Publications
109 // Treated as below the tabs for editing, but must get first value for display above tabs
110 if (this.datasetRelPublications.isEmpty()) {
111 for (DatasetFieldCompoundValue relPubVal : dsf.getDatasetFieldCompoundValues()) {
112 DatasetRelPublication datasetRelPublication = new DatasetRelPublication();
113 datasetRelPublication.setTitle(dsf.getDatasetFieldType().getTitle());
114 datasetRelPublication.setDescription(dsf.getDatasetFieldType().getDescription());
115 for (DatasetField subField : relPubVal.getChildDatasetFields()) {
116 if (subField.getDatasetFieldType().getName().equals(DatasetFieldConstant.publicationCitation)) {
117 datasetRelPublication.setText(subField.getValue());
118 }
119 if (subField.getDatasetFieldType().getName().equals(DatasetFieldConstant.publicationIDNumber)) {
120 datasetRelPublication.setIdNumber(subField.getValue());
121 }
122 if (subField.getDatasetFieldType().getName().equals(DatasetFieldConstant.publicationIDType)) {
123 datasetRelPublication.setIdType(subField.getValue());
124 }
125 if (subField.getDatasetFieldType().getName().equals(DatasetFieldConstant.publicationURL)) {
126 datasetRelPublication.setUrl(subField.getValue());
127 }
128 }
129 this.getDatasetRelPublications().add(datasetRelPublication);
130 }
131 }
132 }
133 }
134
135 datasetVersion.setDatasetFields(initDatasetFields(createBlanks));
136
137 setMetadataValueBlocks(datasetVersion);
138
139 return this;
140 }
141
142 private Dataset getDataset() {
143 return this.datasetVersion.getDataset();
144 }
145
146 private DatasetVersion datasetVersion;
147
148 public DatasetVersion getDatasetVersion() {
149 return datasetVersion;
150 }
151
152 public void setDatasetVersion(DatasetVersion datasetVersion) {
153 this.datasetVersion = datasetVersion;
154 }
155
156 private DatasetField title;
157 private DatasetField description;
158 private DatasetField keyword;
159 private DatasetField subject;
160 private DatasetField notes;
161 private String keywordDisplay;
162
163 public String getKeywordDisplay() {
164 return keywordDisplay;
165 }
166
167 public void setKeywordDisplay(String keywordDisplay) {
168 this.keywordDisplay = keywordDisplay;
169 }
170 private String descriptionDisplay;
171
172 public String getDescriptionDisplay() {
173 return descriptionDisplay;
174 }
175
176 public void setDescriptionDisplay(String descriptionDisplay) {
177 this.descriptionDisplay = descriptionDisplay;
178 }
179
180
181 private List<DatasetRelPublication> datasetRelPublications;
182
183 public DatasetField getTitle() {
184 return title;
185 }
186
187 public void setTitle(DatasetField title) {
188 this.title = title;
189 }
190
191 public DatasetField getDescription() {
192 return description;
193 }
194
195 public void setDescription(DatasetField description) {
196 this.description = description;
197 }
198
199 public DatasetField getKeyword() {
200 return keyword;
201 }
202
203 public void setKeyword(DatasetField keyword) {
204 this.keyword = keyword;
205 }
206
207 public DatasetField getSubject() {
208 return subject;
209 }
210
211 public void setSubject(DatasetField subject) {
212 this.subject = subject;
213 }
214
215 public DatasetField getNotes() {
216 return notes;
217 }
218
219 public void setNotes(DatasetField notes) {
220 this.notes = notes;
221 }
222
223
224
225
226
227 public List<DatasetRelPublication> getDatasetRelPublications() {
228 return datasetRelPublications;
229 }
230
231 public void setDatasetRelPublications(List<DatasetRelPublication> datasetRelPublications) {
232 this.datasetRelPublications = datasetRelPublications;
233 }
234
235
236
237 public String getRelPublicationCitation() {
238 if (!this.datasetRelPublications.isEmpty()) {
239 return this.getDatasetRelPublications().get(0).getText();
240 } else {
241 return "";
242 }
243 }
244
245 public String getRelPublicationId() {
246 if (!this.datasetRelPublications.isEmpty()) {
247 if (!(this.getDatasetRelPublications().get(0).getIdNumber() == null) && !this.getDatasetRelPublications().get(0).getIdNumber().isEmpty()){
248 return this.getDatasetRelPublications().get(0).getIdType() + ": " + this.getDatasetRelPublications().get(0).getIdNumber();
249 } else {
250 return "";
251 }
252 } else {
253 return "";
254 }
255 }
256
257 public String getRelPublicationUrl() {
258 if (!this.datasetRelPublications.isEmpty()) {
259 return this.getDatasetRelPublications().get(0).getUrl();
260 } else {
261 return "";
262 }
263 }
264
265 public String getUNF() {
266 //todo get UNF to calculate and display here.
267 return "";
268 }
269
270 //TODO - make sure getCitation works
271 private String getYearForCitation(String dateString) {
272 //get date to first dash only
273 if (dateString.indexOf("-") > -1) {
274 return dateString.substring(0, dateString.indexOf("-"));
275 }
276 return dateString;
277 }
278
279 public String getReleaseDate() {
280 if (datasetVersion.getReleaseTime() != null) {
281 Date relDate = datasetVersion.getReleaseTime();
282 Calendar calendar = Calendar.getInstance();
283 calendar.setTime(relDate);
284 return Integer.toString(calendar.get(Calendar.YEAR));
285 }
286 return "";
287 }
288
289 public String getCreateDate() {
290 if (datasetVersion.getCreateTime() != null) {
291 Date relDate = datasetVersion.getCreateTime();
292 Calendar calendar = Calendar.getInstance();
293 calendar.setTime(relDate);
294 return Integer.toString(calendar.get(Calendar.YEAR));
295 }
296 return "";
297 }
298
299 public String getProductionDate() {
300 for (DatasetField dsfv : datasetVersion.getDatasetFields()) {
301 if (dsfv.getDatasetFieldType().getName().equals(DatasetFieldConstant.productionDate)) {
302 return dsfv.getValue();
303 }
304 }
305 return "";
306 }
307
308 public String getDistributionDate() {
309 for (DatasetField dsfv : datasetVersion.getDatasetFields()) {
310 if (dsfv.getDatasetFieldType().getName().equals(DatasetFieldConstant.distributionDate)) {
311 return dsfv.getValue();
312 }
313 }
314 return "";
315 }
316
317 // TODO: clean up init methods and get them to work, cascading all the way down.
318 // right now, only work for one level of compound objects
319 private DatasetField initDatasetField(DatasetField dsf, boolean createBlanks) {
320 if (dsf.getDatasetFieldType().isCompound()) {
321 for (DatasetFieldCompoundValue cv : dsf.getDatasetFieldCompoundValues()) {
322 // for each compound value; check the datasetfieldTypes associated with its type
323 for (DatasetFieldType dsfType : dsf.getDatasetFieldType().getChildDatasetFieldTypes()) {
324 boolean add = createBlanks;
325 for (DatasetField subfield : cv.getChildDatasetFields()) {
326 if (dsfType.equals(subfield.getDatasetFieldType())) {
327 add = false;
328 break;
329 }
330 }
331
332 if (add) {
333 cv.getChildDatasetFields().add(DatasetField.createNewEmptyChildDatasetField(dsfType, cv));
334 }
335 }
336
337 sortDatasetFields(cv.getChildDatasetFields());
338 }
339 }
340
341 return dsf;
342 }
343
344 private List<DatasetField> initDatasetFields(boolean createBlanks) {
345 //retList - Return List of values
346 List<DatasetField> retList = new ArrayList();
347 for (DatasetField dsf : this.datasetVersion.getDatasetFields()) {
348 retList.add(initDatasetField(dsf, createBlanks));
349 }
350
351
352 //Test to see that there are values for
353 // all fields in this dataset via metadata blocks
354 //only add if not added above
355 for (MetadataBlock mdb : this.getDataset().getOwner().getMetadataBlocks()) {
356 for (DatasetFieldType dsfType : mdb.getDatasetFieldTypes()) {
357 if (!dsfType.isSubField()) {
358 boolean add = createBlanks;
359 //don't add if already added as a val
360 for (DatasetField dsf : retList) {
361 if (dsfType.equals(dsf.getDatasetFieldType())) {
362 add = false;
363 break;
364 }
365 }
366
367 if (add) {
368 retList.add(DatasetField.createNewEmptyDatasetField(dsfType, this.datasetVersion));
369 }
370 }
371 }
372 }
373
374 //sort via display order on dataset field
375 Collections.sort(retList, new Comparator<DatasetField>() {
376 public int compare(DatasetField d1, DatasetField d2) {
377 int a = d1.getDatasetFieldType().getDisplayOrder();
378 int b = d2.getDatasetFieldType().getDisplayOrder();
379 return Integer.valueOf(a).compareTo(Integer.valueOf(b));
380 }
381 });
382
383 return sortDatasetFields(retList);
384 }
385
386 private List<DatasetField> sortDatasetFields (List<DatasetField> dsfList) {
387 Collections.sort(dsfList, new Comparator<DatasetField>() {
388 public int compare(DatasetField d1, DatasetField d2) {
389 int a = d1.getDatasetFieldType().getDisplayOrder();
390 int b = d2.getDatasetFieldType().getDisplayOrder();
391 return Integer.valueOf(a).compareTo(Integer.valueOf(b));
392 }
393 });
394 return dsfList;
395 }
396
397 public void setMetadataValueBlocks(DatasetVersion datasetVersion) {
398 //TODO: A lot of clean up on the logic of this method
399 metadataBlocksForView.clear();
400 metadataBlocksForEdit.clear();
401 Long dvIdForInputLevel = datasetVersion.getDataset().getOwner().getId();
402
403 if (!dataverseService.find(dvIdForInputLevel).isMetadataBlockRoot()){
404 dvIdForInputLevel = dataverseService.find(dvIdForInputLevel).getMetadataRootId();
405 }
406
407 List<DatasetField> filledInFields = this.datasetVersion.getDatasetFields();
408
409 List <MetadataBlock> actualMDB = new ArrayList();
410
411 actualMDB.addAll(this.datasetVersion.getDataset().getOwner().getMetadataBlocks());
412
413 for (DatasetField dsfv : filledInFields) {
414 if (!dsfv.isEmptyForDisplay()) {
415 MetadataBlock mdbTest = dsfv.getDatasetFieldType().getMetadataBlock();
416 if (!actualMDB.contains(mdbTest)) {
417 actualMDB.add(mdbTest);
418 }
419 }
420 }
421
422 for (MetadataBlock mdb : actualMDB) {
423 mdb.setEmpty(true);
424 mdb.setHasRequired(false);
425 List<DatasetField> datasetFieldsForView = new ArrayList();
426 List<DatasetField> datasetFieldsForEdit = new ArrayList();
427 for (DatasetField dsf : datasetVersion.getDatasetFields()) {
428 if (dsf.getDatasetFieldType().getMetadataBlock().equals(mdb)) {
429 datasetFieldsForEdit.add(dsf);
430 if (dsf.isRequired()) {
431 mdb.setHasRequired(true);
432 }
433 if (!dsf.isEmptyForDisplay()) {
434 mdb.setEmpty(false);
435 datasetFieldsForView.add(dsf);
436 }
437 }
438 }
439
440 if (!datasetFieldsForView.isEmpty()) {
441 metadataBlocksForView.put(mdb, datasetFieldsForView);
442 }
443 if (!datasetFieldsForEdit.isEmpty()) {
444 metadataBlocksForEdit.put(mdb, datasetFieldsForEdit);
445 }
446 }
447 }
448
449 }