From: Mathieu Baudier Date: Tue, 12 Jan 2021 10:52:55 +0000 (+0100) Subject: Introduce custom maintenance service. X-Git-Tag: argeo-suite-2.1.18~40 X-Git-Url: https://git.argeo.org/?p=gpl%2Fargeo-suite.git;a=commitdiff_plain;h=60331bd8849821baaa87be00b97de4eb535dd7ba;hp=8476423fa4b5a74baa2cc17afbaf61b3739be80e Introduce custom maintenance service. --- diff --git a/org.argeo.suite.core/src/org/argeo/suite/core/CustomMaintenanceService.java b/org.argeo.suite.core/src/org/argeo/suite/core/CustomMaintenanceService.java new file mode 100644 index 0000000..2fd15d1 --- /dev/null +++ b/org.argeo.suite.core/src/org/argeo/suite/core/CustomMaintenanceService.java @@ -0,0 +1,69 @@ +package org.argeo.suite.core; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; + +import javax.jcr.ImportUUIDBehavior; +import javax.jcr.ItemExistsException; +import javax.jcr.Node; +import javax.jcr.RepositoryException; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.argeo.entity.EntityType; +import org.argeo.jcr.JcrUtils; +import org.argeo.maintenance.AbstractMaintenanceService; + +/** Base for custom initialisations. */ +public abstract class CustomMaintenanceService extends AbstractMaintenanceService { + private final static Log log = LogFactory.getLog(AbstractMaintenanceService.class); + + protected List getTypologies() { + return new ArrayList<>(); + } + + protected String getTypologiesLoadBase() { + return "/sys/terms"; + } + + protected void loadTypologies(Node customBaseNode) throws RepositoryException, IOException { + List typologies = getTypologies(); + if (!typologies.isEmpty()) { + Node termsBase = JcrUtils.getOrAdd(customBaseNode, EntityType.terms.name(), EntityType.typologies.get()); + for (String terms : typologies) { + loadTerms(customBaseNode, terms); + } + termsBase.getSession().save(); + } + } + + protected void loadTerms(Node termsBase, String name) throws IOException, RepositoryException { + try { + String termsLoadPath = getTypologiesLoadBase() + '/' + name + ".xml"; + URL termsUrl = getClass().getClassLoader().getResource(termsLoadPath); + if (termsUrl == null) + throw new IllegalArgumentException("Terms '" + name + "' not found."); + try (InputStream in = termsUrl.openStream()) { + termsBase.getSession().importXML(termsBase.getPath(), in, + ImportUUIDBehavior.IMPORT_UUID_COLLISION_REPLACE_EXISTING); + } catch (ItemExistsException e) { + log.warn("Terms " + name + " exists with another UUID, removing it..."); + termsBase.getNode(name).remove(); + try (InputStream in = termsUrl.openStream()) { + termsBase.getSession().importXML(termsBase.getPath(), in, + ImportUUIDBehavior.IMPORT_UUID_COLLISION_REPLACE_EXISTING); + } + } + if (log.isDebugEnabled()) + log.debug("Terms '" + name + "' loaded."); + termsBase.getSession().save(); + } catch (RepositoryException | IOException e) { + log.error("Cannot load terms '" + name + "': " + e.getMessage()); + throw e; + } + } + +}