KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > jcr > item > ItemResolver


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.jcr.item;
18
19 import java.util.List JavaDoc;
20
21 import javax.jcr.PathNotFoundException;
22
23 import org.alfresco.jcr.session.SessionImpl;
24 import org.alfresco.service.cmr.repository.NodeRef;
25 import org.alfresco.service.cmr.search.SearchService;
26
27
28 /**
29  * Responsible for finding JCR Items (Nodes, Properties) from Alfresco equivalents
30  *
31  * @author David Caruana
32  *
33  */

34 public class ItemResolver
35 {
36
37     /**
38      * Create an Item from a JCR Path
39      *
40      * @param context session context
41      * @param from starting node for path
42      * @param path the path
43      * @return the Item (Node or Property)
44      * @throws PathNotFoundException
45      */

46     public static ItemImpl findItem(SessionImpl context, NodeRef from, String JavaDoc path)
47         throws PathNotFoundException
48     {
49         ItemImpl item = null;
50         
51         NodeRef nodeRef = getNodeRef(context, from, path);
52         if (nodeRef != null)
53         {
54             item = new NodeImpl(context, nodeRef);
55         }
56         else
57         {
58             // TODO: create property
59
}
60         
61         if (item == null)
62         {
63             throw new PathNotFoundException("Path " + path + " not found.");
64         }
65
66         return item;
67     }
68
69     /**
70      * Create an Node from a JCR Path
71      *
72      * @param context session context
73      * @param from starting node for path
74      * @param path the path
75      * @return the Item (Node or Property)
76      * @throws PathNotFoundException
77      */

78     public static NodeImpl findNode(SessionImpl context, NodeRef from, String JavaDoc path)
79         throws PathNotFoundException
80     {
81         NodeRef nodeRef = getNodeRef(context, from, path);
82         if (nodeRef == null)
83         {
84             throw new PathNotFoundException("A node does not exist at path " + path + " relative to node " + from);
85         }
86         return new NodeImpl(context, nodeRef);
87     }
88     
89     /**
90      * Determine if Item exists
91      *
92      * @param context session context
93      * @param from starting node for path
94      * @param path the path
95      * @return true => exists, false => no it doesn't
96      */

97     public static boolean itemExists(SessionImpl context, NodeRef from, String JavaDoc path)
98     {
99         boolean exists = nodeExists(context, from, path);
100         if (!exists)
101         {
102             // TODO: Check for property
103
}
104         return exists;
105     }
106
107     /**
108      * Determine if Node exists
109      *
110      * @param context session context
111      * @param from starting node for path
112      * @param path the path
113      * @return true => exists, false => no it doesn't
114      */

115     public static boolean nodeExists(SessionImpl context, NodeRef from, String JavaDoc path)
116     {
117         NodeRef nodeRef = getNodeRef(context, from, path);
118         return nodeRef != null;
119     }
120     
121     /**
122      * Gets the Node Reference for the node at the specified path
123      *
124      * @param context session context
125      * @param from the starting node for the path
126      * @param path the path
127      * @return the node reference (or null if not found)
128      */

129     public static NodeRef getNodeRef(SessionImpl context, NodeRef from, String JavaDoc path)
130     {
131         NodeRef nodeRef = null;
132         
133         // TODO: Support JCR Path
134
// TODO: Catch malformed path and return false (per Specification)
135
SearchService search = context.getRepositoryImpl().getServiceRegistry().getSearchService();
136         List JavaDoc<NodeRef> nodeRefs = search.selectNodes(from, path, null, context.getNamespaceResolver(), false);
137         if (nodeRefs != null && nodeRefs.size() > 0)
138         {
139             nodeRef = nodeRefs.get(0);
140         }
141             
142         return nodeRef;
143     }
144     
145 }
146
Popular Tags