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