public void populatePermissions(DocumentConfigurationViewForm form) { DocumentType docType = form.getDocumentType(); Map<String, List<Role>> permRoles = new HashMap<String, List<Role>>(); // loop over the document hierarchy Set<String> seenDocumentPermissions = new HashSet<String>(); while (docType != null) { String documentTypeName = docType.getName(); Predicate p = and( equal("active", "Y"), equal( "attributes[" + KimConstants.AttributeConstants.DOCUMENT_TYPE_NAME + "]", docType.getName())); List<Permission> perms = getPermissionService() .findPermissions(QueryByCriteria.Builder.fromPredicates(p)) .getResults(); for (Permission perm : perms) { PermissionBo permBo = PermissionBo.from(perm); List<String> roleIds = getPermissionService().getRoleIdsForPermission(perm.getNamespaceCode(), perm.getName()); if (!roleIds.isEmpty()) { permRoles.put(perm.getId(), getRoleService().getRoles(roleIds)); } for (String attributeName : permBo.getDetails().keySet()) { addAttributeLabel(form, attributeName); } } // show the section if the current document or permissions exist if (perms.size() > 0 || documentTypeName.equals(form.getDocumentTypeName())) { ArrayList<PermissionForDisplay> dispPerms = new ArrayList<PermissionForDisplay>(perms.size()); for (Permission perm : perms) { PermissionBo permBo = PermissionBo.from(perm); if (permBo.getDetails().size() == 1) { // only the document type // this is a document type-specific permission, check if seen earlier if (seenDocumentPermissions.contains( perm.getTemplate().getNamespaceCode() + "|" + perm.getTemplate().getName())) { dispPerms.add(new PermissionForDisplay(permBo, true)); } else { dispPerms.add(new PermissionForDisplay(permBo, false)); seenDocumentPermissions.add( perm.getTemplate().getNamespaceCode() + "|" + perm.getTemplate().getName()); } } else { // other attributes, can't determine whether this is overridden at another level dispPerms.add(new PermissionForDisplay(permBo, false)); } } form.setPermissionsForDocumentType(documentTypeName, dispPerms); form.addDocumentType(documentTypeName); } docType = docType.getParentDocType(); } form.setPermissionRoles(permRoles); }