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.impl.permission;
017    
018    import javax.persistence.Column;
019    import javax.persistence.Entity;
020    import javax.persistence.Id;
021    import javax.persistence.Table;
022    import org.apache.commons.lang.StringUtils;
023    import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;
024    
025    import java.util.HashMap;
026    import java.util.Iterator;
027    import java.util.List;
028    import java.util.Map;
029    
030    @Entity
031    @Table(name = "KRIM_PERM_T")
032    public class GenericPermissionBo extends PersistableBusinessObjectBase {
033        private static final long serialVersionUID = 1L;
034    
035        @Id
036        @Column(name="PERM_ID")
037        protected String id;
038        protected String namespaceCode;
039        protected String name;
040        protected String description;
041        protected boolean active;
042        protected String templateId;
043        protected String detailValues;
044        protected Map<String,String> details;
045        protected PermissionTemplateBo template = new PermissionTemplateBo();
046        protected List<PermissionAttributeBo> attributeDetails;
047    
048        /**
049         * This constructs a ...
050         *
051         */
052        public GenericPermissionBo() {
053        }
054    
055        public GenericPermissionBo( PermissionBo perm ) {
056            loadFromPermission( perm );
057    
058        }
059    
060        public void loadFromPermission( PermissionBo perm ) {
061            setId( perm.getId() );
062            setNamespaceCode( perm.getNamespaceCode() );
063            setTemplate(perm.getTemplate());
064            setAttributeDetails(perm.getAttributeDetails());
065            setDetailValues(perm.getDetailObjectsValues());
066            setName( perm.getName() );
067            setTemplateId( perm.getTemplateId() );
068            setDescription( perm.getDescription() );
069            setActive( perm.isActive() );
070            setDetails( perm.getAttributes() );
071            setVersionNumber(perm.getVersionNumber());
072            setObjectId(perm.getObjectId());
073            setExtension(perm.getExtension());
074    
075        }
076    
077    
078        public String getDetailValues() {
079            /*StringBuffer sb = new StringBuffer();
080            if ( details != null ) {
081                Iterator<String> keyIter = details.keySet().iterator();
082                while ( keyIter.hasNext() ) {
083                    String key = keyIter.next();
084                    sb.append( key ).append( '=' ).append( details.get( key ) );
085                    if ( keyIter.hasNext() ) {
086                        sb.append( '\n' );
087                    }
088                }
089            }
090            return sb.toString();*/
091            return detailValues;
092        }
093    
094        public void setDetailValues( String detailValues ) {
095            this.detailValues = detailValues;
096            String detailValuesTemp = detailValues;
097            Map<String,String> details = new HashMap<String,String>();
098            if ( detailValuesTemp != null ) {
099                // ensure that all line delimiters are single linefeeds
100                detailValuesTemp = detailValuesTemp.replace( "\r\n", "\n" );
101                detailValuesTemp = detailValuesTemp.replace( '\r', '\n' );
102                if ( StringUtils.isNotBlank( detailValuesTemp ) ) {
103                    String[] values = detailValuesTemp.split( "\n" );
104                    for ( String attrib : values ) {
105                        if ( attrib.indexOf( '=' ) != -1 ) {
106                            String[] keyValueArray = attrib.split( "=", 2 );
107                            details.put( keyValueArray[0].trim(), keyValueArray[1].trim() );
108                        }
109                    }
110                }
111            }
112            this.details = details;
113        }
114    
115        public void setDetailValues( Map<String, String> detailsAttribs ) {
116            StringBuffer sb = new StringBuffer();
117            if ( detailsAttribs != null ) {
118                Iterator<String> keyIter = detailsAttribs.keySet().iterator();
119                while ( keyIter.hasNext() ) {
120                    String key = keyIter.next();
121                    sb.append( key ).append( '=' ).append( detailsAttribs.get( key ) );
122                    if ( keyIter.hasNext() ) {
123                        sb.append( '\n' );
124                    }
125                }
126            }
127            detailValues = sb.toString();
128        }
129    
130        public boolean isActive() {
131            return active;
132        }
133    
134        public void setActive(boolean active) {
135            this.active = active;
136        }
137    
138        public String getDescription() {
139            return description;
140        }
141    
142        public String getId() {
143            return id;
144        }
145    
146        public String getName() {
147            return name;
148        }
149    
150        public PermissionTemplateBo getTemplate() {
151            return template;
152        }
153    
154        public void setDescription(String permissionDescription) {
155            this.description = permissionDescription;
156        }
157    
158        public void setName(String permissionName) {
159            this.name = permissionName;
160        }
161    
162        public void setDetails( Map<String,String> details ) {
163            this.details = details;
164            setDetailValues(details);
165        }
166    
167        public String getTemplateId() {
168            return this.templateId;
169        }
170    
171        public void setTemplateId(String templateId) {
172            this.templateId = templateId;
173        }
174    
175        public void setTemplate(PermissionTemplateBo template) {
176            this.template = template;
177        }
178    
179        public Map<String,String> getDetails() {
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    }