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 StructuredDateInternal {
11 public static final boolean DEFAULT_SCALAR_VALUES_COMPUTED = false;
13 private String displayDate;
15 private String association;
16 private String period;
18 private Date earliestSingleDate;
19 private Date latestDate;
21 private String earliestScalarValue;
22 private String latestScalarValue;
23 private Boolean scalarValuesComputed;
25 public StructuredDateInternal() {
26 scalarValuesComputed = DEFAULT_SCALAR_VALUES_COMPUTED;
29 public String toString() {
32 "\tdisplayDate: " + getDisplayDate() + "\n" +
33 "\tnote: " + getNote() + "\n" +
34 "\tassociation: " + getAssociation() + "\n" +
35 "\tperiod: " + getPeriod() + "\n";
37 if (getEarliestSingleDate() != null) {
40 "\tearliestSingleDate: \n" +
41 getEarliestSingleDate().toString() + "\n";
44 if (getLatestDate() != null) {
48 getLatestDate().toString() + "\n";
55 public boolean equals(Object obj) {
64 if (obj.getClass() != getClass()) {
68 StructuredDateInternal that = (StructuredDateInternal) obj;
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())
82 public void computeScalarValues() {
83 Date earliestDate = getEarliestSingleDate();
84 Date latestDate = getLatestDate();
86 if (earliestDate == null && latestDate == null) {
87 setEarliestScalarValue(null);
88 setLatestScalarValue(null);
93 if (earliestDate == null) {
94 earliestDate = latestDate.copy();
97 earliestDate = earliestDate.copy();
100 if (latestDate == null) {
101 latestDate = earliestDate.copy();
104 latestDate = latestDate.copy();
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");
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");
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");
122 if (earliestDate.getEra() == null) {
123 earliestDate.setEra(Date.DEFAULT_ERA);
126 if (latestDate.getEra() == null) {
127 latestDate.setEra(Date.DEFAULT_ERA);
130 if (earliestDate.getMonth() == null) {
131 earliestDate.setMonth(1);
132 earliestDate.setDay(1);
135 if (latestDate.getMonth() == null) {
136 latestDate.setMonth(12);
137 latestDate.setDay(31);
140 if (earliestDate.getDay() == null) {
141 earliestDate.setDay(1);
144 if (latestDate.getDay() == null) {
145 latestDate.setDay(DateUtils.getDaysInMonth(latestDate.getMonth(), latestDate.getYear(), latestDate.getEra()));
148 // Add one day to the latest day, since that's what the UI does.
149 DateUtils.addDays(latestDate, 1);
151 setEarliestScalarValue(DateUtils.getEarliestScalarValue(earliestDate));
152 setLatestScalarValue(DateUtils.getLatestScalarValue(latestDate));
153 setScalarValuesComputed(true);
156 public static StructuredDateInternal parse(String displayDate) throws StructuredDateFormatException {
157 StructuredDateEvaluator evaluator = new ANTLRStructuredDateEvaluator();
159 return evaluator.evaluate(displayDate);
162 public String getDisplayDate() {
166 public void setDisplayDate(String displayDate) {
167 this.displayDate = displayDate;
170 public String getNote() {
174 public void setNote(String note) {
178 public String getAssociation() {
182 public void setAssociation(String association) {
183 this.association = association;
186 public String getPeriod() {
190 public void setPeriod(String period) {
191 this.period = period;
194 public Date getEarliestSingleDate() {
195 return earliestSingleDate;
198 public void setEarliestSingleDate(Date earliestSingleDate) {
199 this.earliestSingleDate = earliestSingleDate;
202 public Date getLatestDate() {
206 public void setLatestDate(Date latestDate) {
207 this.latestDate = latestDate;
210 public boolean isRange() {
211 return (getLatestDate() != null);
214 public String getEarliestScalarValue() {
215 return earliestScalarValue;
218 public void setEarliestScalarValue(String earliestScalarValue) {
219 this.earliestScalarValue = earliestScalarValue;
222 public Boolean areScalarValuesComputed() {
223 return scalarValuesComputed;
226 public String getLatestScalarValue() {
227 return latestScalarValue;
230 public void setLatestScalarValue(String latestScalarValue) {
231 this.latestScalarValue = latestScalarValue;
234 public void setScalarValuesComputed(Boolean scalarValuesComputed) {
235 this.scalarValuesComputed = scalarValuesComputed;