View Javadoc

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