001 /** 002 * Copyright 2005-2013 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.rice.kim.api.identity.email; 017 018 import org.apache.commons.lang.StringUtils; 019 import org.joda.time.DateTime; 020 import org.kuali.rice.core.api.CoreConstants; 021 import org.kuali.rice.core.api.mo.AbstractDataTransferObject; 022 import org.kuali.rice.core.api.mo.ModelBuilder; 023 import org.kuali.rice.core.api.mo.common.active.InactivatableFromToUtils; 024 import org.kuali.rice.core.api.util.jaxb.DateTimeAdapter; 025 import org.kuali.rice.kim.api.KimApiConstants; 026 import org.kuali.rice.kim.api.identity.CodedAttributeHistory; 027 028 import javax.xml.bind.Element; 029 import javax.xml.bind.annotation.XmlAccessType; 030 import javax.xml.bind.annotation.XmlAccessorType; 031 import javax.xml.bind.annotation.XmlAnyElement; 032 import javax.xml.bind.annotation.XmlElement; 033 import javax.xml.bind.annotation.XmlRootElement; 034 import javax.xml.bind.annotation.XmlType; 035 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; 036 import java.io.Serializable; 037 import java.util.Collection; 038 039 @XmlRootElement(name = EntityEmailHistory.Constants.ROOT_ELEMENT_NAME) 040 @XmlAccessorType(XmlAccessType.NONE) 041 @XmlType(name = EntityEmailHistory.Constants.TYPE_NAME, propOrder = { 042 EntityEmailHistory.Elements.ID, 043 EntityEmailHistory.Elements.ENTITY_TYPE_CODE, 044 EntityEmailHistory.Elements.ENTITY_ID, 045 EntityEmailHistory.Elements.EMAIL_TYPE, 046 EntityEmailHistory.Elements.EMAIL_ADDRESS, 047 EntityEmailHistory.Elements.EMAIL_ADDRESS_UNMASKED, 048 EntityEmailHistory.Elements.SUPPRESS_EMAIL, 049 EntityEmailHistory.Elements.DEFAULT_VALUE, 050 EntityEmailHistory.Elements.ACTIVE, 051 CoreConstants.CommonElements.HISTORY_ID, 052 CoreConstants.CommonElements.ACTIVE_FROM_DATE, 053 CoreConstants.CommonElements.ACTIVE_TO_DATE, 054 CoreConstants.CommonElements.VERSION_NUMBER, 055 CoreConstants.CommonElements.OBJECT_ID, 056 CoreConstants.CommonElements.FUTURE_ELEMENTS 057 }) 058 public class EntityEmailHistory extends AbstractDataTransferObject implements EntityEmailHistoryContract{ 059 060 @XmlElement(name = Elements.ID, required = false) 061 private final String id; 062 @XmlElement(name = Elements.ENTITY_TYPE_CODE, required = false) 063 private final String entityTypeCode; 064 @XmlElement(name = Elements.ENTITY_ID, required = false) 065 private final String entityId; 066 @XmlElement(name = Elements.EMAIL_TYPE, required = false) 067 private final CodedAttributeHistory emailType; 068 @XmlElement(name = Elements.EMAIL_ADDRESS, required = false) 069 private final String emailAddress; 070 @XmlElement(name = Elements.EMAIL_ADDRESS_UNMASKED, required = false) 071 private final String emailAddressUnmasked; 072 @XmlElement(name = Elements.SUPPRESS_EMAIL, required = false) 073 private final boolean suppressEmail; 074 @XmlElement(name = Elements.DEFAULT_VALUE, required = false) 075 private final boolean defaultValue; 076 @XmlElement(name = CoreConstants.CommonElements.HISTORY_ID, required = false) 077 private final Long historyId; 078 @XmlElement(name = CoreConstants.CommonElements.ACTIVE_FROM_DATE, required = false) 079 @XmlJavaTypeAdapter(DateTimeAdapter.class) 080 private final DateTime activeFromDate; 081 @XmlElement(name = CoreConstants.CommonElements.ACTIVE_TO_DATE, required = false) 082 @XmlJavaTypeAdapter(DateTimeAdapter.class) 083 private final DateTime activeToDate; 084 @XmlElement(name = CoreConstants.CommonElements.VERSION_NUMBER, required = false) 085 private final Long versionNumber; 086 @XmlElement(name = CoreConstants.CommonElements.OBJECT_ID, required = false) 087 private final String objectId; 088 @XmlElement(name = Elements.ACTIVE, required = false) 089 private final boolean active; 090 @SuppressWarnings("unused") 091 @XmlAnyElement 092 private final Collection<Element> _futureElements = null; 093 094 /** 095 * Private constructor used only by JAXB. 096 * 097 */ 098 private EntityEmailHistory() { 099 this.id = null; 100 this.entityId = null; 101 this.entityTypeCode = null; 102 this.emailType = null; 103 this.emailAddress = null; 104 this.emailAddressUnmasked = null; 105 this.suppressEmail = false; 106 this.defaultValue = false; 107 this.versionNumber = null; 108 this.objectId = null; 109 this.active = false; 110 this.historyId = null; 111 this.activeFromDate = null; 112 this.activeToDate = null; 113 } 114 115 private EntityEmailHistory(Builder builder) { 116 this.id = builder.getId(); 117 this.entityId = builder.getEntityId(); 118 this.entityTypeCode = builder.getEntityTypeCode(); 119 this.emailType = (builder.getEmailType() != null) ? builder.getEmailType().build() : null; 120 this.emailAddress = builder.getEmailAddress(); 121 this.emailAddressUnmasked = builder.getEmailAddressUnmasked(); 122 this.suppressEmail = builder.isSuppressEmail(); 123 this.defaultValue = builder.isDefaultValue(); 124 this.versionNumber = builder.getVersionNumber(); 125 this.objectId = builder.getObjectId(); 126 this.active = builder.isActive(); 127 this.historyId = builder.getHistoryId(); 128 this.activeFromDate = builder.getActiveFromDate(); 129 this.activeToDate = builder.getActiveToDate(); 130 } 131 132 @Override 133 public String getId() { 134 return this.id; 135 } 136 137 @Override 138 public String getEntityId() { 139 return this.entityId; 140 } 141 142 @Override 143 public String getEntityTypeCode() { 144 return this.entityTypeCode; 145 } 146 147 @Override 148 public CodedAttributeHistory getEmailType() { 149 return this.emailType; 150 } 151 152 @Override 153 public String getEmailAddress() { 154 return this.emailAddress; 155 } 156 157 @Override 158 public String getEmailAddressUnmasked() { 159 return this.emailAddressUnmasked; 160 } 161 162 @Override 163 public boolean isSuppressEmail() { 164 return this.suppressEmail; 165 } 166 167 @Override 168 public boolean isDefaultValue() { 169 return this.defaultValue; 170 } 171 172 @Override 173 public Long getVersionNumber() { 174 return this.versionNumber; 175 } 176 177 @Override 178 public String getObjectId() { 179 return this.objectId; 180 } 181 182 @Override 183 public boolean isActive() { 184 return this.active; 185 } 186 187 @Override 188 public Long getHistoryId() { 189 return this.historyId; 190 } 191 192 @Override 193 public DateTime getActiveFromDate() { 194 return this.activeFromDate; 195 } 196 197 @Override 198 public DateTime getActiveToDate() { 199 return this.activeToDate; 200 } 201 202 @Override 203 public boolean isActiveNow() { 204 return isActive() && InactivatableFromToUtils.isActive(activeFromDate, activeToDate, null); 205 } 206 207 @Override 208 public boolean isActive(DateTime activeAsOf) { 209 return isActive() && InactivatableFromToUtils.isActive(activeFromDate, activeToDate, activeAsOf); 210 } 211 212 /** 213 * A builder which can be used to construct {@link org.kuali.rice.kim.api.identity.email.EntityEmailHistory} instances. Enforces the constraints of the {@link EntityEmailContract}. 214 * 215 */ 216 public final static class Builder 217 implements Serializable, ModelBuilder, EntityEmailHistoryContract 218 { 219 220 private String id; 221 private String entityId; 222 private String entityTypeCode; 223 private CodedAttributeHistory.Builder emailType; 224 private String emailAddressUnmasked; 225 private boolean suppressEmail; 226 private boolean defaultValue; 227 private Long versionNumber; 228 private String objectId; 229 private boolean active; 230 private Long historyId; 231 private DateTime activeFromDate; 232 private DateTime activeToDate; 233 234 private Builder() { 235 236 } 237 238 public static Builder create() { 239 return new Builder(); 240 } 241 242 public static Builder create(EntityEmailHistoryContract contract) { 243 if (contract == null) { 244 throw new IllegalArgumentException("contract was null"); 245 } 246 Builder builder = create(); 247 builder.setId(contract.getId()); 248 builder.setEntityId(contract.getEntityId()); 249 builder.setEntityTypeCode(contract.getEntityTypeCode()); 250 builder.setSuppressEmail(contract.isSuppressEmail()); 251 if (contract.getEmailType() != null) { 252 builder.setEmailType(CodedAttributeHistory.Builder.create(contract.getEmailType())); 253 } 254 builder.setEmailAddress(contract.getEmailAddressUnmasked()); 255 builder.setDefaultValue(contract.isDefaultValue()); 256 builder.setVersionNumber(contract.getVersionNumber()); 257 builder.setObjectId(contract.getObjectId()); 258 builder.setActive(contract.isActive()); 259 builder.setHistoryId(contract.getHistoryId()); 260 builder.setActiveFromDate(contract.getActiveFromDate()); 261 builder.setActiveToDate(contract.getActiveToDate()); 262 return builder; 263 } 264 265 public EntityEmailHistory build() { 266 return new EntityEmailHistory(this); 267 } 268 269 @Override 270 public String getId() { 271 return this.id; 272 } 273 274 @Override 275 public String getEntityTypeCode() { 276 return this.entityTypeCode; 277 } 278 279 @Override 280 public String getEntityId() { 281 return this.entityId; 282 } 283 284 @Override 285 public CodedAttributeHistory.Builder getEmailType() { 286 return this.emailType; 287 } 288 289 @Override 290 public String getEmailAddress() { 291 if (isSuppressEmail()) { 292 return KimApiConstants.RestrictedMasks.RESTRICTED_DATA_MASK; 293 } 294 return this.emailAddressUnmasked; 295 } 296 297 @Override 298 public String getEmailAddressUnmasked() { 299 return this.emailAddressUnmasked; 300 } 301 302 @Override 303 public boolean isSuppressEmail() { 304 return this.suppressEmail; 305 } 306 307 @Override 308 public boolean isDefaultValue() { 309 return this.defaultValue; 310 } 311 312 @Override 313 public Long getVersionNumber() { 314 return this.versionNumber; 315 } 316 317 @Override 318 public String getObjectId() { 319 return this.objectId; 320 } 321 322 @Override 323 public boolean isActive() { 324 return this.active; 325 } 326 327 @Override 328 public Long getHistoryId() { 329 return this.historyId; 330 } 331 332 @Override 333 public boolean isActiveNow() { 334 return isActive() && InactivatableFromToUtils.isActive(activeFromDate, activeToDate, null); 335 } 336 337 @Override 338 public boolean isActive(DateTime activeAsOf) { 339 return isActive() && InactivatableFromToUtils.isActive(activeFromDate, activeToDate, activeAsOf); 340 } 341 342 @Override 343 public DateTime getActiveFromDate() { 344 return this.activeFromDate; 345 } 346 347 @Override 348 public DateTime getActiveToDate() { 349 return this.activeToDate; 350 } 351 352 public void setHistoryId(Long historyId) { 353 this.historyId = historyId; 354 } 355 356 public void setActiveFromDate(DateTime activeFromDate) { 357 this.activeFromDate = activeFromDate; 358 } 359 360 public void setActiveToDate(DateTime activeToDate) { 361 this.activeToDate = activeToDate; 362 } 363 364 public void setId(String id) { 365 if (StringUtils.isWhitespace(id)) { 366 throw new IllegalArgumentException("id is blank"); 367 } 368 this.id = id; 369 } 370 371 public void setEntityTypeCode(String entityTypeCode) { 372 this.entityTypeCode = entityTypeCode; 373 } 374 375 public void setEntityId(String entityId) { 376 this.entityId = entityId; 377 } 378 379 public void setEmailType(CodedAttributeHistory.Builder emailType) { 380 this.emailType = emailType; 381 } 382 383 public void setEmailAddress(String emailAddress) { 384 this.emailAddressUnmasked = emailAddress; 385 } 386 387 private void setSuppressEmail(boolean suppressEmail) { 388 this.suppressEmail = suppressEmail; 389 } 390 391 public void setDefaultValue(boolean defaultValue) { 392 this.defaultValue = defaultValue; 393 } 394 395 public void setVersionNumber(Long versionNumber) { 396 this.versionNumber = versionNumber; 397 } 398 399 public void setObjectId(String objectId) { 400 this.objectId = objectId; 401 } 402 403 public void setActive(boolean active) { 404 this.active = active; 405 } 406 407 } 408 409 410 /** 411 * Defines some internal constants used on this class. 412 * 413 */ 414 static class Constants { 415 416 final static String ROOT_ELEMENT_NAME = "entityEmail"; 417 final static String TYPE_NAME = "EntityEmailType"; 418 } 419 420 421 /** 422 * A private class which exposes constants which define the XML element names to use when this object is marshalled to XML. 423 * 424 */ 425 static class Elements { 426 427 final static String ID = "id"; 428 final static String ENTITY_TYPE_CODE = "entityTypeCode"; 429 final static String ENTITY_ID = "entityId"; 430 final static String EMAIL_TYPE = "emailType"; 431 final static String EMAIL_ADDRESS = "emailAddress"; 432 final static String EMAIL_ADDRESS_UNMASKED = "emailAddressUnmasked"; 433 final static String SUPPRESS_EMAIL = "suppressEmail"; 434 final static String DEFAULT_VALUE = "defaultValue"; 435 final static String ACTIVE = "active"; 436 437 } 438 439 }