[FEATURE]

- adds the base structure for the new second version of the exporter/importer routine
- the abstract super class for the objectentities, the enum for possible fomats and the marshal itself for im-/exporting list of objects
- adds a first implementation of the AbstractResourceMarshal for extending the abstact super 'AbstactMarshal'

git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@3850 8810af33-2d31-482b-a856-94f89814c4df
pull/2/head
tosmers 2016-02-10 18:30:13 +00:00
parent 0040229a9d
commit bd4bf615f3
4 changed files with 260 additions and 0 deletions

View File

@ -0,0 +1,129 @@
/*
* 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 org.libreccm.portation;
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
import org.apache.log4j.Logger;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* Abstract class responsible for ex- and importing entity-objects to several
* file-formats. Every entity-class (e.g. DocRepo.File) needs to have its own
* extension of this class to override the abstract methods, making it
* possible to ex- or import that extending entity-class (e.g. DocRepo
* .FileMarshal).
*
* @author <a href="mailto:tosmers@uni-bremen.de>Tobias Osmers</a>
* @version created the 2/10/16
*/
public abstract class AbstractMarshal<T> {
private static final Logger log = Logger.getLogger(AbstractMarshal.class);
private File file;
// CSV specifics
private CsvMapper mapper;
private CsvSchema schema;
public void init(final Format format, String filename) {
switch (format) {
case CSV:
file = new File(filename);
mapper = new CsvMapper();
schema = getCsvSchema();
schema = mapper.schemaFor(getClassT());
break;
default:
break;
}
}
/* CSV Export/Import */
/**
* Main export method. Exports a list of same typed objects to CSV
* strings and writes them into the same file with a name specific to
* their class.
*
* TODO: throw exception for ui
* TODO: error message not with uuid
*
* @param exportObjects List of {@code T}-tpyed objects being exported to
* CSV
*/
public void exportCSV(final List<T> exportObjects) {
exportObjects.forEach(t -> {
try {
mapper.writer(schema).writeValue(file, t);
} catch (IOException e) {
log.error(String.format("Error writing object with UUID " +
"%d to CSV-file with the name %s.",
1234, file.getName()));
}
});
}
/**
* Main import method. Imports a list of string representing object of
* the same type ({@code T}) from a CSV-file and parses them into the
* corresponding objects.
*
* TODO: throw exception for ui
* TODO: error message not with uuid
*
* @return List of {@code T}-typed objects being imported from CSV
*/
public List<T> importCSV() {
List<T> importObjects = new ArrayList<>();
try {
mapper.readerFor(getClassT()).with(schema).readValues(file)
.forEachRemaining(t -> importObjects.add((T) t));
} catch (IOException e) {
log.error(String.format("Error reading object with UUID %d from " +
"CSV-file with the name %s.", 1234, file.getName()));
}
return importObjects;
}
/**
* Abstract method to get the class of the extending subclass fulfilling
* the generic type {@code T}.
*
* @return Class of the extending subclass.
*/
protected abstract Class getClassT();
/**
* Abstract method to get the CSV schema needed for both ex- and
* importation specific to every extending subclass.
*
* @return The CSV schema specific to the implementing subclass
*/
protected abstract CsvSchema getCsvSchema();
/* JSON Export/Import */
}

View File

@ -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 org.libreccm.portation;
/**
* @author <a href="mailto:tosmers@uni-bremen.de">Tobias Osmers</a>
* @version created the 03.02.2016
*/
public enum Format {
CSV
}

View File

@ -0,0 +1,48 @@
/*
* 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 org.libreccm.portation;
import javax.enterprise.inject.Instance;
import javax.inject.Inject;
import java.util.List;
/**
*
* @author <a href="mailto:tosmers@uni-bremen.de">Tobias Osmers</a>
* @version created the 03.02.2016
*/
public class Marshal {
@Inject
private Instance<AbstractMarshal> abstractExporters;
public void exportObjects(List<Object> objects, Format format, String
filename) {
for (Object obj : objects) {
// abstractExporters.select(obj.getClass(), new AnnotationLiteral<>() {
// });
}
}
public void importObjects(List<String> filenames, Format format) {
for (String file : filenames) {
}
}
}

View File

@ -0,0 +1,56 @@
/*
* 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 org.libreccm.docrepo;
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
import org.libreccm.portation.AbstractMarshal;
/**
* @author <a href="mailto:tosmers@uni-bremen.de>Tobias Osmers</a>
* @version created the 2/10/16
*/
public class AbstractResourceMarshal extends AbstractMarshal<AbstractResource> {
@Override
protected Class getClassT() {
return AbstractResource.class;
}
@Override
protected CsvSchema getCsvSchema() {
return CsvSchema.builder()
.addColumn("object_ID", CsvSchema.ColumnType.NUMBER)
.addColumn("displayName")
.addColumn("permissions", CsvSchema.ColumnType.ARRAY)
.addColumn("categories", CsvSchema.ColumnType.ARRAY)
.addColumn("name")
.addColumn("description")
.addColumn("path")
.addColumn("mimeType")
.addColumn("size", CsvSchema.ColumnType.NUMBER)
.addColumn("creationDate", CsvSchema.ColumnType.NUMBER)
.addColumn("lastModifiedDate", CsvSchema.ColumnType.NUMBER)
.addColumn("creationIp")
.addColumn("lastModifiedIp")
.addColumn("creationUser")
.addColumn("lastModifiedUser")
.addColumn("parent")
.addColumn("repository")
.build();
}
}