KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > jcr > session > WorkspaceImpl


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.session;
18
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.List JavaDoc;
23
24 import javax.jcr.AccessDeniedException;
25 import javax.jcr.InvalidItemStateException;
26 import javax.jcr.InvalidSerializedDataException;
27 import javax.jcr.ItemExistsException;
28 import javax.jcr.NamespaceRegistry;
29 import javax.jcr.NoSuchWorkspaceException;
30 import javax.jcr.PathNotFoundException;
31 import javax.jcr.RepositoryException;
32 import javax.jcr.Session;
33 import javax.jcr.UnsupportedRepositoryOperationException;
34 import javax.jcr.Workspace;
35 import javax.jcr.lock.LockException;
36 import javax.jcr.nodetype.ConstraintViolationException;
37 import javax.jcr.nodetype.NodeTypeManager;
38 import javax.jcr.observation.ObservationManager;
39 import javax.jcr.query.QueryManager;
40 import javax.jcr.version.Version;
41 import javax.jcr.version.VersionException;
42
43 import org.alfresco.jcr.item.ItemResolver;
44 import org.alfresco.jcr.item.JCRPath;
45 import org.alfresco.jcr.query.QueryManagerImpl;
46 import org.alfresco.jcr.util.JCRProxyFactory;
47 import org.alfresco.service.cmr.repository.ChildAssociationRef;
48 import org.alfresco.service.cmr.repository.CopyService;
49 import org.alfresco.service.cmr.repository.NodeRef;
50 import org.alfresco.service.cmr.repository.NodeService;
51 import org.alfresco.service.cmr.repository.Path;
52 import org.alfresco.service.cmr.repository.StoreRef;
53 import org.alfresco.service.namespace.QName;
54 import org.alfresco.util.ParameterCheck;
55 import org.xml.sax.ContentHandler JavaDoc;
56
57 /**
58  * Alfresco implementation of a JCR Workspace
59  *
60  * @author David Caruana
61  */

