From d8c9a0eda85a0b815574d21e083019e5c4275d54 Mon Sep 17 00:00:00 2001 From: tosmers Date: Thu, 14 Jul 2016 16:18:56 +0000 Subject: [PATCH] adds converter for trunk class role and permission git-svn-id: https://svn.libreccm.org/ccm/trunk@4197 8810af33-2d31-482b-a856-94f89814c4df --- ccm-core/src/com/arsdigita/kernel/Role.java | 29 ++++++++ .../kernel/permissions/Permission.java | 64 +++++++++++++--- .../portation/conversion/NgCollection.java | 12 +++ .../categorization/CategoryConversion.java | 41 +++++----- .../core/security/GroupConversion.java | 30 +++++--- .../core/security/PermissionConversion.java | 74 +++++++++++++++++++ .../core/security/RoleConversion.java | 74 +++++++++++++++++++ .../core/workflow/UserTaskConversion.java | 43 +++++++---- .../core/categorization/Categorization.java | 3 +- .../core/security/GroupMembership.java | 4 +- .../modules/core/security/Permission.java | 15 +++- .../portation/modules/core/security/Role.java | 29 +++++--- .../modules/core/security/RoleMembership.java | 9 ++- .../modules/core/workflow/TaskAssignment.java | 9 ++- 14 files changed, 365 insertions(+), 71 deletions(-) create mode 100644 ccm-core/src/com/arsdigita/portation/conversion/core/security/PermissionConversion.java create mode 100644 ccm-core/src/com/arsdigita/portation/conversion/core/security/RoleConversion.java diff --git a/ccm-core/src/com/arsdigita/kernel/Role.java b/ccm-core/src/com/arsdigita/kernel/Role.java index 0c2b3512a..6770c4c4c 100755 --- a/ccm-core/src/com/arsdigita/kernel/Role.java +++ b/ccm-core/src/com/arsdigita/kernel/Role.java @@ -20,6 +20,7 @@ package com.arsdigita.kernel; import com.arsdigita.db.Sequences; import com.arsdigita.domain.DataObjectNotFoundException; +import com.arsdigita.domain.DomainCollection; import com.arsdigita.domain.DomainObject; import com.arsdigita.kernel.permissions.PermissionDescriptor; import com.arsdigita.kernel.permissions.PermissionService; @@ -27,9 +28,13 @@ import com.arsdigita.kernel.permissions.PrivilegeDescriptor; import com.arsdigita.persistence.DataObject; import com.arsdigita.persistence.OID; import com.arsdigita.persistence.PersistenceException; +import com.arsdigita.persistence.Session; +import com.arsdigita.persistence.SessionManager; import com.arsdigita.util.UncheckedWrapperException; import java.math.BigDecimal; import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; /** * @@ -497,4 +502,28 @@ public class Role extends DomainObject { return m_roleGroup; } + /** + * Retrieves all objects of this type stored in the database. Very + * necessary for exporting all entities of the current work environment. + * + * @return List of all roles + */ + public static List getAllObjectRoles() { + List roleList = new ArrayList<>(); + + final Session session = SessionManager.getSession(); + DomainCollection collection = new DomainCollection(session.retrieve( + Role.BASE_DATA_OBJECT_TYPE)); + + while (collection.next()) { + Role role = (Role) collection.getDomainObject(); + if (role != null) { + roleList.add(role); + } + } + + collection.close(); + return roleList; + } + } diff --git a/ccm-core/src/com/arsdigita/kernel/permissions/Permission.java b/ccm-core/src/com/arsdigita/kernel/permissions/Permission.java index 20bb9b9e2..53f489b9c 100755 --- a/ccm-core/src/com/arsdigita/kernel/permissions/Permission.java +++ b/ccm-core/src/com/arsdigita/kernel/permissions/Permission.java @@ -19,6 +19,10 @@ package com.arsdigita.kernel.permissions; +import com.arsdigita.domain.DomainCollection; +import com.arsdigita.kernel.Group; +import com.arsdigita.persistence.Session; +import com.arsdigita.persistence.SessionManager; import com.arsdigita.web.Web; import com.arsdigita.kernel.ACSObject; @@ -36,7 +40,9 @@ import com.arsdigita.persistence.OID; import java.math.BigDecimal; +import java.util.ArrayList; import java.util.Date; +import java.util.List; import javax.servlet.http.HttpServletRequest; import org.apache.log4j.Logger; @@ -59,6 +65,7 @@ public class Permission extends DomainObject { // The names of the attributes we use when creating permission // objects static final String OBJECT_ID = "objectId"; + static final String ID = "id"; static final String PARTY_ID = "partyId"; static final String PRIVILEGE = "privilege"; @@ -69,7 +76,7 @@ public class Permission extends DomainObject { * */ @Override - protected String getBaseDataObjectType() { + public String getBaseDataObjectType() { return BASE_DATA_OBJECT_TYPE; } @@ -111,6 +118,19 @@ public class Permission extends DomainObject { super(oid); } + /** + * Gets the value of the ID property. + * + * This is a convenience method that is roughly equivalent + * to getOID().get("id"). In general, it should be used + * instead of the getOID method to get any ACSObject's ID. + * + * @return the value of the ID property. + */ + public BigDecimal getID() { + return (BigDecimal) get(ID); + } + /** * Returns the OID of the Party that is * the grantee of the privilege associated with this @@ -123,7 +143,7 @@ public class Permission extends DomainObject { * @see com.arsdigita.kernel.permissions.PrivilegeDescriptor * @see com.arsdigita.persistence.OID */ - OID getPartyOID() { + public OID getPartyOID() { return new OID(Party.BASE_DATA_OBJECT_TYPE, get(PARTY_ID)); } @@ -141,7 +161,7 @@ public class Permission extends DomainObject { * @see com.arsdigita.kernel.permissions.PrivilegeDescriptor * @see com.arsdigita.persistence.OID */ - void setPartyOID(OID partyOID) { + public void setPartyOID(OID partyOID) { set(PARTY_ID, partyOID.get("id")); } @@ -156,7 +176,7 @@ public class Permission extends DomainObject { * @see com.arsdigita.kernel.permissions.PrivilegeDescriptor * @see com.arsdigita.persistence.OID */ - OID getACSObject() { + public OID getACSObject() { return new OID("com.arsdigita.kernel.ACSObject", get(OBJECT_ID)); } @@ -173,7 +193,7 @@ public class Permission extends DomainObject { * @see com.arsdigita.kernel.permissions.PrivilegeDescriptor * @see com.arsdigita.persistence.OID */ - void setACSObjectOID(OID acsObjectOID) { + public void setACSObjectOID(OID acsObjectOID) { set(OBJECT_ID, acsObjectOID.get("id")); } @@ -187,7 +207,7 @@ public class Permission extends DomainObject { * @see com.arsdigita.kernel.permissions.PrivilegeDescriptor * @see com.arsdigita.persistence.OID */ - PrivilegeDescriptor getPrivilege() { + public PrivilegeDescriptor getPrivilege() { return PrivilegeDescriptor.get((String) get(PRIVILEGE)); } @@ -203,14 +223,14 @@ public class Permission extends DomainObject { * @see com.arsdigita.kernel.permissions.PrivilegeDescriptor * @see com.arsdigita.persistence.OID */ - void setPrivilege(PrivilegeDescriptor privilege) { + public void setPrivilege(PrivilegeDescriptor privilege) { set(PRIVILEGE, privilege.getName()); } /** * Get the user who created the object (may be null) */ - User getCreationUser() { + public User getCreationUser() { Object o = get("creationUser"); if (o == null) { return null; @@ -221,14 +241,14 @@ public class Permission extends DomainObject { /** * Get the creation date */ - Date getCreationDate() { + public Date getCreationDate() { return (Date) get("creationDate"); } /** * Get the creation IP address (may be null) */ - String getCreationIP() { + public String getCreationIP() { return (String) get("creationIP"); } @@ -298,4 +318,28 @@ public class Permission extends DomainObject { set("creationDate", date); set("creationIP", ip); } + + /** + * Retrieves all objects of this type stored in the database. Very + * necessary for exporting all entities of the current work environment. + * + * @return List of all permissions + */ + public static List getAllObjectPermissions() { + List permissionList = new ArrayList<>(); + + final Session session = SessionManager.getSession(); + DomainCollection collection = new DomainCollection(session.retrieve( + Group.BASE_DATA_OBJECT_TYPE)); + + while (collection.next()) { + Permission permission = (Permission) collection.getDomainObject(); + if (permission != null) { + permissionList.add(permission); + } + } + + collection.close(); + return permissionList; + } } diff --git a/ccm-core/src/com/arsdigita/portation/conversion/NgCollection.java b/ccm-core/src/com/arsdigita/portation/conversion/NgCollection.java index 36ffbb2a7..116fc29f9 100644 --- a/ccm-core/src/com/arsdigita/portation/conversion/NgCollection.java +++ b/ccm-core/src/com/arsdigita/portation/conversion/NgCollection.java @@ -24,8 +24,12 @@ import com.arsdigita.portation.modules.core.core.CcmObject; import com.arsdigita.portation.modules.core.security.Group; import com.arsdigita.portation.modules.core.security.GroupMembership; import com.arsdigita.portation.modules.core.security.Party; +import com.arsdigita.portation.modules.core.security.Permission; +import com.arsdigita.portation.modules.core.security.Role; +import com.arsdigita.portation.modules.core.security.RoleMembership; import com.arsdigita.portation.modules.core.security.User; import com.arsdigita.portation.modules.core.workflow.Task; +import com.arsdigita.portation.modules.core.workflow.TaskAssignment; import com.arsdigita.portation.modules.core.workflow.UserTask; import com.arsdigita.portation.modules.core.workflow.Workflow; @@ -51,5 +55,13 @@ public class NgCollection { public static Map tasks = new HashMap<>(); public static Map userTasks = new HashMap<>(); + public static Map roles = new HashMap<>(); + public static Map roleMemberships = new HashMap<>(); + + public static Map taskAssignments = new HashMap<>(); + + public static Map permissions = new HashMap<>(); + + private NgCollection() {} } diff --git a/ccm-core/src/com/arsdigita/portation/conversion/core/categorization/CategoryConversion.java b/ccm-core/src/com/arsdigita/portation/conversion/core/categorization/CategoryConversion.java index bb5b2e75e..cdcc197cb 100644 --- a/ccm-core/src/com/arsdigita/portation/conversion/core/categorization/CategoryConversion.java +++ b/ccm-core/src/com/arsdigita/portation/conversion/core/categorization/CategoryConversion.java @@ -51,17 +51,20 @@ public class CategoryConversion { */ private static void setAssociations( List trunkCategories) { - Category category, parentCategory; - for (com.arsdigita.categorization.Category trunkCategory : trunkCategories) { - category = NgCollection.categories.get(trunkCategory.getID() + Category category = NgCollection.categories.get(trunkCategory + .getID() .longValue()); // set parent associations - parentCategory = NgCollection.categories.get(trunkCategory + Category parentCategory = NgCollection.categories.get(trunkCategory .getDefaultParentCategory().getID().longValue()); - setParentCategory(category, parentCategory); + if (category != null && parentCategory != null) { + // set parent and opposed association + category.setParentCategory(parentCategory); + parentCategory.addSubCategory(category); + } // create categorizations only for category typed objects CategorizedCollection categorizedCollection = trunkCategory @@ -71,30 +74,22 @@ public class CategoryConversion { } } - private static void setParentCategory(Category category, Category - parentCategory) { - if (category != null && parentCategory != null) { - // set parent and opposed association - category.setParentCategory(parentCategory); - parentCategory.addSubCategory(category); - } - } - private static void createCategorizations(Category category, CategorizedCollection categorizedObjects) { - CcmObject categorizedObject; Categorization categorization; - while (categorizedObjects.next()) { - categorizedObject = NgCollection.ccmObjects.get(((ACSObject) + CcmObject categorizedObject = NgCollection.ccmObjects.get(((ACSObject) categorizedObjects.getDomainObject()).getID().longValue()); - // create categorizations - categorization = new Categorization(category, - categorizedObject); - // set opposed associations - category.addObject(categorization); - categorizedObject.addCategory(categorization); + if (category != null && categorizedObject != null) { + // create categorizations + Categorization categorization = new Categorization(category, + categorizedObject); + + // set opposed associations + category.addObject(categorization); + categorizedObject.addCategory(categorization); + } } } } diff --git a/ccm-core/src/com/arsdigita/portation/conversion/core/security/GroupConversion.java b/ccm-core/src/com/arsdigita/portation/conversion/core/security/GroupConversion.java index 2e6c87e81..38aa78646 100644 --- a/ccm-core/src/com/arsdigita/portation/conversion/core/security/GroupConversion.java +++ b/ccm-core/src/com/arsdigita/portation/conversion/core/security/GroupConversion.java @@ -24,6 +24,7 @@ import com.arsdigita.portation.modules.core.security.Group; import com.arsdigita.portation.modules.core.security.GroupMembership; import com.arsdigita.portation.modules.core.security.User; +import java.util.ArrayList; import java.util.List; /** @@ -33,8 +34,16 @@ import java.util.List; public class GroupConversion { public static void convertAll() { - List trunkGroups = com.arsdigita.kernel - .Group.getAllObjectGroups(); + List trunkGroups, + roleGroups = new ArrayList<>(); + trunkGroups = com.arsdigita.kernel.Group.getAllObjectGroups(); + + List trunkRoles = com.arsdigita.kernel + .Role.getAllObjectRoles(); + trunkRoles.forEach(role -> roleGroups.add(role.getGroup())); + + // remove subgroups representing roles + trunkGroups.removeAll(roleGroups); // create groups trunkGroups.forEach(Group::new); @@ -44,10 +53,9 @@ public class GroupConversion { private static void setAssociations( List trunkGroups) { - Group group; - for (com.arsdigita.kernel.Group trunkGroup : trunkGroups) { - group = NgCollection.groups.get(trunkGroup.getID().longValue()); + Group group = NgCollection.groups.get(trunkGroup.getID() + .longValue()); // create groupMemberships UserCollection userCollection = trunkGroup.getMemberUsers(); @@ -61,12 +69,14 @@ public class GroupConversion { User member = NgCollection.users.get(userCollection.getUser() .getID().longValue()); - // create groupMemeberships - GroupMembership groupMembership = new GroupMembership(group, member); + if (group != null && member != null) { + // create groupMemeberships + GroupMembership groupMembership = new GroupMembership(group, member); - // set adverse associations - group.addMembership(groupMembership); - member.addGroupMembership(groupMembership); + // set adverse associations + group.addMembership(groupMembership); + member.addGroupMembership(groupMembership); + } } } diff --git a/ccm-core/src/com/arsdigita/portation/conversion/core/security/PermissionConversion.java b/ccm-core/src/com/arsdigita/portation/conversion/core/security/PermissionConversion.java new file mode 100644 index 000000000..81a515f50 --- /dev/null +++ b/ccm-core/src/com/arsdigita/portation/conversion/core/security/PermissionConversion.java @@ -0,0 +1,74 @@ +/* + * Copyright (C) 2015 LibreCCM Foundation. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ +package com.arsdigita.portation.conversion.core.security; + +import com.arsdigita.portation.conversion.NgCollection; +import com.arsdigita.portation.modules.core.core.CcmObject; +import com.arsdigita.portation.modules.core.security.Permission; +import com.arsdigita.portation.modules.core.security.Role; +import com.arsdigita.portation.modules.core.security.User; + +import java.math.BigDecimal; +import java.util.List; + +/** + * @author Tobias Osmers<\a> @@ -28,11 +29,17 @@ import com.arsdigita.portation.Identifiable; public class RoleMembership implements Identifiable { private long membershipId; + private Role role; private Party member; - public RoleMembership() { + public RoleMembership(final Role role, final Party member) { + this.membershipId = NgCollection.roleMemberships.size() + 1; + this.role = role; + this.member = member; + + NgCollection.roleMemberships.put(this.membershipId, this); } @Override diff --git a/ccm-core/src/com/arsdigita/portation/modules/core/workflow/TaskAssignment.java b/ccm-core/src/com/arsdigita/portation/modules/core/workflow/TaskAssignment.java index fdaaf416d..6becab966 100644 --- a/ccm-core/src/com/arsdigita/portation/modules/core/workflow/TaskAssignment.java +++ b/ccm-core/src/com/arsdigita/portation/modules/core/workflow/TaskAssignment.java @@ -20,6 +20,7 @@ package com.arsdigita.portation.modules.core.workflow; import com.arsdigita.portation.AbstractMarshaller; import com.arsdigita.portation.Identifiable; +import com.arsdigita.portation.conversion.NgCollection; import com.arsdigita.portation.modules.core.security.Role; /** @@ -29,11 +30,17 @@ import com.arsdigita.portation.modules.core.security.Role; public class TaskAssignment implements Identifiable { private long taskAssignmentId; + private UserTask task; private Role role; - public TaskAssignment() { + public TaskAssignment(final UserTask task, final Role role) { + this.taskAssignmentId = NgCollection.taskAssignments.size() + 1; + this.task = task; + this.role = role; + + NgCollection.taskAssignments.put(this.taskAssignmentId, this); } @Override