001/**
002 * Copyright 2005-2015 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.external;
017
018import java.io.Serializable;
019import java.util.Collection;
020import javax.xml.bind.annotation.XmlAccessType;
021import javax.xml.bind.annotation.XmlAccessorType;
022import javax.xml.bind.annotation.XmlAnyElement;
023import javax.xml.bind.annotation.XmlElement;
024import javax.xml.bind.annotation.XmlRootElement;
025import javax.xml.bind.annotation.XmlType;
026
027import org.apache.commons.lang.StringUtils;
028import org.kuali.rice.core.api.CoreConstants;
029import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
030import org.kuali.rice.core.api.mo.ModelBuilder;
031import org.w3c.dom.Element;
032
033@XmlRootElement(name = EntityExternalIdentifier.Constants.ROOT_ELEMENT_NAME)
034@XmlAccessorType(XmlAccessType.NONE)
035@XmlType(name = EntityExternalIdentifier.Constants.TYPE_NAME, propOrder = {
036    EntityExternalIdentifier.Elements.ID,
037    EntityExternalIdentifier.Elements.ENTITY_ID,
038    EntityExternalIdentifier.Elements.EXTERNAL_IDENTIFIER_TYPE_CODE,
039    EntityExternalIdentifier.Elements.EXTERNAL_IDENTIFIER_TYPE,
040    EntityExternalIdentifier.Elements.EXTERNAL_ID,
041    CoreConstants.CommonElements.VERSION_NUMBER,
042    CoreConstants.CommonElements.OBJECT_ID,
043    CoreConstants.CommonElements.FUTURE_ELEMENTS
044})
045public final class EntityExternalIdentifier extends AbstractDataTransferObject
046    implements EntityExternalIdentifierContract
047{
048    @XmlElement(name = Elements.ENTITY_ID, required = false)
049    private final String entityId;
050    @XmlElement(name = Elements.EXTERNAL_IDENTIFIER_TYPE_CODE, required = false)
051    private final String externalIdentifierTypeCode;
052    @XmlElement(name = Elements.EXTERNAL_IDENTIFIER_TYPE, required = false)
053    private final EntityExternalIdentifierType externalIdentifierType;
054    @XmlElement(name = Elements.EXTERNAL_ID, required = false)
055    private final String externalId;
056    @XmlElement(name = CoreConstants.CommonElements.VERSION_NUMBER, required = false)
057    private final Long versionNumber;
058    @XmlElement(name = CoreConstants.CommonElements.OBJECT_ID, required = false)
059    private final String objectId;
060    @XmlElement(name = Elements.ID, required = false)
061    private final String id;
062    @SuppressWarnings("unused")
063    @XmlAnyElement
064    private final Collection<Element> _futureElements = null;
065
066    /**
067     * Private constructor used only by JAXB.
068     * 
069     */
070    private EntityExternalIdentifier() {
071        this.entityId = null;
072        this.externalIdentifierTypeCode = null;
073        this.externalIdentifierType = null;
074        this.externalId = null;
075        this.versionNumber = null;
076        this.objectId = null;
077        this.id = null;
078    }
079
080    private EntityExternalIdentifier(Builder builder) {
081        this.entityId = builder.getEntityId();
082        this.externalIdentifierTypeCode = builder.getExternalIdentifierTypeCode();
083        this.externalIdentifierType = builder.getExternalIdentifierType() != null ? builder.getExternalIdentifierType().build() : null;
084        this.externalId = builder.getExternalId();
085        this.versionNumber = builder.getVersionNumber();
086        this.objectId = builder.getObjectId();
087        this.id = builder.getId();
088    }
089
090    @Override
091    public String getEntityId() {
092        return this.entityId;
093    }
094
095    @Override
096    public String getExternalIdentifierTypeCode() {
097        return this.externalIdentifierTypeCode;
098    }
099
100    @Override
101    public EntityExternalIdentifierType getExternalIdentifierType() {
102        return this.externalIdentifierType;
103    }
104
105    @Override
106    public String getExternalId() {
107        return this.externalId;
108    }
109
110    @Override
111    public Long getVersionNumber() {
112        return this.versionNumber;
113    }
114
115    @Override
116    public String getObjectId() {
117        return this.objectId;
118    }
119
120    @Override
121    public String getId() {
122        return this.id;
123    }
124
125    /**
126     * A builder which can be used to construct {@link EntityExternalIdentifier} instances.  Enforces the constraints of the {@link EntityExternalIdentifierContract}.
127     * 
128     */
129    public final static class Builder
130        implements Serializable, ModelBuilder, EntityExternalIdentifierContract
131    {
132
133        private String entityId;
134        private String externalIdentifierTypeCode;
135        private EntityExternalIdentifierType.Builder externalIdentifierType;
136        private String externalId;
137        private Long versionNumber;
138        private String objectId;
139        private String id;
140
141        private Builder() { }
142
143        public static Builder create() {
144            return new Builder();
145        }
146
147        public static Builder create(EntityExternalIdentifierContract contract) {
148            if (contract == null) {
149                throw new IllegalArgumentException("contract was null");
150            }
151            Builder builder = create();
152            builder.setEntityId(contract.getEntityId());
153            builder.setExternalIdentifierTypeCode(contract.getExternalIdentifierTypeCode());
154            if (contract.getExternalIdentifierType() != null) {
155                builder.setExternalIdentifierType(
156                        EntityExternalIdentifierType.Builder.create(contract.getExternalIdentifierType()));
157            }
158            builder.setExternalId(contract.getExternalId());
159            builder.setVersionNumber(contract.getVersionNumber());
160            builder.setObjectId(contract.getObjectId());
161            builder.setId(contract.getId());
162            return builder;
163        }
164
165        public EntityExternalIdentifier build() {
166            return new EntityExternalIdentifier(this);
167        }
168
169        @Override
170        public String getEntityId() {
171            return this.entityId;
172        }
173
174        @Override
175        public String getExternalIdentifierTypeCode() {
176            return this.externalIdentifierTypeCode;
177        }
178
179        @Override
180        public EntityExternalIdentifierType.Builder getExternalIdentifierType() {
181            return this.externalIdentifierType;
182        }
183
184        @Override
185        public String getExternalId() {
186            return this.externalId;
187        }
188
189        @Override
190        public Long getVersionNumber() {
191            return this.versionNumber;
192        }
193
194        @Override
195        public String getObjectId() {
196            return this.objectId;
197        }
198
199        @Override
200        public String getId() {
201            return this.id;
202        }
203
204        public void setEntityId(String entityId) {
205            this.entityId = entityId;
206        }
207
208        public void setExternalIdentifierTypeCode(String externalIdentifierTypeCode) {
209            this.externalIdentifierTypeCode = externalIdentifierTypeCode;
210        }
211
212        public void setExternalIdentifierType(EntityExternalIdentifierType.Builder externalIdentifierType) {
213            this.externalIdentifierType = externalIdentifierType;
214        }
215
216        public void setExternalId(String externalId) {
217            this.externalId = externalId;
218        }
219
220        public void setVersionNumber(Long versionNumber) {
221            this.versionNumber = versionNumber;
222        }
223
224        public void setObjectId(String objectId) {
225            this.objectId = objectId;
226        }
227
228        public void setId(String id) {
229            if (StringUtils.isWhitespace(id)) {
230                throw new IllegalArgumentException("id is blank");
231            }
232            this.id = id;
233        }
234
235    }
236
237
238    /**
239     * Defines some internal constants used on this class.
240     * 
241     */
242    static class Constants {
243
244        final static String ROOT_ELEMENT_NAME = "entityExternalIdentifier";
245        final static String TYPE_NAME = "EntityExternalIdentifierType";
246    }
247
248
249    /**
250     * A private class which exposes constants which define the XML element names to use when this object is marshalled to XML.
251     * 
252     */
253    static class Elements {
254
255        final static String ENTITY_ID = "entityId";
256        final static String EXTERNAL_IDENTIFIER_TYPE = "externalIdentifierType";
257        final static String EXTERNAL_IDENTIFIER_TYPE_CODE = "externalIdentifierTypeCode";
258        final static String EXTERNAL_ID = "externalId";
259        final static String ID = "id";
260
261    }
262
263}