main structure of the AbstractMarshaller is done and contains allready the case for XML. Now it just has to be tested...

git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@3914 8810af33-2d31-482b-a856-94f89814c4df
pull/2/head
tosmers 2016-03-09 16:50:26 +00:00
parent dbd7af3c4d
commit 465029c738
7 changed files with 145 additions and 72 deletions

View File

@ -200,12 +200,19 @@
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-csv</artifactId>
</dependency>
<dependency> <!-- better xml library -->
<groupId>org.codehaus.woodstox</groupId>
<artifactId>woodstox-core-asl</artifactId>
</dependency>
</dependencies>

View File

@ -18,10 +18,18 @@
*/
package org.libreccm.portation;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.JacksonXmlModule;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import org.apache.log4j.Logger;
import org.libreccm.core.Identifiable;
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;
/**
@ -39,31 +47,33 @@ public abstract class AbstractMarshaller<I extends Identifiable> {
private static final Logger log = Logger.getLogger(AbstractMarshaller.class);
private Format format;
private String filename;
private File exportFile;
private List<File> importFiles;
// CSV specifics
// XML specifics
ObjectMapper xmlMapper;
// JSON specifics
// XML specifics
// CSV specifics
/**
*
* @param format
*/
private void init(final Format format) {
public void init(final Format format, String baseFileName) {
this.format = format;
this.filename = baseFileName;
switch (this.format) {
case CSV:
case XML:
// for additional configuration
JacksonXmlModule module = new JacksonXmlModule();
module.setDefaultUseWrapper(false);
xmlMapper = new XmlMapper(module);
break;
case JSON:
break;
case XML:
case CSV:
break;
default:
@ -71,57 +81,101 @@ public abstract class AbstractMarshaller<I extends Identifiable> {
}
}
public void init(final Format format, final String filename) {
exportFile = new File(filename);
init(format);
public void exportList(final List<I> exportList) {
File file = new File(filename + "_" + getObjectClass().toString());
FileWriter fileWriter = null;
try {
fileWriter = new FileWriter(file);
} catch (IOException e) {
log.error(String.format("Unable open a fileWriter for the file " +
"with the name %s.", file.getName()));
}
public void init(final Format format, final List<String> filenames) {
filenames.forEach(fname -> importFiles.add(new File(fname)));
init(format);
}
if (fileWriter != null) {
for (I object : exportList) {
String line = null;
/**
*
* @param exportObjects List of {@code T}-tpyed objects being exported
*/
public void exportEntities(final List<I> exportObjects) {
switch (format) {
case CSV:
case XML:
try {
line = xmlMapper.writeValueAsString(object);
} catch (JsonProcessingException e) {
log.error(String.format("Unable to write object %s " +
"as XML string.", object.getUuid()));
}
break;
case JSON:
break;
case XML:
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()));
}
}
}
}
}
/**
*
* @return List of {@code T}-typed objects being imported
*/
protected abstract Class<I> getObjectClass();
protected abstract void insertObjectIntoDB(I object);
public List<I> importEntities() {
List<I> objects = new ArrayList<>();
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()));
}
if (lines != null) {
for (String line : lines) {
I object = null;
switch (format) {
case CSV:
case XML:
try {
object = xmlMapper.readValue(line, getObjectClass());
} catch (IOException e) {
log.error(String.format("Unable to read object " +
"from XML string:\n \"%s\"", line));
}
break;
case JSON:
break;
case XML:
case CSV:
break;
default:
break;
}
return null;
objects.add(object);
insertObjectIntoDB(object);
}
}
return objects;
}
}

View File

@ -131,7 +131,7 @@ public class Marshaller {
iterator.next();
marshaller.init(format, filename);
marshaller.exportEntities(list);
marshaller.exportList(list);
}
}

View File

@ -18,8 +18,6 @@
*/
package org.libreccm.docrepo;
import org.apache.oro.text.perl.Perl5Util;
import javax.activation.MimetypesFileTypeMap;
import javax.inject.Inject;
@ -98,22 +96,23 @@ public abstract class AbstractResourceManager<T extends AbstractResource> {
* @return true for a system-valid resource name, otherwise false
*/
public boolean isValidNewResourceName(String name, T resource) {
Perl5Util perl5Util = new Perl5Util();
final String INVALID_START_PATTERN = "/^[.]+/";
final String INVALID_NAME_PATTERN = "/[^a-zA-Z0-9\\_\\.\\-\\ ]+/";
final String EXTENSION_PATTERN = "/^([^\\.].*)(\\.\\w+)$/";
// adds an extension if non-existent
if (!perl5Util.match(EXTENSION_PATTERN, name)) {
if (perl5Util.match(EXTENSION_PATTERN, resource.getName())) {
name += perl5Util.group(2);
if (name.matches(EXTENSION_PATTERN)) {
if (resource.getName().matches(EXTENSION_PATTERN)) {
// TODO: what to do here?
//name += perl5Util.group(2);
name.isEmpty();
}
}
// checks pattern of the name
boolean validName = !(perl5Util.match(INVALID_START_PATTERN, name) ||
perl5Util.match(INVALID_NAME_PATTERN, name));
boolean validName = !(name.matches(INVALID_START_PATTERN) || name
.matches(INVALID_NAME_PATTERN));
// checks duplication of the name; database access (mind performance)
validName &= (fileRepository.findByName(name).isEmpty() &&

View File

@ -28,4 +28,13 @@ import org.libreccm.portation.Marshals;
@Marshals(AbstractResource.class)
public class AbstractResourceMarshaller extends AbstractMarshaller<AbstractResource> {
@Override
protected Class<AbstractResource> getObjectClass() {
return null;
}
@Override
protected void insertObjectIntoDB(AbstractResource object) {
}
}

View File

@ -38,8 +38,7 @@ import java.util.stream.Collectors;
* @version 01/10/2015
*/
public abstract class AbstractResourceRepository<T extends AbstractResource>
extends
AbstractAuditedEntityRepository<Long, T> {
extends AbstractAuditedEntityRepository<Long, T> {
protected Class classOfT;

17
pom.xml
View File

@ -426,12 +426,6 @@
<version>1.2</version>
</dependency>
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>3.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
@ -447,12 +441,23 @@
<artifactId>jackson-databind</artifactId>
<version>2.7.0</version>
</dependency>
<dependency> <!-- for xml ex-/import -->
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.7.0</version>
</dependency>
<dependency> <!-- for csv ex-/import -->
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-csv</artifactId>
<version>2.7.0</version>
</dependency>
<dependency> <!-- better xml library -->
<groupId>org.codehaus.woodstox</groupId>
<artifactId>woodstox-core-asl</artifactId>
<version>4.4.1</version>
</dependency>
<!--
**********************
Dependencies for tests