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