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-94f89814c4dfpull/2/head
parent
dbd7af3c4d
commit
465029c738
|
|
@ -200,12 +200,19 @@
|
||||||
<groupId>com.fasterxml.jackson.core</groupId>
|
<groupId>com.fasterxml.jackson.core</groupId>
|
||||||
<artifactId>jackson-databind</artifactId>
|
<artifactId>jackson-databind</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.fasterxml.jackson.dataformat</groupId>
|
||||||
|
<artifactId>jackson-dataformat-xml</artifactId>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.fasterxml.jackson.dataformat</groupId>
|
<groupId>com.fasterxml.jackson.dataformat</groupId>
|
||||||
<artifactId>jackson-dataformat-csv</artifactId>
|
<artifactId>jackson-dataformat-csv</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency> <!-- better xml library -->
|
||||||
|
<groupId>org.codehaus.woodstox</groupId>
|
||||||
|
<artifactId>woodstox-core-asl</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -18,10 +18,18 @@
|
||||||
*/
|
*/
|
||||||
package org.libreccm.portation;
|
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.apache.log4j.Logger;
|
||||||
import org.libreccm.core.Identifiable;
|
import org.libreccm.core.Identifiable;
|
||||||
|
|
||||||
import java.io.File;
|
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;
|
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 static final Logger log = Logger.getLogger(AbstractMarshaller.class);
|
||||||
|
|
||||||
private Format format;
|
private Format format;
|
||||||
|
private String filename;
|
||||||
|
|
||||||
private File exportFile;
|
// XML specifics
|
||||||
private List<File> importFiles;
|
ObjectMapper xmlMapper;
|
||||||
|
|
||||||
// CSV specifics
|
|
||||||
|
|
||||||
// JSON specifics
|
// JSON specifics
|
||||||
|
|
||||||
// XML specifics
|
// CSV specifics
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @param format
|
public void init(final Format format, String baseFileName) {
|
||||||
*/
|
|
||||||
private void init(final Format format) {
|
|
||||||
this.format = format;
|
this.format = format;
|
||||||
|
this.filename = baseFileName;
|
||||||
|
|
||||||
switch (this.format) {
|
switch (this.format) {
|
||||||
case CSV:
|
case XML:
|
||||||
|
// for additional configuration
|
||||||
|
JacksonXmlModule module = new JacksonXmlModule();
|
||||||
|
module.setDefaultUseWrapper(false);
|
||||||
|
xmlMapper = new XmlMapper(module);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case JSON:
|
case JSON:
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case XML:
|
case CSV:
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
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 init(final Format format, final List<String> filenames) {
|
public void exportList(final List<I> exportList) {
|
||||||
filenames.forEach(fname -> importFiles.add(new File(fname)));
|
File file = new File(filename + "_" + getObjectClass().toString());
|
||||||
init(format);
|
FileWriter fileWriter = null;
|
||||||
}
|
|
||||||
|
|
||||||
|
try {
|
||||||
/**
|
fileWriter = new FileWriter(file);
|
||||||
*
|
} catch (IOException e) {
|
||||||
* @param exportObjects List of {@code T}-tpyed objects being exported
|
log.error(String.format("Unable open a fileWriter for the file " +
|
||||||
*/
|
"with the name %s.", file.getName()));
|
||||||
public void exportEntities(final List<I> exportObjects) {
|
|
||||||
switch (format) {
|
|
||||||
case CSV:
|
|
||||||
break;
|
|
||||||
|
|
||||||
case JSON:
|
|
||||||
break;
|
|
||||||
|
|
||||||
case XML:
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (fileWriter != null) {
|
||||||
|
for (I object : exportList) {
|
||||||
|
String line = null;
|
||||||
|
|
||||||
|
switch (format) {
|
||||||
|
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 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()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
protected abstract Class<I> getObjectClass();
|
||||||
*
|
protected abstract void insertObjectIntoDB(I object);
|
||||||
* @return List of {@code T}-typed objects being imported
|
|
||||||
*/
|
|
||||||
public List<I> importEntities() {
|
public List<I> importEntities() {
|
||||||
switch (format) {
|
List<I> objects = new ArrayList<>();
|
||||||
case CSV:
|
|
||||||
break;
|
|
||||||
|
|
||||||
case JSON:
|
File file = new File(filename);
|
||||||
break;
|
List<String> lines = null;
|
||||||
|
|
||||||
case XML:
|
try {
|
||||||
break;
|
lines = Files.readAllLines(file.toPath());
|
||||||
|
} catch (IOException e) {
|
||||||
default:
|
log.error(String.format("Unable to read lines of the file with " +
|
||||||
break;
|
"name %s.", file.getName()));
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
|
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 object " +
|
||||||
|
"from XML string:\n \"%s\"", line));
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case JSON:
|
||||||
|
break;
|
||||||
|
|
||||||
|
case CSV:
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
objects.add(object);
|
||||||
|
insertObjectIntoDB(object);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return objects;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -131,7 +131,7 @@ public class Marshaller {
|
||||||
iterator.next();
|
iterator.next();
|
||||||
|
|
||||||
marshaller.init(format, filename);
|
marshaller.init(format, filename);
|
||||||
marshaller.exportEntities(list);
|
marshaller.exportList(list);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -18,8 +18,6 @@
|
||||||
*/
|
*/
|
||||||
package org.libreccm.docrepo;
|
package org.libreccm.docrepo;
|
||||||
|
|
||||||
import org.apache.oro.text.perl.Perl5Util;
|
|
||||||
|
|
||||||
import javax.activation.MimetypesFileTypeMap;
|
import javax.activation.MimetypesFileTypeMap;
|
||||||
import javax.inject.Inject;
|
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
|
* @return true for a system-valid resource name, otherwise false
|
||||||
*/
|
*/
|
||||||
public boolean isValidNewResourceName(String name, T resource) {
|
public boolean isValidNewResourceName(String name, T resource) {
|
||||||
Perl5Util perl5Util = new Perl5Util();
|
|
||||||
|
|
||||||
final String INVALID_START_PATTERN = "/^[.]+/";
|
final String INVALID_START_PATTERN = "/^[.]+/";
|
||||||
final String INVALID_NAME_PATTERN = "/[^a-zA-Z0-9\\_\\.\\-\\ ]+/";
|
final String INVALID_NAME_PATTERN = "/[^a-zA-Z0-9\\_\\.\\-\\ ]+/";
|
||||||
final String EXTENSION_PATTERN = "/^([^\\.].*)(\\.\\w+)$/";
|
final String EXTENSION_PATTERN = "/^([^\\.].*)(\\.\\w+)$/";
|
||||||
|
|
||||||
// adds an extension if non-existent
|
// adds an extension if non-existent
|
||||||
if (!perl5Util.match(EXTENSION_PATTERN, name)) {
|
if (name.matches(EXTENSION_PATTERN)) {
|
||||||
if (perl5Util.match(EXTENSION_PATTERN, resource.getName())) {
|
if (resource.getName().matches(EXTENSION_PATTERN)) {
|
||||||
name += perl5Util.group(2);
|
// TODO: what to do here?
|
||||||
|
//name += perl5Util.group(2);
|
||||||
|
name.isEmpty();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// checks pattern of the name
|
// checks pattern of the name
|
||||||
boolean validName = !(perl5Util.match(INVALID_START_PATTERN, name) ||
|
boolean validName = !(name.matches(INVALID_START_PATTERN) || name
|
||||||
perl5Util.match(INVALID_NAME_PATTERN, name));
|
.matches(INVALID_NAME_PATTERN));
|
||||||
|
|
||||||
// checks duplication of the name; database access (mind performance)
|
// checks duplication of the name; database access (mind performance)
|
||||||
validName &= (fileRepository.findByName(name).isEmpty() &&
|
validName &= (fileRepository.findByName(name).isEmpty() &&
|
||||||
|
|
|
||||||
|
|
@ -28,4 +28,13 @@ import org.libreccm.portation.Marshals;
|
||||||
@Marshals(AbstractResource.class)
|
@Marshals(AbstractResource.class)
|
||||||
public class AbstractResourceMarshaller extends AbstractMarshaller<AbstractResource> {
|
public class AbstractResourceMarshaller extends AbstractMarshaller<AbstractResource> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Class<AbstractResource> getObjectClass() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void insertObjectIntoDB(AbstractResource object) {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -38,8 +38,7 @@ import java.util.stream.Collectors;
|
||||||
* @version 01/10/2015
|
* @version 01/10/2015
|
||||||
*/
|
*/
|
||||||
public abstract class AbstractResourceRepository<T extends AbstractResource>
|
public abstract class AbstractResourceRepository<T extends AbstractResource>
|
||||||
extends
|
extends AbstractAuditedEntityRepository<Long, T> {
|
||||||
AbstractAuditedEntityRepository<Long, T> {
|
|
||||||
|
|
||||||
protected Class classOfT;
|
protected Class classOfT;
|
||||||
|
|
||||||
|
|
|
||||||
17
pom.xml
17
pom.xml
|
|
@ -426,12 +426,6 @@
|
||||||
<version>1.2</version>
|
<version>1.2</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.opencsv</groupId>
|
|
||||||
<artifactId>opencsv</artifactId>
|
|
||||||
<version>3.6</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.fasterxml.jackson.core</groupId>
|
<groupId>com.fasterxml.jackson.core</groupId>
|
||||||
<artifactId>jackson-core</artifactId>
|
<artifactId>jackson-core</artifactId>
|
||||||
|
|
@ -447,12 +441,23 @@
|
||||||
<artifactId>jackson-databind</artifactId>
|
<artifactId>jackson-databind</artifactId>
|
||||||
<version>2.7.0</version>
|
<version>2.7.0</version>
|
||||||
</dependency>
|
</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 -->
|
<dependency> <!-- for csv ex-/import -->
|
||||||
<groupId>com.fasterxml.jackson.dataformat</groupId>
|
<groupId>com.fasterxml.jackson.dataformat</groupId>
|
||||||
<artifactId>jackson-dataformat-csv</artifactId>
|
<artifactId>jackson-dataformat-csv</artifactId>
|
||||||
<version>2.7.0</version>
|
<version>2.7.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency> <!-- better xml library -->
|
||||||
|
<groupId>org.codehaus.woodstox</groupId>
|
||||||
|
<artifactId>woodstox-core-asl</artifactId>
|
||||||
|
<version>4.4.1</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!--
|
<!--
|
||||||
**********************
|
**********************
|
||||||
Dependencies for tests
|
Dependencies for tests
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue