diff --git a/ccm-core/src/org/libreccm/categorization/CategoriesExporter.java b/ccm-core/src/org/libreccm/categorization/CategoriesExporter.java index 139a3c8ed..8805e81cf 100644 --- a/ccm-core/src/org/libreccm/categorization/CategoriesExporter.java +++ b/ccm-core/src/org/libreccm/categorization/CategoriesExporter.java @@ -58,6 +58,8 @@ public class CategoriesExporter extends AbstractDomainObjectsExporter try (JsonGenerator jsonGenerator = jsonFactory .createGenerator(targetFile, JsonEncoding.UTF8)) { + setPrettyPrinter(jsonGenerator); + jsonGenerator.writeStartObject(); jsonGenerator.writeNumberField("objectId", diff --git a/ccm-core/src/org/libreccm/categorization/CategorizationsExporter.java b/ccm-core/src/org/libreccm/categorization/CategorizationsExporter.java index 0113d3dcd..da98d5928 100644 --- a/ccm-core/src/org/libreccm/categorization/CategorizationsExporter.java +++ b/ccm-core/src/org/libreccm/categorization/CategorizationsExporter.java @@ -90,6 +90,8 @@ public class CategorizationsExporter try (JsonGenerator jsonGenerator = jsonFactory .createGenerator(targetFile, JsonEncoding.UTF8)) { + setPrettyPrinter(jsonGenerator); + jsonGenerator.writeStartObject(); jsonGenerator.writeNumberField("categorizationId", diff --git a/ccm-core/src/org/libreccm/core/AbstractResourceTypesExporter.java b/ccm-core/src/org/libreccm/core/AbstractResourceTypesExporter.java index b5b844da9..4aea4deb7 100644 --- a/ccm-core/src/org/libreccm/core/AbstractResourceTypesExporter.java +++ b/ccm-core/src/org/libreccm/core/AbstractResourceTypesExporter.java @@ -43,6 +43,8 @@ public abstract class AbstractResourceTypesExporter try (JsonGenerator jsonGenerator = jsonFactory .createGenerator(targetFilePath.toFile(), JsonEncoding.UTF8)) { + setPrettyPrinter(jsonGenerator); + jsonGenerator.writeStartObject(); jsonGenerator.writeNumberField("resourceTypeId", diff --git a/ccm-core/src/org/libreccm/export/AbstractDomainObjectsExporter.java b/ccm-core/src/org/libreccm/export/AbstractDomainObjectsExporter.java index ddce6acd7..5fa1d6732 100644 --- a/ccm-core/src/org/libreccm/export/AbstractDomainObjectsExporter.java +++ b/ccm-core/src/org/libreccm/export/AbstractDomainObjectsExporter.java @@ -8,6 +8,9 @@ import com.arsdigita.persistence.Session; import com.arsdigita.persistence.SessionManager; import com.arsdigita.web.WebConfig; +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; + import java.nio.charset.StandardCharsets; import java.nio.file.Path; import java.util.ArrayList; @@ -120,13 +123,23 @@ public abstract class AbstractDomainObjectsExporter { .resolve(String.format("%s.json", uuid)); } + protected void setPrettyPrinter(final JsonGenerator jsonGenerator) { + + final DefaultPrettyPrinter prettyPrinter + = new DefaultPrettyPrinter(); + prettyPrinter.indentArraysWith( + DefaultPrettyPrinter.Lf2SpacesIndenter.instance); + jsonGenerator.setPrettyPrinter(prettyPrinter); + } + /** * Retrieves all {@link DomainObject}s of the type returned by * {@link #exportsBaseDataObjectType()} and calls * {@link #exportDomainObject(com.arsdigita.domain.DomainObject, java.nio.file.Path)} * for each of them. - * + * * @param targetDir target directory for the export. + * * @return The list of uuids of the the exported entites. */ @SuppressWarnings("unchecked") @@ -143,11 +156,15 @@ public abstract class AbstractDomainObjectsExporter { final DomainObject domainObject = DomainObjectFactory .newInstance(dataObject); - if (!(exportsType().isAssignableFrom(domainObject.getClass()))) { - throw new ExportException(String.format( - "DomainObject is not of type \"%s\" but of type \"%s\".", - exportsType().getName(), - domainObject.getClass().getName())); +// if (!(exportsType().isAssignableFrom(domainObject.getClass()))) { +// throw new ExportException(String.format( +// "DomainObject is not of type \"%s\" but of type \"%s\".", +// exportsType().getName(), +// domainObject.getClass().getName())); +// } + if (!exportsType().equals(domainObject.getClass())) { + // Is not exact type (sub class?). Skip. + continue; } domainObjects.add((T) domainObject); @@ -155,6 +172,8 @@ public abstract class AbstractDomainObjectsExporter { final List uuids = new ArrayList<>(); for (final T domainObject : domainObjects) { + System.out.printf("Exporting domain object %s...%n", + domainObject.getOID().toString()); final List createdUuids = exportDomainObject(domainObject, targetDir); uuids.addAll(createdUuids); diff --git a/ccm-core/src/org/libreccm/export/ExportManager.java b/ccm-core/src/org/libreccm/export/ExportManager.java index a44d8c90b..9efbef2a8 100644 --- a/ccm-core/src/org/libreccm/export/ExportManager.java +++ b/ccm-core/src/org/libreccm/export/ExportManager.java @@ -11,8 +11,13 @@ import java.util.Map; import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; +import com.fasterxml.jackson.core.util.MinimalPrettyPrinter; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; import java.io.IOException; +import java.io.StringWriter; import java.nio.file.Files; import java.nio.file.Path; import java.time.LocalDateTime; @@ -86,9 +91,14 @@ public final class ExportManager { try (final JsonGenerator manifestGenerator = jsonFactory .createGenerator(manifestFilePath.toFile(), JsonEncoding.UTF8)) { - manifestGenerator.writeStartObject(); + final DefaultPrettyPrinter prettyPrinter + = new DefaultPrettyPrinter(); + prettyPrinter.indentArraysWith( + DefaultPrettyPrinter.Lf2SpacesIndenter.instance); + manifestGenerator.setPrettyPrinter(prettyPrinter); manifestGenerator.writeStartObject(); + manifestGenerator.writeStringField( "created", LocalDateTime.now(ZoneId.of("UTC")).toString()); @@ -102,7 +112,7 @@ public final class ExportManager { manifestGenerator.writeString(type); } - manifestGenerator.writeEndObject(); + manifestGenerator.writeEndArray(); manifestGenerator.writeObjectFieldStart("entities"); @@ -121,7 +131,6 @@ public final class ExportManager { manifestGenerator.writeEndObject(); - manifestGenerator.writeEndObject(); } catch (IOException ex) { throw new UncheckedWrapperException(ex); } diff --git a/ccm-core/src/org/libreccm/security/GroupMembershipsExporter.java b/ccm-core/src/org/libreccm/security/GroupMembershipsExporter.java index ec61b58e7..f75a63dbd 100644 --- a/ccm-core/src/org/libreccm/security/GroupMembershipsExporter.java +++ b/ccm-core/src/org/libreccm/security/GroupMembershipsExporter.java @@ -69,6 +69,8 @@ public class GroupMembershipsExporter try (final JsonGenerator jsonGenerator = jsonFactory .createGenerator(targetFilePath.toFile(), JsonEncoding.UTF8)) { + setPrettyPrinter(jsonGenerator); + jsonGenerator.writeStartObject(); jsonGenerator.writeNumberField("membershipId", IdSequence.getInstance().nextId()); diff --git a/ccm-core/src/org/libreccm/security/GroupsExporter.java b/ccm-core/src/org/libreccm/security/GroupsExporter.java index da5b8f732..4b9b06d40 100644 --- a/ccm-core/src/org/libreccm/security/GroupsExporter.java +++ b/ccm-core/src/org/libreccm/security/GroupsExporter.java @@ -49,6 +49,8 @@ public class GroupsExporter extends AbstractDomainObjectsExporter { try (final JsonGenerator jsonGenerator = jsonFactory .createGenerator(targetFile, JsonEncoding.UTF8)) { + setPrettyPrinter(jsonGenerator); + jsonGenerator.writeStartObject(); jsonGenerator.writeNumberField("partyId", IdSequence.getInstance().nextId()); diff --git a/ccm-core/src/org/libreccm/security/PermissionsExporter.java b/ccm-core/src/org/libreccm/security/PermissionsExporter.java index 63261bec8..cd1d887fb 100644 --- a/ccm-core/src/org/libreccm/security/PermissionsExporter.java +++ b/ccm-core/src/org/libreccm/security/PermissionsExporter.java @@ -3,9 +3,14 @@ package org.libreccm.security; import com.arsdigita.domain.DomainObject; import com.arsdigita.domain.DomainObjectFactory; import com.arsdigita.kernel.Group; +import com.arsdigita.kernel.Role; import com.arsdigita.kernel.RoleCollection; import com.arsdigita.kernel.permissions.Permission; +import com.arsdigita.persistence.DataCollection; +import com.arsdigita.persistence.Filter; import com.arsdigita.persistence.OID; +import com.arsdigita.persistence.Session; +import com.arsdigita.persistence.SessionManager; import com.arsdigita.util.UncheckedWrapperException; import com.arsdigita.web.WebConfig; @@ -82,6 +87,8 @@ public class PermissionsExporter extends AbstractDomainObjectsExporter { try (final JsonGenerator jsonGenerator = jsonFactory .createGenerator(targetFilePath.toFile(), JsonEncoding.UTF8)) { + setPrettyPrinter(jsonGenerator); + jsonGenerator.writeStartObject(); jsonGenerator.writeNumberField("roleId", diff --git a/ccm-core/src/org/libreccm/security/UsersExporter.java b/ccm-core/src/org/libreccm/security/UsersExporter.java index cd302bf57..d61c2bf47 100644 --- a/ccm-core/src/org/libreccm/security/UsersExporter.java +++ b/ccm-core/src/org/libreccm/security/UsersExporter.java @@ -51,6 +51,8 @@ public class UsersExporter extends AbstractDomainObjectsExporter throws IOException; @Override - public final List exportDomainObject(final T domainObject, + public final List exportDomainObject(final T task, final Path targetDir) { - final String uuid = generateUuid(domainObject); + final String uuid = generateUuid(task); final Path targetFilePath = generateTargetFilePath(targetDir, uuid); final JsonFactory jsonFactory = new JsonFactory(); try (JsonGenerator jsonGenerator = jsonFactory .createGenerator(targetFilePath.toFile(), JsonEncoding.UTF8)) { + setPrettyPrinter(jsonGenerator); + jsonGenerator.writeStartObject(); jsonGenerator.writeNumberField("taskId", @@ -50,26 +52,26 @@ public abstract class AbstractTasksExporter jsonGenerator.writeObjectFieldStart("label"); jsonGenerator.writeStringField( KernelConfig.getConfig().getDefaultLanguage(), - domainObject.getLabel()); + task.getLabel()); jsonGenerator.writeEndObject(); jsonGenerator.writeObjectFieldStart("description"); jsonGenerator.writeStringField( KernelConfig.getConfig().getDefaultLanguage(), - domainObject.getDescription()); + task.getDescription()); jsonGenerator.writeEndObject(); - jsonGenerator.writeBooleanField("active", domainObject.isActive()); + jsonGenerator.writeBooleanField("active", task.isActive()); jsonGenerator.writeStringField("taskState", - domainObject.getStateString()); + task.getStateString()); - final Workflow workflow = domainObject.getWorkflow(); + final Workflow workflow = task.getWorkflow(); final String workflowUuid = generateUuid(workflow); jsonGenerator.writeStringField("workflow", workflowUuid); jsonGenerator.writeArrayFieldStart("comments"); - final Iterator comments = domainObject.getComments(); + final Iterator comments = task.getComments(); while (comments.hasNext()) { final TaskComment comment = (TaskComment) comments.next(); @@ -78,7 +80,7 @@ public abstract class AbstractTasksExporter } jsonGenerator.writeEndArray(); - exportTaskProperties(domainObject, jsonGenerator); + exportTaskProperties(task, jsonGenerator); jsonGenerator.writeEndObject(); diff --git a/ccm-core/src/org/libreccm/workflow/TaskCommentsExporter.java b/ccm-core/src/org/libreccm/workflow/TaskCommentsExporter.java index 4505dbcf6..9b8737953 100644 --- a/ccm-core/src/org/libreccm/workflow/TaskCommentsExporter.java +++ b/ccm-core/src/org/libreccm/workflow/TaskCommentsExporter.java @@ -51,14 +51,16 @@ public class TaskCommentsExporter try (JsonGenerator jsonGenerator = jsonFactory .createGenerator(targetFile, JsonEncoding.UTF8)) { + setPrettyPrinter(jsonGenerator); + jsonGenerator.writeStartObject(); jsonGenerator.writeNumberField( "taskCommentId", IdSequence.getInstance().nextId()); jsonGenerator.writeStringField("uuid", uuid); - - jsonGenerator.writeStringField("comment", + + jsonGenerator.writeStringField("comment", domainObject.getComment()); jsonGenerator.writeEndObject(); diff --git a/ccm-core/src/org/libreccm/workflow/TaskDependenciesExporter.java b/ccm-core/src/org/libreccm/workflow/TaskDependenciesExporter.java index 75b81bc5c..8d4c4b7fe 100644 --- a/ccm-core/src/org/libreccm/workflow/TaskDependenciesExporter.java +++ b/ccm-core/src/org/libreccm/workflow/TaskDependenciesExporter.java @@ -83,6 +83,8 @@ public class TaskDependenciesExporter try (final JsonGenerator jsonGenerator = jsonFactory .createGenerator(targetFile, JsonEncoding.UTF8)) { + setPrettyPrinter(jsonGenerator); + jsonGenerator.writeStartObject(); jsonGenerator.writeNumberField("taskDependencyId", diff --git a/ccm-core/src/org/libreccm/workflow/WorkflowsExporter.java b/ccm-core/src/org/libreccm/workflow/WorkflowsExporter.java index 944e75927..4b185bc63 100644 --- a/ccm-core/src/org/libreccm/workflow/WorkflowsExporter.java +++ b/ccm-core/src/org/libreccm/workflow/WorkflowsExporter.java @@ -2,6 +2,7 @@ package org.libreccm.workflow; import com.arsdigita.kernel.ACSObject; import com.arsdigita.kernel.KernelConfig; +import com.arsdigita.util.UncheckedWrapperException; import com.arsdigita.workflow.simple.Workflow; import com.arsdigita.workflow.simple.WorkflowTemplate; @@ -55,6 +56,8 @@ public class WorkflowsExporter extends AbstractDomainObjectsExporter { try (JsonGenerator jsonGenerator = jsonFactory .createGenerator(targetFile, JsonEncoding.UTF8)) { + setPrettyPrinter(jsonGenerator); + jsonGenerator.writeStartObject(); jsonGenerator.writeNumberField("workflowId", @@ -63,35 +66,35 @@ public class WorkflowsExporter extends AbstractDomainObjectsExporter { jsonGenerator.writeBooleanField( "abstractWorkflow", domainObject instanceof WorkflowTemplate); - + jsonGenerator.writeObjectFieldStart("name"); jsonGenerator.writeStringField( - KernelConfig.getConfig().getDefaultLanguage(), + KernelConfig.getConfig().getDefaultLanguage(), domainObject.getDisplayName()); jsonGenerator.writeEndObject(); - + jsonGenerator.writeObjectFieldStart("description"); jsonGenerator.writeStringField( - KernelConfig.getConfig().getDefaultLanguage(), + KernelConfig.getConfig().getDefaultLanguage(), domainObject.getDescription()); jsonGenerator.writeEndObject(); - - jsonGenerator.writeStringField("state", + + jsonGenerator.writeStringField("state", domainObject.getStateString()); - + jsonGenerator.writeBooleanField("active", domainObject.isActive()); - + final ACSObject object = domainObject.getObject(); final String objectUuid = generateUuid(object); jsonGenerator.writeStringField("object", objectUuid); - + jsonGenerator.writeEndObject(); } catch (IOException ex) { - + throw new UncheckedWrapperException(ex); } - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + return Arrays.asList(new String[]{uuid}); } }