001/**
002 * Copyright 2005-2016 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 */
016package org.kuali.rice.kim.impl.permission;
017
018import javax.persistence.Column;
019import javax.persistence.Entity;
020import javax.persistence.Id;
021import javax.persistence.Table;
022import org.apache.commons.lang.StringUtils;
023import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;
024
025import java.util.HashMap;
026import java.util.Iterator;
027import java.util.List;
028import java.util.Map;
029
030@Entity
031@Table(name = "KRIM_PERM_T")
032public 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    }
097
098    public void setDetailValues( Map<String, String> detailsAttribs ) {
099        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}