1 package org.collectionspace.services.structureddate;
3 import org.apache.commons.lang.builder.EqualsBuilder;
4 import org.collectionspace.services.structureddate.antlr.ANTLRStructuredDateEvaluator;
8 * A CollectionSpace structured date.
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
16 public static final boolean DEFAULT_SCALAR_VALUES_COMPUTED = true;
18 private String displayDate;
20 private String association;
21 private String period;
23 private Date earliestSingleDate;
24 private Date latestDate;
26 private String earliestScalarDate;
27 private String latestScalarDate;
28 private Boolean scalarValuesComputed;
30 public StructuredDate() {
31 scalarValuesComputed = DEFAULT_SCALAR_VALUES_COMPUTED;
34 public String toString() {
37 "\tdisplayDate: " + getDisplayDate() + "\n" +
38 "\tnote: " + getNote() + "\n" +
39 "\tassociation: " + getAssociation() + "\n" +
40 "\tperiod: " + getPeriod() + "\n";
42 if (getEarliestSingleDate() != null) {
45 "\tearliestSingleDate: \n" +
46 getEarliestSingleDate().toString() + "\n";
49 if (getLatestDate() != null) {
53 getLatestDate().toString() + "\n";
60 public boolean equals(Object obj) {
69 if (obj.getClass() != getClass()) {
73 StructuredDate that = (StructuredDate) obj;
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())
87 public void computeScalarValues() {
88 Date earliestDate = getEarliestSingleDate();
89 Date latestDate = getLatestDate();
91 if (earliestDate == null && latestDate == null) {
92 setEarliestScalarDate(null);
93 setLatestScalarDate(null);
98 if (earliestDate == null) {
99 earliestDate = latestDate.copy();
102 earliestDate = earliestDate.copy();
105 if (latestDate == null) {
106 latestDate = earliestDate.copy();
109 latestDate = latestDate.copy();
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");
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");
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");
127 if (earliestDate.getEra() == null) {
128 earliestDate.setEra(Date.DEFAULT_ERA);
131 if (latestDate.getEra() == null) {
132 latestDate.setEra(Date.DEFAULT_ERA);
135 if (earliestDate.getMonth() == null) {
136 earliestDate.setMonth(1);
137 earliestDate.setDay(1);
140 if (latestDate.getMonth() == null) {
141 latestDate.setMonth(12);
142 latestDate.setDay(31);
145 if (earliestDate.getDay() == null) {
146 earliestDate.setDay(1);
149 if (latestDate.getDay() == null) {
150 latestDate.setDay(DateUtils.getDaysInMonth(latestDate.getMonth(), latestDate.getYear(), latestDate.getEra()));
153 // Add one day to the latest day, since that's what the UI does.
154 // DateUtils.addDays(latestDate, 1);
156 setEarliestScalarDate(DateUtils.getEarliestTimestamp(earliestDate));
157 setLatestScalarDate(DateUtils.getLatestTimestamp(latestDate));
160 public static StructuredDate parse(String displayDate) throws StructuredDateFormatException {
161 StructuredDateEvaluator evaluator = new ANTLRStructuredDateEvaluator();
163 return evaluator.evaluate(displayDate);
166 public String getDisplayDate() {
170 public void setDisplayDate(String displayDate) {
171 this.displayDate = displayDate;
174 public String getNote() {
178 public void setNote(String note) {
182 public String getAssociation() {
186 public void setAssociation(String association) {
187 this.association = association;
190 public String getPeriod() {
194 public void setPeriod(String period) {
195 this.period = period;
198 public Date getEarliestSingleDate() {
199 return earliestSingleDate;
202 public void setEarliestSingleDate(Date earliestSingleDate) {
203 this.earliestSingleDate = earliestSingleDate;
206 public Date getLatestDate() {
210 public void setLatestDate(Date latestDate) {
211 this.latestDate = latestDate;
214 public boolean isRange() {
215 return (getLatestDate() != null);
218 public String getEarliestScalarDate() {
219 return earliestScalarDate;
222 public void setEarliestScalarDate(String earliestScalarDate) {
223 this.earliestScalarDate = earliestScalarDate;
226 public Boolean areScalarValuesComputed() {
227 return scalarValuesComputed;
230 public String getLatestScalarDate() {
231 return latestScalarDate;
234 public void setLatestScalarDate(String latestScalarDate) {
235 this.latestScalarDate = latestScalarDate;
238 public void setScalarValuesComputed(Boolean scalarValuesComputed) {
239 this.scalarValuesComputed = scalarValuesComputed;