001    /**
002     * Copyright 2005-2013 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.inquiry;
017    
018    import org.apache.commons.beanutils.PropertyUtils;
019    import org.kuali.rice.coreservice.impl.namespace.NamespaceBo;
020    import org.kuali.rice.kim.api.KimConstants;
021    import org.kuali.rice.kim.api.permission.PermissionService;
022    import org.kuali.rice.kim.api.role.Role;
023    import org.kuali.rice.kim.api.role.RoleService;
024    import org.kuali.rice.kim.api.services.KimApiServiceLocator;
025    import org.kuali.rice.kim.impl.permission.PermissionAttributeBo;
026    import org.kuali.rice.kim.impl.permission.PermissionBo;
027    import org.kuali.rice.kim.impl.permission.UberPermissionBo;
028    import org.kuali.rice.kim.impl.role.RoleBo;
029    import org.kuali.rice.kim.impl.role.RolePermissionBo;
030    import org.kuali.rice.kim.lookup.RoleLookupableHelperServiceImpl;
031    import org.kuali.rice.kns.lookup.HtmlData;
032    import org.kuali.rice.krad.bo.BusinessObject;
033    import org.kuali.rice.krad.service.KRADServiceLocator;
034    import org.kuali.rice.krad.uif.widget.Inquiry;
035    import org.kuali.rice.krad.util.ObjectUtils;
036    
037    import java.util.ArrayList;
038    import java.util.HashMap;
039    import java.util.List;
040    import java.util.Map;
041    
042    /**
043     * This is a description of what this class does - bhargavp don't forget to fill this in. 
044     * 
045     * @author Kuali Rice Team (rice.collab@kuali.org)
046     *
047     */
048    public class PermissionInquirableImpl extends RoleMemberInquirableImpl {
049    
050            transient private static PermissionService permissionService;
051    
052            @Override
053            public void buildInquirableLink(Object dataObject, String propertyName, Inquiry inquiry){
054                    
055                    if(NAME.equals(propertyName) || NAME_TO_DISPLAY.equals(propertyName)){
056                            Map<String, String> primaryKeys = new HashMap<String, String>();
057                            primaryKeys.put(KimConstants.PrimaryKeyConstants.PERMISSION_ID, KimConstants.PrimaryKeyConstants.PERMISSION_ID);
058                            inquiry.buildInquiryLink(dataObject, propertyName, UberPermissionBo.class, primaryKeys);
059                    } else if(NAMESPACE_CODE.equals(propertyName) || TEMPLATE_NAMESPACE_CODE.equals(propertyName)){
060                            Map<String, String> primaryKeys = new HashMap<String, String>();
061                            primaryKeys.put(propertyName, "code");
062                            inquiry.buildInquiryLink(dataObject, propertyName, NamespaceBo.class, primaryKeys);
063            } else if(DETAIL_OBJECTS.equals(propertyName)){
064                    //return getAttributesInquiryUrl(businessObject, DETAIL_OBJECTS);
065                    super.buildInquirableLink(dataObject, propertyName, inquiry);
066            } else if(ASSIGNED_TO_ROLES.equals(propertyName)){
067    //              return getAssignedRoleInquiryUrl(businessObject);
068                    super.buildInquirableLink(dataObject, propertyName, inquiry);
069            }else{
070                    super.buildInquirableLink(dataObject, propertyName, inquiry);
071            }
072            }
073            
074        @Override
075        public HtmlData getInquiryUrl(BusinessObject businessObject, String attributeName, boolean forceInquiry) {
076            /*
077             *  - permission detail values (attribute name and value separated by colon, commas between attributes)
078                     *      - required role qualifiers (attribute name and value separated by colon, commas between attributes)
079                     *      - list of roles assigned: role type, role namespace, role name
080             */
081                    if(NAME.equals(attributeName) || NAME_TO_DISPLAY.equals(attributeName)){
082                            List<String> primaryKeys = new ArrayList<String>();
083                            primaryKeys.add(KimConstants.PrimaryKeyConstants.PERMISSION_ID);
084                            return getInquiryUrlForPrimaryKeys(UberPermissionBo.class, businessObject, primaryKeys, null);
085                    } else if(NAMESPACE_CODE.equals(attributeName) || TEMPLATE_NAMESPACE_CODE.equals(attributeName)){
086                            List<String> primaryKeys = new ArrayList<String>();
087                            primaryKeys.add("code");
088                            NamespaceBo parameterNamespace = new NamespaceBo();
089                            parameterNamespace.setCode((String)ObjectUtils.getPropertyValue(businessObject, attributeName));
090                            return getInquiryUrlForPrimaryKeys(NamespaceBo.class, parameterNamespace, primaryKeys, null);
091            } else if(DETAIL_OBJECTS.equals(attributeName)){
092                    //return getAttributesInquiryUrl(businessObject, DETAIL_OBJECTS);
093            } else if(ASSIGNED_TO_ROLES.equals(attributeName)){
094                    return getAssignedRoleInquiryUrl(businessObject);
095            }
096                    
097            return super.getInquiryUrl(businessObject, attributeName, forceInquiry);
098        }
099    
100        protected HtmlData getAttributesInquiryUrl(BusinessObject businessObject, String attributeName){
101            List<PermissionAttributeBo> permissionAttributeData =
102                    (List<PermissionAttributeBo>)ObjectUtils.getPropertyValue(businessObject, attributeName);
103            List<HtmlData.AnchorHtmlData> htmlData = new ArrayList<HtmlData.AnchorHtmlData>();
104                    List<String> primaryKeys = new ArrayList<String>();
105                    primaryKeys.add(ATTRIBUTE_DATA_ID);
106            for(PermissionAttributeBo permissionAttributeDataImpl: permissionAttributeData){
107                    htmlData.add(getInquiryUrlForPrimaryKeys(PermissionAttributeBo.class, permissionAttributeDataImpl, primaryKeys,
108                            getKimAttributeLabelFromDD(permissionAttributeDataImpl.getKimAttribute().getAttributeName())+
109                            KimConstants.KimUIConstants.NAME_VALUE_SEPARATOR+
110                            permissionAttributeDataImpl.getAttributeValue()));
111            }
112            return new HtmlData.MultipleAnchorHtmlData(htmlData);
113        }
114    
115        protected HtmlData getAssignedRoleInquiryUrl(BusinessObject businessObject){
116            UberPermissionBo permission = (UberPermissionBo)businessObject;
117            List<RoleBo> assignedToRoles = permission.getAssignedToRoles();
118            List<HtmlData.AnchorHtmlData> htmlData = new ArrayList<HtmlData.AnchorHtmlData>();
119                    List<String> primaryKeys = new ArrayList<String>();
120                    primaryKeys.add(ROLE_ID);
121                    if(assignedToRoles!=null && !assignedToRoles.isEmpty()){
122                            RoleService roleService = KimApiServiceLocator.getRoleService();
123                            for(RoleBo roleImpl: assignedToRoles){
124                                    Role roleInfo = roleService.getRole(roleImpl.getId());
125                                    HtmlData.AnchorHtmlData inquiryHtmlData = getInquiryUrlForPrimaryKeys(RoleBo.class, roleInfo, primaryKeys,
126                                            roleInfo.getNamespaceCode()+ " " + roleInfo.getName());
127                                    inquiryHtmlData.setHref(RoleLookupableHelperServiceImpl.getCustomRoleInquiryHref(inquiryHtmlData.getHref()));
128                                    inquiryHtmlData.setTarget(HtmlData.AnchorHtmlData.TARGET_BLANK);
129                            htmlData.add(inquiryHtmlData);
130                    }
131                    }
132            return new HtmlData.MultipleAnchorHtmlData(htmlData);
133        }
134    
135            @Override
136            public Object retrieveDataObject(Map fieldValues){
137            return getBusinessObject(fieldValues);
138        }
139        
140            /**
141             * This overridden method ...
142             * 
143             * @see org.kuali.rice.krad.inquiry.KualiInquirableImpl#getBusinessObject(java.util.Map)
144             */
145            @SuppressWarnings("unchecked")
146            @Override
147            public BusinessObject getBusinessObject(Map fieldValues) {
148                    Map<String, String> criteria = new HashMap<String, String>();
149                    criteria.put("id", fieldValues.get("id").toString());
150                    PermissionBo permissionBo = KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(PermissionBo.class, criteria);
151                    return getPermissionsSearchResultsCopy(permissionBo);
152            }
153    
154            public PermissionService getPermissionService() {
155                    if (permissionService == null ) {
156                            permissionService = KimApiServiceLocator.getPermissionService();
157                    }
158                    return permissionService;
159            }
160            
161            private PermissionBo getPermissionsSearchResultsCopy(PermissionBo permissionSearchResult){
162                    UberPermissionBo permissionSearchResultCopy = new UberPermissionBo();
163                    try{
164                            PropertyUtils.copyProperties(permissionSearchResultCopy, permissionSearchResult);
165                    } catch(Exception ex){
166                            throw new RuntimeException(ex);
167                    }
168                    Map<String, String> criteria = new HashMap<String, String>();
169                    criteria.put("permissionId", permissionSearchResultCopy.getId());
170                    List<RolePermissionBo> rolePermissions =
171                            (List<RolePermissionBo>) KRADServiceLocator.getBusinessObjectService().findMatching(RolePermissionBo.class, criteria);
172                    List<RoleBo> assignedToRoles = new ArrayList<RoleBo>();
173                    for(RolePermissionBo rolePermissionImpl: rolePermissions){
174                            assignedToRoles.add(getRoleImpl(rolePermissionImpl.getRoleId()));
175                    }
176                    permissionSearchResultCopy.setAssignedToRoles(assignedToRoles);
177                    return permissionSearchResultCopy;
178            }
179    
180    }