1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.kim.lookup; |
17 | |
|
18 | |
import org.apache.commons.beanutils.PropertyUtils; |
19 | |
import org.apache.commons.lang.StringUtils; |
20 | |
import org.kuali.rice.core.api.membership.MemberType; |
21 | |
import org.kuali.rice.kim.api.KimConstants; |
22 | |
import org.kuali.rice.kim.api.role.RoleService; |
23 | |
import org.kuali.rice.kim.api.services.KimApiServiceLocator; |
24 | |
import org.kuali.rice.kim.impl.permission.GenericPermissionBo; |
25 | |
import org.kuali.rice.kim.impl.permission.PermissionBo; |
26 | |
import org.kuali.rice.kim.impl.permission.UberPermissionBo; |
27 | |
import org.kuali.rice.kim.impl.role.RoleBo; |
28 | |
import org.kuali.rice.kim.impl.role.RolePermissionBo; |
29 | |
import org.kuali.rice.kns.lookup.HtmlData; |
30 | |
import org.kuali.rice.krad.bo.BusinessObject; |
31 | |
import org.kuali.rice.krad.lookup.CollectionIncomplete; |
32 | |
import org.kuali.rice.krad.service.KRADServiceLocatorWeb; |
33 | |
import org.kuali.rice.krad.service.LookupService; |
34 | |
import org.kuali.rice.krad.util.KRADConstants; |
35 | |
import org.kuali.rice.krad.util.UrlFactory; |
36 | |
|
37 | |
import java.lang.reflect.InvocationTargetException; |
38 | |
import java.util.ArrayList; |
39 | |
import java.util.HashMap; |
40 | |
import java.util.List; |
41 | |
import java.util.Map; |
42 | |
import java.util.Properties; |
43 | |
|
44 | 0 | public class PermissionLookupableHelperServiceImpl extends RoleMemberLookupableHelperServiceImpl { |
45 | |
|
46 | |
private static final long serialVersionUID = -3578448525862270477L; |
47 | |
|
48 | |
private transient LookupService lookupService; |
49 | |
private transient RoleService roleService; |
50 | |
private volatile String genericPermissionDocumentTypeName; |
51 | |
|
52 | |
@Override |
53 | |
public List<HtmlData> getCustomActionUrls(BusinessObject businessObject, List pkNames) { |
54 | 0 | List<HtmlData> htmlDataList = new ArrayList<HtmlData>(); |
55 | |
|
56 | 0 | businessObject = new GenericPermissionBo ( (UberPermissionBo)businessObject); |
57 | 0 | if (allowsMaintenanceEditAction(businessObject)) { |
58 | 0 | htmlDataList.add(getUrlData(businessObject, KRADConstants.MAINTENANCE_EDIT_METHOD_TO_CALL, pkNames)); |
59 | |
} |
60 | 0 | if (allowsMaintenanceNewOrCopyAction()) { |
61 | 0 | htmlDataList.add(getUrlData(businessObject, KRADConstants.MAINTENANCE_COPY_METHOD_TO_CALL, pkNames)); |
62 | |
} |
63 | 0 | return htmlDataList; |
64 | |
} |
65 | |
|
66 | |
protected String getActionUrlHref(BusinessObject businessObject, String methodToCall, List pkNames){ |
67 | 0 | Properties parameters = new Properties(); |
68 | 0 | parameters.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, methodToCall); |
69 | 0 | parameters.put(KRADConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, businessObject.getClass().getName()); |
70 | 0 | parameters.put(KRADConstants.OVERRIDE_KEYS, KimConstants.PrimaryKeyConstants.PERMISSION_ID); |
71 | 0 | parameters.put(KRADConstants.COPY_KEYS, KimConstants.PrimaryKeyConstants.PERMISSION_ID); |
72 | 0 | if (StringUtils.isNotBlank(getReturnLocation())) { |
73 | 0 | parameters.put(KRADConstants.RETURN_LOCATION_PARAMETER, getReturnLocation()); |
74 | |
} |
75 | 0 | parameters.putAll(getParametersFromPrimaryKey(businessObject, pkNames)); |
76 | 0 | return UrlFactory.parameterizeUrl(KRADConstants.MAINTENANCE_ACTION, parameters); |
77 | |
} |
78 | |
|
79 | |
@Override |
80 | |
protected String getMaintenanceDocumentTypeName() { |
81 | |
|
82 | |
|
83 | 0 | String g = genericPermissionDocumentTypeName; |
84 | 0 | if (g == null) { |
85 | 0 | synchronized (this) { |
86 | 0 | g = genericPermissionDocumentTypeName; |
87 | 0 | if (g == null) { |
88 | 0 | genericPermissionDocumentTypeName = g = getMaintenanceDocumentDictionaryService().getDocumentTypeName( |
89 | |
GenericPermissionBo.class); |
90 | |
} |
91 | 0 | } |
92 | |
} |
93 | |
|
94 | 0 | return g; |
95 | |
} |
96 | |
|
97 | |
@Override |
98 | |
protected List<? extends BusinessObject> getMemberSearchResults(Map<String, String> searchCriteria, boolean unbounded) { |
99 | 0 | Map<String, String> permissionSearchCriteria = buildSearchCriteria(searchCriteria); |
100 | 0 | Map<String, String> roleSearchCriteria = buildRoleSearchCriteria(searchCriteria); |
101 | 0 | boolean permissionCriteriaEmpty = permissionSearchCriteria==null || permissionSearchCriteria.isEmpty(); |
102 | 0 | boolean roleCriteriaEmpty = roleSearchCriteria==null || roleSearchCriteria.isEmpty(); |
103 | |
|
104 | 0 | List<UberPermissionBo> permissionSearchResultsCopy = new CollectionIncomplete<UberPermissionBo>(new ArrayList<UberPermissionBo>(), new Long(0)); |
105 | 0 | if(!permissionCriteriaEmpty && !roleCriteriaEmpty){ |
106 | 0 | permissionSearchResultsCopy = getCombinedSearchResults(permissionSearchCriteria, roleSearchCriteria, unbounded); |
107 | 0 | } else if(permissionCriteriaEmpty && !roleCriteriaEmpty){ |
108 | 0 | permissionSearchResultsCopy = getPermissionsWithRoleSearchCriteria(roleSearchCriteria, unbounded); |
109 | 0 | } else if(!permissionCriteriaEmpty && roleCriteriaEmpty){ |
110 | 0 | permissionSearchResultsCopy = getPermissionsWithPermissionSearchCriteria(permissionSearchCriteria, unbounded); |
111 | 0 | } else if(permissionCriteriaEmpty && roleCriteriaEmpty){ |
112 | 0 | return getAllPermissions(unbounded); |
113 | |
} |
114 | 0 | return permissionSearchResultsCopy; |
115 | |
} |
116 | |
|
117 | |
private List<UberPermissionBo> getAllPermissions(boolean unbounded){ |
118 | 0 | List<UberPermissionBo> permissions = searchPermissions(new HashMap<String, String>(), unbounded); |
119 | 0 | for(UberPermissionBo permission: permissions) { |
120 | 0 | populateAssignedToRoles(permission); |
121 | |
} |
122 | 0 | return permissions; |
123 | |
} |
124 | |
|
125 | |
private List<UberPermissionBo> getCombinedSearchResults( |
126 | |
Map<String, String> permissionSearchCriteria, Map<String, String> roleSearchCriteria, boolean unbounded){ |
127 | 0 | List<UberPermissionBo> permissionSearchResults = searchPermissions(permissionSearchCriteria, unbounded); |
128 | 0 | List<RoleBo> roleSearchResults = searchRoles(roleSearchCriteria, unbounded); |
129 | 0 | List<UberPermissionBo> permissionsForRoleSearchResults = getPermissionsForRoleSearchResults(roleSearchResults, unbounded); |
130 | 0 | List<UberPermissionBo> matchedPermissions = new CollectionIncomplete<UberPermissionBo>( |
131 | |
new ArrayList<UberPermissionBo>(), getActualSizeIfTruncated(permissionsForRoleSearchResults)); |
132 | 0 | if((permissionSearchResults!=null && !permissionSearchResults.isEmpty()) && |
133 | |
(permissionsForRoleSearchResults!=null && !permissionsForRoleSearchResults.isEmpty())){ |
134 | 0 | for(UberPermissionBo permission: permissionSearchResults){ |
135 | 0 | for(UberPermissionBo permissionFromRoleSearch: permissionsForRoleSearchResults){ |
136 | 0 | if(permissionFromRoleSearch.getId().equals(permission.getId())) { |
137 | 0 | matchedPermissions.add(permissionFromRoleSearch); |
138 | |
} |
139 | |
} |
140 | |
} |
141 | |
} |
142 | 0 | return matchedPermissions; |
143 | |
} |
144 | |
|
145 | |
private List<UberPermissionBo> searchPermissions(Map<String, String> permissionSearchCriteria, boolean unbounded){ |
146 | 0 | return getPermissionsSearchResultsCopy(new ArrayList<PermissionBo>(getLookupService().findCollectionBySearchHelper( |
147 | |
PermissionBo.class, permissionSearchCriteria, unbounded))); |
148 | |
|
149 | |
} |
150 | |
|
151 | |
private List<UberPermissionBo> getPermissionsWithRoleSearchCriteria(Map<String, String> roleSearchCriteria, boolean unbounded){ |
152 | 0 | return getPermissionsForRoleSearchResults(searchRoles(roleSearchCriteria, unbounded), unbounded); |
153 | |
} |
154 | |
|
155 | |
private List<UberPermissionBo> getPermissionsForRoleSearchResults(List<RoleBo> roleSearchResults, boolean unbounded){ |
156 | 0 | Long actualSizeIfTruncated = getActualSizeIfTruncated(roleSearchResults); |
157 | 0 | List<UberPermissionBo> permissions = new ArrayList<UberPermissionBo>(); |
158 | |
List<UberPermissionBo> tempPermissions; |
159 | 0 | List<String> collectedPermissionIds = new ArrayList<String>(); |
160 | |
Map<String, String> permissionCriteria; |
161 | |
|
162 | 0 | for(RoleBo roleImpl: roleSearchResults){ |
163 | 0 | permissionCriteria = new HashMap<String, String>(); |
164 | 0 | permissionCriteria.put("rolePermissions.roleId", roleImpl.getId()); |
165 | 0 | tempPermissions = searchPermissions(permissionCriteria, unbounded); |
166 | 0 | actualSizeIfTruncated += getActualSizeIfTruncated(tempPermissions); |
167 | 0 | for(UberPermissionBo permission: tempPermissions){ |
168 | 0 | if(!collectedPermissionIds.contains(permission.getId())){ |
169 | 0 | populateAssignedToRoles(permission); |
170 | 0 | collectedPermissionIds.add(permission.getId()); |
171 | 0 | permissions.add(permission); |
172 | |
} |
173 | |
} |
174 | |
|
175 | 0 | List<String> parentRoleIds = KimApiServiceLocator.getRoleService().getMemberParentRoleIds(MemberType.ROLE.getCode(), roleImpl.getId()); |
176 | 0 | for (String parentRoleId : parentRoleIds) { |
177 | 0 | Map<String, String> roleSearchCriteria = new HashMap<String, String>(); |
178 | 0 | roleSearchCriteria.put("roleId", parentRoleId); |
179 | |
|
180 | 0 | permissions = mergePermissionLists(permissions, getPermissionsWithRoleSearchCriteria(roleSearchCriteria, unbounded)); |
181 | 0 | } |
182 | 0 | } |
183 | |
|
184 | 0 | return new CollectionIncomplete<UberPermissionBo>(permissions, actualSizeIfTruncated); |
185 | |
} |
186 | |
|
187 | |
|
188 | |
private void populateAssignedToRoles(UberPermissionBo permission){ |
189 | |
Map<String, String> criteria; |
190 | 0 | for(RolePermissionBo rolePermission: permission.getRolePermissions()){ |
191 | 0 | if ( rolePermission.isActive() ) { |
192 | 0 | criteria = new HashMap<String, String>(); |
193 | 0 | criteria.put("id", rolePermission.getRoleId()); |
194 | |
|
195 | 0 | RoleBo roleBo = getBusinessObjectService().findByPrimaryKey(RoleBo.class, criteria); |
196 | 0 | permission.getAssignedToRoles().add(roleBo); |
197 | |
|
198 | 0 | } |
199 | |
} |
200 | 0 | } |
201 | |
|
202 | |
private List<UberPermissionBo> getPermissionsWithPermissionSearchCriteria( |
203 | |
Map<String, String> permissionSearchCriteria, boolean unbounded){ |
204 | 0 | String detailCriteriaStr = permissionSearchCriteria.remove( DETAIL_CRITERIA ); |
205 | 0 | Map<String, String> detailCriteria = parseDetailCriteria(detailCriteriaStr); |
206 | |
|
207 | 0 | final List<UberPermissionBo> permissions = searchPermissions(permissionSearchCriteria, unbounded); |
208 | 0 | List<UberPermissionBo> filteredPermissions = new CollectionIncomplete<UberPermissionBo>( |
209 | |
new ArrayList<UberPermissionBo>(), getActualSizeIfTruncated(permissions)); |
210 | 0 | for(UberPermissionBo perm: permissions){ |
211 | 0 | if ( detailCriteria.isEmpty() ) { |
212 | 0 | filteredPermissions.add(perm); |
213 | 0 | populateAssignedToRoles(perm); |
214 | |
} else { |
215 | 0 | if ( isMapSubset( new HashMap<String, String>(perm.getDetails()), detailCriteria ) ) { |
216 | 0 | filteredPermissions.add(perm); |
217 | 0 | populateAssignedToRoles(perm); |
218 | |
} |
219 | |
} |
220 | |
} |
221 | 0 | return filteredPermissions; |
222 | |
} |
223 | |
|
224 | |
private List<UberPermissionBo> getPermissionsSearchResultsCopy(List<PermissionBo> permissionSearchResults){ |
225 | 0 | List<UberPermissionBo> permissionSearchResultsCopy = new CollectionIncomplete<UberPermissionBo>( |
226 | |
new ArrayList<UberPermissionBo>(), getActualSizeIfTruncated(permissionSearchResults)); |
227 | 0 | for(PermissionBo permissionBo: permissionSearchResults){ |
228 | 0 | UberPermissionBo permissionCopy = new UberPermissionBo(); |
229 | |
|
230 | |
try { |
231 | 0 | PropertyUtils.copyProperties(permissionCopy, permissionBo); |
232 | |
|
233 | 0 | } catch (IllegalAccessException e) { |
234 | 0 | throw new RuntimeException("unable to copy properties"); |
235 | 0 | } catch (InvocationTargetException e) { |
236 | 0 | throw new RuntimeException("unable to copy properties"); |
237 | 0 | } catch (NoSuchMethodException e) { |
238 | 0 | throw new RuntimeException("unable to copy properties"); |
239 | 0 | } |
240 | |
|
241 | 0 | permissionSearchResultsCopy.add(permissionCopy); |
242 | 0 | } |
243 | 0 | return permissionSearchResultsCopy; |
244 | |
} |
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
public synchronized LookupService getLookupService() { |
250 | 0 | if ( lookupService == null ) { |
251 | 0 | lookupService = KRADServiceLocatorWeb.getLookupService(); |
252 | |
} |
253 | 0 | return lookupService; |
254 | |
} |
255 | |
|
256 | |
public synchronized RoleService getRoleService() { |
257 | 0 | if (roleService == null) { |
258 | 0 | roleService = KimApiServiceLocator.getRoleService(); |
259 | |
} |
260 | 0 | return roleService; |
261 | |
} |
262 | |
|
263 | |
private List<UberPermissionBo> mergePermissionLists(List<UberPermissionBo> perm1, List<UberPermissionBo> perm2) { |
264 | 0 | List<UberPermissionBo> returnList = new ArrayList<UberPermissionBo>(perm1); |
265 | 0 | List<String> permissionIds = new ArrayList<String>(perm1.size()); |
266 | 0 | for (UberPermissionBo perm : returnList) { |
267 | 0 | permissionIds.add(perm.getId()); |
268 | |
} |
269 | 0 | for (int i=0; i<perm2.size(); i++) { |
270 | 0 | if (!permissionIds.contains(perm2.get(i).getId())) { |
271 | 0 | returnList.add(perm2.get(i)); |
272 | |
} |
273 | |
} |
274 | 0 | return returnList; |
275 | |
} |
276 | |
} |