View Javadoc
1   /**
2    * Copyright 2005-2015 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.inquiry;
17  
18  import java.util.ArrayList;
19  import java.util.Collections;
20  import java.util.HashMap;
21  import java.util.List;
22  import java.util.Map;
23  
24  import org.apache.commons.beanutils.PropertyUtils;
25  import org.kuali.rice.core.api.criteria.QueryByCriteria;
26  import org.kuali.rice.coreservice.impl.namespace.NamespaceBo;
27  import org.kuali.rice.kim.api.KimConstants;
28  import org.kuali.rice.kim.api.role.Role;
29  import org.kuali.rice.kim.api.role.RoleService;
30  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
31  import org.kuali.rice.kim.impl.permission.PermissionAttributeBo;
32  import org.kuali.rice.kim.impl.permission.PermissionBo;
33  import org.kuali.rice.kim.impl.permission.UberPermissionBo;
34  import org.kuali.rice.kim.impl.role.RoleBo;
35  import org.kuali.rice.kim.impl.role.RolePermissionBo;
36  import org.kuali.rice.kim.lookup.RoleLookupableHelperServiceImpl;
37  import org.kuali.rice.kns.lookup.HtmlData;
38  import org.kuali.rice.krad.bo.BusinessObject;
39  import org.kuali.rice.krad.data.DataObjectWrapper;
40  import org.kuali.rice.krad.data.KradDataServiceLocator;
41  import org.kuali.rice.krad.uif.widget.Inquiry;
42  import org.kuali.rice.krad.util.KRADPropertyConstants;
43  
44  /**
45   * This is a description of what this class does - bhargavp don't forget to fill this in.
46   *
47   * @author Kuali Rice Team (rice.collab@kuali.org)
48   *
49   */
50  @SuppressWarnings({"unchecked", "deprecation"})
51  public class PermissionInquirableImpl extends KimInquirableImpl {
52      private static final long serialVersionUID = 1L;
53  
54  	@Override
55  	public void buildInquirableLink(Object dataObject, String propertyName, Inquiry inquiry){
56  
57  		if(NAME.equals(propertyName) || NAME_TO_DISPLAY.equals(propertyName)){
58  			Map<String, String> primaryKeys = new HashMap<String, String>();
59  			primaryKeys.put(KimConstants.PrimaryKeyConstants.PERMISSION_ID, KimConstants.PrimaryKeyConstants.PERMISSION_ID);
60  			inquiry.buildInquiryLink(dataObject, propertyName, UberPermissionBo.class, primaryKeys);
61  		} else if(NAMESPACE_CODE.equals(propertyName) || TEMPLATE_NAMESPACE_CODE.equals(propertyName)){
62  			Map<String, String> primaryKeys = new HashMap<String, String>();
63  			primaryKeys.put(propertyName, "code");
64  			inquiry.buildInquiryLink(dataObject, propertyName, NamespaceBo.class, primaryKeys);
65          } else if(DETAIL_OBJECTS.equals(propertyName)){
66          	super.buildInquirableLink(dataObject, propertyName, inquiry);
67          } else if(ASSIGNED_TO_ROLES.equals(propertyName)){
68          	super.buildInquirableLink(dataObject, propertyName, inquiry);
69          }else{
70          	super.buildInquirableLink(dataObject, propertyName, inquiry);
71          }
72  	}
73  
74      @Override
75      public HtmlData getInquiryUrl(BusinessObject businessObject, String attributeName, boolean forceInquiry) {
76      	/*
77      	 *  - permission detail values (attribute name and value separated by colon, commas between attributes)
78  		 *	- required role qualifiers (attribute name and value separated by colon, commas between attributes)
79  		 *	- list of roles assigned: role type, role namespace, role name
80      	 */
81  		if(NAME.equals(attributeName) || NAME_TO_DISPLAY.equals(attributeName)){
82  			return getInquiryUrlForPrimaryKeys(UberPermissionBo.class, businessObject, Collections.singletonList(KimConstants.PrimaryKeyConstants.PERMISSION_ID), null);
83  		} else if(NAMESPACE_CODE.equals(attributeName) || TEMPLATE_NAMESPACE_CODE.equals(attributeName)){
84  			NamespaceBo parameterNamespace = new NamespaceBo();
85              String code = (String) KradDataServiceLocator.getDataObjectService().wrap(businessObject).getPropertyValueNullSafe(attributeName);
86  			parameterNamespace.setCode(code);
87  			return getInquiryUrlForPrimaryKeys(NamespaceBo.class, parameterNamespace, Collections.singletonList(KRADPropertyConstants.CODE), null);
88          } else if(DETAIL_OBJECTS.equals(attributeName)){
89          	//return getAttributesInquiryUrl(businessObject, DETAIL_OBJECTS);
90          } else if(ASSIGNED_TO_ROLES.equals(attributeName)){
91          	return getAssignedRoleInquiryUrl(businessObject);
92          }
93  
94          return super.getInquiryUrl(businessObject, attributeName, forceInquiry);
95      }
96  
97      protected HtmlData getAttributesInquiryUrl(BusinessObject businessObject, String attributeName){
98          DataObjectWrapper<BusinessObject> wrapper = KradDataServiceLocator.getDataObjectService().wrap(businessObject);
99          List<PermissionAttributeBo> permissionAttributeData =
100     		(List<PermissionAttributeBo>) wrapper.getPropertyValueNullSafe(attributeName);
101     	List<HtmlData.AnchorHtmlData> htmlData = new ArrayList<HtmlData.AnchorHtmlData>();
102 		List<String> primaryKeys = new ArrayList<String>();
103 		primaryKeys.add(ATTRIBUTE_DATA_ID);
104     	for(PermissionAttributeBo permissionAttributeDataImpl: permissionAttributeData){
105     		htmlData.add(getInquiryUrlForPrimaryKeys(PermissionAttributeBo.class, permissionAttributeDataImpl, primaryKeys,
106     			getKimAttributeLabelFromDD(permissionAttributeDataImpl.getKimAttribute().getAttributeName())+
107     			KimConstants.KimUIConstants.NAME_VALUE_SEPARATOR+
108     			permissionAttributeDataImpl.getAttributeValue()));
109     	}
110     	return new HtmlData.MultipleAnchorHtmlData(htmlData);
111     }
112 
113     protected HtmlData getAssignedRoleInquiryUrl(BusinessObject businessObject){
114     	UberPermissionBo permission = (UberPermissionBo)businessObject;
115     	List<RoleBo> assignedToRoles = permission.getAssignedToRoles();
116     	List<HtmlData.AnchorHtmlData> htmlData = new ArrayList<HtmlData.AnchorHtmlData>();
117 		if(assignedToRoles!=null && !assignedToRoles.isEmpty()){
118 			RoleService roleService = KimApiServiceLocator.getRoleService();
119 			for(RoleBo roleImpl: assignedToRoles){
120 				Role roleInfo = roleService.getRole(roleImpl.getId());
121 				HtmlData.AnchorHtmlData inquiryHtmlData = getInquiryUrlForPrimaryKeys(RoleBo.class, roleInfo, Collections.singletonList(ID),
122         				roleInfo.getNamespaceCode()+ " " + roleInfo.getName());
123 				inquiryHtmlData.setHref(RoleLookupableHelperServiceImpl.getCustomRoleInquiryHref(inquiryHtmlData.getHref()));
124 				inquiryHtmlData.setTarget(HtmlData.AnchorHtmlData.TARGET_BLANK);
125         		htmlData.add(inquiryHtmlData);
126         	}
127 		}
128     	return new HtmlData.MultipleAnchorHtmlData(htmlData);
129     }
130 
131 	@Override
132 	public Object retrieveDataObject(@SuppressWarnings("rawtypes") Map fieldValues){
133         if ( fieldValues.get(ID) == null ) {
134             return null;
135         }
136         PermissionBo permissionBo = getDataObjectService().find(PermissionBo.class, fieldValues.get(ID).toString());
137         return getPermissionsSearchResultsCopy(permissionBo);
138     }
139 
140 	private PermissionBo getPermissionsSearchResultsCopy(PermissionBo permissionSearchResult){
141 		UberPermissionBo permissionSearchResultCopy = new UberPermissionBo();
142 		try{
143 			PropertyUtils.copyProperties(permissionSearchResultCopy, permissionSearchResult);
144 		} catch(Exception ex){
145 			throw new RuntimeException(ex);
146 		}
147 		List<RolePermissionBo> rolePermissions = getDataObjectService().findMatching(RolePermissionBo.class, QueryByCriteria.Builder.forAttribute("permissionId", permissionSearchResultCopy.getId()).build() ).getResults();
148 		List<RoleBo> assignedToRoles = new ArrayList<RoleBo>();
149 		for(RolePermissionBo rolePermissionImpl: rolePermissions){
150             if (rolePermissionImpl.isActive())  {
151 			    assignedToRoles.add( getDataObjectService().find(RoleBo.class, rolePermissionImpl.getRoleId()) );
152             }
153 		}
154 		permissionSearchResultCopy.setAssignedToRoles(assignedToRoles);
155 		return permissionSearchResultCopy;
156 	}
157 
158 }