Introduce custom maintenance service.
[gpl/argeo-suite.git] / org.argeo.suite.core / src / org / argeo / suite / core / CustomMaintenanceService.java
1 package org.argeo.suite.core;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.net.URL;
6 import java.util.ArrayList;
7 import java.util.List;
8
9 import javax.jcr.ImportUUIDBehavior;
10 import javax.jcr.ItemExistsException;
11 import javax.jcr.Node;
12 import javax.jcr.RepositoryException;
13
14 import org.apache.commons.logging.Log;
15 import org.apache.commons.logging.LogFactory;
16 import org.argeo.entity.EntityType;
17 import org.argeo.jcr.JcrUtils;
18 import org.argeo.maintenance.AbstractMaintenanceService;
19
20 /** Base for custom initialisations. */
21 public abstract class CustomMaintenanceService extends AbstractMaintenanceService {
22         private final static Log log = LogFactory.getLog(AbstractMaintenanceService.class);
23
24         protected List<String> getTypologies() {
25                 return new ArrayList<>();
26         }
27
28         protected String getTypologiesLoadBase() {
29                 return "/sys/terms";
30         }
31
32         protected void loadTypologies(Node customBaseNode) throws RepositoryException, IOException {
33                 List<String> typologies = getTypologies();
34                 if (!typologies.isEmpty()) {
35                         Node termsBase = JcrUtils.getOrAdd(customBaseNode, EntityType.terms.name(), EntityType.typologies.get());
36                         for (String terms : typologies) {
37                                 loadTerms(customBaseNode, terms);
38                         }
39                         termsBase.getSession().save();
40                 }
41         }
42
43         protected void loadTerms(Node termsBase, String name) throws IOException, RepositoryException {
44                 try {
45                         String termsLoadPath = getTypologiesLoadBase() + '/' + name + ".xml";
46                         URL termsUrl = getClass().getClassLoader().getResource(termsLoadPath);
47                         if (termsUrl == null)
48                                 throw new IllegalArgumentException("Terms '" + name + "' not found.");
49                         try (InputStream in = termsUrl.openStream()) {
50                                 termsBase.getSession().importXML(termsBase.getPath(), in,
51                                                 ImportUUIDBehavior.IMPORT_UUID_COLLISION_REPLACE_EXISTING);
52                         } catch (ItemExistsException e) {
53                                 log.warn("Terms " + name + " exists with another UUID, removing it...");
54                                 termsBase.getNode(name).remove();
55                                 try (InputStream in = termsUrl.openStream()) {
56                                         termsBase.getSession().importXML(termsBase.getPath(), in,
57                                                         ImportUUIDBehavior.IMPORT_UUID_COLLISION_REPLACE_EXISTING);
58                                 }
59                         }
60                         if (log.isDebugEnabled())
61                                 log.debug("Terms '" + name + "' loaded.");
62                         termsBase.getSession().save();
63                 } catch (RepositoryException | IOException e) {
64                         log.error("Cannot load terms '" + name + "': " + e.getMessage());
65                         throw e;
66                 }
67         }
68
69 }