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.impl.responsibility;
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.responsibility.ResponsibilityService;
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.role.RoleBo;
26  import org.kuali.rice.kim.impl.role.RoleResponsibilityBo;
27  import org.kuali.rice.kim.inquiry.RoleMemberInquirableImpl;
28  import org.kuali.rice.kim.lookup.RoleLookupableHelperServiceImpl;
29  import org.kuali.rice.kns.lookup.HtmlData;
30  import org.kuali.rice.krad.bo.BusinessObject;
31  import org.kuali.rice.krad.service.KRADServiceLocator;
32  import org.kuali.rice.krad.uif.widget.Inquiry;
33  import org.kuali.rice.krad.util.ObjectUtils;
34  
35  import java.util.ArrayList;
36  import java.util.Collections;
37  import java.util.HashMap;
38  import java.util.List;
39  import java.util.Map;
40  
41  public class ResponsibilityInquirableImpl extends RoleMemberInquirableImpl {
42  
43  	protected final String KIM_RESPONSIBILITY_REQUIRED_ATTRIBUTE_ID = "kimResponsibilityRequiredAttributeId";
44  	protected final String RESPONSIBILITY_ID = "id";
45  	transient private static ResponsibilityService responsibilityService;
46  
47  	@Override
48  	public void buildInquirableLink(Object dataObject, String propertyName, Inquiry inquiry){
49  
50  		if(NAME.equals(propertyName) || NAME_TO_DISPLAY.equals(propertyName)){
51  			Map<String, String> primaryKeys = new HashMap<String, String>();
52  			primaryKeys.put(RESPONSIBILITY_ID, RESPONSIBILITY_ID);
53  			inquiry.buildInquiryLink(dataObject, propertyName, UberResponsibilityBo.class, primaryKeys);
54      	} else if(NAMESPACE_CODE.equals(propertyName) || TEMPLATE_NAMESPACE_CODE.equals(propertyName)){
55      		Map<String, String> primaryKeys = new HashMap<String, String>();
56  			primaryKeys.put(propertyName, "code");
57  			inquiry.buildInquiryLink(dataObject, propertyName, NamespaceBo.class, primaryKeys);
58          } else if(DETAIL_OBJECTS.equals(propertyName)){
59          	//return getAttributesInquiryUrl(businessObject, DETAIL_OBJECTS);
60          	super.buildInquirableLink(dataObject, propertyName, inquiry);
61          } else if(ASSIGNED_TO_ROLES.equals(propertyName)){
62  //        	return getAssignedRoleInquiryUrl(dataObject);
63          	super.buildInquirableLink(dataObject, propertyName, inquiry);
64          }else{
65          	super.buildInquirableLink(dataObject, propertyName, inquiry);
66          }
67  	}
68  
69      @Override
70      public HtmlData getInquiryUrl(BusinessObject businessObject, String attributeName, boolean forceInquiry) {
71      	/*
72      	 *  - responsibility detail values (attribute name and value separated by colon, commas between attributes)
73  		 *	- required role qualifiers (attribute name and value separated by colon, commas between attributes)
74  		 *	- list of roles assigned: role type, role namespace, role name
75      	 */
76      	if(NAME.equals(attributeName) || NAME_TO_DISPLAY.equals(attributeName)){
77  			List<String> primaryKeys = new ArrayList<String>();
78  			primaryKeys.add(RESPONSIBILITY_ID);
79  			return getInquiryUrlForPrimaryKeys(UberResponsibilityBo.class, businessObject, primaryKeys, null);
80      	} else if(NAMESPACE_CODE.equals(attributeName) || TEMPLATE_NAMESPACE_CODE.equals(attributeName)){
81  			List<String> primaryKeys = new ArrayList<String>();
82  			primaryKeys.add("code");
83  			NamespaceBo parameterNamespace = new NamespaceBo();
84  			parameterNamespace.setCode((String)ObjectUtils.getPropertyValue(businessObject, attributeName));
85  			return getInquiryUrlForPrimaryKeys(NamespaceBo.class, parameterNamespace, primaryKeys, null);
86          } else if(DETAIL_OBJECTS.equals(attributeName)){
87          	//return getAttributesInquiryUrl(businessObject, DETAIL_OBJECTS);
88          } else if(ASSIGNED_TO_ROLES.equals(attributeName)){
89          	return getAssignedRoleInquiryUrl(businessObject);
90          }
91  
92          return super.getInquiryUrl(businessObject, attributeName, forceInquiry);
93      }
94  
95      @SuppressWarnings("unchecked")
96  	protected HtmlData getAttributesInquiryUrl(BusinessObject businessObject, String attributeName){
97      	List<ResponsibilityAttributeBo> responsibilityAttributeData =
98      		(List<ResponsibilityAttributeBo>)ObjectUtils.getPropertyValue(businessObject, attributeName);
99      	List<HtmlData.AnchorHtmlData> htmlData = new ArrayList<HtmlData.AnchorHtmlData>();
100 		List<String> primaryKeys = new ArrayList<String>();
101 		primaryKeys.add(ATTRIBUTE_DATA_ID);
102     	for(ResponsibilityAttributeBo responsibilityAttributeDataImpl: responsibilityAttributeData){
103     		htmlData.add(getInquiryUrlForPrimaryKeys(ResponsibilityAttributeBo.class, responsibilityAttributeDataImpl, primaryKeys,
104     			getKimAttributeLabelFromDD(responsibilityAttributeDataImpl.getKimAttribute().getAttributeName())+ KimConstants.KimUIConstants.NAME_VALUE_SEPARATOR+
105     			responsibilityAttributeDataImpl.getAttributeValue()));
106     	}
107     	return new HtmlData.MultipleAnchorHtmlData(htmlData);
108     }
109 
110     @SuppressWarnings("unchecked")
111 	protected HtmlData getAssignedRoleInquiryUrl(BusinessObject businessObject){
112     	UberResponsibilityBo responsibility = (UberResponsibilityBo)businessObject;
113     	List<RoleBo> assignedToRoles = responsibility.getAssignedToRoles();
114     	List<HtmlData.AnchorHtmlData> htmlData = new ArrayList<HtmlData.AnchorHtmlData>();
115 		if(assignedToRoles!=null && !assignedToRoles.isEmpty()){
116 			List<String> primaryKeys = Collections.singletonList(ROLE_ID);
117 			RoleService roleService = KimApiServiceLocator.getRoleService();
118 			for(RoleBo roleImpl: assignedToRoles){
119 				Role role = roleService.getRole(roleImpl.getId());
120 				HtmlData.AnchorHtmlData inquiryHtmlData = getInquiryUrlForPrimaryKeys(RoleBo.class, role, primaryKeys,
121         				role.getNamespaceCode()+" "+
122         				role.getName());
123 				inquiryHtmlData.setHref(RoleLookupableHelperServiceImpl.getCustomRoleInquiryHref(inquiryHtmlData.getHref()));
124         		htmlData.add(inquiryHtmlData);
125         	}
126 		}
127     	return new HtmlData.MultipleAnchorHtmlData(htmlData);
128     }
129 
130     @Override
131 	public Object retrieveDataObject(Map fieldValues){
132     	return getBusinessObject(fieldValues);
133     }
134 
135 	@SuppressWarnings("unchecked")
136 	@Override
137 	public BusinessObject getBusinessObject(Map fieldValues) {
138 		ResponsibilityBo responsibilityImpl
139                 = KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(ResponsibilityBo.class, Collections.singletonMap(
140                 "id", fieldValues.get("id").toString()));
141 		return getResponsibilitiesSearchResultsCopy(responsibilityImpl);
142 	}
143 
144 	public ResponsibilityService getResponsibilityService() {
145 		if (responsibilityService == null ) {
146 			responsibilityService = KimApiServiceLocator.getResponsibilityService();
147 		}
148 		return responsibilityService;
149 	}
150 
151 	@SuppressWarnings("unchecked")
152 	private ResponsibilityBo getResponsibilitiesSearchResultsCopy(ResponsibilityBo responsibilitySearchResult){
153 		UberResponsibilityBo responsibilitySearchResultCopy = new UberResponsibilityBo();
154 		try{
155 			PropertyUtils.copyProperties(responsibilitySearchResultCopy, responsibilitySearchResult);
156 		} catch(Exception ex){
157             throw new RuntimeException(ex);
158 		}
159 		Map<String, String> criteria = new HashMap<String, String>();
160 		criteria.put("responsibilityId", responsibilitySearchResultCopy.getId());
161 		List<RoleResponsibilityBo> roleResponsibilitys =
162 			(List<RoleResponsibilityBo>) KRADServiceLocator.getBusinessObjectService().findMatching(RoleResponsibilityBo.class, criteria);
163 		List<RoleBo> assignedToRoles = new ArrayList<RoleBo>();
164 		for(RoleResponsibilityBo roleResponsibilityImpl: roleResponsibilitys){
165 			assignedToRoles.add(getRoleImpl(roleResponsibilityImpl.getRoleId()));
166 		}
167 		responsibilitySearchResultCopy.setAssignedToRoles(assignedToRoles);
168 		return responsibilitySearchResultCopy;
169 	}
170 
171 }