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