diff --git a/ccm-core/src/com/arsdigita/kernel/ACSObject.java b/ccm-core/src/com/arsdigita/kernel/ACSObject.java index 166279186..8438854d3 100755 --- a/ccm-core/src/com/arsdigita/kernel/ACSObject.java +++ b/ccm-core/src/com/arsdigita/kernel/ACSObject.java @@ -36,6 +36,7 @@ import com.arsdigita.persistence.metadata.Property; import java.math.BigDecimal; import java.sql.SQLException; +import com.arsdigita.portation.AbstractMarshaller; import org.apache.log4j.Logger; import org.apache.log4j.Priority; diff --git a/ccm-core/src/com/arsdigita/kernel/Identifiable.java b/ccm-core/src/com/arsdigita/kernel/Identifiable.java new file mode 100644 index 000000000..dfb4cd9db --- /dev/null +++ b/ccm-core/src/com/arsdigita/kernel/Identifiable.java @@ -0,0 +1,30 @@ +/* + * 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.kernel; + +import com.arsdigita.portation.AbstractMarshaller; + +/** + * @author Tobias Osmers + * @version created the 2/10/16 + */ +public abstract class AbstractMarshaller { + + private static final Logger log = Logger.getLogger(AbstractMarshaller.class); + + private Format format; + private String filename; + + // XML specifics + ObjectMapper xmlMapper; + + // JSON specifics + + // CSV specifics + + + + public void prepare(final Format format, String filename, boolean + indentation) { + this.format = format; + this.filename = filename; + + switch (this.format) { + case XML: + // for additional configuration + JacksonXmlModule module = new JacksonXmlModule(); + module.setDefaultUseWrapper(false); + xmlMapper = new XmlMapper(module); + if (indentation) { + xmlMapper.enable(SerializationFeature.INDENT_OUTPUT); + } + break; + + case JSON: + break; + + case CSV: + break; + + default: + break; + } + } + + + public void exportList(final List exportList) { + File file = new File(filename); + FileWriter fileWriter = null; + + try { + fileWriter = new FileWriter(file); + } catch (IOException e) { + log.error(String.format("Unable to open a fileWriter for the file" + + " with the name %s.", file.getName())); + } + if (fileWriter != null) { + for (I object : exportList) { + String line = null; + + switch (format) { + case XML: + try { + line = xmlMapper.writeValueAsString(object); + //log.info(line); + } catch (IOException e) { + log.error(String.format("Unable to write objetct " + + "of class %s as XML string with name %s.", + object.getClass(), file.getName()), e); + } + break; + + case JSON: + break; + + case CSV: + break; + + default: + break; + } + + if (line != null) { + try { + fileWriter.write(line); + fileWriter.write(System.getProperty("line.separator")); + } catch (IOException e) { + log.error(String.format("Unable to write to file with the" + + " name %s.", file.getName())); + } + } + } + + try { + fileWriter.close(); + } catch (IOException e) { + log.error(String.format("Unable to close a fileWriter for the" + + " file with the name %s.", file.getName())); + } + + } + } + + 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/Format.java b/ccm-core/src/com/arsdigita/portation/Format.java new file mode 100644 index 000000000..0b3c7d696 --- /dev/null +++ b/ccm-core/src/com/arsdigita/portation/Format.java @@ -0,0 +1,27 @@ +/* + * 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; + +/** + * @author Tobias Osmers + * @version created the 03.02.2016 + */ +public enum Format { + CSV, XML, JSON +} diff --git a/ccm-core/src/com/arsdigita/portation/Marshaller.java b/ccm-core/src/com/arsdigita/portation/Marshaller.java new file mode 100644 index 000000000..394876814 --- /dev/null +++ b/ccm-core/src/com/arsdigita/portation/Marshaller.java @@ -0,0 +1,167 @@ +/* + * 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; + +import com.arsdigita.kernel.Identifiable; +import org.apache.log4j.Logger; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +/** + * Central class for exporting and importing objects of this system stored in + * the database. + * + * Exporting or importing object classes need to implement + * interface identifiable. + * + * @author Tobias Osmers + * @version created the 03.02.2016 + */ +public class Marshaller { + private static final Logger log = Logger.getLogger(Marshaller.class); + + // Assigns lists with objects of the same type as values to their typ as + // key. + private Map, List> classListMap; + + + /** + * Main export method. Organizes the objects into list of the same type + * and calls a second export method for each list. + * + * @param objects All objects to be exported + * @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, + String filename) { + putObjects(objects); + + for (Map.Entry, List> + classListEntry : classListMap.entrySet()) { + exportList(classListEntry.getValue(), classListEntry.getKey(), + format, filename); + } + } + + /** + * Organizes a list of different {@link Identifiable} objects into a map + * assigning lists of the same type to their type as values to a key. The + * type which all objects of that list have in common is their key. + * That opens the possibility of being certain of the objects types in + * the list. Guarantied through this implementation. + * + * @param objects list of all objects being organized + */ + private void putObjects(List objects) { + for (Identifiable object : objects) { + Class type = object.getClass(); + + if (classListMap.containsKey(type)) { + classListMap.get(type).add(object); + } else { + List values = new ArrayList<>(); + values.add(object); + classListMap.put(type, values); + } + } + } + + /** + * Selects the right marshaller for the given type, initializes that + * marshaller for the given export wishes and calls the export method of + * that marshaller upon the given list of same typed objects. + * + * Naming convention for the export file name: + * __. + * + * @param list List of objects to be exported of the same type + * @param type The class of the type + * @param format The export style + * @param filename The filename + * @param The type of the current marshaller + */ + private void exportList(List list, Class type, Format format, String filename) { + @SuppressWarnings("unchecked") + AbstractMarshaller marshaller = (AbstractMarshaller) list.get + (0).getMarshaller(); + + marshaller.prepare(format, filename + "__" + type.toString(), + false); + 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/versioning/VersionedACSObject.java b/ccm-core/src/com/arsdigita/versioning/VersionedACSObject.java index f2a368c5b..ae857c636 100755 --- a/ccm-core/src/com/arsdigita/versioning/VersionedACSObject.java +++ b/ccm-core/src/com/arsdigita/versioning/VersionedACSObject.java @@ -27,12 +27,11 @@ import com.arsdigita.persistence.OID; import com.arsdigita.persistence.SessionManager; import com.arsdigita.persistence.metadata.ObjectType; import com.arsdigita.util.Assert; +import org.apache.log4j.Logger; import java.math.BigInteger; import java.util.Date; -import org.apache.log4j.Logger; - // old versioning /** diff --git a/ccm-docrepo/src/com/arsdigita/docrepo/File.java b/ccm-docrepo/src/com/arsdigita/docrepo/File.java index b7705c560..85ce40053 100644 --- a/ccm-docrepo/src/com/arsdigita/docrepo/File.java +++ b/ccm-docrepo/src/com/arsdigita/docrepo/File.java @@ -24,11 +24,14 @@ import com.arsdigita.mimetypes.MimeType; import com.arsdigita.persistence.DataObject; import com.arsdigita.persistence.OID; import com.arsdigita.persistence.metadata.Property; +import com.arsdigita.portation.AbstractMarshaller; import com.arsdigita.util.Assert; +import org.apache.log4j.Logger; +import org.apache.oro.text.perl.Perl5Util; import javax.activation.DataHandler; -import javax.activation.FileDataSource; import javax.activation.DataSource; +import javax.activation.FileDataSource; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -37,9 +40,6 @@ import java.math.BigDecimal; import java.util.Iterator; import java.util.Vector; -import org.apache.log4j.Logger; -import org.apache.oro.text.perl.Perl5Util; - /** * Represents a File in the document manager application. * @@ -47,7 +47,7 @@ import org.apache.oro.text.perl.Perl5Util; * @author Ron Henderson (ron@arsdigita.com) * @version $Id: File.java pboy $ */ -public class File extends ResourceImpl { +public class File extends ResourceImpl { /** Logger instance for debugging support. */ protected static Logger s_log = Logger.getLogger(File.class); diff --git a/tools-ng/ecdc/conf/build.properties b/tools-ng/ecdc/conf/build.properties index b135fb280..61add31f2 100644 --- a/tools-ng/ecdc/conf/build.properties +++ b/tools-ng/ecdc/conf/build.properties @@ -45,8 +45,8 @@ compile.nowarn=on # Optionally specifiy version of target JVM. # By default the version of the build system is determined and used as target. -compile.source=1.6 -compile.target=1.6 +compile.source=1.8 +compile.target=1.8 compile.encoding=UTF-8 # These properties are specific to Jikes