View Javadoc

1   /*
2    * Copyright 2007-2009 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 responsibilitys 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.kim.bo.impl.ResponsibilityImpl;
26  import org.kuali.rice.kim.bo.impl.RoleImpl;
27  import org.kuali.rice.kim.bo.role.dto.KimRoleInfo;
28  import org.kuali.rice.kim.bo.role.impl.KimResponsibilityImpl;
29  import org.kuali.rice.kim.bo.role.impl.ResponsibilityAttributeDataImpl;
30  import org.kuali.rice.kim.bo.role.impl.RoleResponsibilityImpl;
31  import org.kuali.rice.kim.lookup.RoleLookupableHelperServiceImpl;
32  import org.kuali.rice.kim.service.KIMServiceLocator;
33  import org.kuali.rice.kim.service.ResponsibilityService;
34  import org.kuali.rice.kim.service.RoleService;
35  import org.kuali.rice.kim.util.KimConstants;
36  import org.kuali.rice.kns.bo.BusinessObject;
37  import org.kuali.rice.kns.bo.Namespace;
38  import org.kuali.rice.kns.lookup.HtmlData;
39  import org.kuali.rice.kns.lookup.HtmlData.AnchorHtmlData;
40  import org.kuali.rice.kns.lookup.HtmlData.MultipleAnchorHtmlData;
41  import org.kuali.rice.kns.service.KNSServiceLocator;
42  import org.kuali.rice.kns.util.ObjectUtils;
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  public class ResponsibilityInquirableImpl extends RoleMemberInquirableImpl {
51  
52  	protected final String KIM_RESPONSIBILITY_REQUIRED_ATTRIBUTE_ID = "kimResponsibilityRequiredAttributeId";
53  	protected final String RESPONSIBILITY_ID = "responsibilityId";
54  	transient private static ResponsibilityService responsibilityService;
55  	
56      /**
57       * @see org.kuali.kfs.sys.businessobject.inquiry.KfsInquirableImpl#getInquiryUrl(org.kuali.rice.kns.bo.BusinessObject,
58       *      java.lang.String, boolean)
59       */
60      @Override
61      public HtmlData getInquiryUrl(BusinessObject businessObject, String attributeName, boolean forceInquiry) {
62      	/*
63      	 *  - responsibility detail values (attribute name and value separated by colon, commas between attributes)
64  		 *	- required role qualifiers (attribute name and value separated by colon, commas between attributes)
65  		 *	- list of roles assigned: role type, role namespace, role name
66      	 */
67      	if(NAME.equals(attributeName) || NAME_TO_DISPLAY.equals(attributeName)){
68  			List<String> primaryKeys = new ArrayList<String>();
69  			primaryKeys.add(RESPONSIBILITY_ID);
70  			return getInquiryUrlForPrimaryKeys(ResponsibilityImpl.class, businessObject, primaryKeys, null);
71      	} else if(NAMESPACE_CODE.equals(attributeName) || TEMPLATE_NAMESPACE_CODE.equals(attributeName)){
72  			List<String> primaryKeys = new ArrayList<String>();
73  			primaryKeys.add("code");
74  			Namespace parameterNamespace = new Namespace();
75  			parameterNamespace.setCode((String)ObjectUtils.getPropertyValue(businessObject, attributeName));
76  			return getInquiryUrlForPrimaryKeys(Namespace.class, parameterNamespace, primaryKeys, null);
77          } else if(DETAIL_OBJECTS.equals(attributeName)){
78          	//return getAttributesInquiryUrl(businessObject, DETAIL_OBJECTS);
79          } else if(ASSIGNED_TO_ROLES.equals(attributeName)){
80          	return getAssignedRoleInquiryUrl(businessObject);
81          }
82  		
83          return super.getInquiryUrl(businessObject, attributeName, forceInquiry);
84      }
85  
86      @SuppressWarnings("unchecked")
87  	protected HtmlData getAttributesInquiryUrl(BusinessObject businessObject, String attributeName){
88      	List<ResponsibilityAttributeDataImpl> responsibilityAttributeData = 
89      		(List<ResponsibilityAttributeDataImpl>)ObjectUtils.getPropertyValue(businessObject, attributeName);
90      	List<AnchorHtmlData> htmlData = new ArrayList<AnchorHtmlData>();
91  		List<String> primaryKeys = new ArrayList<String>();
92  		primaryKeys.add(ATTRIBUTE_DATA_ID);
93      	for(ResponsibilityAttributeDataImpl responsibilityAttributeDataImpl: responsibilityAttributeData){
94      		htmlData.add(getInquiryUrlForPrimaryKeys(ResponsibilityAttributeDataImpl.class, responsibilityAttributeDataImpl, primaryKeys, 
95      			getKimAttributeLabelFromDD(responsibilityAttributeDataImpl.getKimAttribute().getAttributeName())+KimConstants.KimUIConstants.NAME_VALUE_SEPARATOR+
96      			responsibilityAttributeDataImpl.getAttributeValue()));
97      	}
98      	return new MultipleAnchorHtmlData(htmlData);
99      }
100 
101     @SuppressWarnings("unchecked")
102 	protected HtmlData getAssignedRoleInquiryUrl(BusinessObject businessObject){
103     	ResponsibilityImpl responsibility = (ResponsibilityImpl)businessObject;
104     	List<RoleImpl> assignedToRoles = responsibility.getAssignedToRoles();
105     	List<AnchorHtmlData> htmlData = new ArrayList<AnchorHtmlData>();
106 		if(assignedToRoles!=null && !assignedToRoles.isEmpty()){
107 			List<String> primaryKeys = Collections.singletonList(ROLE_ID);
108 			RoleService roleService = KIMServiceLocator.getRoleService();
109 			for(RoleImpl roleImpl: assignedToRoles){
110 				KimRoleInfo roleInfo = roleService.getRole(roleImpl.getRoleId());
111 				AnchorHtmlData inquiryHtmlData = getInquiryUrlForPrimaryKeys(RoleImpl.class, roleInfo, primaryKeys, 
112         				roleInfo.getNamespaceCode()+" "+
113         				roleInfo.getRoleName());
114 				inquiryHtmlData.setHref(RoleLookupableHelperServiceImpl.getCustomRoleInquiryHref(inquiryHtmlData.getHref()));
115         		htmlData.add(inquiryHtmlData);
116         	}
117 		}
118     	return new MultipleAnchorHtmlData(htmlData);
119     }
120 
121 	/**
122 	 * This overridden method ...
123 	 * 
124 	 * @see org.kuali.rice.kns.inquiry.KualiInquirableImpl#getBusinessObject(java.util.Map)
125 	 */
126 	@SuppressWarnings("unchecked")
127 	@Override
128 	public BusinessObject getBusinessObject(Map fieldValues) {
129 		Map<String, String> criteria = new HashMap<String, String>();
130 		criteria.put("responsibilityId", fieldValues.get("responsibilityId").toString());
131 		KimResponsibilityImpl responsibilityImpl = (KimResponsibilityImpl)KNSServiceLocator.getBusinessObjectService().findByPrimaryKey(KimResponsibilityImpl.class, criteria);
132 		return getResponsibilitiesSearchResultsCopy(responsibilityImpl);
133 	}
134 
135 	public ResponsibilityService getResponsibilityService() {
136 		if (responsibilityService == null ) {
137 			responsibilityService = KIMServiceLocator.getResponsibilityService();
138 		}
139 		return responsibilityService;
140 	}
141 	
142 	@SuppressWarnings("unchecked")
143 	private ResponsibilityImpl getResponsibilitiesSearchResultsCopy(KimResponsibilityImpl responsibilitySearchResult){
144 		ResponsibilityImpl responsibilitySearchResultCopy = new ResponsibilityImpl();
145 		try{
146 			PropertyUtils.copyProperties(responsibilitySearchResultCopy, responsibilitySearchResult);
147 		} catch(Exception ex){
148 			//TODO: remove this
149 			ex.printStackTrace();
150 		}
151 		Map<String, String> criteria = new HashMap<String, String>();
152 		criteria.put("responsibilityId", responsibilitySearchResultCopy.getResponsibilityId());
153 		List<RoleResponsibilityImpl> roleResponsibilitys = 
154 			(List<RoleResponsibilityImpl>)KNSServiceLocator.getBusinessObjectService().findMatching(RoleResponsibilityImpl.class, criteria);
155 		List<RoleImpl> assignedToRoles = new ArrayList<RoleImpl>();
156 		for(RoleResponsibilityImpl roleResponsibilityImpl: roleResponsibilitys){
157 			assignedToRoles.add(getRoleImpl(roleResponsibilityImpl.getRoleId()));
158 		}
159 		responsibilitySearchResultCopy.setAssignedToRoles(assignedToRoles);
160 		return responsibilitySearchResultCopy;
161 	}
162 
163 }