View Javadoc

1   /**
2    * Copyright 2005-2014 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 org.apache.commons.beanutils.PropertyUtils;
19  import org.kuali.rice.coreservice.impl.namespace.NamespaceBo;
20  import org.kuali.rice.kim.api.KimConstants;
21  import org.kuali.rice.kim.api.permission.PermissionService;
22  import org.kuali.rice.kim.api.role.Role;
23  import org.kuali.rice.kim.api.role.RoleService;
24  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
25  import org.kuali.rice.kim.impl.permission.PermissionAttributeBo;
26  import org.kuali.rice.kim.impl.permission.PermissionBo;
27  import org.kuali.rice.kim.impl.permission.UberPermissionBo;
28  import org.kuali.rice.kim.impl.role.RoleBo;
29  import org.kuali.rice.kim.impl.role.RolePermissionBo;
30  import org.kuali.rice.kim.lookup.RoleLookupableHelperServiceImpl;
31  import org.kuali.rice.kns.lookup.HtmlData;
32  import org.kuali.rice.krad.bo.BusinessObject;
33  import org.kuali.rice.krad.service.KRADServiceLocator;
34  import org.kuali.rice.krad.uif.widget.Inquiry;
35  import org.kuali.rice.krad.util.ObjectUtils;
36  
37  import java.util.ArrayList;
38  import java.util.HashMap;
39  import java.util.List;
40  import java.util.Map;
41  
42  /**
43   * This is a description of what this class does - bhargavp don't forget to fill this in. 
44   * 
45   * @author Kuali Rice Team (rice.collab@kuali.org)
46   *
47   */
48  public class PermissionInquirableImpl extends RoleMemberInquirableImpl {
49  
50  	transient private static PermissionService permissionService;
51  
52  	@Override
53  	public void buildInquirableLink(Object dataObject, String propertyName, Inquiry inquiry){
54  		
55  		if(NAME.equals(propertyName) || NAME_TO_DISPLAY.equals(propertyName)){
56  			Map<String, String> primaryKeys = new HashMap<String, String>();
57  			primaryKeys.put(KimConstants.PrimaryKeyConstants.PERMISSION_ID, KimConstants.PrimaryKeyConstants.PERMISSION_ID);
58  			inquiry.buildInquiryLink(dataObject, propertyName, UberPermissionBo.class, primaryKeys);
59  		} else if(NAMESPACE_CODE.equals(propertyName) || TEMPLATE_NAMESPACE_CODE.equals(propertyName)){
60  			Map<String, String> primaryKeys = new HashMap<String, String>();
61  			primaryKeys.put(propertyName, "code");
62  			inquiry.buildInquiryLink(dataObject, propertyName, NamespaceBo.class, primaryKeys);
63          } else if(DETAIL_OBJECTS.equals(propertyName)){
64          	//return getAttributesInquiryUrl(businessObject, DETAIL_OBJECTS);
65          	super.buildInquirableLink(dataObject, propertyName, inquiry);
66          } else if(ASSIGNED_TO_ROLES.equals(propertyName)){
67  //        	return getAssignedRoleInquiryUrl(businessObject);
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  			List<String> primaryKeys = new ArrayList<String>();
83  			primaryKeys.add(KimConstants.PrimaryKeyConstants.PERMISSION_ID);
84  			return getInquiryUrlForPrimaryKeys(UberPermissionBo.class, businessObject, primaryKeys, null);
85  		} else if(NAMESPACE_CODE.equals(attributeName) || TEMPLATE_NAMESPACE_CODE.equals(attributeName)){
86  			List<String> primaryKeys = new ArrayList<String>();
87  			primaryKeys.add("code");
88  			NamespaceBo parameterNamespace = new NamespaceBo();
89  			parameterNamespace.setCode((String)ObjectUtils.getPropertyValue(businessObject, attributeName));
90  			return getInquiryUrlForPrimaryKeys(NamespaceBo.class, parameterNamespace, primaryKeys, null);
91          } else if(DETAIL_OBJECTS.equals(attributeName)){
92          	//return getAttributesInquiryUrl(businessObject, DETAIL_OBJECTS);
93          } else if(ASSIGNED_TO_ROLES.equals(attributeName)){
94          	return getAssignedRoleInquiryUrl(businessObject);
95          }
96  		
97          return super.getInquiryUrl(businessObject, attributeName, forceInquiry);
98      }
99  
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 }