1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kim.api.identity.principal;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.joda.time.DateTime;
20 import org.kuali.rice.core.api.CoreConstants;
21 import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
22 import org.kuali.rice.core.api.mo.ModelBuilder;
23 import org.kuali.rice.core.api.mo.common.active.InactivatableFromToUtils;
24 import org.kuali.rice.core.api.util.jaxb.DateTimeAdapter;
25 import org.kuali.rice.kim.api.KimConstants;
26 import org.w3c.dom.Element;
27
28 import javax.xml.bind.annotation.XmlAccessType;
29 import javax.xml.bind.annotation.XmlAccessorType;
30 import javax.xml.bind.annotation.XmlAnyElement;
31 import javax.xml.bind.annotation.XmlElement;
32 import javax.xml.bind.annotation.XmlRootElement;
33 import javax.xml.bind.annotation.XmlType;
34 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
35 import java.io.Serializable;
36 import java.util.Collection;
37
38 @XmlRootElement(name = PrincipalHistory.Constants.ROOT_ELEMENT_NAME)
39 @XmlAccessorType(XmlAccessType.NONE)
40 @XmlType(name = PrincipalHistory.Constants.TYPE_NAME, propOrder = {
41 PrincipalHistory.Elements.PRINCIPAL_ID,
42 PrincipalHistory.Elements.PRINCIPAL_NAME,
43 PrincipalHistory.Elements.ENTITY_ID,
44 PrincipalHistory.Elements.ACTIVE,
45 CoreConstants.CommonElements.HISTORY_ID,
46 CoreConstants.CommonElements.ACTIVE_FROM_DATE,
47 CoreConstants.CommonElements.ACTIVE_TO_DATE,
48 CoreConstants.CommonElements.VERSION_NUMBER,
49 CoreConstants.CommonElements.OBJECT_ID,
50 CoreConstants.CommonElements.FUTURE_ELEMENTS
51 })
52 public final class PrincipalHistory extends AbstractDataTransferObject
53 implements PrincipalHistoryContract
54 {
55
56 @XmlElement(name = Elements.PRINCIPAL_ID, required = false)
57 private final String principalId;
58 @XmlElement(name = Elements.PRINCIPAL_NAME, required = false)
59 private final String principalName;
60 @XmlElement(name = Elements.ENTITY_ID, required = false)
61 private final String entityId;
62 @XmlElement(name = Elements.ACTIVE, required = false)
63 private final boolean active;
64 @XmlElement(name = CoreConstants.CommonElements.HISTORY_ID, required = false)
65 private final Long historyId;
66 @XmlElement(name = CoreConstants.CommonElements.ACTIVE_FROM_DATE, required = false)
67 @XmlJavaTypeAdapter(DateTimeAdapter.class)
68 private final DateTime activeFromDate;
69 @XmlElement(name = CoreConstants.CommonElements.ACTIVE_TO_DATE, required = false)
70 @XmlJavaTypeAdapter(DateTimeAdapter.class)
71 private final DateTime activeToDate;
72 @XmlElement(name = CoreConstants.CommonElements.VERSION_NUMBER, required = false)
73 private final Long versionNumber;
74 @XmlElement(name = CoreConstants.CommonElements.OBJECT_ID, required = false)
75 private final String objectId;
76 @SuppressWarnings("unused")
77 @XmlAnyElement
78 private final Collection<Element> _futureElements = null;
79
80
81
82
83
84 private PrincipalHistory() {
85 this.principalId = null;
86 this.principalName = null;
87 this.entityId = null;
88 this.active = false;
89 this.versionNumber = null;
90 this.objectId = null;
91 this.historyId = null;
92 this.activeFromDate = null;
93 this.activeToDate = null;
94 }
95
96 private PrincipalHistory(Builder builder) {
97 this.principalId = builder.getPrincipalId();
98 this.principalName = builder.getPrincipalName();
99 this.entityId = builder.getEntityId();
100 this.active = builder.isActive();
101 this.versionNumber = builder.getVersionNumber();
102 this.objectId = builder.getObjectId();
103 this.historyId = builder.getHistoryId();
104 this.activeFromDate = builder.getActiveFromDate();
105 this.activeToDate = builder.getActiveToDate();
106 }
107
108 @Override
109 public String getPrincipalId() {
110 return this.principalId;
111 }
112
113 @Override
114 public String getPrincipalName() {
115 return this.principalName;
116 }
117
118 @Override
119 public String getEntityId() {
120 return this.entityId;
121 }
122
123 @Override
124 public boolean isActive() {
125 return this.active;
126 }
127
128 @Override
129 public Long getVersionNumber() {
130 return this.versionNumber;
131 }
132
133 @Override
134 public String getObjectId() {
135 return this.objectId;
136 }
137
138 @Override
139 public Long getHistoryId() {
140 return this.historyId;
141 }
142
143 @Override
144 public DateTime getActiveFromDate() {
145 return this.activeFromDate;
146 }
147
148 @Override
149 public DateTime getActiveToDate() {
150 return this.activeToDate;
151 }
152
153 @Override
154 public boolean isActiveNow() {
155 return isActive() && InactivatableFromToUtils.isActive(activeFromDate, activeToDate, null);
156 }
157
158 @Override
159 public boolean isActive(DateTime activeAsOf) {
160 return isActive() && InactivatableFromToUtils.isActive(activeFromDate, activeToDate, activeAsOf);
161 }
162
163
164
165
166
167 public final static class Builder
168 implements Serializable, ModelBuilder, PrincipalHistoryContract
169 {
170
171 private String principalId;
172 private String principalName;
173 private String entityId;
174 private boolean active;
175 private Long versionNumber;
176 private String objectId;
177 private Long historyId;
178 private DateTime activeFromDate;
179 private DateTime activeToDate;
180
181 private Builder(String principalName) {
182 setPrincipalName(principalName);
183 }
184
185 public static Builder create(String principalName) {
186 return new Builder(principalName);
187 }
188
189 public static Builder create(PrincipalHistoryContract contract) {
190 if (contract == null) {
191 throw new IllegalArgumentException("contract was null");
192 }
193 Builder builder = create(contract.getPrincipalName());
194 builder.setPrincipalId(contract.getPrincipalId());
195 builder.setEntityId(contract.getEntityId());
196 builder.setActive(contract.isActive());
197 builder.setVersionNumber(contract.getVersionNumber());
198 builder.setObjectId(contract.getObjectId());
199 builder.setHistoryId(contract.getHistoryId());
200 builder.setActiveFromDate(contract.getActiveFromDate());
201 builder.setActiveToDate(contract.getActiveToDate());
202 return builder;
203 }
204
205 public PrincipalHistory build() {
206 return new PrincipalHistory(this);
207 }
208
209
210 @Override
211 public String getPrincipalId() {
212 return this.principalId;
213 }
214
215 @Override
216 public String getPrincipalName() {
217 return this.principalName;
218 }
219
220 @Override
221 public String getEntityId() {
222 return this.entityId;
223 }
224
225 @Override
226 public boolean isActive() {
227 return this.active;
228 }
229
230 @Override
231 public Long getVersionNumber() {
232 return this.versionNumber;
233 }
234
235 @Override
236 public String getObjectId() {
237 return this.objectId;
238 }
239
240 @Override
241 public Long getHistoryId() {
242 return this.historyId;
243 }
244
245 @Override
246 public boolean isActiveNow() {
247 return isActive() && InactivatableFromToUtils.isActive(activeFromDate, activeToDate, null);
248 }
249
250 @Override
251 public boolean isActive(DateTime activeAsOf) {
252 return isActive() && InactivatableFromToUtils.isActive(activeFromDate, activeToDate, activeAsOf);
253 }
254
255 @Override
256 public DateTime getActiveFromDate() {
257 return this.activeFromDate;
258 }
259
260 @Override
261 public DateTime getActiveToDate() {
262 return this.activeToDate;
263 }
264
265 public void setHistoryId(Long historyId) {
266 this.historyId = historyId;
267 }
268
269 public void setActiveFromDate(DateTime activeFromDate) {
270 this.activeFromDate = activeFromDate;
271 }
272
273 public void setActiveToDate(DateTime activeToDate) {
274 this.activeToDate = activeToDate;
275 }
276
277 public void setPrincipalId(String principalId) {
278 if (StringUtils.isWhitespace(principalId)) {
279 throw new IllegalArgumentException("principalId is blank");
280 }
281 this.principalId = principalId;
282 }
283
284 public void setPrincipalName(String principalName) {
285 if (StringUtils.isEmpty(principalName)) {
286 throw new IllegalArgumentException("principalName is blank");
287 }
288 this.principalName = principalName;
289 }
290
291 public void setEntityId(String entityId) {
292 this.entityId = entityId;
293 }
294
295 public void setActive(boolean active) {
296 this.active = active;
297 }
298
299 public void setVersionNumber(Long versionNumber) {
300 this.versionNumber = versionNumber;
301 }
302
303 public void setObjectId(String objectId) {
304 this.objectId = objectId;
305 }
306
307 }
308
309
310
311
312
313
314 static class Constants {
315
316 final static String ROOT_ELEMENT_NAME = "principal";
317 final static String TYPE_NAME = "PrincipalType";
318 }
319
320
321
322
323
324
325 static class Elements {
326
327 final static String PRINCIPAL_ID = "principalId";
328 final static String PRINCIPAL_NAME = "principalName";
329 final static String ENTITY_ID = "entityId";
330 final static String ACTIVE = "active";
331
332 }
333
334 public static class Cache {
335 public static final String NAME = KimConstants.Namespaces.KIM_NAMESPACE_2_0 + "/" + PrincipalHistory.Constants.TYPE_NAME;
336 }
337 }