1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
69
70
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
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 }