From b1a35b0ed95d89d38b65cf6a587866df0701a6a8 Mon Sep 17 00:00:00 2001 From: tosmers Date: Tue, 31 May 2016 16:19:39 +0000 Subject: [PATCH] adds cli tool for exportation; adds User and Group implementation especially designed for portation; modifies the UserExporter git-svn-id: https://svn.libreccm.org/ccm/trunk@4123 8810af33-2d31-482b-a856-94f89814c4df --- ccm-core/bin/ccm-run | 2 +- ccm-core/src/com/arsdigita/kernel/User.java | 14 +- .../portation/AbstractMarshaller.java | 82 ++------- .../com/arsdigita/portation/Identifiable.java | 3 + .../com/arsdigita/portation/Marshaller.java | 61 +------ .../portation/categories/Group/Group.java | 50 ++++++ .../portation/categories/User/User.java | 156 ++++++++++++++++++ .../categories/User/UserMarshaller.java | 28 ++++ .../portation/cmd/ExportCliTool.java | 117 +++++++++++++ .../arsdigita/portation/cmd/UserExport.java | 64 +++++++ ccm-core/src/log4j.properties | 2 +- 11 files changed, 440 insertions(+), 139 deletions(-) create mode 100644 ccm-core/src/com/arsdigita/portation/categories/Group/Group.java create mode 100644 ccm-core/src/com/arsdigita/portation/categories/User/User.java create mode 100644 ccm-core/src/com/arsdigita/portation/categories/User/UserMarshaller.java create mode 100644 ccm-core/src/com/arsdigita/portation/cmd/ExportCliTool.java create mode 100644 ccm-core/src/com/arsdigita/portation/cmd/UserExport.java diff --git a/ccm-core/bin/ccm-run b/ccm-core/bin/ccm-run index a58fde6e9..ad2f894f1 100644 --- a/ccm-core/bin/ccm-run +++ b/ccm-core/bin/ccm-run @@ -79,7 +79,7 @@ files=$(ls ${CCM_LIB_DIR}/ccm-core*.jar 2> /dev/null | wc -l) if [ "$files" == "0" ] then - echo "Error: CCM_LIB_DIR is invalid \(no ccm-core*.jar file\(s\) in CCM_LIB_DIR\)." + echo "Error: CCMfont_LIB_DIR is invalid \(no ccm-core*.jar file\(s\) in CCM_LIB_DIR\)." exit 1 fi diff --git a/ccm-core/src/com/arsdigita/kernel/User.java b/ccm-core/src/com/arsdigita/kernel/User.java index ef82ac4a9..a2456e692 100755 --- a/ccm-core/src/com/arsdigita/kernel/User.java +++ b/ccm-core/src/com/arsdigita/kernel/User.java @@ -19,24 +19,17 @@ package com.arsdigita.kernel; // Identity class. -import java.math.BigDecimal; import com.arsdigita.domain.DataObjectNotFoundException; import com.arsdigita.domain.DomainObjectFactory; import com.arsdigita.kernel.permissions.PermissionDescriptor; import com.arsdigita.kernel.permissions.PermissionService; import com.arsdigita.kernel.permissions.PrivilegeDescriptor; -import com.arsdigita.persistence.DataAssociation; -import com.arsdigita.persistence.DataAssociationCursor; -import com.arsdigita.persistence.DataObject; -import com.arsdigita.persistence.DataOperation; -import com.arsdigita.persistence.DataQuery; -import com.arsdigita.persistence.Filter; -import com.arsdigita.persistence.OID; -import com.arsdigita.persistence.PersistenceException; -import com.arsdigita.persistence.SessionManager; +import com.arsdigita.persistence.*; import com.arsdigita.persistence.metadata.ObjectType; +import java.math.BigDecimal; + /** * Represents a user. * @@ -476,5 +469,4 @@ public class User extends Party { public void setBanned(boolean b) { set(BANNED, new Boolean(b)); } - } diff --git a/ccm-core/src/com/arsdigita/portation/AbstractMarshaller.java b/ccm-core/src/com/arsdigita/portation/AbstractMarshaller.java index 2ce599035..be974b218 100644 --- a/ccm-core/src/com/arsdigita/portation/AbstractMarshaller.java +++ b/ccm-core/src/com/arsdigita/portation/AbstractMarshaller.java @@ -18,6 +18,8 @@ */ package com.arsdigita.portation; +import com.fasterxml.jackson.annotation.JsonAutoDetect; +import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.dataformat.xml.JacksonXmlModule; @@ -27,8 +29,6 @@ import org.apache.log4j.Logger; import java.io.File; import java.io.FileWriter; import java.io.IOException; -import java.nio.file.Files; -import java.util.ArrayList; import java.util.List; /** @@ -51,19 +51,13 @@ public abstract class AbstractMarshaller { // XML specifics ObjectMapper xmlMapper; - // JSON specifics - // CSV specifics - - - - public void prepare(final Format format, String filename, boolean - indentation) { + public void prepare(final Format format, String filename, boolean indentation) { this.format = format; - this.filename = filename; switch (this.format) { case XML: + this.filename = filename + ".xml"; // for additional configuration JacksonXmlModule module = new JacksonXmlModule(); module.setDefaultUseWrapper(false); @@ -71,12 +65,7 @@ public abstract class AbstractMarshaller { if (indentation) { xmlMapper.enable(SerializationFeature.INDENT_OUTPUT); } - break; - - case JSON: - break; - - case CSV: + xmlMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); break; default: @@ -84,6 +73,12 @@ public abstract class AbstractMarshaller { } } + public void prepare(final Format format, String folderPath, String filename, boolean indentation) { + File file = new File(folderPath); + if (file.exists() || file.mkdirs()) { + prepare(format, folderPath + "/" + filename, indentation); + } + } public void exportList(final List exportList) { File file = new File(filename); @@ -106,17 +101,11 @@ public abstract class AbstractMarshaller { //log.info(line); } catch (IOException e) { log.error(String.format("Unable to write objetct " + - "of class %s as XML string with name %s.", + "of %s as XML string with name %s.", object.getClass(), file.getName()), e); } break; - case JSON: - break; - - case CSV: - break; - default: break; } @@ -141,51 +130,4 @@ public abstract class AbstractMarshaller { } } - - protected abstract Class getObjectClass(); - protected abstract void insertIntoDb(I object); - - public List importFile() { - File file = new File(filename); - - List lines = null; - try { - lines = Files.readAllLines(file.toPath()); - } catch (IOException e) { - log.error(String.format("Unable to read lines of the file with " + - "name %s.", file.getName())); - } - - List objects = new ArrayList(); - if (lines != null) { - for (String line : lines) { - I object = null; - switch (format) { - case XML: - try { - object = xmlMapper.readValue(line, getObjectClass()); - } catch (IOException e) { - log.error(String.format("Unable to read objects " + - "from XML line:\n \"%s\"", line), e); - } - break; - - case JSON: - break; - - case CSV: - break; - - default: - break; - } - - assert object != null; - insertIntoDb(object); - objects.add(object); - } - } - return objects; - } - } diff --git a/ccm-core/src/com/arsdigita/portation/Identifiable.java b/ccm-core/src/com/arsdigita/portation/Identifiable.java index f8b15fbf5..9011b6999 100644 --- a/ccm-core/src/com/arsdigita/portation/Identifiable.java +++ b/ccm-core/src/com/arsdigita/portation/Identifiable.java @@ -24,5 +24,8 @@ package com.arsdigita.portation; */ public interface Identifiable { + String getTrunkClass(); + void setTrunkClass(String trunkClass); + AbstractMarshaller getMarshaller(); } diff --git a/ccm-core/src/com/arsdigita/portation/Marshaller.java b/ccm-core/src/com/arsdigita/portation/Marshaller.java index b70a8f873..bdd494df2 100644 --- a/ccm-core/src/com/arsdigita/portation/Marshaller.java +++ b/ccm-core/src/com/arsdigita/portation/Marshaller.java @@ -21,6 +21,7 @@ package com.arsdigita.portation; import org.apache.log4j.Logger; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -39,7 +40,7 @@ public class Marshaller { // Assigns lists with objects of the same type as values to their typ as // key. - private Map, List> classListMap; + private Map, List> classListMap = new HashMap<>(); /** @@ -50,12 +51,13 @@ public class Marshaller { * @param format The export style/format e.g. CSV or JSON * @param filename The name of the file to be exported to */ - public void exportObjects(List objects, Format format, + public void exportObjects(List objects, Format format, String filename) { putObjects(objects); for (Map.Entry, List> classListEntry : classListMap.entrySet()) { + exportList(classListEntry.getValue(), classListEntry.getKey(), format, filename); } @@ -70,7 +72,7 @@ public class Marshaller { * * @param objects list of all objects being organized */ - private void putObjects(List objects) { + private void putObjects(List objects) { for (Identifiable object : objects) { Class type = object.getClass(); @@ -109,58 +111,5 @@ public class Marshaller { marshaller.exportList(list); } - /** - * Selects the right marshaller for each file being imported depending on - * the filename. Therefore the filename has to contain the name of the - * class this file stores objects for. The marshaller will then be - * initialized and be called for importing the objects contained in the - * file being processed. - * - * Naming convention for the import file name: - * __. - * - * @param filenames List of filenames for the files wishing to be imported - * @param format The import style - * @param The type of the current marshaller - */ - public void importObjects( - List filenames, Format format) { - for (String filename : filenames) { - String[] splitFilename = filename.split("__"); - String className = - splitFilename[splitFilename.length].split(".")[0]; - - try { - Class clazz = Class.forName(className); - @SuppressWarnings("unchecked") - Class type = clazz.asSubclass(Identifiable.class); - - I instance = null; - try { - instance = type.newInstance(); - } catch (InstantiationException | IllegalAccessException e) { - log.error(String.format("Error finding an instance for " + - "the given type %s.", type.getName()), e); - } - - if (instance != null) { - @SuppressWarnings("unchecked") - AbstractMarshaller marshaller = (AbstractMarshaller) - instance.getMarshaller(); - - marshaller.prepare(format, filename, false); - marshaller.importFile(); - } else { - log.error(String.format("Class instance for type %s has " + - "has null value!", type.getName())); - } - } catch (ClassNotFoundException e) { - log.error(String.format("Error finding class for given name: " + - "%s.", className), e); - } - } - } - - } diff --git a/ccm-core/src/com/arsdigita/portation/categories/Group/Group.java b/ccm-core/src/com/arsdigita/portation/categories/Group/Group.java new file mode 100644 index 000000000..accc73f1f --- /dev/null +++ b/ccm-core/src/com/arsdigita/portation/categories/Group/Group.java @@ -0,0 +1,50 @@ +/* + * 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.categories.Group; + +import com.arsdigita.portation.AbstractMarshaller; +import com.arsdigita.portation.Identifiable; + +/** + * @author Tobias Osmers<\a> + * @version created the 31.05.16 + */ +public class User implements Identifiable { + private static final Logger logger = Logger.getLogger(User.class); + + private String trunkClass; + private long id; + private String name; + private String personName; + private String screenName; + private String displayName; + private List groups; + private String primaryMailAdress; + private List mailAdresses; + + public User(com.arsdigita.kernel.User sysUser) { + this.trunkClass = sysUser.getClass().getName(); + + this.id = sysUser.getID().longValue(); + this.name = sysUser.getName(); + this.personName = sysUser.getPersonName().toString(); + this.screenName = sysUser.getScreenName(); + this.displayName = sysUser.getDisplayName(); + this.groups = convertGroups(sysUser.getGroups()); + this.primaryMailAdress = sysUser.getPrimaryEmail().getEmailAddress(); + this.mailAdresses = convertMailAdresses(sysUser.getAlternateEmails()); + } + + private List convertGroups(com.arsdigita.kernel.GroupCollection groupCollection) { + List groups = new ArrayList<>(); + if (groupCollection != null) { + while (groupCollection.next()) { + groups.add(new Group(groupCollection.getGroup())); + } + groupCollection.close(); + } else { + logger.error("A Failed to export, due to empty user list."); + } + return groups; + } + + private List convertMailAdresses(Iterator it) { + List mailAdresses = new ArrayList<>(); + if (it != null) { + while (it.hasNext()) { + mailAdresses.add(((EmailAddress) it.next()).getEmailAddress()); + } + } else { + logger.error("A Failed to export, due to empty user list."); + } + return mailAdresses; + } + + @Override + public String getTrunkClass() { + return trunkClass; + } + + @Override + public void setTrunkClass(String trunkClass) { + this.trunkClass = trunkClass; + } + + @Override + public AbstractMarshaller getMarshaller() { + return new UserMarshaller(); + } + + public long getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getPersonName() { + return personName; + } + + public void setPersonName(String personName) { + this.personName = personName; + } + + public String getScreenName() { + return screenName; + } + + public void setScreenName(String screenName) { + this.screenName = screenName; + } + + public String getDisplayName() { + return displayName; + } + + public void setDisplayName(String displayName) { + this.displayName = displayName; + } + + public List getGroups() { + return groups; + } + + public void setGroups(List groups) { + this.groups = groups; + } + + public List getMailAdresses() { + return mailAdresses; + } + + public void setMailAdresses(List mailAdresses) { + this.mailAdresses = mailAdresses; + } +} diff --git a/ccm-core/src/com/arsdigita/portation/categories/User/UserMarshaller.java b/ccm-core/src/com/arsdigita/portation/categories/User/UserMarshaller.java new file mode 100644 index 000000000..337c46796 --- /dev/null +++ b/ccm-core/src/com/arsdigita/portation/categories/User/UserMarshaller.java @@ -0,0 +1,28 @@ +/* + * 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.categories.User; + +import com.arsdigita.portation.AbstractMarshaller; + +/** + * @author Tobias Osmers<\a> + * @version created the 25.05.16 + */ +public class ExportCliTool extends Program { + + private final static Logger logger = Logger.getLogger(ExportCliTool.class); + + private ExportCliTool() { + super("Export Commandline Tool", + "1.0.0", + "Exportation of POJOs..."); + } + + public static void main(String[] args) { + new ExportCliTool().run(args); + } + + @Override + protected void doRun(CommandLine cmdLine) { + final String[] args = cmdLine.getArgs(); + + if (args.length < 1) { + printUsage(); + System.exit(-1); + } + + final String command = args[0]; + System.out.printf("Command ist %s\n", command); + + switch (command) { + case "help": + printUsage(); + break; + case "export": + createTestFolder(); + export(args); + break; + default: + printUsage(); + break; + } + } + + private void printUsage() { + System.err.printf( + "\t\t\t--- ExportCliTool ---\n" + + "usage:\t []\n" + + "\n" + + "Available commands:\n" + + "\tlist \t\t Shows information on how to use this tool.\n" + + "\texport \t\t Exports the chosen category to xml file.\n" + + "\n" + + "Available categories for export:\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" + ); + } + + private void export(String[] args) { + if (args.length < 2) { + printUsage(); + System.exit(-1); + } + + final String category = args[1]; + + switch (category) { + case "users": + try { + System.out.printf("\nStarting export of users...\n\n"); + UserExport userExport = new UserExport(); + userExport.export(); + System.out.printf("\n...done!\n\n"); + } catch (Exception ex) { + logger.error("ERROR", ex); + } + break; + case "groups": + break; + default: + printUsage(); + break; + } + } + + private void createTestFolder() { + + } +} diff --git a/ccm-core/src/com/arsdigita/portation/cmd/UserExport.java b/ccm-core/src/com/arsdigita/portation/cmd/UserExport.java new file mode 100644 index 000000000..f223ce8b3 --- /dev/null +++ b/ccm-core/src/com/arsdigita/portation/cmd/UserExport.java @@ -0,0 +1,64 @@ +/* + * 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.cmd; + +import com.arsdigita.portation.categories.User.User; +import com.arsdigita.kernel.UserCollection; +import com.arsdigita.portation.Format; +import com.arsdigita.portation.categories.User.UserMarshaller; +import org.apache.log4j.Logger; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * @author System.out.println(l.toString())); + Arrays.stream(com.arsdigita.kernel.User.class.getFields()).forEach(l -> System.out.println(l.toString())); + } + + public void export() { + UserMarshaller userMarshaller = new UserMarshaller(); + userMarshaller.prepare(Format.XML, "PortationTestFiles", "test1", true); + userMarshaller.exportList(users); + } +} diff --git a/ccm-core/src/log4j.properties b/ccm-core/src/log4j.properties index 410ea406d..7a64dc09e 100755 --- a/ccm-core/src/log4j.properties +++ b/ccm-core/src/log4j.properties @@ -58,7 +58,7 @@ log4j.logger.com.arsdigita.web.CCMApplicationContextListener=INFO # Package redhat.persistence # ========================== # For debugging all queries run by persistence -#log4j.logger.com.redhat.persistence.engine.rdbms.RDBMSEngine=INFO +#log4j.logger.com.redhat.persistence.engine.rdbms.RDBMSEngine=iu # Package templating # ==================