62 public class WorkspaceImpl implements Workspace
63 {
64     
65     private SessionImpl session;
66     private Workspace proxy = null;
67     private QueryManagerImpl queryManager = null;
68     
69     /**
70      * Construct
71      *
72      * @param session the session
73      */

74     public WorkspaceImpl(SessionImpl session)
75     {
76         this.session = session;
77     }
78     
79     /**
80      * Get proxied JCR Workspace
81      *
82      * @return proxied JCR Workspace
83      */

84     public Workspace getProxy()
85     {
86         if (proxy == null)
87         {
88             proxy = (Workspace)JCRProxyFactory.create(this, Workspace.class, session);
89         }
90         return proxy;
91     }
92     
93     
94     /* (non-Javadoc)
95      * @see javax.jcr.Workspace#getSession()
96      */

97     public Session getSession()
98     {
99         return session.getProxy();
100     }
101
102     /* (non-Javadoc)
103      * @see javax.jcr.Workspace#getName()
104      */

105     public String JavaDoc getName()
106     {
107         return session.getWorkspaceStore().getIdentifier();
108     }
109
110     /* (non-Javadoc)
111      * @see javax.jcr.Workspace#copy(java.lang.String, java.lang.String)
112      */

113     public void copy(String JavaDoc srcAbsPath, String JavaDoc destAbsPath) throws ConstraintViolationException, VersionException, AccessDeniedException, PathNotFoundException, ItemExistsException, LockException, RepositoryException
114     {
115         ParameterCheck.mandatoryString("srcAbsPath", srcAbsPath);
116         ParameterCheck.mandatoryString("destAbsPath", destAbsPath);
117         
118         // find source node
119
NodeService nodeService = session.getRepositoryImpl().getServiceRegistry().getNodeService();
120         NodeRef rootRef = nodeService.getRootNode(session.getWorkspaceStore());
121         NodeRef sourceRef = ItemResolver.getNodeRef(session, rootRef, srcAbsPath);
122         if (sourceRef == null)
123         {
124             throw new PathNotFoundException("Source path " + srcAbsPath + " cannot be found.");
125         }
126         
127         // find dest node
128
NodeRef destRef = null;
129         QName destName = null;
130         Path destPath = new JCRPath(session.getNamespaceResolver(), destAbsPath).getPath();
131         if (destPath.size() == 1)
132         {
133             destRef = rootRef;
134             destName = ((JCRPath.SimpleElement)destPath.get(0)).getQName();
135         }
136         else
137         {
138             Path destParentPath = destPath.subPath(destPath.size() -2);
139             destRef = ItemResolver.getNodeRef(session, rootRef, destParentPath.toPrefixString(session.getNamespaceResolver()));
140             if (destRef == null)
141             {
142                 throw new PathNotFoundException("Destination path " + destParentPath + " cannot be found.");
143             }
144             destName = ((JCRPath.SimpleElement)destPath.get(destPath.size() -1)).getQName();
145         }
146         
147         // validate name
148
// TODO: Replace with proper name validation
149
if (destName.getLocalName().indexOf('[') != -1 || destName.getLocalName().indexOf(']') != -1)
150         {
151             throw new RepositoryException("Node name '" + destName + "' is invalid");
152         }
153         
154         // determine child association type for destination
155
ChildAssociationRef childAssocRef = nodeService.getPrimaryParent(sourceRef);
156         
157         // copy node
158
CopyService copyService = session.getRepositoryImpl().getServiceRegistry().getCopyService();
159         copyService.copy(sourceRef, destRef, childAssocRef.getTypeQName(), destName);
160
161         // finally save
162
session.save();
163     }
164
165     /* (non-Javadoc)
166      * @see javax.jcr.Workspace#copy(java.lang.String, java.lang.String, java.lang.String)
167      */

168     public void copy(String JavaDoc srcWorkspace, String JavaDoc srcAbsPath, String JavaDoc destAbsPath) throws NoSuchWorkspaceException, ConstraintViolationException, VersionException, AccessDeniedException, PathNotFoundException, ItemExistsException, LockException, RepositoryException
169     {
170         throw new UnsupportedRepositoryOperationException();
171     }
172
173     /* (non-Javadoc)
174      * @see javax.jcr.Workspace#clone(java.lang.String, java.lang.String, java.lang.String, boolean)
175      */

176     public void clone(String JavaDoc srcWorkspace, String JavaDoc srcAbsPath, String JavaDoc destAbsPath, boolean removeExisting) throws NoSuchWorkspaceException, ConstraintViolationException, VersionException, AccessDeniedException, PathNotFoundException, ItemExistsException, LockException, RepositoryException
177     {
178         throw new UnsupportedRepositoryOperationException();
179     }
180
181     /* (non-Javadoc)
182      * @see javax.jcr.Workspace#move(java.lang.String, java.lang.String)
183      */

184     public void move(String JavaDoc srcAbsPath, String JavaDoc destAbsPath) throws ConstraintViolationException, VersionException, AccessDeniedException, PathNotFoundException, ItemExistsException, LockException, RepositoryException
185     {
186         session.move(srcAbsPath, destAbsPath);
187         session.save();
188     }
189
190     /* (non-Javadoc)
191      * @see javax.jcr.Workspace#restore(javax.jcr.version.Version[], boolean)
192      */

193     public void restore(Version[] versions, boolean removeExisting) throws ItemExistsException, UnsupportedRepositoryOperationException, VersionException, LockException, InvalidItemStateException, RepositoryException
194     {
195         throw new UnsupportedRepositoryOperationException();
196     }
197
198     /* (non-Javadoc)
199      * @see javax.jcr.Workspace#getQueryManager()
200      */

201     public QueryManager getQueryManager() throws RepositoryException
202     {
203         if (queryManager == null)
204         {
205             queryManager = new QueryManagerImpl(session);
206         }
207         return queryManager;
208     }
209
210     /* (non-Javadoc)
211      * @see javax.jcr.Workspace#getNamespaceRegistry()
212      */

213     public NamespaceRegistry getNamespaceRegistry() throws RepositoryException
214     {
215         return session.getRepositoryImpl().getNamespaceRegistry();
216     }
217
218     /* (non-Javadoc)
219      * @see javax.jcr.Workspace#getNodeTypeManager()
220      */

221     public NodeTypeManager getNodeTypeManager() throws RepositoryException
222     {
223         return session.getTypeManager();
224     }
225
226     /* (non-Javadoc)
227      * @see javax.jcr.Workspace#getObservationManager()
228      */

229     public ObservationManager getObservationManager() throws UnsupportedRepositoryOperationException, RepositoryException
230     {
231         throw new UnsupportedRepositoryOperationException();
232     }
233
234     /* (non-Javadoc)
235      * @see javax.jcr.Workspace#getAccessibleWorkspaceNames()
236      */

237     public String JavaDoc[] getAccessibleWorkspaceNames() throws RepositoryException
238     {
239         NodeService nodeService = session.getRepositoryImpl().getServiceRegistry().getNodeService();
240         List JavaDoc<StoreRef> storeRefs = nodeService.getStores();
241         List JavaDoc<String JavaDoc> workspaceStores = new ArrayList JavaDoc<String JavaDoc>();
242         for (StoreRef storeRef : storeRefs)
243         {
244             if (storeRef.getProtocol().equals(StoreRef.PROTOCOL_WORKSPACE))
245             {
246                 workspaceStores.add(storeRef.getIdentifier());
247             }
248         }
249         return workspaceStores.toArray(new String JavaDoc[workspaceStores.size()]);
250     }
251
252     /* (non-Javadoc)
253      * @see javax.jcr.Workspace#getImportContentHandler(java.lang.String, int)
254      */

255     public ContentHandler JavaDoc getImportContentHandler(String JavaDoc parentAbsPath, int uuidBehavior) throws PathNotFoundException, ConstraintViolationException, VersionException, LockException, AccessDeniedException, RepositoryException
256     {
257         return session.getImportContentHandler(parentAbsPath, uuidBehavior);
258     }
259
260     /* (non-Javadoc)
261      * @see javax.jcr.Workspace#importXML(java.lang.String, java.io.InputStream, int)
262      */

263     public void importXML(String JavaDoc parentAbsPath, InputStream JavaDoc in, int uuidBehavior) throws IOException JavaDoc, PathNotFoundException, ItemExistsException, ConstraintViolationException, InvalidSerializedDataException, LockException, AccessDeniedException, RepositoryException
264     {
265         session.importXML(parentAbsPath, in, uuidBehavior);
266     }
267
268 }
269
Popular Tags