comparison src/main/java/edu/harvard/iq/dataverse/DatasetFieldCompoundValue.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 java.io.Serializable;
9 import java.util.ArrayList;
10 import java.util.Comparator;
11 import java.util.LinkedHashMap;
12 import java.util.List;
13 import java.util.Map;
14 import java.util.ResourceBundle;
15 import javax.persistence.CascadeType;
16 import javax.persistence.Entity;
17 import javax.persistence.GeneratedValue;
18 import javax.persistence.GenerationType;
19 import javax.persistence.Id;
20 import javax.persistence.Index;
21 import javax.persistence.ManyToOne;
22 import javax.persistence.OneToMany;
23 import javax.persistence.OrderBy;
24 import javax.persistence.Table;
25 import org.apache.commons.lang.StringUtils;
26
27 /**
28 *
29 * @author gdurand
30 */
31 @Entity
32 @Table(indexes = {@Index(columnList="parentdatasetfield_id")})
33 public class DatasetFieldCompoundValue implements Serializable {
34
35 private static final long serialVersionUID = 1L;
36
37 public static final Comparator<DatasetFieldCompoundValue> DisplayOrder = new Comparator<DatasetFieldCompoundValue>() {
38 @Override
39 public int compare(DatasetFieldCompoundValue o1, DatasetFieldCompoundValue o2) {
40 return Integer.compare(o1.getDisplayOrder(),
41 o2.getDisplayOrder());
42 }
43 };
44
45 public static DatasetFieldCompoundValue createNewEmptyDatasetFieldCompoundValue(DatasetField dsf) {
46 DatasetFieldCompoundValue compoundValue = new DatasetFieldCompoundValue();
47 compoundValue.setParentDatasetField(dsf);
48
49 for (DatasetFieldType dsfType : dsf.getDatasetFieldType().getChildDatasetFieldTypes()) {
50 compoundValue.getChildDatasetFields().add(DatasetField.createNewEmptyChildDatasetField(dsfType, compoundValue));
51 }
52
53 return compoundValue;
54 }
55
56 @Id
57 @GeneratedValue(strategy = GenerationType.IDENTITY)
58 private Long id;
59
60 private int displayOrder;
61
62 @ManyToOne(cascade = CascadeType.MERGE)
63 private DatasetField parentDatasetField;
64
65 @OneToMany(mappedBy = "parentDatasetFieldCompoundValue", orphanRemoval = true, cascade = {CascadeType.REMOVE, CascadeType.MERGE, CascadeType.PERSIST})
66 @OrderBy("datasetFieldType ASC")
67 private List<DatasetField> childDatasetFields = new ArrayList();
68
69 public Long getId() {
70 return id;
71 }
72
73 public void setId(Long id) {
74 this.id = id;
75 }
76
77 public int getDisplayOrder() {
78 return displayOrder;
79 }
80
81 public void setDisplayOrder(int displayOrder) {
82 this.displayOrder = displayOrder;
83 }
84
85 public DatasetField getParentDatasetField() {
86 return parentDatasetField;
87 }
88
89 public void setParentDatasetField(DatasetField parentDatasetField) {
90 this.parentDatasetField = parentDatasetField;
91 }
92
93 public List<DatasetField> getChildDatasetFields() {
94 return childDatasetFields;
95 }
96
97 public void setChildDatasetFields(List<DatasetField> childDatasetFields) {
98 this.childDatasetFields = childDatasetFields;
99 }
100
101 @Override
102 public int hashCode() {
103 int hash = 0;
104 hash += (id != null ? id.hashCode() : 0);
105 return hash;
106 }
107
108 @Override
109 public boolean equals(Object object) {
110 if (!(object instanceof DatasetFieldCompoundValue)) {
111 return false;
112 }
113 DatasetFieldCompoundValue other = (DatasetFieldCompoundValue) object;
114 return !((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id)));
115 }
116
117 @Override
118 public String toString() {
119 return "edu.harvard.iq.dataverse.DatasetFieldCompoundValue[ id=" + id + " ]";
120 }
121
122 public DatasetFieldCompoundValue copy(DatasetField parent) {
123 DatasetFieldCompoundValue compoundValue = new DatasetFieldCompoundValue();
124 compoundValue.setParentDatasetField(parent);
125 compoundValue.setDisplayOrder(displayOrder);
126
127 for (DatasetField subField : childDatasetFields) {
128 compoundValue.getChildDatasetFields().add(subField.copyChild(compoundValue));
129 }
130
131 return compoundValue;
132 }
133
134 public Map<DatasetField,String> getDisplayValueMap() {
135 // todo - this currently only supports child datasetfields with single values
136 // need to determine how we would want to handle multiple
137 Map fieldMap = new LinkedHashMap();
138
139 for (DatasetField childDatasetField : childDatasetFields) {
140 // skip the value if it is empty or N/A
141 if (!StringUtils.isBlank(childDatasetField.getValue()) && !DatasetField.NA_VALUE.equals(childDatasetField.getValue())) {
142 String format = childDatasetField.getDatasetFieldType().getDisplayFormat();
143 if (StringUtils.isBlank(format)) {
144 format = "#VALUE";
145 }
146
147 // replace the special values in the format (note: we replace #VALUE last since we don't
148 // want any issues if the value itself has #NAME in it)
149 String displayValue = format
150 .replace("#NAME", childDatasetField.getDatasetFieldType().getTitle())
151 //todo: this should be handled in more generic way for any other text that can then be internationalized
152 // if we need to use replaceAll for regexp, then make sure to use: java.util.regex.Matcher.quoteReplacement(<target string>)
153 .replace("#EMAIL", ResourceBundle.getBundle("Bundle").getString("dataset.email.hiddenMessage"))
154 .replace("#VALUE", childDatasetField.getValue());
155
156 fieldMap.put(childDatasetField,displayValue);
157 }
158 }
159
160 return fieldMap;
161 }
162 }