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 org.apache.commons.beanutils.PropertyUtils; |
19 | |
import org.apache.commons.lang.StringUtils; |
20 | |
import org.apache.log4j.Logger; |
21 | |
import org.kuali.rice.core.api.membership.MemberType; |
22 | |
import org.kuali.rice.kew.api.KewApiConstants; |
23 | |
import org.kuali.rice.kim.api.KimConstants; |
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.lookup.RoleMemberLookupableHelperServiceImpl; |
28 | |
import org.kuali.rice.kns.lookup.HtmlData; |
29 | |
import org.kuali.rice.krad.bo.BusinessObject; |
30 | |
import org.kuali.rice.krad.lookup.CollectionIncomplete; |
31 | |
import org.kuali.rice.krad.service.KRADServiceLocatorWeb; |
32 | |
import org.kuali.rice.krad.service.LookupService; |
33 | |
import org.kuali.rice.krad.util.KRADConstants; |
34 | |
import org.kuali.rice.krad.util.UrlFactory; |
35 | |
|
36 | |
import java.lang.reflect.InvocationTargetException; |
37 | |
import java.util.ArrayList; |
38 | |
import java.util.HashMap; |
39 | |
import java.util.List; |
40 | |
import java.util.Map; |
41 | |
import java.util.Properties; |
42 | |
|
43 | 0 | public class ResponsibilityLookupableHelperServiceImpl extends RoleMemberLookupableHelperServiceImpl { |
44 | |
|
45 | 0 | private static final Logger LOG = Logger.getLogger( ResponsibilityLookupableHelperServiceImpl.class ); |
46 | |
|
47 | |
private static final long serialVersionUID = -2882500971924192124L; |
48 | |
|
49 | |
private static LookupService lookupService; |
50 | |
|
51 | 0 | private static boolean reviewResponsibilityDocumentTypeNameLoaded = false; |
52 | 0 | private static String reviewResponsibilityDocumentTypeName = null; |
53 | |
|
54 | |
@SuppressWarnings("unchecked") |
55 | |
@Override |
56 | |
public List<HtmlData> getCustomActionUrls(BusinessObject businessObject, List pkNames) { |
57 | 0 | List<HtmlData> htmlDataList = new ArrayList<HtmlData>(); |
58 | |
|
59 | 0 | if ( (((UberResponsibilityBo)businessObject).getTemplate() != null) |
60 | |
&& ((UberResponsibilityBo)businessObject).getTemplate().getName().equals( KewApiConstants.DEFAULT_RESPONSIBILITY_TEMPLATE_NAME ) ) { |
61 | 0 | ReviewResponsibilityBo reviewResp = new ReviewResponsibilityBo( (UberResponsibilityBo)businessObject ); |
62 | 0 | businessObject = reviewResp; |
63 | 0 | if (allowsMaintenanceEditAction(businessObject)) { |
64 | 0 | htmlDataList.add(getUrlData(businessObject, KRADConstants.MAINTENANCE_EDIT_METHOD_TO_CALL, pkNames)); |
65 | |
} |
66 | 0 | if (allowsMaintenanceNewOrCopyAction()) { |
67 | 0 | htmlDataList.add(getUrlData(businessObject, KRADConstants.MAINTENANCE_COPY_METHOD_TO_CALL, pkNames)); |
68 | |
} |
69 | |
} |
70 | 0 | return htmlDataList; |
71 | |
} |
72 | |
|
73 | |
@SuppressWarnings("unchecked") |
74 | |
protected String getActionUrlHref(BusinessObject businessObject, String methodToCall, List pkNames){ |
75 | 0 | Properties parameters = new Properties(); |
76 | 0 | parameters.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, methodToCall); |
77 | |
|
78 | 0 | parameters.put(KRADConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, businessObject.getClass().getName()); |
79 | 0 | parameters.put(KRADConstants.OVERRIDE_KEYS, KimConstants.PrimaryKeyConstants.RESPONSIBILITY_ID); |
80 | 0 | parameters.put(KRADConstants.COPY_KEYS, KimConstants.PrimaryKeyConstants.RESPONSIBILITY_ID); |
81 | 0 | if (StringUtils.isNotBlank(getReturnLocation())) { |
82 | 0 | parameters.put(KRADConstants.RETURN_LOCATION_PARAMETER, getReturnLocation()); |
83 | |
} |
84 | 0 | parameters.putAll(getParametersFromPrimaryKey(businessObject, pkNames)); |
85 | 0 | return UrlFactory.parameterizeUrl(KRADConstants.MAINTENANCE_ACTION, parameters); |
86 | |
} |
87 | |
|
88 | |
@Override |
89 | |
protected String getMaintenanceDocumentTypeName() { |
90 | 0 | if ( !reviewResponsibilityDocumentTypeNameLoaded ) { |
91 | 0 | reviewResponsibilityDocumentTypeName = getMaintenanceDocumentDictionaryService().getDocumentTypeName(ReviewResponsibilityBo.class); |
92 | 0 | reviewResponsibilityDocumentTypeNameLoaded = true; |
93 | |
} |
94 | 0 | return reviewResponsibilityDocumentTypeName; |
95 | |
} |
96 | |
|
97 | |
@Override |
98 | |
protected List<? extends BusinessObject> getMemberSearchResults(Map<String, String> searchCriteria, boolean unbounded) { |
99 | 0 | Map<String, String> responsibilitySearchCriteria = buildSearchCriteria(searchCriteria); |
100 | 0 | Map<String, String> roleSearchCriteria = buildRoleSearchCriteria(searchCriteria); |
101 | 0 | boolean responsibilityCriteriaEmpty = responsibilitySearchCriteria==null || responsibilitySearchCriteria.isEmpty(); |
102 | 0 | boolean roleCriteriaEmpty = roleSearchCriteria==null || roleSearchCriteria.isEmpty(); |
103 | |
|
104 | 0 | List<UberResponsibilityBo> responsibilitySearchResultsCopy = new CollectionIncomplete<UberResponsibilityBo>(new ArrayList<UberResponsibilityBo>(), new Long(0)); |
105 | 0 | if(!responsibilityCriteriaEmpty && !roleCriteriaEmpty){ |
106 | 0 | responsibilitySearchResultsCopy = getCombinedSearchResults(responsibilitySearchCriteria, roleSearchCriteria, unbounded); |
107 | 0 | } else if(responsibilityCriteriaEmpty && !roleCriteriaEmpty){ |
108 | 0 | responsibilitySearchResultsCopy = getResponsibilitiesWithRoleSearchCriteria(roleSearchCriteria, unbounded); |
109 | 0 | } else if(!responsibilityCriteriaEmpty && roleCriteriaEmpty){ |
110 | 0 | responsibilitySearchResultsCopy = getResponsibilitiesWithResponsibilitySearchCriteria(responsibilitySearchCriteria, unbounded); |
111 | 0 | } else if(responsibilityCriteriaEmpty && roleCriteriaEmpty){ |
112 | 0 | return getAllResponsibilities(unbounded); |
113 | |
} |
114 | 0 | return responsibilitySearchResultsCopy; |
115 | |
} |
116 | |
|
117 | |
private List<UberResponsibilityBo> getAllResponsibilities(boolean unbounded){ |
118 | 0 | List<UberResponsibilityBo> responsibilities = searchResponsibilities(new HashMap<String, String>(), unbounded); |
119 | 0 | for(UberResponsibilityBo responsibility: responsibilities) { |
120 | 0 | populateAssignedToRoles(responsibility); |
121 | |
} |
122 | 0 | return responsibilities; |
123 | |
} |
124 | |
|
125 | |
private List<UberResponsibilityBo> getCombinedSearchResults( |
126 | |
Map<String, String> responsibilitySearchCriteria, Map<String, String> roleSearchCriteria, boolean unbounded){ |
127 | 0 | List<UberResponsibilityBo> responsibilitySearchResults = searchResponsibilities(responsibilitySearchCriteria, unbounded); |
128 | 0 | List<RoleBo> roleSearchResults = searchRoles(roleSearchCriteria, unbounded); |
129 | 0 | List<UberResponsibilityBo> responsibilitiesForRoleSearchResults = getResponsibilitiesForRoleSearchResults(roleSearchResults, unbounded); |
130 | 0 | List<UberResponsibilityBo> matchedResponsibilities = new CollectionIncomplete<UberResponsibilityBo>( |
131 | |
new ArrayList<UberResponsibilityBo>(), getActualSizeIfTruncated(responsibilitiesForRoleSearchResults)); |
132 | 0 | if((responsibilitySearchResults!=null && !responsibilitySearchResults.isEmpty()) && |
133 | |
(responsibilitiesForRoleSearchResults!=null && !responsibilitiesForRoleSearchResults.isEmpty())){ |
134 | 0 | for(UberResponsibilityBo responsibility: responsibilitySearchResults){ |
135 | 0 | for(UberResponsibilityBo responsibilityFromRoleSearch: responsibilitiesForRoleSearchResults){ |
136 | 0 | if(responsibilityFromRoleSearch.getId().equals(responsibility.getId())) { |
137 | 0 | matchedResponsibilities.add(responsibilityFromRoleSearch); |
138 | |
} |
139 | |
} |
140 | |
} |
141 | |
} |
142 | |
|
143 | 0 | return matchedResponsibilities; |
144 | |
} |
145 | |
|
146 | |
@SuppressWarnings("unchecked") |
147 | |
private List<UberResponsibilityBo> searchResponsibilities(Map<String, String> responsibilitySearchCriteria, boolean unbounded){ |
148 | 0 | return getResponsibilitiesSearchResultsCopy((List<ResponsibilityBo>) |
149 | |
getLookupService().findCollectionBySearchHelper( |
150 | |
ResponsibilityBo.class, responsibilitySearchCriteria, unbounded)); |
151 | |
} |
152 | |
|
153 | |
private List<UberResponsibilityBo> getResponsibilitiesWithRoleSearchCriteria(Map<String, String> roleSearchCriteria, boolean unbounded){ |
154 | 0 | List<RoleBo> roleSearchResults = searchRoles(roleSearchCriteria, unbounded); |
155 | 0 | return getResponsibilitiesForRoleSearchResults(roleSearchResults, unbounded); |
156 | |
} |
157 | |
|
158 | |
private List<UberResponsibilityBo> getResponsibilitiesForRoleSearchResults(List<RoleBo> roleSearchResults, boolean unbounded){ |
159 | 0 | Long actualSizeIfTruncated = getActualSizeIfTruncated(roleSearchResults); |
160 | 0 | List<UberResponsibilityBo> responsibilities = new ArrayList<UberResponsibilityBo>(); |
161 | |
List<UberResponsibilityBo> tempResponsibilities; |
162 | 0 | List<String> collectedResponsibilityIds = new ArrayList<String>(); |
163 | |
Map<String, String> responsibilityCriteria; |
164 | |
|
165 | 0 | for(RoleBo roleImpl: roleSearchResults){ |
166 | 0 | responsibilityCriteria = new HashMap<String, String>(); |
167 | 0 | responsibilityCriteria.put("roleResponsibilities.roleId", roleImpl.getId()); |
168 | 0 | tempResponsibilities = searchResponsibilities(responsibilityCriteria, unbounded); |
169 | 0 | actualSizeIfTruncated += getActualSizeIfTruncated(tempResponsibilities); |
170 | 0 | for(UberResponsibilityBo responsibility: tempResponsibilities){ |
171 | 0 | if(!collectedResponsibilityIds.contains(responsibility.getId())){ |
172 | 0 | populateAssignedToRoles(responsibility); |
173 | 0 | collectedResponsibilityIds.add(responsibility.getId()); |
174 | 0 | responsibilities.add(responsibility); |
175 | |
} |
176 | |
|
177 | 0 | List<String> parentRoleIds = KimApiServiceLocator.getRoleService().getMemberParentRoleIds(MemberType.ROLE.getCode(), roleImpl.getId()); |
178 | 0 | for (String parentRoleId : parentRoleIds) { |
179 | 0 | Map<String, String> roleSearchCriteria = new HashMap<String, String>(); |
180 | 0 | roleSearchCriteria.put("roleId", parentRoleId); |
181 | |
|
182 | 0 | responsibilities = mergeResponsibilityLists(responsibilities, getResponsibilitiesWithRoleSearchCriteria(roleSearchCriteria, unbounded)); |
183 | 0 | } |
184 | 0 | } |
185 | |
} |
186 | 0 | return new CollectionIncomplete<UberResponsibilityBo>(responsibilities, actualSizeIfTruncated); |
187 | |
} |
188 | |
|
189 | |
private void populateAssignedToRoles(UberResponsibilityBo responsibility){ |
190 | 0 | Map<String, String> criteria = new HashMap<String, String>(); |
191 | 0 | if ( responsibility.getAssignedToRoles().isEmpty() ) { |
192 | 0 | for(RoleResponsibilityBo roleResponsibility: responsibility.getRoleResponsibilities()){ |
193 | 0 | criteria.put(KimConstants.PrimaryKeyConstants.ID, roleResponsibility.getRoleId()); |
194 | 0 | responsibility.getAssignedToRoles().add(getBusinessObjectService().findByPrimaryKey(RoleBo.class, criteria)); |
195 | |
} |
196 | |
} |
197 | 0 | } |
198 | |
|
199 | |
private List<UberResponsibilityBo> getResponsibilitiesWithResponsibilitySearchCriteria(Map<String, String> responsibilitySearchCriteria, boolean unbounded){ |
200 | 0 | String detailCriteriaStr = responsibilitySearchCriteria.remove( DETAIL_CRITERIA ); |
201 | 0 | Map<String, String> detailCriteria = parseDetailCriteria(detailCriteriaStr); |
202 | 0 | final List<UberResponsibilityBo> responsibilities = searchResponsibilities(responsibilitySearchCriteria, unbounded); |
203 | 0 | List<UberResponsibilityBo> filteredResponsibilities = new CollectionIncomplete<UberResponsibilityBo>( |
204 | |
new ArrayList<UberResponsibilityBo>(), getActualSizeIfTruncated(responsibilities)); |
205 | 0 | for(UberResponsibilityBo responsibility: responsibilities){ |
206 | 0 | if ( detailCriteria.isEmpty() ) { |
207 | 0 | filteredResponsibilities.add(responsibility); |
208 | 0 | populateAssignedToRoles(responsibility); |
209 | |
} else { |
210 | 0 | if ( isMapSubset( new HashMap<String, String>(responsibility.getAttributes()), detailCriteria ) ) { |
211 | 0 | filteredResponsibilities.add(responsibility); |
212 | 0 | populateAssignedToRoles(responsibility); |
213 | |
} |
214 | |
} |
215 | |
} |
216 | 0 | return filteredResponsibilities; |
217 | |
} |
218 | |
|
219 | |
private List<UberResponsibilityBo> getResponsibilitiesSearchResultsCopy(List<ResponsibilityBo> responsibilitySearchResults){ |
220 | 0 | List<UberResponsibilityBo> responsibilitySearchResultsCopy = new CollectionIncomplete<UberResponsibilityBo>( |
221 | |
new ArrayList<UberResponsibilityBo>(), getActualSizeIfTruncated(responsibilitySearchResults)); |
222 | 0 | for(ResponsibilityBo responsibilityImpl: responsibilitySearchResults){ |
223 | 0 | UberResponsibilityBo responsibilityCopy = new UberResponsibilityBo(); |
224 | |
|
225 | |
try { |
226 | 0 | PropertyUtils.copyProperties(responsibilityCopy, responsibilityImpl); |
227 | 0 | } catch (IllegalAccessException e) { |
228 | 0 | throw new RuntimeException("unable to copy properties"); |
229 | 0 | } catch (InvocationTargetException e) { |
230 | 0 | throw new RuntimeException("unable to copy properties"); |
231 | 0 | } catch (NoSuchMethodException e) { |
232 | 0 | throw new RuntimeException("unable to copy properties"); |
233 | 0 | } |
234 | |
|
235 | 0 | responsibilitySearchResultsCopy.add(responsibilityCopy); |
236 | 0 | } |
237 | 0 | return responsibilitySearchResultsCopy; |
238 | |
} |
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
|
244 | |
public LookupService getLookupService() { |
245 | 0 | if ( lookupService == null ) { |
246 | 0 | lookupService = KRADServiceLocatorWeb.getLookupService(); |
247 | |
} |
248 | 0 | return lookupService; |
249 | |
} |
250 | |
|
251 | |
private List<UberResponsibilityBo> mergeResponsibilityLists(List<UberResponsibilityBo> perm1, List<UberResponsibilityBo> perm2) { |
252 | 0 | List<UberResponsibilityBo> returnList = new ArrayList<UberResponsibilityBo>(perm1); |
253 | 0 | List<String> responsibilityIds = new ArrayList<String>(perm1.size()); |
254 | 0 | for (UberResponsibilityBo perm : returnList) { |
255 | 0 | responsibilityIds.add(perm.getId()); |
256 | |
} |
257 | 0 | for (int i=0; i<perm2.size(); i++) { |
258 | 0 | if (!responsibilityIds.contains(perm2.get(i).getId())) { |
259 | 0 | returnList.add(perm2.get(i)); |
260 | |
} |
261 | |
} |
262 | 0 | return returnList; |
263 | |
} |
264 | |
} |