View Javadoc

1   /**
2    * Copyright 2005-2013 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.impl.permission;
17  
18  import javax.persistence.Column;
19  import javax.persistence.Entity;
20  import javax.persistence.Id;
21  import javax.persistence.Table;
22  import org.apache.commons.lang.StringUtils;
23  import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;
24  
25  import java.util.HashMap;
26  import java.util.Iterator;
27  import java.util.List;
28  import java.util.Map;
29  
30  @Entity
31  @Table(name = "KRIM_PERM_T")
32  public class GenericPermissionBo extends PersistableBusinessObjectBase {
33      private static final long serialVersionUID = 1L;
34  
35      @Id
36      @Column(name="PERM_ID")
37      protected String id;
38      protected String namespaceCode;
39      protected String name;
40      protected String description;
41      protected boolean active;
42      protected String templateId;
43      protected String detailValues;
44      protected Map<String,String> details;
45      protected PermissionTemplateBo template = new PermissionTemplateBo();
46      protected List<PermissionAttributeBo> attributeDetails;
47  
48      /**
49       * This constructs a ...
50       *
51       */
52      public GenericPermissionBo() {
53      }
54  
55      public GenericPermissionBo( PermissionBo perm ) {
56          loadFromPermission( perm );
57  
58      }
59  
60      public void loadFromPermission( PermissionBo perm ) {
61          setId( perm.getId() );
62          setNamespaceCode( perm.getNamespaceCode() );
63          setTemplate(perm.getTemplate());
64          setAttributeDetails(perm.getAttributeDetails());
65          setDetailValues(perm.getDetailObjectsValues());
66          setName( perm.getName() );
67          setTemplateId( perm.getTemplateId() );
68          setDescription( perm.getDescription() );
69          setActive( perm.isActive() );
70          setDetails( perm.getAttributes() );
71          setVersionNumber(perm.getVersionNumber());
72          setObjectId(perm.getObjectId());
73          setExtension(perm.getExtension());
74  
75      }
76  
77  
78      public String getDetailValues() {
79          /*StringBuffer sb = new StringBuffer();
80          if ( details != null ) {
81              Iterator<String> keyIter = details.keySet().iterator();
82              while ( keyIter.hasNext() ) {
83                  String key = keyIter.next();
84                  sb.append( key ).append( '=' ).append( details.get( key ) );
85                  if ( keyIter.hasNext() ) {
86                      sb.append( '\n' );
87                  }
88              }
89          }
90          return sb.toString();*/
91          return detailValues;
92      }
93  
94      public void setDetailValues( String detailValues ) {
95          this.detailValues = detailValues;
96      }
97  
98      public void setDetailValues( Map<String, String> detailsAttribs ) {
99          StringBuffer sb = new StringBuffer();
100         if ( detailsAttribs != null ) {
101             Iterator<String> keyIter = detailsAttribs.keySet().iterator();
102             while ( keyIter.hasNext() ) {
103                 String key = keyIter.next();
104                 sb.append( key ).append( '=' ).append( detailsAttribs.get( key ) );
105                 if ( keyIter.hasNext() ) {
106                     sb.append( '\n' );
107                 }
108             }
109         }
110         detailValues = sb.toString();
111     }
112 
113     public boolean isActive() {
114         return active;
115     }
116 
117     public void setActive(boolean active) {
118         this.active = active;
119     }
120 
121     public String getDescription() {
122         return description;
123     }
124 
125     public String getId() {
126         return id;
127     }
128 
129     public String getName() {
130         return name;
131     }
132 
133     public PermissionTemplateBo getTemplate() {
134         return template;
135     }
136 
137     public void setDescription(String permissionDescription) {
138         this.description = permissionDescription;
139     }
140 
141     public void setName(String permissionName) {
142         this.name = permissionName;
143     }
144 
145     public void setDetails( Map<String,String> details ) {
146         this.details = details;
147         setDetailValues(details);
148     }
149 
150     public String getTemplateId() {
151         return this.templateId;
152     }
153 
154     public void setTemplateId(String templateId) {
155         this.templateId = templateId;
156     }
157 
158     public void setTemplate(PermissionTemplateBo template) {
159         this.template = template;
160     }
161 
162     public Map<String, String> getDetails() {
163         String detailValuesTemp = this.detailValues;
164         Map<String, String> detailsTemp = new HashMap<String, String>();
165         if (detailValuesTemp != null) {
166             // ensure that all line delimiters are single linefeeds
167             detailValuesTemp = detailValuesTemp.replace("\r\n", "\n");
168             detailValuesTemp = detailValuesTemp.replace('\r', '\n');
169             if (StringUtils.isNotBlank(detailValuesTemp)) {
170                 String[] values = detailValuesTemp.split("\n");
171                 for (String attrib : values) {
172                     if (attrib.indexOf('=') != -1) {
173                         String[] keyValueArray = attrib.split("=", 2);
174                         detailsTemp.put(keyValueArray[0].trim(), keyValueArray[1].trim());
175                     }
176                 }
177             }
178         }
179         this.details = detailsTemp;
180         return details;
181     }
182 
183     public String getNamespaceCode() {
184         return this.namespaceCode;
185     }
186 
187     public void setNamespaceCode(String namespaceCode) {
188         this.namespaceCode = namespaceCode;
189     }
190 
191     public void setId(String id) {
192         this.id = id;
193     }
194 
195     public List<PermissionAttributeBo> getAttributeDetails() {
196         return attributeDetails;
197     }
198 
199     public void setAttributeDetails(List<PermissionAttributeBo> attributeDetails) {
200         this.attributeDetails = attributeDetails;
201     }
202 
203     @Override
204     public void refreshNonUpdateableReferences() {
205         // do nothing - not a persistable object
206     }
207     @Override
208     public void refreshReferenceObject(String referenceObjectName) {
209         // do nothing - not a persistable object
210     }
211 
212     @Override
213     protected void prePersist() {
214         throw new UnsupportedOperationException( "This object should never be persisted.");
215     }
216 
217     @Override
218     protected void preUpdate() {
219         throw new UnsupportedOperationException( "This object should never be persisted.");
220     }
221 
222     @Override
223     protected void preRemove() {
224         throw new UnsupportedOperationException( "This object should never be persisted.");
225     }
226 
227     public static PermissionBo toPermissionBo(GenericPermissionBo bo) {
228         PermissionBo permission = new PermissionBo();
229         permission.setTemplateId(bo.getTemplateId());
230         permission.setId(bo.getId());
231         permission.setTemplate(bo.getTemplate());
232         permission.setActive(bo.isActive());
233         permission.setDescription(bo.getDescription());
234         permission.setName(bo.getName());
235         permission.setNamespaceCode(bo.namespaceCode);
236         permission.setAttributeDetails(bo.getAttributeDetails());
237         permission.setAttributes(bo.getDetails());
238         permission.setVersionNumber(bo.versionNumber);
239         permission.setObjectId(bo.getObjectId());
240         permission.setExtension(bo.getExtension());
241         return permission;
242     }
243 }