001    /**
002     * Copyright 2005-2012 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.role;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.apache.commons.lang.builder.EqualsBuilder;
020    import org.apache.commons.lang.builder.HashCodeBuilder;
021    import org.apache.commons.lang.builder.ToStringBuilder;
022    import org.kuali.rice.core.api.CoreConstants;
023    import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
024    import org.kuali.rice.core.api.mo.ModelBuilder;
025    import org.kuali.rice.core.api.mo.ModelObjectComplete;
026    import org.kuali.rice.kim.api.KimConstants;
027    import org.w3c.dom.Element;
028    
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 java.util.Collection;
036    
037    
038    /**
039     * This is a description of what this class does - shyu don't forget to fill this in.
040     *
041     * @author Kuali Rice Team (rice.collab@kuali.org)
042     */
043    @XmlRootElement(name = Role.Constants.ROOT_ELEMENT_NAME)
044    @XmlAccessorType(XmlAccessType.NONE)
045    @XmlType(name = Role.Constants.TYPE_NAME, propOrder = {
046            Role.Elements.ID,
047            Role.Elements.NAME,
048            Role.Elements.NAMESPACE_CODE,
049            Role.Elements.DESCRIPTION,
050            Role.Elements.KIM_TYPE_ID,
051            Role.Elements.ACTIVE,
052            CoreConstants.CommonElements.VERSION_NUMBER,
053            CoreConstants.CommonElements.OBJECT_ID,
054            CoreConstants.CommonElements.FUTURE_ELEMENTS
055    })
056    public final class Role extends AbstractDataTransferObject implements RoleContract {
057        private static final long serialVersionUID = 1L;
058    
059        @XmlElement(name = Role.Elements.ID, required = true)
060        private final String id;
061    
062        @XmlElement(name = Role.Elements.NAME, required = true)
063        private final String name;
064    
065        @XmlElement(name = Role.Elements.NAMESPACE_CODE, required = true)
066        private final String namespaceCode;
067    
068        @XmlElement(name = Role.Elements.DESCRIPTION)
069        private final String description;
070    
071        @XmlElement(name = Role.Elements.KIM_TYPE_ID, required = true)
072        private final String kimTypeId;
073    
074        @XmlElement(name = CoreConstants.CommonElements.ACTIVE, required = true)
075        private final boolean active;
076    
077        @XmlElement(name = CoreConstants.CommonElements.VERSION_NUMBER)
078        private final Long versionNumber;
079    
080        @XmlElement(name = CoreConstants.CommonElements.OBJECT_ID, required = false)
081        private final String objectId;
082    
083        @SuppressWarnings("unused")
084        @XmlAnyElement
085        private final Collection<Element> _futureElements = null;
086    
087    
088        /**
089         * This constructor should never be called except during JAXB unmarshalling.
090         */
091        @SuppressWarnings("unused")
092        private Role() {
093            id = null;
094            name = null;
095            namespaceCode = null;
096            description = null;
097            kimTypeId = null;
098            active = false;
099            objectId = null;
100            versionNumber = null;
101        }
102    
103        private Role(Builder builder) {
104            id = builder.getId();
105            name = builder.getName();
106            namespaceCode = builder.getNamespaceCode();
107            description = builder.getDescription();
108            kimTypeId = builder.getKimTypeId();
109            active = builder.isActive();
110            versionNumber = builder.getVersionNumber();
111            objectId = builder.getObjectId();
112        }
113    
114    
115        /**
116         * Unique identifier for this role.
117         */
118        @Override
119        public String getId() {
120            return id;
121        }
122    
123        /**
124         * Namespace for this role - identifies the system/module to which this role applies
125         */
126        @Override
127        public String getNamespaceCode() {
128            return namespaceCode;
129        }
130    
131        /**
132         * Name for this role.  This value will be seen by the users.
133         */
134        @Override
135        public String getName() {
136            return name;
137        }
138    
139        /**
140         * Verbose description of the role and functionally what permissions it implies.
141         */
142        @Override
143        public String getDescription() {
144            return description;
145        }
146    
147        /**
148         * Type identifier for this role.  This will control what additional attributes are available
149         */
150        @Override
151        public String getKimTypeId() {
152            return kimTypeId;
153        }
154    
155        @Override
156        public Long getVersionNumber() {
157            return versionNumber;
158        }
159    
160        @Override
161        public String getObjectId() {
162            return objectId;
163        }
164    
165        @Override
166        public boolean isActive() {
167            return active;
168        }
169    
170        public static final class Builder implements RoleContract, ModelBuilder, ModelObjectComplete {
171    
172            private String id;
173            private String name;
174            private String namespaceCode;
175            private String description;
176            private String kimTypeId;
177            private boolean active;
178            private Long versionNumber;
179            private String objectId;
180    
181            private Builder() {
182            }
183    
184            public static Builder create() {
185                return new Builder();
186            }
187    
188            public static Builder create(String id, String name, String namespaceCode, String description, String kimTypeId) {
189                Builder b = new Builder();
190                b.setId(id);
191                b.setName(name);
192                b.setNamespaceCode(namespaceCode);
193                b.setDescription(description);
194                b.setKimTypeId(kimTypeId);
195                b.setActive(true);
196    
197                return b;
198            }
199    
200            public static Builder create(RoleContract roleContract) {
201                Builder b = new Builder();
202                b.setId(roleContract.getId());
203                b.setName(roleContract.getName());
204                b.setNamespaceCode(roleContract.getNamespaceCode());
205                b.setDescription(roleContract.getDescription());
206                b.setKimTypeId(roleContract.getKimTypeId());
207                b.setActive(roleContract.isActive());
208                b.setVersionNumber(roleContract.getVersionNumber());
209                b.setObjectId(roleContract.getObjectId());
210                return b;
211            }
212    
213            @Override
214            public Role build() {
215                return new Role(this);
216            }
217    
218            public void setId(String id) {
219                if (StringUtils.isWhitespace(id)) {
220                    throw new IllegalArgumentException("id cannot be blank");
221                }
222                this.id = id;
223            }
224    
225            @Override
226            public String getId() {
227                return id;
228            }
229    
230            public void setNamespaceCode(String namespaceCode) {
231                if (StringUtils.isBlank(namespaceCode)) {
232                    throw new IllegalArgumentException("namespaceCode cannot be blank or null");
233                }
234                this.namespaceCode = namespaceCode;
235            }
236    
237            @Override
238            public String getNamespaceCode() {
239                return namespaceCode;
240            }
241    
242            public void setName(String name) {
243                if (StringUtils.isBlank(name)) {
244                    throw new IllegalArgumentException("name cannot be blank or null");
245                }
246                this.name = name;
247            }
248    
249            @Override
250            public String getName() {
251                return name;
252            }
253    
254            public void setDescription(String description) {
255                this.description = description;
256            }
257    
258            @Override
259            public String getDescription() {
260                return description;
261            }
262    
263            public void setKimTypeId(String kimTypeId) {
264                if (StringUtils.isBlank(kimTypeId)) {
265                    throw new IllegalArgumentException("kimTypeId cannot be blank or null");
266                }
267                this.kimTypeId = kimTypeId;
268            }
269    
270            @Override
271            public String getKimTypeId() {
272                return kimTypeId;
273            }
274    
275            public void setActive(boolean active) {
276                this.active = active;
277            }
278    
279            @Override
280            public boolean isActive() {
281                return active;
282            }
283    
284            public void setVersionNumber(Long versionNumber) {
285                this.versionNumber = versionNumber;
286            }
287    
288            @Override
289            public Long getVersionNumber() {
290                return versionNumber;
291            }
292    
293            public void setObjectId(String objectId) {
294                this.objectId = objectId;
295            }
296    
297            @Override
298            public String getObjectId() {
299                return objectId;
300            }
301    
302            @Override
303            public int hashCode() {
304                return HashCodeBuilder.reflectionHashCode(this);
305            }
306    
307            @Override
308            public boolean equals(Object obj) {
309                return EqualsBuilder.reflectionEquals(obj, this);
310            }
311    
312            @Override
313            public String toString() {
314                return ToStringBuilder.reflectionToString(this);
315            }
316        }
317    
318        /**
319         * A private class which exposes constants which define the XML element names to use
320         * when this object is marshalled to XML.
321         */
322        static class Elements {
323            final static String ID = "id";
324            final static String NAME = "name";
325            final static String DESCRIPTION = "description";
326            final static String KIM_TYPE_ID = "kimTypeId";
327            final static String NAMESPACE_CODE = "namespaceCode";
328            final static String ACTIVE = "active";
329        }
330    
331        /**
332         * Defines some internal constants used on this class.
333         */
334        static class Constants {
335            final static String ROOT_ELEMENT_NAME = "role";
336            final static String TYPE_NAME = "RoleType";
337        }
338    
339        public static class Cache {
340            public static final String NAME = KimConstants.Namespaces.KIM_NAMESPACE_2_0 + "/" + Role.Constants.TYPE_NAME;
341        }
342    }