]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
b37bb606b858b2328538780114bb0bdbdefefd92
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.structureddate;
2
3 import org.apache.commons.lang.builder.EqualsBuilder;
4 import org.collectionspace.services.structureddate.antlr.ANTLRStructuredDateEvaluator;
5
6
7 /**
8  * A CollectionSpace structured date. 
9  */
10 public class StructuredDate {
11         // The UI layer is interpreting scalarValuesComputed as follows:
12         //  - If true, the UI should compute scalar values
13         //  - If false (or null), the UI should not compute scalar values
14         // Given that interpretation, scalarValuesComputed should default
15         // to true.
16         public static final boolean DEFAULT_SCALAR_VALUES_COMPUTED = true;
17         
18         private String displayDate;
19         private String note;
20         private String association;
21         private String period;
22         
23         private Date earliestSingleDate;
24         private Date latestDate;
25         
26         private String earliestScalarDate;
27         private String latestScalarDate;
28         private Boolean scalarValuesComputed;
29                 
30         public StructuredDate() {
31                 scalarValuesComputed = DEFAULT_SCALAR_VALUES_COMPUTED;
32         }
33
34         public String toString() {
35                 String string =
36                         "\n" +
37                         "\tdisplayDate: " + getDisplayDate() + "\n" +
38                         "\tnote:        " + getNote() + "\n" +
39                         "\tassociation: " + getAssociation() + "\n" +
40                         "\tperiod:      " + getPeriod() + "\n";
41                 
42                 if (getEarliestSingleDate() != null) {
43                         string += 
44                                 "\n" +
45                                 "\tearliestSingleDate: \n" +
46                                 getEarliestSingleDate().toString() + "\n";
47                 }
48                 
49                 if (getLatestDate() != null) {
50                         string += 
51                                 "\n" +
52                                 "\tlatestDate: \n" +
53                                 getLatestDate().toString() + "\n";
54                 }
55                 
56                 return string;
57         }
58         
59         @Override
60         public boolean equals(Object obj) {
61                 if (obj == null) { 
62                         return false;
63                 }
64                 
65                 if (obj == this) {
66                         return true;
67                 }
68                 
69                 if (obj.getClass() != getClass()) {
70                         return false;
71                 }
72                 
73                 StructuredDate that = (StructuredDate) obj;
74
75                 return 
76                         new EqualsBuilder()
77                                 .append(this.getDisplayDate(), that.getDisplayDate())
78                                 .append(this.getAssociation(), that.getAssociation())
79                                 .append(this.getNote(), that.getNote())
80                                 .append(this.getPeriod(), that.getPeriod())
81                                 .append(this.getEarliestSingleDate(), that.getEarliestSingleDate())
82                                 .append(this.getLatestDate(), that.getLatestDate())
83                                 .append(this.areScalarValuesComputed(), that.areScalarValuesComputed())
84                                 .isEquals();
85         }
86         
87         public void computeScalarValues() {
88                 Date earliestDate = getEarliestSingleDate();
89                 Date latestDate = getLatestDate();
90                 
91                 if (earliestDate == null && latestDate == null) {
92                         setEarliestScalarDate(null);
93                         setLatestScalarDate(null);
94                         
95                         return;
96                 }
97                 
98                 if (earliestDate == null) {
99                         earliestDate = latestDate.copy();
100                 }
101                 else {
102                         earliestDate = earliestDate.copy();
103                 }
104                 
105                 if (latestDate == null) {
106                         latestDate = earliestDate.copy();
107                 }
108                 else {
109                         latestDate = latestDate.copy();
110                 }
111                 
112                 if (earliestDate.getYear() == null || latestDate.getYear() == null) {
113                         // The dates must at least specify a year.
114                         throw new InvalidDateException("year must not be null");
115                 }
116                 
117                 if (earliestDate.getDay() != null && earliestDate.getMonth() == null) {
118                         // If a day is specified, the month must be specified.
119                         throw new InvalidDateException("month may not be null when day is not null");
120                 }
121
122                 if (latestDate.getDay() != null && latestDate.getMonth() == null) {
123                         // If a day is specified, the month must be specified.
124                         throw new InvalidDateException("month may not be null when day is not null");
125                 }
126                 
127                 if (earliestDate.getEra() == null) {
128                         earliestDate.setEra(Date.DEFAULT_ERA);
129                 }
130                 
131                 if (latestDate.getEra() == null) {
132                         latestDate.setEra(Date.DEFAULT_ERA);
133                 }
134                 
135                 if (earliestDate.getMonth() == null) {
136                         earliestDate.setMonth(1);
137                         earliestDate.setDay(1);
138                 }
139                 
140                 if (latestDate.getMonth() == null) {
141                         latestDate.setMonth(12);
142                         latestDate.setDay(31);
143                 }
144                 
145                 if (earliestDate.getDay() == null) {
146                         earliestDate.setDay(1);
147                 }
148                 
149                 if (latestDate.getDay() == null) {
150                         latestDate.setDay(DateUtils.getDaysInMonth(latestDate.getMonth(), latestDate.getYear(), latestDate.getEra()));
151                 }
152                 
153                 // Add one day to the latest day, since that's what the UI does.
154                 // DateUtils.addDays(latestDate, 1);
155                 
156                 setEarliestScalarDate(DateUtils.getEarliestTimestamp(earliestDate));
157                 setLatestScalarDate(DateUtils.getLatestTimestamp(latestDate));
158         }
159         
160         public static StructuredDate parse(String displayDate) throws StructuredDateFormatException {
161                 StructuredDateEvaluator evaluator = new ANTLRStructuredDateEvaluator();
162                 
163                 return evaluator.evaluate(displayDate);
164         }
165
166         public String getDisplayDate() {
167                 return displayDate;
168         }
169
170         public void setDisplayDate(String displayDate) {
171                 this.displayDate = displayDate;
172         }
173
174         public String getNote() {
175                 return note;
176         }
177
178         public void setNote(String note) {
179                 this.note = note;
180         }
181
182         public String getAssociation() {
183                 return association;
184         }
185
186         public void setAssociation(String association) {
187                 this.association = association;
188         }
189
190         public String getPeriod() {
191                 return period;
192         }
193
194         public void setPeriod(String period) {
195                 this.period = period;
196         }
197
198         public Date getEarliestSingleDate() {
199                 return earliestSingleDate;
200         }
201
202         public void setEarliestSingleDate(Date earliestSingleDate) {
203                 this.earliestSingleDate = earliestSingleDate;
204         }
205
206         public Date getLatestDate() {
207                 return latestDate;
208         }
209
210         public void setLatestDate(Date latestDate) {
211                 this.latestDate = latestDate;
212         }
213                 
214         public boolean isRange() {
215                 return (getLatestDate() != null);
216         }
217
218         public String getEarliestScalarDate() {
219                 return earliestScalarDate;
220         }
221
222         public void setEarliestScalarDate(String earliestScalarDate) {
223                 this.earliestScalarDate = earliestScalarDate;
224         }
225
226         public Boolean areScalarValuesComputed() {
227                 return scalarValuesComputed;
228         }
229
230         public String getLatestScalarDate() {
231                 return latestScalarDate;
232         }
233
234         public void setLatestScalarDate(String latestScalarDate) {
235                 this.latestScalarDate = latestScalarDate;
236         }
237
238         public void setScalarValuesComputed(Boolean scalarValuesComputed) {
239                 this.scalarValuesComputed = scalarValuesComputed;
240         }
241 }