]> git.argeo.org Git - gpl/argeo-suite.git/blob - JcrEntityDefinition.java
f76eb63fa96491b4df5874f246ff896de56cdfa1
[gpl/argeo-suite.git] / JcrEntityDefinition.java
1 package org.argeo.entity.core;
2
3 import java.util.Map;
4
5 import javax.jcr.Node;
6 import javax.jcr.Repository;
7 import javax.jcr.RepositoryException;
8 import javax.jcr.Session;
9
10 import org.argeo.api.NodeUtils;
11 import org.argeo.entity.EntityConstants;
12 import org.argeo.entity.EntityDefinition;
13 import org.argeo.entity.EntityNames;
14 import org.argeo.entity.EntityTypes;
15 import org.argeo.jcr.Jcr;
16 import org.argeo.jcr.JcrUtils;
17 import org.osgi.framework.BundleContext;
18
19 /** An entity definition based on a JCR data structure. */
20 public class JcrEntityDefinition implements EntityDefinition {
21 private Repository repository;
22
23 private String type;
24 private String defaultEditoryId;
25
26 public void init(BundleContext bundleContext, Map<String, String> properties) throws RepositoryException {
27 Session adminSession = NodeUtils.openDataAdminSession(repository, null);
28 try {
29 type = properties.get(EntityConstants.TYPE);
30 if (type == null)
31 throw new IllegalArgumentException("Entity type property " + EntityConstants.TYPE + " must be set.");
32 defaultEditoryId = properties.get(EntityConstants.DEFAULT_EDITORY_ID);
33 String definitionPath = EntityNames.ENTITY_DEFINITIONS_PATH + '/' + type;
34 if (!adminSession.itemExists(definitionPath)) {
35 Node entityDefinition = JcrUtils.mkdirs(adminSession, definitionPath, EntityTypes.ENTITY_DEFINITION);
36 // entityDefinition.addMixin(EntityTypes.ENTITY_DEFINITION);
37 adminSession.save();
38 }
39 initJcr(adminSession);
40 } finally {
41 Jcr.logout(adminSession);
42 }
43 }
44
45 /** To be overridden in order to perform additional initialisations. */
46 protected void initJcr(Session adminSession) throws RepositoryException {
47
48 }
49
50 public void destroy(BundleContext bundleContext, Map<String, String> properties) throws RepositoryException {
51
52 }
53
54 @Override
55 public String getEditorId(Node entity) {
56 return defaultEditoryId;
57 }
58
59 @Override
60 public String getType() {
61 return type;
62 }
63
64 protected Repository getRepository() {
65 return repository;
66 }
67
68 public void setRepository(Repository repository) {
69 this.repository = repository;
70 }
71
72 public String toString() {
73 return "Entity Definition " + getType();
74 }
75
76 }