diff --git a/ccm-core/src/com/arsdigita/kernel/Party.java b/ccm-core/src/com/arsdigita/kernel/Party.java index 3ab1f8a77..fb4dabff8 100755 --- a/ccm-core/src/com/arsdigita/kernel/Party.java +++ b/ccm-core/src/com/arsdigita/kernel/Party.java @@ -19,19 +19,20 @@ package com.arsdigita.kernel; import com.arsdigita.domain.DataObjectNotFoundException; -import com.arsdigita.persistence.OID; -import com.arsdigita.persistence.metadata.ObjectType; -import com.arsdigita.persistence.DataCollection; +import com.arsdigita.domain.DomainCollection; import com.arsdigita.persistence.DataAssociation; import com.arsdigita.persistence.DataAssociationCursor; +import com.arsdigita.persistence.DataCollection; import com.arsdigita.persistence.DataObject; +import com.arsdigita.persistence.OID; +import com.arsdigita.persistence.Session; import com.arsdigita.persistence.SessionManager; +import com.arsdigita.persistence.metadata.ObjectType; +import org.apache.log4j.Logger; import java.util.ArrayList; -import java.util.List; import java.util.Iterator; - -import org.apache.log4j.Logger; +import java.util.List; /** * Represents a party, which can either be a group or a @@ -320,4 +321,29 @@ public abstract class Party extends ACSObject { public void setURI(String uri) { set("uri", uri); } + + /** + * 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 groups + */ + public static List getAllObjectParties() { + List partyList = new ArrayList<>(); + + final Session session = SessionManager.getSession(); + DomainCollection collection = new DomainCollection(session.retrieve( + Party.BASE_DATA_OBJECT_TYPE)); + + while (collection.next()) { + Party party = (Party) collection.getDomainObject(); + if (party != null) { + partyList.add(party); + } + } + + collection.close(); + return partyList; + } + } diff --git a/ccm-core/src/com/arsdigita/portation/cmd/ExportCliTool.java b/ccm-core/src/com/arsdigita/portation/cmd/ExportCliTool.java index d7a90e25e..606e756c8 100644 --- a/ccm-core/src/com/arsdigita/portation/cmd/ExportCliTool.java +++ b/ccm-core/src/com/arsdigita/portation/cmd/ExportCliTool.java @@ -18,12 +18,14 @@ */ package com.arsdigita.portation.cmd; +import com.arsdigita.portation.conversion.MainConverter; import com.arsdigita.util.cmd.Program; import org.apache.commons.cli.CommandLine; import org.apache.log4j.Logger; /** - * Commandline tool to exportUsers all the objects of a specified class to a xml-file. + * A Commandline tool for exporting all the objects of specified classes to + * one or many specified file types. * * @author []\n" + + "\n" + + "\t\t\t --- ExportCliTool ---\n" + + "\n" + + "usage:\t [] \t (module class optional)\n" + "\n" + "Available commands:\n" + - "\tlist \t\t Shows information on how to use this tool.\n" + - "\texportUsers \t\t Exports the chosen category to xml file.\n" + + "\thelp" + + "\t\t\t\t Shows information on how to use this tool.\n" + + "\texport " + + "\t\t Exports the chosen module class to a file.\n" + "\n" + - "Available categories for exportUsers:\n" + - " \t\t users \t all users of the system\n" + - " \t\t groups \t all groups of the system\n" + - "Use for exporting java objects of a specified class to a xml-file.\n" + "Available module-classes for export:\n" + + " \t\t categories \t\t all categories of the system\n" + + " \t\t categorizations \t\t all categorizations of the system\n" + + " \t\t users \t\t all users of the system\n" + + " \t\t groups \t\t all groups of the system\n" + + " \t\t groupMemberships\t\t all groupsMemberships of the system\n" + + " \t\t roles \t\t all roles of the system\n" + + " \t\t roleMemberships \t\t all roleMemberships of the system\n" + + " \t\t workflows \t\t all workflows of the system\n" + + " \t\t userTasks \t\t all userTasks of the system\n" + + " \t\t taskAssignments \t\t all taskAssignments of the system\n" + + " \t\t permissions \t\t all permissions of the system\n" + + "\n" + + "Do use for exporting java objects of a specified class.\n" + + "\n" ); } } diff --git a/ccm-core/src/com/arsdigita/portation/conversion/MainConversion.java b/ccm-core/src/com/arsdigita/portation/conversion/MainConversion.java deleted file mode 100644 index f09429a42..000000000 --- a/ccm-core/src/com/arsdigita/portation/conversion/MainConversion.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * 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; - -/** - * @author Tobias Osmers + * @version created the 6/27/16 + */ +public class MainConverter { + + /** + * Method, to start all the different converter classes in a specific + * order, so that dependencies can only be set, where the objects have + * already been created. + */ + public static void startConversionToNg() { + CategoryConversion.convertAll(); + UserConversion.convertAll(); + GroupConversion.convertAll(); + RoleConversion.convertAll(); + WorkflowConversion.convertAll(); + UserTaskConversion.convertAll(); + PermissionConversion.convertAll(); + } +} diff --git a/ccm-core/src/com/arsdigita/portation/conversion/NgCollection.java b/ccm-core/src/com/arsdigita/portation/conversion/NgCollection.java index 116fc29f9..b963cde1d 100644 --- a/ccm-core/src/com/arsdigita/portation/conversion/NgCollection.java +++ b/ccm-core/src/com/arsdigita/portation/conversion/NgCollection.java @@ -37,8 +37,11 @@ import java.util.HashMap; import java.util.Map; /** + * Storage class for all ng-objects after conversion. This also helps for an + * easier access for the restoration of the dependencies. + * * @author Tobias Osmers - * @version created the 6/29/16 + * @version created the 29.6.16 */ public class CategoryConversion { + /** + * Retrieves all trunk-{@link com.arsdigita.categorization.Category}s from + * the persistent storage and collects them in a list. Then calls for + * creating the equivalent ng-{@link Category}s focusing on keeping all the + * associations in tact. + */ public static void convertAll() { List trunkCategories = com .arsdigita.categorization.Category.getAllObjectCategories(); - // create categories - trunkCategories.forEach(Category::new); - - setAssociations(trunkCategories); + createCategoryAndSetAssociations(trunkCategories); } /** - * Sets associations. Needs to be separate, so that all categories have - * been converted before. Otherwise it will be complex to get parent. + * Creates the equivalent ng-class of the {@code Category} and restores + * the associations to other classes. * - * @param trunkCategories + * @param trunkCategories List of all + * {@link com.arsdigita.categorization.Category}s + * from this old trunk-system. */ - private static void setAssociations( + private static void createCategoryAndSetAssociations( List trunkCategories) { for (com.arsdigita.categorization.Category trunkCategory : trunkCategories) { - Category category = NgCollection.categories.get(trunkCategory - .getID() - .longValue()); + // create categories + Category category = new Category(trunkCategory); - // set parent associations - Category parentCategory = NgCollection.categories.get(trunkCategory - .getDefaultParentCategory().getID().longValue()); - if (category != null && parentCategory != null) { - // set parent and opposed association + + // set parent and opposed association + Category parentCategory = null; + try { + com.arsdigita.categorization.Category defaultParent = + trunkCategory.getDefaultParentCategory(); + + if (defaultParent != null) { + parentCategory = NgCollection.categories.get( + defaultParent.getID().longValue()); + } + } catch (Exception e) {} + if (parentCategory != null) { category.setParentCategory(parentCategory); parentCategory.addSubCategory(category); } - // create categorizations only for category typed objects + // categorizations only for category typed objects CategorizedCollection categorizedCollection = trunkCategory .getObjects(com.arsdigita.categorization.Category .BASE_DATA_OBJECT_TYPE); @@ -74,6 +91,15 @@ public class CategoryConversion { } } + /** + * Method for creating {@link Categorization}s between {@link Category}s + * and {@link CcmObject}s which is an association-class and has not been + * existent in this old system. + * + * @param category The {@link Category} + * @param categorizedObjects A collection of the {@code Categorization}s + * as they are represented in this trunk-system + */ private static void createCategorizations(Category category, CategorizedCollection categorizedObjects) { 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 38aa78646..0a9da456c 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 @@ -28,11 +28,22 @@ import java.util.ArrayList; import java.util.List; /** + * Class for converting all trunk-{@link com.arsdigita.kernel.Group}s into + * ng-{@link Group}s as preparation for a successful export of all trunk + * classes into the new ng-system. + * * @author Tobias Osmers - * @version created the 7/14/16 + * @version created the 14.7.16 */ public class PermissionConversion { + /** + * Retrieves all trunk-{@link com.arsdigita.kernel.permissions.Permission}s + * from the persistent storage and collects them in a list. Then calls for + * creating the equivalent ng-{@link Permission}s focusing on keeping all + * the associations in tact. The association to the {@code + * grantee}-{@link Role} has to be recreated separately. + */ public static void convertAll() { List trunkPermissions = com.arsdigita.kernel.permissions.Permission .getAllObjectPermissions(); createPermissionsAndSetAssociations(trunkPermissions); + + setGranteeDependency(trunkPermissions); } + /** + * Creates the equivalent ng-class of the {@code Permission} and restores + * the associations to other classes. + * + * @param trunkPermissions List of all + * {@link com.arsdigita.kernel.permissions.Permission}s + * from the old trunk-system + */ private static void createPermissionsAndSetAssociations(List trunkPermissions) { for (com.arsdigita.kernel.permissions.Permission trunkPermission : @@ -57,13 +85,6 @@ public class PermissionConversion { object.addPermission(permission); } - // set grantee and opposed associations - Role role = NgCollection.roles.get(0); //Todo: fix "0" - if (role != null) { - permission.setGrantee(role); - role.addPermission(permission); - } - // set creationUser User creationUser = NgCollection.users.get(trunkPermission .getCreationUser().getID().longValue()); @@ -71,4 +92,82 @@ public class PermissionConversion { permission.setCreationUser(creationUser); } } + + /** + * Method recreating the association to class {@link Role} representing the + * {@code grantee} of a Permission. Because the {@code grantee} in the + * trunk-{@link com.arsdigita.kernel.permissions.Permission} is instance + * of the trunk-{@link Party}-class, there need to be separated two + * cases: + * a) were the {@code grantee} of the trunk-system is of class + * {@link com.arsdigita.kernel.Group} therefore listing {@code + * Roles}, represented by this {@code Group}, which represent + * the {@code grantee} of ng-{@link Permission}s. + * b) were the {@code grantee} of the trunk-system is of class + * {@link com.arsdigita.kernel.User} therefore having no {@code + * Role}-representation, which has specifically to be created. + * + * @param trunkPermissions List of all + * {@link com.arsdigita.kernel.permissions.Permission}s + * from the old trunk-system + */ + private static void setGranteeDependency(List trunkPermissions) { + for (com.arsdigita.kernel.permissions.Permission trunkPermission : + trunkPermissions) { + Permission permission = NgCollection.permissions.get + (trunkPermission.getID().longValue()); + + BigDecimal trunkGranteeId = (BigDecimal) trunkPermission + .getPartyOID().get("id"); + List trunkParties = Party.getAllObjectParties(); + trunkParties.stream().filter(p -> Objects.equals(p.getID(), + trunkGranteeId)).collect(Collectors.toList()); + + for (Party trunkGranteeParty : trunkParties) { + // grantee instance of Group, possibly multiple roles + if (trunkGranteeParty instanceof Group) { + RoleCollection granteeCollection = ((Group) + trunkGranteeParty).getRoles(); + boolean multipleGrantees = false; + while (granteeCollection.next()) { + Role role = NgCollection.roles.get(granteeCollection + .getRole().getID().longValue()); + + // set grantee and opposed associations + if (!multipleGrantees) { + permission.setGrantee(role); + role.addPermission(permission); + multipleGrantees = true; + } else { + Permission duplicatePermission = new Permission + (permission); + duplicatePermission.setGrantee(role); + role.addPermission(duplicatePermission); + } + } + // grantee instance of User, new Role necessary + } else if (trunkGranteeParty instanceof com.arsdigita.kernel + .User) { + com.arsdigita.kernel.User trunkGranteeUser = (com + .arsdigita.kernel.User) trunkGranteeParty; + + // create new role for this user and its membership + User member = NgCollection.users.get + (trunkGranteeUser.getID().longValue()); + // might cause problems cause the + // task assignments are missing + Role granteeRole = new Role(member.getName() + "_role"); + RoleMembership roleMembership = new RoleMembership + (granteeRole, member); + member.addRoleMembership(roleMembership); + granteeRole.addMembership(roleMembership); + + // set grantee and opposed association + permission.setGrantee(granteeRole); + granteeRole.addPermission(permission); + } + } + } + } } diff --git a/ccm-core/src/com/arsdigita/portation/conversion/core/security/RoleConversion.java b/ccm-core/src/com/arsdigita/portation/conversion/core/security/RoleConversion.java index c40689f80..b610c0886 100644 --- a/ccm-core/src/com/arsdigita/portation/conversion/core/security/RoleConversion.java +++ b/ccm-core/src/com/arsdigita/portation/conversion/core/security/RoleConversion.java @@ -28,33 +28,57 @@ import com.arsdigita.portation.modules.core.security.RoleMembership; import java.util.List; /** + * Class for converting all trunk-{@link com.arsdigita.kernel.Role}s into + * ng-{@link Role}s as preparation for a successful export of all trunk + * classes into the new ng-system. + * * @author Tobias Osmers - * @version created the 7/4/16 + * @version created the 4.7.16 */ public class UserConversion { + /** + * Retrieves all trunk-{@link com.arsdigita.kernel.User}s from the + * persistent storage and collects them in a list. Then calls for + * creating the equivalent ng-{@link User}s. + */ public static void convertAll() { List trunkUsers = com.arsdigita.kernel .User.getAllObjectUsers(); diff --git a/ccm-core/src/com/arsdigita/portation/conversion/core/workflow/UserTaskConversion.java b/ccm-core/src/com/arsdigita/portation/conversion/core/workflow/UserTaskConversion.java index b64558906..9b99c7372 100644 --- a/ccm-core/src/com/arsdigita/portation/conversion/core/workflow/UserTaskConversion.java +++ b/ccm-core/src/com/arsdigita/portation/conversion/core/workflow/UserTaskConversion.java @@ -19,9 +19,12 @@ package com.arsdigita.portation.conversion.core.workflow; -import com.arsdigita.kernel.UserCollection; +import com.arsdigita.kernel.GroupCollection; +import com.arsdigita.kernel.RoleCollection; import com.arsdigita.portation.conversion.NgCollection; +import com.arsdigita.portation.modules.core.security.Role; import com.arsdigita.portation.modules.core.security.User; +import com.arsdigita.portation.modules.core.workflow.TaskAssignment; import com.arsdigita.portation.modules.core.workflow.UserTask; import com.arsdigita.portation.modules.core.workflow.Workflow; import com.arsdigita.workflow.simple.Task; @@ -30,11 +33,23 @@ import java.util.Iterator; import java.util.List; /** + * Class for converting all + * trunk-{@link com.arsdigita.workflow.simple.UserTask}s into + * ng-{@link UserTask}s as preparation for a successful export of all trunk + * classes into the new ng-system. + * * @author Tobias Osmers - * @version created the 6/27/16 + * @version created the 27.6.16 */ public class WorkflowConversion { + /** + * Retrieves all trunk-{@link com.arsdigita.workflow.simple.Workflow}s from + * the persistent storage and collects them in a list. Then calls for + * creating the equivalent ng-{@link Workflow}s. + */ public static void convertAll() { List trunkWorkflows = com.arsdigita.workflow.simple.Workflow.getAllObjectWorkflows(); diff --git a/ccm-core/src/com/arsdigita/portation/modules/core/categorization/Category.java b/ccm-core/src/com/arsdigita/portation/modules/core/categorization/Category.java index a96f41b38..c91a3b1e4 100644 --- a/ccm-core/src/com/arsdigita/portation/modules/core/categorization/Category.java +++ b/ccm-core/src/com/arsdigita/portation/modules/core/categorization/Category.java @@ -19,6 +19,7 @@ package com.arsdigita.portation.modules.core.categorization; import com.arsdigita.categorization.CategoryLocalization; +import com.arsdigita.categorization.CategoryLocalizationCollection; import com.arsdigita.portation.AbstractMarshaller; import com.arsdigita.portation.Identifiable; import com.arsdigita.portation.conversion.NgCollection; @@ -66,11 +67,17 @@ public class Category extends CcmObject { this.uniqueId = trunkCategory.getID().toString(); this.name = trunkCategory.getName(); - CategoryLocalization categoryLocalization = trunkCategory - .getCategoryLocalizationCollection().getCategoryLocalization(); - Locale locale = new Locale(categoryLocalization.getLocale()); - this.title.addValue(locale, categoryLocalization.getName()); - this.description.addValue(locale, categoryLocalization.getDescription()); + CategoryLocalizationCollection categoryLocalizationCollection = + trunkCategory.getCategoryLocalizationCollection(); + if (categoryLocalizationCollection != null && + categoryLocalizationCollection.next()) { + CategoryLocalization categoryLocalization = trunkCategory + .getCategoryLocalizationCollection().getCategoryLocalization(); + + Locale locale = new Locale(categoryLocalization.getLocale()); + this.title.addValue(locale, categoryLocalization.getName()); + this.description.addValue(locale, categoryLocalization.getDescription()); + } this.enabled = trunkCategory.isEnabled(); this.visible = trunkCategory.isVisible(); @@ -81,8 +88,13 @@ public class Category extends CcmObject { //this.parentCategory - this.categoryOrder = trunkCategory.getDefaultParentCategory() - .getNumberOfChildCategories() + 1; + com.arsdigita.categorization.Category defaultParent = null; + try { + defaultParent = trunkCategory.getDefaultParentCategory(); + } catch (Exception e) {} + this.categoryOrder = defaultParent != null + ? defaultParent.getNumberOfChildCategories() + 1 + : 0; NgCollection.categories.put(this.getObjectId(), this); } diff --git a/ccm-core/src/com/arsdigita/portation/modules/core/security/Permission.java b/ccm-core/src/com/arsdigita/portation/modules/core/security/Permission.java index 0a3e6995c..1247a8c55 100644 --- a/ccm-core/src/com/arsdigita/portation/modules/core/security/Permission.java +++ b/ccm-core/src/com/arsdigita/portation/modules/core/security/Permission.java @@ -55,6 +55,21 @@ public class Permission implements Identifiable { NgCollection.permissions.put(this.permissionId, this); } + public Permission(final Permission ngPermission) { + this.permissionId = NgCollection.permissions.size() + 1; + this.grantedPrivilege = ngPermission.getGrantedPrivilege(); + + this.object = ngPermission.getObject(); + this.grantee = ngPermission.getGrantee(); + this.creationUser = ngPermission.getCreationUser(); + + this.creationDate = ngPermission.getCreationDate(); + this.creationIp = ngPermission.getCreationIp(); + + NgCollection.permissions.put(this.permissionId, this); + + } + @Override public AbstractMarshaller getMarshaller() { return new PermissionMarshaller(); diff --git a/ccm-core/src/com/arsdigita/portation/modules/core/security/Role.java b/ccm-core/src/com/arsdigita/portation/modules/core/security/Role.java index 7ae161935..97eacdda8 100644 --- a/ccm-core/src/com/arsdigita/portation/modules/core/security/Role.java +++ b/ccm-core/src/com/arsdigita/portation/modules/core/security/Role.java @@ -42,7 +42,7 @@ public class Role implements Identifiable { private List permissions; private List assignedTasks; - public Role(com.arsdigita.kernel.Role trunkRole) { + public Role(final com.arsdigita.kernel.Role trunkRole) { this.roleId = trunkRole.getID().longValue(); this.name = trunkRole.getName(); @@ -54,6 +54,18 @@ public class Role implements Identifiable { NgCollection.roles.put(this.roleId, this); } + public Role(final String name) { + this.roleId = NgCollection.roles.size() + 1; + this.name = name; + + this.memberships = new HashSet<>(); + + this.permissions = new ArrayList<>(); + this.assignedTasks = new ArrayList<>(); + + NgCollection.roles.put(this.roleId, this); + } + @Override public AbstractMarshaller getMarshaller() { return new RoleMarshaller();