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.CascadeType;
019    import javax.persistence.Column;
020    import javax.persistence.Entity;
021    import javax.persistence.FetchType;
022    import javax.persistence.Id;
023    import javax.persistence.JoinColumn;
024    import javax.persistence.OneToMany;
025    import javax.persistence.OneToOne;
026    import javax.persistence.Table;
027    import javax.persistence.Transient;
028    import org.hibernate.annotations.Fetch;
029    import org.hibernate.annotations.FetchMode;
030    import org.hibernate.annotations.Type;
031    import org.kuali.rice.kim.api.KimConstants;
032    import org.kuali.rice.kim.api.permission.Permission;
033    import org.kuali.rice.kim.api.permission.PermissionContract;
034    import org.kuali.rice.kim.api.services.KimApiServiceLocator;
035    import org.kuali.rice.kim.api.type.KimType;
036    import org.kuali.rice.kim.api.type.KimTypeAttribute;
037    import org.kuali.rice.kim.api.type.KimTypeInfoService;
038    import org.kuali.rice.kim.impl.common.attribute.KimAttributeDataBo;
039    import org.kuali.rice.kim.impl.role.RolePermissionBo;
040    import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;
041    import org.kuali.rice.krad.service.DataDictionaryService;
042    import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
043    import org.springframework.util.AutoPopulatingList;
044    
045    import java.util.Iterator;
046    import java.util.List;
047    import java.util.Map;
048    
049    @Entity
050    @Table(name = "KRIM_PERM_T")
051    public class PermissionBo extends PersistableBusinessObjectBase implements PermissionContract {
052        private static final long serialVersionUID = 1L;
053    
054        @Id
055        @Column(name = "PERM_ID")
056        private String id;
057    
058        @Column(name = "NMSPC_CD")
059        private String namespaceCode;
060    
061        @Column(name = "NM")
062        private String name;
063    
064        @Column(name = "DESC_TXT", length = 400)
065        private String description;
066    
067        @Column(name = "PERM_TMPL_ID")
068        private String templateId;
069    
070        @Column(name = "ACTV_IND")
071        @Type(type = "yes_no")
072        private boolean active;
073    
074        @OneToOne(targetEntity = PermissionTemplateBo.class, cascade = {}, fetch = FetchType.EAGER)
075        @JoinColumn(name = "PERM_TMPL_ID", insertable = false, updatable = false)
076        private PermissionTemplateBo template = new PermissionTemplateBo();
077    
078        @OneToMany(targetEntity = PermissionAttributeBo.class, cascade = {CascadeType.ALL}, fetch = FetchType.EAGER, mappedBy = "id")
079        @Fetch(value = FetchMode.SELECT)
080        private List<PermissionAttributeBo> attributeDetails;
081    
082        @Transient
083        private Map<String,String> attributes;
084    
085        @OneToMany(targetEntity = RolePermissionBo.class, cascade = {CascadeType.ALL}, fetch = FetchType.EAGER, mappedBy = "id")
086        @Fetch(value = FetchMode.SELECT)
087        private List<RolePermissionBo> rolePermissions = new AutoPopulatingList(RolePermissionBo.class);
088    
089        @Transient
090        private KimTypeInfoService kimTypeInfoService;
091        @Transient
092        private DataDictionaryService dataDictionaryService;
093    
094    
095        public Map<String,String> getAttributes() {
096            return attributeDetails != null ? KimAttributeDataBo.toAttributes(attributeDetails) : attributes;
097        }
098    
099        //TODO: rename/fix later - only including this method and attributeDetails field for Role conversion
100        public Map<String,String> getDetails() {
101            return attributeDetails != null ? KimAttributeDataBo.toAttributes(attributeDetails) : attributes;
102        }
103    
104        @Override
105        public String getId() {
106            return id;
107        }
108    
109        public void setId(String id) {
110            this.id = id;
111        }
112    
113        @Override
114        public String getNamespaceCode() {
115            return namespaceCode;
116        }
117    
118        public void setNamespaceCode(String namespaceCode) {
119            this.namespaceCode = namespaceCode;
120        }
121    
122        @Override
123        public String getName() {
124            return name;
125        }
126    
127        public void setName(String name) {
128            this.name = name;
129        }
130    
131        @Override
132        public String getDescription() {
133            return description;
134        }
135    
136        public void setDescription(String description) {
137            this.description = description;
138        }
139    
140        public String getTemplateId() {
141            return templateId;
142        }
143    
144        public void setTemplateId(String templateId) {
145            this.templateId = templateId;
146        }
147    
148        @Override
149        public boolean isActive() {
150            return active;
151        }
152    
153        public void setActive(boolean active) {
154            this.active = active;
155        }
156    
157        public List<PermissionAttributeBo> getAttributeDetails() {
158            return attributeDetails;
159        }
160    
161        public void setAttributeDetails(List<PermissionAttributeBo> attributeDetails) {
162            this.attributeDetails = attributeDetails;
163        }
164    
165        public List<RolePermissionBo> getRolePermissions() {
166            return rolePermissions;
167        }
168    
169        public void setRolePermissions(List<RolePermissionBo> rolePermissions) {
170            this.rolePermissions = rolePermissions;
171        }
172    
173        public KimTypeInfoService getKimTypeInfoService() {
174            return kimTypeInfoService;
175        }
176    
177        public void setKimTypeInfoService(KimTypeInfoService kimTypeInfoService) {
178            this.kimTypeInfoService = kimTypeInfoService;
179        }
180    
181        public void setAttributes(Map<String, String> attributes) {
182            this.attributes = attributes;
183        }
184    
185        /**
186         * Converts a mutable bo to its immutable counterpart
187         * @param bo the mutable business object
188         * @return the immutable object
189         */
190        public static Permission to(PermissionBo bo) {
191            if (bo == null) {
192                return null;
193            }
194    
195            return Permission.Builder.create(bo).build();
196        }
197    
198        /**
199         * Converts a immutable object to its mutable counterpart
200         * @param im immutable object
201         * @return the mutable bo
202         */
203        public static PermissionBo from(Permission im) {
204            if (im == null) {
205                return null;
206            }
207    
208            PermissionBo bo = new PermissionBo();
209            bo.setId(im.getId());
210            bo.setNamespaceCode(im.getNamespaceCode());
211            bo.setName(im.getName());
212            bo.setDescription(im.getDescription());
213            bo.setActive(im.isActive());
214            bo.setTemplateId(im.getTemplate() != null ? im.getTemplate().getId() : null);
215            bo.setTemplate(PermissionTemplateBo.from(im.getTemplate()));
216            bo.setAttributes(im.getAttributes());
217            bo.setVersionNumber(im.getVersionNumber());
218            bo.setObjectId(im.getObjectId());
219    
220            return bo;
221        }
222    
223        @Override
224        public PermissionTemplateBo getTemplate() {
225            return template;
226        }
227    
228        public void setTemplate(PermissionTemplateBo template) {
229            this.template = template;
230        }
231    
232        public String getDetailObjectsValues() {
233            StringBuffer detailObjectsToDisplayBuffer = new StringBuffer();
234            Iterator<PermissionAttributeBo> permIter = attributeDetails.iterator();
235            while (permIter.hasNext()) {
236                PermissionAttributeBo permissionAttributeData = permIter.next();
237                detailObjectsToDisplayBuffer.append(permissionAttributeData.getAttributeValue());
238                if (permIter.hasNext()) {
239                    detailObjectsToDisplayBuffer.append(KimConstants.KimUIConstants.COMMA_SEPARATOR);
240                }
241            }
242            return detailObjectsToDisplayBuffer.toString();
243        }
244    
245        public String getDetailObjectsToDisplay() {
246            final KimType kimType = getTypeInfoService().getKimType( getTemplate().getKimTypeId() );
247    
248    
249            StringBuffer detailObjects = new StringBuffer();
250            Iterator<PermissionAttributeBo> permIter = attributeDetails.iterator();
251            while (permIter.hasNext()) {
252                PermissionAttributeBo bo = permIter.next();
253                detailObjects.append(getKimAttributeLabelFromDD(kimType.getAttributeDefinitionById(bo.getKimAttributeId())))
254                        .append(":")
255                        .append(bo.getAttributeValue());
256                if (permIter.hasNext()) {
257                    detailObjects.append(",");
258                }
259            }
260            /*return attributeDetails.collect {
261                getKimAttributeLabelFromDD(kimType.getAttributeDefinitionById(it.kimAttributeId)) + ":" + it.attributeValue
262            }.join(",")*/
263            return detailObjects.toString();
264        }
265    
266        private String getKimAttributeLabelFromDD( KimTypeAttribute attribute ){
267            return getDataDictionaryService().getAttributeLabel(attribute.getKimAttribute().getComponentName(), attribute.getKimAttribute().getAttributeName() );
268        }
269    
270    
271        private DataDictionaryService getDataDictionaryService() {
272            if(dataDictionaryService == null){
273                dataDictionaryService = KRADServiceLocatorWeb.getDataDictionaryService();
274            }
275            return dataDictionaryService;
276        }
277    
278    
279        private KimTypeInfoService getTypeInfoService() {
280            if(kimTypeInfoService == null){
281                kimTypeInfoService = KimApiServiceLocator.getKimTypeInfoService();
282            }
283            return kimTypeInfoService;
284        }
285    }