1 package org
.argeo
.suite
.core
;
3 import java
.io
.IOException
;
4 import java
.io
.InputStream
;
6 import java
.util
.ArrayList
;
9 import javax
.jcr
.ImportUUIDBehavior
;
10 import javax
.jcr
.ItemExistsException
;
11 import javax
.jcr
.Node
;
12 import javax
.jcr
.RepositoryException
;
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
;
20 /** Base for custom initialisations. */
21 public abstract class CustomMaintenanceService
extends AbstractMaintenanceService
{
22 private final static Log log
= LogFactory
.getLog(AbstractMaintenanceService
.class);
24 protected List
<String
> getTypologies() {
25 return new ArrayList
<>();
28 protected String
getTypologiesLoadBase() {
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(termsBase
, terms
);
39 termsBase
.getSession().save();
43 protected void loadTerms(Node termsBase
, String name
) throws IOException
, RepositoryException
{
45 // if (termsBase.hasNode(name))
48 String termsLoadPath
= getTypologiesLoadBase() + '/' + name
+ ".xml";
49 URL termsUrl
= getClass().getClassLoader().getResource(termsLoadPath
);
51 throw new IllegalArgumentException("Terms '" + name
+ "' not found.");
52 try (InputStream in
= termsUrl
.openStream()) {
53 termsBase
.getSession().importXML(termsBase
.getPath(), in
,
54 ImportUUIDBehavior
.IMPORT_UUID_COLLISION_REPLACE_EXISTING
);
55 } catch (ItemExistsException e
) {
56 log
.warn("Terms " + name
+ " exists with another UUID, removing it...");
57 termsBase
.getNode(name
).remove();
58 try (InputStream in
= termsUrl
.openStream()) {
59 termsBase
.getSession().importXML(termsBase
.getPath(), in
,
60 ImportUUIDBehavior
.IMPORT_UUID_COLLISION_REPLACE_EXISTING
);
63 if (log
.isDebugEnabled())
64 log
.debug("Terms '" + name
+ "' loaded.");
65 termsBase
.getSession().save();
66 } catch (RepositoryException
| IOException e
) {
67 log
.error("Cannot load terms '" + name
+ "': " + e
.getMessage());