This project has retired. For details please refer to its
Attic page.
JcrTypeHandlerManager xref
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.chemistry.opencmis.jcr.type;
20
21 import org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException;
22 import org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException;
23 import org.apache.chemistry.opencmis.jcr.JcrNode;
24 import org.apache.chemistry.opencmis.jcr.JcrTypeManager;
25 import org.apache.chemistry.opencmis.jcr.PathManager;
26 import org.apache.chemistry.opencmis.jcr.query.IdentifierMap;
27 import org.apache.chemistry.opencmis.jcr.util.Predicate;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30
31 import javax.jcr.Node;
32 import javax.jcr.RepositoryException;
33 import java.util.HashMap;
34 import java.util.Map;
35
36
37
38
39 public class JcrTypeHandlerManager {
40
41 private static final Log log = LogFactory.getLog(JcrTypeHandlerManager.class);
42
43 private final PathManager pathManager;
44 private final JcrTypeManager typeManager;
45 private final Map<String, JcrTypeHandler> typeHandlers = new HashMap<String, JcrTypeHandler>();
46
47 public JcrTypeHandlerManager(PathManager pathManager, JcrTypeManager typeManager) {
48 this.pathManager = pathManager;
49 this.typeManager = typeManager;
50 }
51
52 public void addHandler(JcrTypeHandler typeHandler) {
53 if (typeManager.addType(typeHandler.getTypeDefinition())) {
54 typeHandlers.put(typeHandler.getTypeId(), typeHandler);
55 typeHandler.initialize(pathManager, typeManager, this);
56 }
57 }
58
59 public JcrTypeHandler getTypeHandler(String typeId) {
60 JcrTypeHandler typeHandler = typeHandlers.get(typeId);
61 if (typeHandler == null) {
62 throw new CmisObjectNotFoundException("Type '" + typeId + "' is unknown!");
63 }
64 return typeHandler;
65 }
66
67 public JcrFolderTypeHandler getFolderTypeHandler(String typeId) {
68 JcrTypeHandler typeHandler = getTypeHandler(typeId);
69 if (!(typeHandler instanceof JcrFolderTypeHandler)) {
70 throw new CmisObjectNotFoundException("Type '" + typeId + "' is not a folder!");
71 }
72 return (JcrFolderTypeHandler) typeHandler;
73 }
74
75 public JcrDocumentTypeHandler getDocumentTypeHandler(String typeId) {
76 JcrTypeHandler typeHandler = getTypeHandler(typeId);
77 if (!(typeHandler instanceof JcrDocumentTypeHandler)) {
78 throw new CmisObjectNotFoundException("Type '" + typeId + "' is not a document!");
79 }
80 return (JcrDocumentTypeHandler) typeHandler;
81 }
82
83 public Predicate<Node> getNodePredicate() {
84 return new Predicate<Node>() {
85 public boolean evaluate(Node node) {
86 try {
87 for (JcrTypeHandler typeHandler : typeHandlers.values()) {
88 if (typeHandler.canHandle(node)) {
89 return true;
90 }
91 }
92 return false;
93 }
94 catch (RepositoryException e) {
95 log.debug(e.getMessage(), e);
96 throw new CmisRuntimeException(e.getMessage(), e);
97 }
98 }
99 };
100 }
101
102 public IdentifierMap getIdentifierMap(String typeId) {
103 JcrTypeHandler typeHandler = getTypeHandler(typeId);
104 IdentifierMap identifierMap = typeHandler.getIdentifierMap();
105 if (identifierMap == null) {
106 throw new CmisRuntimeException("Not supported: query for type " + typeId);
107 }
108 return identifierMap;
109 }
110
111 public JcrNode create(Node node) {
112 try {
113 for (JcrTypeHandler typeHandler : typeHandlers.values()) {
114 if (typeHandler.canHandle(node)) {
115 return typeHandler.getJcrNode(node);
116 }
117 }
118 throw new CmisObjectNotFoundException("No object type for object '" + node.getIdentifier() + "'");
119 }
120 catch (RepositoryException e) {
121 log.debug(e.getMessage(), e);
122 throw new CmisObjectNotFoundException(e.getMessage(), e);
123 }
124 }
125 }