View Javadoc
1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kim.api.role;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.api.CoreConstants;
20  import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
21  import org.kuali.rice.core.api.mo.ModelBuilder;
22  import org.w3c.dom.Element;
23  
24  import javax.xml.bind.annotation.XmlAccessType;
25  import javax.xml.bind.annotation.XmlAccessorType;
26  import javax.xml.bind.annotation.XmlAnyElement;
27  import javax.xml.bind.annotation.XmlElement;
28  import javax.xml.bind.annotation.XmlRootElement;
29  import javax.xml.bind.annotation.XmlType;
30  import java.io.Serializable;
31  import java.util.Collection;
32  
33  
34  @XmlRootElement(name = RolePermission.Constants.ROOT_ELEMENT_NAME)
35  @XmlAccessorType(XmlAccessType.NONE)
36  @XmlType(name = RolePermission.Constants.TYPE_NAME, propOrder = {
37          RolePermission.Elements.ID,
38          RolePermission.Elements.ROLE_ID,
39          RolePermission.Elements.PERMISSION_ID,
40          RolePermission.Elements.ACTIVE,
41          CoreConstants.CommonElements.VERSION_NUMBER,
42          CoreConstants.CommonElements.OBJECT_ID,
43          CoreConstants.CommonElements.FUTURE_ELEMENTS
44  })
45  public class RolePermission extends AbstractDataTransferObject implements RolePermissionContract {
46      @XmlElement(name = Elements.ID, required = false)
47      private final String id;
48  
49      @XmlElement(name = RolePermission.Elements.ROLE_ID, required = false)
50      private final String roleId;
51  
52      @XmlElement(name = RolePermission.Elements.PERMISSION_ID, required = false)
53      private final String permissionId;
54  
55      @XmlElement(name = RolePermission.Elements.ACTIVE, required = false)
56      private final boolean active;
57  
58      @XmlElement(name = CoreConstants.CommonElements.VERSION_NUMBER, required = false)
59      private final Long versionNumber;
60  
61      @XmlElement(name = CoreConstants.CommonElements.OBJECT_ID, required = false)
62      private final String objectId;
63  
64      @SuppressWarnings("unused")
65      @XmlAnyElement
66      private final Collection<Element> _futureElements = null;
67  
68      /**
69       * A constructor to be used only by JAXB unmarshalling.
70       */
71      private RolePermission() {
72          this.id = null;
73          this.roleId = null;
74          this.permissionId = null;
75          this.active = false;
76          this.versionNumber = null;
77          this.objectId = null;
78      }
79  
80      /**
81       * A constructor using the Builder.
82       *
83       * @param builder
84       */
85      public RolePermission(Builder builder) {
86          this.id = builder.getId();
87          this.roleId = builder.getRoleId();
88          this.permissionId = builder.getPermissionId();
89          this.active = builder.isActive();
90          this.versionNumber = builder.getVersionNumber();
91          this.objectId = builder.getObjectId();
92      }
93  
94      @Override
95      public String getId() {
96          return id;
97      }
98  
99      @Override
100     public String getPermissionId() {
101         return permissionId;
102     }
103 
104     @Override
105     public String getRoleId() {
106         return roleId;
107     }
108 
109     @Override
110     public boolean isActive() {
111         return active;
112     }
113 
114     @Override
115     public Long getVersionNumber() {
116         return versionNumber;
117     }
118 
119     @Override
120     public String getObjectId() {
121         return objectId;
122     }
123 
124     /**
125      * This builder constructs a RolePermission enforcing the constraints of the {@link RolePermissionContract}.
126      */
127     public static final class Builder implements RolePermissionContract, ModelBuilder, Serializable {
128         private String id;
129         private String roleId;
130         private String permissionId;
131         private Long versionNumber = 1L;
132         private String objectId;
133         private boolean active;
134 
135         private Builder(String id, String roleId, String permissionId) {
136             setId(id);
137             setRoleId(roleId);
138             setPermissionId(permissionId);
139         }
140 
141         /**
142          * Creates a RolePermission with the required fields.
143          */
144         public static Builder create(String id, String roleId, String permissionId) {
145             return new Builder(id, roleId, permissionId);
146         }
147 
148         /**
149          * Creates a RolePermission from an existing {@link RolePermissionContract}.
150          */
151         public static Builder create(RolePermissionContract contract) {
152             Builder builder = new Builder(contract.getId(), contract.getRoleId(), contract.getPermissionId());
153             builder.setActive(contract.isActive());
154             builder.setVersionNumber(contract.getVersionNumber());
155             builder.setObjectId(contract.getObjectId());
156 
157             return builder;
158         }
159 
160         @Override
161         public String getId() {
162             return id;
163         }
164 
165         public void setId(final String id) {
166             if (StringUtils.isWhitespace(id)) {
167                 throw new IllegalArgumentException("id is blank");
168             }
169             this.id = id;
170         }
171 
172         @Override
173         public String getPermissionId() {
174             return permissionId;
175         }
176 
177         public void setPermissionId(final String permissionId) {
178             this.permissionId = permissionId;
179         }
180 
181         @Override
182         public String getRoleId() {
183             return roleId;
184         }
185 
186         public void setRoleId(final String roleId) {
187             this.roleId = roleId;
188         }
189 
190         @Override
191         public Long getVersionNumber() {
192             return versionNumber;
193         }
194 
195         public void setVersionNumber(Long versionNumber) {
196             if (versionNumber != null && versionNumber <= 0) {
197                 throw new IllegalArgumentException("versionNumber is invalid");
198             }
199             this.versionNumber = versionNumber;
200         }
201 
202         @Override
203         public String getObjectId() {
204             return objectId;
205         }
206 
207         public void setObjectId(final String objectId) {
208             this.objectId = objectId;
209         }
210 
211         @Override
212         public boolean isActive() {
213             return active;
214         }
215 
216         public void setActive(final boolean active) {
217             this.active = active;
218         }
219 
220         @Override
221         public RolePermission build() {
222             if (versionNumber == null || versionNumber <= 0) {
223                 throw new IllegalStateException("versionNumber is invalid");
224             }
225             if (StringUtils.isWhitespace(id)) {
226                 throw new IllegalStateException("id is blank");
227             }
228             return new RolePermission(this);
229         }
230     }
231 
232     /**
233      * Defines some internal constants used on this class.
234      */
235     static class Constants {
236         final static String ROOT_ELEMENT_NAME = "rolePermission";
237         final static String TYPE_NAME = "RolePermissionType";
238     }
239 
240     /**
241      * A private class which exposes constants which define the XML element names to use
242      * when this object is marshalled to XML.
243      */
244     static class Elements {
245         final static String ID = "id";
246         final static String PERMISSION_ID = "permissionId";
247         final static String ROLE_ID = "roleId";
248         final static String ACTIVE = "active";
249     }
250 }