X-Git-Url: https://git.argeo.org/?p=gpl%2Fargeo-suite.git;a=blobdiff_plain;f=org.argeo.entity.core%2Fsrc%2Forg%2Fargeo%2Fentity%2Fcore%2FJcrEntityDefinition.java;fp=org.argeo.entity.core%2Fsrc%2Forg%2Fargeo%2Fentity%2Fcore%2FJcrEntityDefinition.java;h=15fb812c0567505fef6c80ee9a9b228bb444780e;hp=0000000000000000000000000000000000000000;hb=a55bb0dc7e9fbcefb645d34ce24b326d1506a623;hpb=a91a037c1d7d94a47e356918952a106b5071cf5c diff --git a/org.argeo.entity.core/src/org/argeo/entity/core/JcrEntityDefinition.java b/org.argeo.entity.core/src/org/argeo/entity/core/JcrEntityDefinition.java new file mode 100644 index 0000000..15fb812 --- /dev/null +++ b/org.argeo.entity.core/src/org/argeo/entity/core/JcrEntityDefinition.java @@ -0,0 +1,76 @@ +package org.argeo.entity.core; + +import java.util.Map; + +import javax.jcr.Node; +import javax.jcr.Repository; +import javax.jcr.RepositoryException; +import javax.jcr.Session; + +import org.argeo.api.NodeUtils; +import org.argeo.entity.EntityConstants; +import org.argeo.entity.EntityDefinition; +import org.argeo.entity.EntityNames; +import org.argeo.entity.EntityTypes; +import org.argeo.jcr.Jcr; +import org.argeo.jcr.JcrUtils; +import org.osgi.framework.BundleContext; + +/** An entity definition based on a JCR data structure. */ +public class JcrEntityDefinition implements EntityDefinition { + private Repository repository; + + private String type; + private String defaultEditoryId; + + public void init(BundleContext bundleContext, Map properties) throws RepositoryException { + Session adminSession = NodeUtils.openDataAdminSession(repository, null); + try { + type = properties.get(EntityConstants.TYPE); + if (type == null) + throw new IllegalArgumentException("Entity type property " + EntityConstants.TYPE + " must be set."); + defaultEditoryId = properties.get(EntityConstants.DEFAULT_EDITORY_ID); + String definitionPath = EntityNames.ENTITY_DEFINITIONS_PATH + '/' + type; + if (!adminSession.itemExists(definitionPath)) { + Node entityDefinition = JcrUtils.mkdirs(adminSession, definitionPath); + entityDefinition.addMixin(EntityTypes.ENTITY_DEFINITION); + adminSession.save(); + } + initJcr(adminSession); + } finally { + Jcr.logout(adminSession); + } + } + + /** To be overridden in order to perform additional initialisations. */ + protected void initJcr(Session adminSession) throws RepositoryException { + + } + + public void destroy(BundleContext bundleContext, Map properties) throws RepositoryException { + + } + + @Override + public String getEditorId(Node entity) { + return defaultEditoryId; + } + + @Override + public String getType() { + return type; + } + + protected Repository getRepository() { + return repository; + } + + public void setRepository(Repository repository) { + this.repository = repository; + } + + public String toString() { + return "Entity Definition " + getType(); + } + +}