4460 |
21 Apr 17 |
nicklas |
1 |
package net.sf.basedb.reggie.dao; |
4460 |
21 Apr 17 |
nicklas |
2 |
|
4460 |
21 Apr 17 |
nicklas |
3 |
import java.util.Date; |
4460 |
21 Apr 17 |
nicklas |
4 |
|
4460 |
21 Apr 17 |
nicklas |
5 |
import net.sf.basedb.core.Annotatable; |
4460 |
21 Apr 17 |
nicklas |
6 |
import net.sf.basedb.core.DbControl; |
4460 |
21 Apr 17 |
nicklas |
7 |
import net.sf.basedb.core.Registered; |
4460 |
21 Apr 17 |
nicklas |
8 |
|
4460 |
21 Apr 17 |
nicklas |
9 |
/** |
4460 |
21 Apr 17 |
nicklas |
Class for holding a reference date and the source it is derived from. |
4460 |
21 Apr 17 |
nicklas |
11 |
|
4460 |
21 Apr 17 |
nicklas |
@author nicklas |
4460 |
21 Apr 17 |
nicklas |
@since 4.10 |
4460 |
21 Apr 17 |
nicklas |
14 |
*/ |
4460 |
21 Apr 17 |
nicklas |
15 |
public class ReferenceDateWithSource |
4460 |
21 Apr 17 |
nicklas |
16 |
{ |
4460 |
21 Apr 17 |
nicklas |
17 |
|
4460 |
21 Apr 17 |
nicklas |
18 |
/** |
4460 |
21 Apr 17 |
nicklas |
Create a reference date and source from existing annotations on an item. |
4460 |
21 Apr 17 |
nicklas |
It will try to create a reference date in the following order: |
4460 |
21 Apr 17 |
nicklas |
21 |
|
4460 |
21 Apr 17 |
nicklas |
1. Load existing {@link Annotationtype#REFERENCE_DATE} and {@link Annotationtype#REFERENCE_DATE_SOURCE}. |
4460 |
21 Apr 17 |
nicklas |
Both values must exist. |
4701 |
16 Mar 18 |
nicklas |
2. Load existing {@link Annotationtype#INCA2_a_diag_dat} and create a {@link ReferenceDateSource#INCA_DIAGNOSIS_DATE} |
4460 |
21 Apr 17 |
nicklas |
reference |
4460 |
21 Apr 17 |
nicklas |
3. Load existing {@link Annotationtype#CONSENT_DATE} and create a {@link ReferenceDateSource#CONSENT_DATE} |
4460 |
21 Apr 17 |
nicklas |
reference |
4460 |
21 Apr 17 |
nicklas |
4. If the item is a {@link Registered} item use the {@link Registered#getEntryDate()} |
4460 |
21 Apr 17 |
nicklas |
to create a {@link ReferenceDateSource#REGISTRATION_DATE} reference |
4460 |
21 Apr 17 |
nicklas |
30 |
*/ |
4460 |
21 Apr 17 |
nicklas |
31 |
public static ReferenceDateWithSource getFromAnnotations(DbControl dc, Annotatable item) |
4460 |
21 Apr 17 |
nicklas |
32 |
{ |
4460 |
21 Apr 17 |
nicklas |
33 |
Date refDate = (Date)Annotationtype.REFERENCE_DATE.getAnnotationValue(dc, item); |
4460 |
21 Apr 17 |
nicklas |
34 |
String refSource = (String)Annotationtype.REFERENCE_DATE_SOURCE.getAnnotationValue(dc, item); |
4701 |
16 Mar 18 |
nicklas |
35 |
Date incaDiaDat = (Date)Annotationtype.INCA2_a_diag_dat.getAnnotationValue(dc, item); |
4460 |
21 Apr 17 |
nicklas |
36 |
Date consentDate = (Date)Annotationtype.CONSENT_DATE.getAnnotationValue(dc, item); |
4460 |
21 Apr 17 |
nicklas |
37 |
|
4460 |
21 Apr 17 |
nicklas |
38 |
ReferenceDateWithSource refDateAndSource = null; |
4460 |
21 Apr 17 |
nicklas |
39 |
if (refDate != null && refSource != null) |
4460 |
21 Apr 17 |
nicklas |
40 |
{ |
4460 |
21 Apr 17 |
nicklas |
41 |
refDateAndSource = new ReferenceDateWithSource(refDate, ReferenceDateSource.fromTitle(refSource)); |
4460 |
21 Apr 17 |
nicklas |
42 |
} |
4701 |
16 Mar 18 |
nicklas |
43 |
else if (incaDiaDat != null) |
4460 |
21 Apr 17 |
nicklas |
44 |
{ |
4701 |
16 Mar 18 |
nicklas |
45 |
refDateAndSource = new ReferenceDateWithSource(incaDiaDat, ReferenceDateSource.INCA_DIAGNOSIS_DATE); |
4460 |
21 Apr 17 |
nicklas |
46 |
} |
4460 |
21 Apr 17 |
nicklas |
47 |
else if (consentDate != null) |
4460 |
21 Apr 17 |
nicklas |
48 |
{ |
4460 |
21 Apr 17 |
nicklas |
49 |
refDateAndSource = new ReferenceDateWithSource(consentDate, ReferenceDateSource.CONSENT_DATE); |
4460 |
21 Apr 17 |
nicklas |
50 |
} |
4460 |
21 Apr 17 |
nicklas |
51 |
else if (item instanceof Registered) |
4460 |
21 Apr 17 |
nicklas |
52 |
{ |
4460 |
21 Apr 17 |
nicklas |
53 |
refDate = ((Registered)item).getEntryDate(); |
4460 |
21 Apr 17 |
nicklas |
54 |
refDateAndSource = new ReferenceDateWithSource(refDate, ReferenceDateSource.REGISTRATION_DATE); |
4460 |
21 Apr 17 |
nicklas |
55 |
} |
4460 |
21 Apr 17 |
nicklas |
56 |
|
4460 |
21 Apr 17 |
nicklas |
57 |
return refDateAndSource; |
4460 |
21 Apr 17 |
nicklas |
58 |
} |
4460 |
21 Apr 17 |
nicklas |
59 |
|
4460 |
21 Apr 17 |
nicklas |
60 |
private final Date date; |
4460 |
21 Apr 17 |
nicklas |
61 |
private final ReferenceDateSource source; |
4460 |
21 Apr 17 |
nicklas |
62 |
|
4460 |
21 Apr 17 |
nicklas |
63 |
public ReferenceDateWithSource(Date date, ReferenceDateSource source) |
4460 |
21 Apr 17 |
nicklas |
64 |
{ |
4460 |
21 Apr 17 |
nicklas |
65 |
this.date = date; |
4460 |
21 Apr 17 |
nicklas |
66 |
this.source = source; |
4460 |
21 Apr 17 |
nicklas |
67 |
} |
4460 |
21 Apr 17 |
nicklas |
68 |
|
4460 |
21 Apr 17 |
nicklas |
69 |
/** |
4460 |
21 Apr 17 |
nicklas |
Get the reference date. |
4460 |
21 Apr 17 |
nicklas |
71 |
*/ |
4460 |
21 Apr 17 |
nicklas |
72 |
public Date getDate() |
4460 |
21 Apr 17 |
nicklas |
73 |
{ |
4460 |
21 Apr 17 |
nicklas |
74 |
return date; |
4460 |
21 Apr 17 |
nicklas |
75 |
} |
4460 |
21 Apr 17 |
nicklas |
76 |
|
4460 |
21 Apr 17 |
nicklas |
77 |
/** |
4460 |
21 Apr 17 |
nicklas |
Get the source of the reference date. |
4460 |
21 Apr 17 |
nicklas |
79 |
*/ |
4460 |
21 Apr 17 |
nicklas |
80 |
public ReferenceDateSource getSource() |
4460 |
21 Apr 17 |
nicklas |
81 |
{ |
4460 |
21 Apr 17 |
nicklas |
82 |
return source; |
4460 |
21 Apr 17 |
nicklas |
83 |
} |
4460 |
21 Apr 17 |
nicklas |
84 |
|
4460 |
21 Apr 17 |
nicklas |
85 |
/** |
4460 |
21 Apr 17 |
nicklas |
Checks if THIS reference date should be replaced with the REPLACEMENT reference |
4460 |
21 Apr 17 |
nicklas |
date. If the reference dates have different sources, the one with the lowest |
4460 |
21 Apr 17 |
nicklas |
ordinal in the {@link ReferenceDateSource} enum is returned. If they have |
4460 |
21 Apr 17 |
nicklas |
the same sources, the main rule is that an earlier date replaces a later date, |
4460 |
21 Apr 17 |
nicklas |
except that INCA diagnosis date is always replaced. |
4460 |
21 Apr 17 |
nicklas |
91 |
|
4460 |
21 Apr 17 |
nicklas |
@param replacement A reference date that may or may not replace the current reference date |
4460 |
21 Apr 17 |
nicklas |
@return This instance or the replacement depending on the priority rules |
4460 |
21 Apr 17 |
nicklas |
94 |
*/ |
4460 |
21 Apr 17 |
nicklas |
95 |
public ReferenceDateWithSource getThisOrTheReplacement(ReferenceDateWithSource replacement) |
4460 |
21 Apr 17 |
nicklas |
96 |
{ |
4460 |
21 Apr 17 |
nicklas |
97 |
if (replacement == null) return this; |
4460 |
21 Apr 17 |
nicklas |
98 |
|
4460 |
21 Apr 17 |
nicklas |
// To begin with, we assume the current reference date is the winner to |
4460 |
21 Apr 17 |
nicklas |
100 |
ReferenceDateWithSource winner = this; |
4460 |
21 Apr 17 |
nicklas |
101 |
|
4460 |
21 Apr 17 |
nicklas |
102 |
if (replacement.source == ReferenceDateSource.INCA_DIAGNOSIS_DATE) |
4460 |
21 Apr 17 |
nicklas |
103 |
{ |
4460 |
21 Apr 17 |
nicklas |
// If the replacement is an INCA diagnosis date it always wins |
4460 |
21 Apr 17 |
nicklas |
105 |
winner = replacement; |
4460 |
21 Apr 17 |
nicklas |
106 |
} |
4460 |
21 Apr 17 |
nicklas |
107 |
else if (replacement.source.ordinal() < source.ordinal()) |
4460 |
21 Apr 17 |
nicklas |
108 |
{ |
4460 |
21 Apr 17 |
nicklas |
// If the replacement has higher priority it wins |
4460 |
21 Apr 17 |
nicklas |
110 |
winner = replacement; |
4460 |
21 Apr 17 |
nicklas |
111 |
} |
4460 |
21 Apr 17 |
nicklas |
112 |
else if (replacement.source == source && replacement.date.before(date)) |
4460 |
21 Apr 17 |
nicklas |
113 |
{ |
4460 |
21 Apr 17 |
nicklas |
// Same source, but earlier date, the replacement wins |
4460 |
21 Apr 17 |
nicklas |
115 |
winner = replacement; |
4460 |
21 Apr 17 |
nicklas |
116 |
} |
4460 |
21 Apr 17 |
nicklas |
117 |
|
4460 |
21 Apr 17 |
nicklas |
118 |
return winner; |
4460 |
21 Apr 17 |
nicklas |
119 |
} |
4460 |
21 Apr 17 |
nicklas |
120 |
|
4460 |
21 Apr 17 |
nicklas |
121 |
/** |
4460 |
21 Apr 17 |
nicklas |
Update an existing item with the reference date and source. |
4460 |
21 Apr 17 |
nicklas |
123 |
*/ |
4460 |
21 Apr 17 |
nicklas |
124 |
public void updateAnnotations(DbControl dc, Annotatable item) |
4460 |
21 Apr 17 |
nicklas |
125 |
{ |
4460 |
21 Apr 17 |
nicklas |
126 |
Annotationtype.REFERENCE_DATE.setAnnotationValue(dc, item, date); |
4460 |
21 Apr 17 |
nicklas |
127 |
Annotationtype.REFERENCE_DATE_SOURCE.setAnnotationValue(dc, item, source.getTitle()); |
4460 |
21 Apr 17 |
nicklas |
128 |
} |
4460 |
21 Apr 17 |
nicklas |
129 |
|
4477 |
03 May 17 |
nicklas |
130 |
@Override |
4477 |
03 May 17 |
nicklas |
131 |
public boolean equals(Object other) |
4477 |
03 May 17 |
nicklas |
132 |
{ |
4477 |
03 May 17 |
nicklas |
133 |
if (other == null || other.getClass() != this.getClass()) return false; |
4477 |
03 May 17 |
nicklas |
134 |
ReferenceDateWithSource o = (ReferenceDateWithSource)other; |
4477 |
03 May 17 |
nicklas |
135 |
return source == o.source && date.equals(o.date); |
4477 |
03 May 17 |
nicklas |
136 |
} |
4477 |
03 May 17 |
nicklas |
137 |
|
4477 |
03 May 17 |
nicklas |
138 |
@Override |
4477 |
03 May 17 |
nicklas |
139 |
public int hashCode() |
4477 |
03 May 17 |
nicklas |
140 |
{ |
4477 |
03 May 17 |
nicklas |
141 |
return 13 * source.hashCode() + date.hashCode(); |
4477 |
03 May 17 |
nicklas |
142 |
} |
4477 |
03 May 17 |
nicklas |
143 |
|
4460 |
21 Apr 17 |
nicklas |
144 |
|
4460 |
21 Apr 17 |
nicklas |
145 |
} |