001/**
002 * Copyright 2005-2014 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 */
016package org.kuali.rice.kim.api.identity.email;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.core.api.CoreConstants;
020import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
021import org.kuali.rice.core.api.mo.ModelBuilder;
022import org.kuali.rice.kim.api.KimApiConstants;
023import org.kuali.rice.kim.api.identity.CodedAttribute;
024
025import javax.xml.bind.Element;
026import javax.xml.bind.annotation.XmlAccessType;
027import javax.xml.bind.annotation.XmlAccessorType;
028import javax.xml.bind.annotation.XmlAnyElement;
029import javax.xml.bind.annotation.XmlElement;
030import javax.xml.bind.annotation.XmlRootElement;
031import javax.xml.bind.annotation.XmlType;
032import java.io.Serializable;
033import java.util.Collection;
034
035@XmlRootElement(name = EntityEmail.Constants.ROOT_ELEMENT_NAME)
036@XmlAccessorType(XmlAccessType.NONE)
037@XmlType(name = EntityEmail.Constants.TYPE_NAME, propOrder = {
038    EntityEmail.Elements.ID,
039    EntityEmail.Elements.ENTITY_TYPE_CODE,
040    EntityEmail.Elements.ENTITY_ID,
041    EntityEmail.Elements.EMAIL_TYPE,
042    EntityEmail.Elements.EMAIL_ADDRESS,
043    EntityEmail.Elements.EMAIL_ADDRESS_UNMASKED,
044    EntityEmail.Elements.SUPPRESS_EMAIL,
045    EntityEmail.Elements.DEFAULT_VALUE,
046    EntityEmail.Elements.ACTIVE,
047    CoreConstants.CommonElements.VERSION_NUMBER,
048    CoreConstants.CommonElements.OBJECT_ID,
049    CoreConstants.CommonElements.FUTURE_ELEMENTS
050})
051public class EntityEmail extends AbstractDataTransferObject implements EntityEmailContract{
052    
053    @XmlElement(name = Elements.ID, required = false)
054    private final String id;
055    @XmlElement(name = Elements.ENTITY_TYPE_CODE, required = false)
056    private final String entityTypeCode;
057    @XmlElement(name = Elements.ENTITY_ID, required = false)
058    private final String entityId;
059    @XmlElement(name = Elements.EMAIL_TYPE, required = false)
060    private final CodedAttribute emailType;
061    @XmlElement(name = Elements.EMAIL_ADDRESS, required = false)
062    private final String emailAddress;
063    @XmlElement(name = Elements.EMAIL_ADDRESS_UNMASKED, required = false)
064    private final String emailAddressUnmasked;
065    @XmlElement(name = Elements.SUPPRESS_EMAIL, required = false)
066    private final boolean suppressEmail;
067    @XmlElement(name = Elements.DEFAULT_VALUE, required = false)
068    private final boolean defaultValue;
069    @XmlElement(name = CoreConstants.CommonElements.VERSION_NUMBER, required = false)
070    private final Long versionNumber;
071    @XmlElement(name = CoreConstants.CommonElements.OBJECT_ID, required = false)
072    private final String objectId;
073    @XmlElement(name = Elements.ACTIVE, required = false)
074    private final boolean active;
075    @SuppressWarnings("unused")
076    @XmlAnyElement
077    private final Collection<Element> _futureElements = null;
078
079    /**
080     * Private constructor used only by JAXB.
081     * 
082     */
083    private EntityEmail() {
084        this.id = null;
085        this.entityId = null;
086        this.entityTypeCode = null;
087        this.emailType = null;
088        this.emailAddress = null;
089        this.emailAddressUnmasked = null;
090        this.suppressEmail = false;
091        this.defaultValue = false;
092        this.versionNumber = null;
093        this.objectId = null;
094        this.active = false;
095    }
096
097    private EntityEmail(Builder builder) {
098        this.id = builder.getId();
099        this.entityId = builder.getEntityId();
100        this.entityTypeCode = builder.getEntityTypeCode();
101        this.emailType = (builder.getEmailType() != null) ? builder.getEmailType().build() : null;
102        this.emailAddress = builder.getEmailAddress();
103        this.emailAddressUnmasked = builder.getEmailAddressUnmasked();
104        this.suppressEmail = builder.isSuppressEmail();
105        this.defaultValue = builder.isDefaultValue();
106        this.versionNumber = builder.getVersionNumber();
107        this.objectId = builder.getObjectId();
108        this.active = builder.isActive();
109    }
110
111    @Override
112    public String getId() {
113        return this.id;
114    }
115
116    @Override
117    public String getEntityId() {
118        return this.entityId;
119    }
120
121    @Override
122    public String getEntityTypeCode() {
123        return this.entityTypeCode;
124    }
125
126    @Override
127    public CodedAttribute getEmailType() {
128        return this.emailType;
129    }
130
131    @Override
132    public String getEmailAddress() {
133        return this.emailAddress;
134    }
135
136    @Override
137    public String getEmailAddressUnmasked() {
138        return this.emailAddressUnmasked;
139    }
140
141    @Override
142    public boolean isSuppressEmail() {
143        return this.suppressEmail;
144    }
145
146    @Override
147    public boolean isDefaultValue() {
148        return this.defaultValue;
149    }
150
151    @Override
152    public Long getVersionNumber() {
153        return this.versionNumber;
154    }
155
156    @Override
157    public String getObjectId() {
158        return this.objectId;
159    }
160
161    @Override
162    public boolean isActive() {
163        return this.active;
164    }
165
166    /**
167     * A builder which can be used to construct {@link EntityEmail} instances.  Enforces the constraints of the {@link org.kuali.rice.kim.api.identity.email.EntityEmailContract}.
168     * 
169     */
170    public final static class Builder
171        implements Serializable, ModelBuilder, EntityEmailContract
172    {
173
174        private String id;
175        private String entityId;
176        private String entityTypeCode;
177        private CodedAttribute.Builder emailType;
178        private String emailAddressUnmasked;
179        private boolean suppressEmail;
180        private boolean defaultValue;
181        private Long versionNumber;
182        private String objectId;
183        private boolean active;
184
185        private Builder() {
186
187        }
188
189        public static Builder create() {
190            return new Builder();
191        }
192
193        public static Builder create(EntityEmailContract contract) {
194            if (contract == null) {
195                throw new IllegalArgumentException("contract was null");
196            }
197            Builder builder = create();
198            builder.setId(contract.getId());
199            builder.setEntityId(contract.getEntityId());
200            builder.setEntityTypeCode(contract.getEntityTypeCode());
201            builder.setSuppressEmail(contract.isSuppressEmail());
202            if (contract.getEmailType() != null) {
203                builder.setEmailType(CodedAttribute.Builder.create(contract.getEmailType()));
204            }
205            builder.setEmailAddress(contract.getEmailAddressUnmasked());
206            builder.setDefaultValue(contract.isDefaultValue());
207            builder.setVersionNumber(contract.getVersionNumber());
208            builder.setObjectId(contract.getObjectId());
209            builder.setActive(contract.isActive());
210            return builder;
211        }
212
213        public EntityEmail build() {
214            return new EntityEmail(this);
215        }
216
217        @Override
218        public String getId() {
219            return this.id;
220        }
221
222        @Override
223        public String getEntityTypeCode() {
224            return this.entityTypeCode;
225        }
226
227        @Override
228        public String getEntityId() {
229            return this.entityId;
230        }
231
232        @Override
233        public CodedAttribute.Builder getEmailType() {
234            return this.emailType;
235        }
236
237        @Override
238        public String getEmailAddress() {
239            if (isSuppressEmail()) {
240                return KimApiConstants.RestrictedMasks.RESTRICTED_DATA_MASK;
241            }
242            return this.emailAddressUnmasked;
243        }
244
245        @Override
246        public String getEmailAddressUnmasked() {
247            return this.emailAddressUnmasked;
248        }
249
250        @Override
251        public boolean isSuppressEmail() {
252            return this.suppressEmail;
253        }
254
255        @Override
256        public boolean isDefaultValue() {
257            return this.defaultValue;
258        }
259
260        @Override
261        public Long getVersionNumber() {
262            return this.versionNumber;
263        }
264
265        @Override
266        public String getObjectId() {
267            return this.objectId;
268        }
269
270        @Override
271        public boolean isActive() {
272            return this.active;
273        }
274
275        public void setId(String id) {
276            if (StringUtils.isWhitespace(id)) {
277                throw new IllegalArgumentException("id is blank");
278            }
279            this.id = id;
280        }
281
282        public void setEntityTypeCode(String entityTypeCode) {
283            this.entityTypeCode = entityTypeCode;
284        }
285
286        public void setEntityId(String entityId) {
287            this.entityId = entityId;
288        }
289
290        public void setEmailType(CodedAttribute.Builder emailType) {
291            this.emailType = emailType;
292        }
293
294        public void setEmailAddress(String emailAddress) {
295            this.emailAddressUnmasked = emailAddress;
296        }
297
298        private void setSuppressEmail(boolean suppressEmail) {
299            this.suppressEmail = suppressEmail;
300        }
301
302        public void setDefaultValue(boolean defaultValue) {
303            this.defaultValue = defaultValue;
304        }
305
306        public void setVersionNumber(Long versionNumber) {
307            this.versionNumber = versionNumber;
308        }
309
310        public void setObjectId(String objectId) {
311            this.objectId = objectId;
312        }
313
314        public void setActive(boolean active) {
315            this.active = active;
316        }
317
318    }
319
320
321    /**
322     * Defines some internal constants used on this class.
323     * 
324     */
325    static class Constants {
326
327        final static String ROOT_ELEMENT_NAME = "entityEmail";
328        final static String TYPE_NAME = "EntityEmailType";
329    }
330
331
332    /**
333     * A private class which exposes constants which define the XML element names to use when this object is marshalled to XML.
334     * 
335     */
336    static class Elements {
337
338        final static String ID = "id";
339        final static String ENTITY_TYPE_CODE = "entityTypeCode";
340        final static String ENTITY_ID = "entityId";
341        final static String EMAIL_TYPE = "emailType";
342        final static String EMAIL_ADDRESS = "emailAddress";
343        final static String EMAIL_ADDRESS_UNMASKED = "emailAddressUnmasked";
344        final static String SUPPRESS_EMAIL = "suppressEmail";
345        final static String DEFAULT_VALUE = "defaultValue";
346        final static String ACTIVE = "active";
347
348    }
349
350}