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