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