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
master
tosmers 2016-05-31 16:19:39 +00:00
parent dad0195074
commit b1a35b0ed9
11 changed files with 440 additions and 139 deletions

View File

@ -79,7 +79,7 @@ files=$(ls ${CCM_LIB_DIR}/ccm-core*.jar 2> /dev/null | wc -l)
if [ "$files" == "0" ] if [ "$files" == "0" ]
then 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 exit 1
fi fi

View File

@ -19,24 +19,17 @@
package com.arsdigita.kernel; package com.arsdigita.kernel;
// Identity class. // Identity class.
import java.math.BigDecimal;
import com.arsdigita.domain.DataObjectNotFoundException; import com.arsdigita.domain.DataObjectNotFoundException;
import com.arsdigita.domain.DomainObjectFactory; import com.arsdigita.domain.DomainObjectFactory;
import com.arsdigita.kernel.permissions.PermissionDescriptor; import com.arsdigita.kernel.permissions.PermissionDescriptor;
import com.arsdigita.kernel.permissions.PermissionService; import com.arsdigita.kernel.permissions.PermissionService;
import com.arsdigita.kernel.permissions.PrivilegeDescriptor; import com.arsdigita.kernel.permissions.PrivilegeDescriptor;
import com.arsdigita.persistence.DataAssociation; import com.arsdigita.persistence.*;
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.metadata.ObjectType; import com.arsdigita.persistence.metadata.ObjectType;
import java.math.BigDecimal;
/** /**
* Represents a user. * Represents a user.
* *
@ -476,5 +469,4 @@ public class User extends Party {
public void setBanned(boolean b) { public void setBanned(boolean b) {
set(BANNED, new Boolean(b)); set(BANNED, new Boolean(b));
} }
} }

View File

@ -18,6 +18,8 @@
*/ */
package com.arsdigita.portation; 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.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.xml.JacksonXmlModule; import com.fasterxml.jackson.dataformat.xml.JacksonXmlModule;
@ -27,8 +29,6 @@ import org.apache.log4j.Logger;
import java.io.File; import java.io.File;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List; import java.util.List;
/** /**
@ -51,19 +51,13 @@ public abstract class AbstractMarshaller<I extends Identifiable> {
// XML specifics // XML specifics
ObjectMapper xmlMapper; 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.format = format;
this.filename = filename;
switch (this.format) { switch (this.format) {
case XML: case XML:
this.filename = filename + ".xml";
// for additional configuration // for additional configuration
JacksonXmlModule module = new JacksonXmlModule(); JacksonXmlModule module = new JacksonXmlModule();
module.setDefaultUseWrapper(false); module.setDefaultUseWrapper(false);
@ -71,12 +65,7 @@ public abstract class AbstractMarshaller<I extends Identifiable> {
if (indentation) { if (indentation) {
xmlMapper.enable(SerializationFeature.INDENT_OUTPUT); xmlMapper.enable(SerializationFeature.INDENT_OUTPUT);
} }
break; xmlMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
case JSON:
break;
case CSV:
break; break;
default: default:
@ -84,6 +73,12 @@ public abstract class AbstractMarshaller<I extends Identifiable> {
} }
} }
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<I> exportList) { public void exportList(final List<I> exportList) {
File file = new File(filename); File file = new File(filename);
@ -106,17 +101,11 @@ public abstract class AbstractMarshaller<I extends Identifiable> {
//log.info(line); //log.info(line);
} catch (IOException e) { } catch (IOException e) {
log.error(String.format("Unable to write objetct " + 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); object.getClass(), file.getName()), e);
} }
break; break;
case JSON:
break;
case CSV:
break;
default: default:
break; break;
} }
@ -141,51 +130,4 @@ public abstract class AbstractMarshaller<I extends Identifiable> {
} }
} }
protected abstract Class<I> getObjectClass();
protected abstract void insertIntoDb(I object);
public List<I> importFile() {
File file = new File(filename);
List<String> 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<I> objects = new ArrayList<I>();
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;
}
} }

View File

@ -24,5 +24,8 @@ package com.arsdigita.portation;
*/ */
public interface Identifiable { public interface Identifiable {
String getTrunkClass();
void setTrunkClass(String trunkClass);
AbstractMarshaller<? extends Identifiable> getMarshaller(); AbstractMarshaller<? extends Identifiable> getMarshaller();
} }

View File

