From a926101a291ee649ee6df4f499e6c839d89f1c76 Mon Sep 17 00:00:00 2001 From: jensp Date: Wed, 26 Sep 2018 17:50:37 +0000 Subject: [PATCH] CcmNG: First part of a revised system for import and export. git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@5696 8810af33-2d31-482b-a856-94f89814c4df --- .../org/libreccm/imexport/EntityExporter.java | 44 ++++++++++ .../org/libreccm/imexport/EntityImporter.java | 36 ++++++++ .../org/libreccm/imexport/Exportable.java | 37 ++++++++ .../org/libreccm/imexport/ExportedEntity.java | 59 +++++++++++++ .../java/org/libreccm/imexport/Exports.java | 43 +++++++++ .../org/libreccm/imexport/ImportExport.java | 88 +++++++++++++++++++ .../java/org/libreccm/imexport/Imports.java | 44 ++++++++++ 7 files changed, 351 insertions(+) create mode 100644 ccm-core/src/main/java/org/libreccm/imexport/EntityExporter.java create mode 100644 ccm-core/src/main/java/org/libreccm/imexport/EntityImporter.java create mode 100644 ccm-core/src/main/java/org/libreccm/imexport/Exportable.java create mode 100644 ccm-core/src/main/java/org/libreccm/imexport/ExportedEntity.java create mode 100644 ccm-core/src/main/java/org/libreccm/imexport/Exports.java create mode 100644 ccm-core/src/main/java/org/libreccm/imexport/ImportExport.java create mode 100644 ccm-core/src/main/java/org/libreccm/imexport/Imports.java diff --git a/ccm-core/src/main/java/org/libreccm/imexport/EntityExporter.java b/ccm-core/src/main/java/org/libreccm/imexport/EntityExporter.java new file mode 100644 index 000000000..a4e31e6cd --- /dev/null +++ b/ccm-core/src/main/java/org/libreccm/imexport/EntityExporter.java @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2018 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.imexport; + +import javax.enterprise.context.RequestScoped; + +/** + * Interface for exporters. Implementation must be annotated with + * {@link Exports} to register the implementation in the Import/Export system. + * + * Implementations must be CDI beans with annotated with {@link RequestScoped}. + * + * @author Jens Pelzetter + * @param The type of the entity which is processed by the implementation. + */ +public interface EntityExporter { + + /** + * Converts the provided entity to a JSON object and an optional array of + * associations. + * + * @param entity The entity to export. + * + * @return The JSON representation of the entity. + */ + ExportedEntity exportEntity(T entity); + +} diff --git a/ccm-core/src/main/java/org/libreccm/imexport/EntityImporter.java b/ccm-core/src/main/java/org/libreccm/imexport/EntityImporter.java new file mode 100644 index 000000000..d1099fa35 --- /dev/null +++ b/ccm-core/src/main/java/org/libreccm/imexport/EntityImporter.java @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2018 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.imexport; + +import javax.json.JsonObject; + +/** + * Imports an entity. Implementations must be annotated with {@link Imports} to + * register the implementation in the Import/Export system. + * + * Implementations must be CDI beans with annotated with {@link RequestScoped}. + * + * @author Jens Pelzetter + * @param The entity type which can processed by the implementation. + */ +public interface EntityImporter { + + T importEntity(JsonObject data); + +} diff --git a/ccm-core/src/main/java/org/libreccm/imexport/Exportable.java b/ccm-core/src/main/java/org/libreccm/imexport/Exportable.java new file mode 100644 index 000000000..77cf2919b --- /dev/null +++ b/ccm-core/src/main/java/org/libreccm/imexport/Exportable.java @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2018 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.imexport; + +/** + * Every entity which is suitable for importing and exporting must implement + * this interface. + * + * @author Jens Pelzetter + */ +public interface Exportable { + + /** + * Return the UUID of the entity which identifies the object independently + * from any database primary keys. + * + * @return The UUID of implementing entity. + */ + String getUuid(); + +} diff --git a/ccm-core/src/main/java/org/libreccm/imexport/ExportedEntity.java b/ccm-core/src/main/java/org/libreccm/imexport/ExportedEntity.java new file mode 100644 index 000000000..71e55b6f0 --- /dev/null +++ b/ccm-core/src/main/java/org/libreccm/imexport/ExportedEntity.java @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2018 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.imexport; + +import java.util.Objects; +import java.util.Optional; + +import javax.json.JsonArray; +import javax.json.JsonObject; + +/** + * A transfer object used by {@link Exporter} to wrap the exported object and + * optionally the associations extracted from the object. + * + * @author Jens Pelzetter + */ +public class ExportedEntity { + + private final JsonObject entity; + private final JsonArray associations; + + public ExportedEntity(final JsonObject entity) { + + this.entity = Objects.requireNonNull(entity); + this.associations = null; + } + + public ExportedEntity(final JsonObject entity, + final JsonArray associations) { + + this.entity = Objects.requireNonNull(entity); + this.associations = Objects.requireNonNull(associations); + } + + public JsonObject getEntity() { + return entity; + } + + public Optional getAssociations() { + return Optional.of(associations); + } + +} diff --git a/ccm-core/src/main/java/org/libreccm/imexport/Exports.java b/ccm-core/src/main/java/org/libreccm/imexport/Exports.java new file mode 100644 index 000000000..f26eb27bb --- /dev/null +++ b/ccm-core/src/main/java/org/libreccm/imexport/Exports.java @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2018 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.imexport; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import javax.inject.Qualifier; + +/** + * Declares which entity type a implementation of {@link Exporter} can process. + * + * @author Jens Pelzetter + */ +@Retention(RetentionPolicy.RUNTIME) +@Qualifier +@Target({ElementType.TYPE, + ElementType.PARAMETER, + ElementType.FIELD, + ElementType.METHOD}) +public @interface Exports { + + Class value(); + +} diff --git a/ccm-core/src/main/java/org/libreccm/imexport/ImportExport.java b/ccm-core/src/main/java/org/libreccm/imexport/ImportExport.java new file mode 100644 index 000000000..3bb73655c --- /dev/null +++ b/ccm-core/src/main/java/org/libreccm/imexport/ImportExport.java @@ -0,0 +1,88 @@ +/* + * Copyright (C) 2018 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.imexport; + + +import org.libreccm.files.CcmFilesConfiguration; + +import java.util.List; + +import javax.enterprise.context.RequestScoped; +import javax.enterprise.inject.Any; +import javax.enterprise.inject.Instance; +import javax.inject.Inject; + +/** + * Central service for importing and exporting entities. + * + * @author Jens Pelzetter + */ +@RequestScoped +public class ImportExport { + + @Inject + @Any + private Instance> importers; + + @Inject + @Any + private Instance> exporters; + + /** + * Exports the provided entities. The export will be written to a to the + * {@code exports} directory in the CCM files directory. If {@code split} is + * {@code false} a file with the name provided by {@link exportName} will be + * generated. Otherwise a directory with the provided name will be + * generated. All files will be placed into that directory. For the main + * file the provided name will be used. + * + * + * @param entities The entities to export. + * @param exportName The name file to which the export is written. + * @param split Split the entities by package? + * + * @see CcmFilesConfiguration#dataPath + */ + public void exportEntities(final List entities, + final String exportName, + final boolean split) { + + throw new UnsupportedOperationException(); + } + + /** + * Imports all entities from the files in the {@link imports} directory inside + * the CCM files data directory. The data to import can either be a file with + * the provided name or a directory with the provided name. If it is a directory + * the entry file must also use the provided name. + * + * If an entity which is part of the import already exists in the database + * the values from the import are used to update the entity. + * + * @param importName The name of the import. + * + * @see CcmFilesConfiguration#dataPath + */ + public void importEntities(final String importName) { + + throw new UnsupportedOperationException(); + + } + +} diff --git a/ccm-core/src/main/java/org/libreccm/imexport/Imports.java b/ccm-core/src/main/java/org/libreccm/imexport/Imports.java new file mode 100644 index 000000000..31a30778f --- /dev/null +++ b/ccm-core/src/main/java/org/libreccm/imexport/Imports.java @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2018 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.imexport; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import javax.inject.Qualifier; + +/** + * Declares which entity types an implementation of {@link Importer} can + * process. + * + * @author Jens Pelzetter + */ +@Retention(RetentionPolicy.RUNTIME) +@Qualifier +@Target({ElementType.TYPE, + ElementType.PARAMETER, + ElementType.FIELD, + ElementType.METHOD}) +public @interface Imports { + + Class value(); + +}