@ -21,6 +21,7 @@ package com.arsdigita.portation;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; 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 // Assigns lists with objects of the same type as values to their typ as
// key. // key.
private Map<Class<? extends Identifiable>, List<Identifiable>> classListMap; private Map<Class<? extends Identifiable>, List<Identifiable>> classListMap = new HashMap<>();
/** /**
@ -50,12 +51,13 @@ public class Marshaller {
* @param format The export style/format e.g. CSV or JSON * @param format The export style/format e.g. CSV or JSON
* @param filename The name of the file to be exported to * @param filename The name of the file to be exported to
*/ */
public void exportObjects(List<Identifiable> objects, Format format, public void exportObjects(List<? extends Identifiable> objects, Format format,
String filename) { String filename) {
putObjects(objects); putObjects(objects);
for (Map.Entry<Class<? extends Identifiable>, List<Identifiable>> for (Map.Entry<Class<? extends Identifiable>, List<Identifiable>>
classListEntry : classListMap.entrySet()) { classListEntry : classListMap.entrySet()) {
exportList(classListEntry.getValue(), classListEntry.getKey(), exportList(classListEntry.getValue(), classListEntry.getKey(),
format, filename); format, filename);
} }
@ -70,7 +72,7 @@ public class Marshaller {
* *
* @param objects list of all objects being organized * @param objects list of all objects being organized
*/ */
private void putObjects(List<Identifiable> objects) { private void putObjects(List<? extends Identifiable> objects) {
for (Identifiable object : objects) { for (Identifiable object : objects) {
Class<? extends Identifiable> type = object.getClass(); Class<? extends Identifiable> type = object.getClass();
@ -109,58 +111,5 @@ public class Marshaller {
marshaller.exportList(list); 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:
* <basic file name>__<type/class name>.<format>
*
* @param filenames List of filenames for the files wishing to be imported
* @param format The import style
* @param <I> The type of the current marshaller
*/
public <I extends Identifiable> void importObjects(
List<String> 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<I> 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<I> marshaller = (AbstractMarshaller<I>)
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);
}
}
}
} }

View File

@ -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 <a href="mailto:tosmers@uni-bremen.de>Tobias Osmers<\a>
* @version created the 31.05.16
*/
public class Group implements Identifiable {
private String trunkClass;
public Group(com.arsdigita.kernel.Group sysGroup) {
this.trunkClass = sysGroup.getClass().getName();
}
@Override
public String getTrunkClass() {
return trunkClass;
}
@Override
public void setTrunkClass(String trunkClass) {
this.trunkClass = trunkClass;
}
@Override
public AbstractMarshaller<? extends Identifiable> getMarshaller() {
return null;
}
}

View File

@ -0,0 +1,156 @@
/*
* 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.kernel.EmailAddress;
import com.arsdigita.portation.AbstractMarshaller;
import com.arsdigita.portation.Identifiable;
import com.arsdigita.portation.categories.Group.Group;
import org.apache.log4j.Logger;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* @author <a href="mailto:tosmers@uni-bremen.de>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<Group> groups;
private String primaryMailAdress;
private List<String> 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<Group> convertGroups(com.arsdigita.kernel.GroupCollection groupCollection) {
List<Group> 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<String> convertMailAdresses(Iterator it) {
List<String> 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<? extends Identifiable> 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<Group> getGroups() {
return groups;
}
public void setGroups(List<Group> groups) {
this.groups = groups;
}
public List<String> getMailAdresses() {
return mailAdresses;
}
public void setMailAdresses(List<String> mailAdresses) {
this.mailAdresses = mailAdresses;
}
}

View File

@ -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 <a href="mailto:tosmers@uni-bremen.de>Tobias Osmers<\a>
* @version created the 25.05.16
*/
public class UserMarshaller extends AbstractMarshaller<User> {
}

View File

@ -0,0 +1,117 @@
/*
* 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.util.cmd.Program;
import org.apache.commons.cli.CommandLine;
import org.apache.log4j.Logger;
/**
* Commandline tool to export all the objects of a specified class to a xml-file.
*
* @author <a href="mailto:tosmers@uni-bremen.de>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<command> [<category>]\n" +
"\n" +
"Available commands:\n" +
"\tlist \t\t Shows information on how to use this tool.\n" +
"\texport <category> \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() {
}
}

View File

@ -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 <a href="mailto:tosmers@uni-bremen.de>Tobias Osmers<\a>
* @version created the 25.05.16
*/
class UserExport {
private final static Logger logger = Logger.getLogger(UserExport.class);
private final UserCollection userCollection = com.arsdigita.kernel.User.retrieveAll();
private List<User> users = new ArrayList<>();
UserExport() {
prepare();
}
private void prepare() {
if (userCollection != null) {
while (userCollection.next()) {
users.add(new User(userCollection.getUser()));
}
userCollection.close();
} else {
logger.error("A Failed to export, due to empty user list.");
}
Arrays.stream(com.arsdigita.kernel.User.class.getDeclaredFields()).forEach(l -> 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);
}
}

View File

@ -58,7 +58,7 @@ log4j.logger.com.arsdigita.web.CCMApplicationContextListener=INFO
# Package redhat.persistence # Package redhat.persistence
# ========================== # ==========================
# For debugging all queries run by 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 # Package templating
# ================== # ==================