KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > jcr > impl > core > ItemImpl


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5
6 package org.exoplatform.services.jcr.impl.core;
7
8 import java.util.ArrayList JavaDoc;
9
10 import javax.jcr.Item;
11 import javax.jcr.ItemNotFoundException;
12 import javax.jcr.Node;
13 import javax.jcr.NodeIterator;
14 import javax.jcr.PathNotFoundException;
15 import javax.jcr.RepositoryException;
16 import javax.jcr.StringIterator;
17 import javax.jcr.Ticket;
18 import javax.jcr.UnsupportedRepositoryOperationException;
19 import javax.jcr.access.AccessDeniedException;
20 import javax.jcr.lock.Lock;
21
22 import org.apache.commons.logging.Log;
23 import org.exoplatform.services.jcr.core.ItemLocation;
24 import org.exoplatform.services.jcr.impl.util.EntityCollection;
25 import org.exoplatform.services.log.LogUtil;
26
27
28 /**
29  * Created by The eXo Platform SARL .
30  *
31  * @author <a HREF="mailto:geaz@users.sourceforge.net">Gennady Azarenkov</a>
32  * @version $Id: ItemImpl.java,v 1.14 2004/09/16 15:26:53 geaz Exp $
33  */

34
35 abstract public class ItemImpl implements Item {
36
37   protected Log log;
38
39   protected ItemLocation location;
40   protected TicketImpl ticket;
41
42
43   /**
44    * Level1 constructor
45    */

46   public ItemImpl(String JavaDoc absPath) throws PathNotFoundException {
47     log = LogUtil.getLog("org.exoplatform.services.jcr");
48
49     if (absPath == null || absPath.length() == 0 || !(absPath.startsWith("/")))
50       throw new PathNotFoundException("ItemImpl() Invalid Path '" + absPath + "'!");
51
52     this.location = new ItemLocation(absPath);
53   }
54
55   /**
56    * 6.2.4
57    * Returns the path to this Item.
58    * In level 2: If this Item has multiple paths, this method
59    * returns one of those paths. How the implementation
60    * chooses which path to return is left unspecified, the only
61    * requirement is that for a given Item this method always
62    * returns the same path. To get all paths to this Item use getPaths().
63    */

64   public String JavaDoc getPath() {
65     return location.getPath();
66   }
67
68   /**
69    * 7.1.1
70    * Returns all paths to this Item.
71    * In level 1: Returns the path to this Item, wrapped in an
72    * array. This is the same path returned by getPath().
73    */

74 /* public StringIterator getPaths() {
75     ArrayList list = new ArrayList();
76     list.addAll(ticket.getNodesManager().getPaths(getPath()));
77     EntityCollection paths = new EntityCollection(list);
78     return paths;
79   }
80 */

81   /**
82    * 6.2.4
83    * Returns the name of this Item. The name is the last item
84    * in the path. If this Item is the root node of the repository
85    * (i.e. if this.getDepth() == 0), an empty string will be returned.
86    * In level 2: Returns one of the names of this Item. The
87    * name returned is the last item in the path returned by getPath().
88    */

89   public String JavaDoc getName() {
90     return location.getName();
91   }
92
93
94   /**
95    * 6.2.4
96    * Returns the ancestor of the specified absolute degree.
97    * An ancestor of absolute degree x is the Item that is x
98    * levels down along the path from the root node to this Item.
99    * degree = 0 returns the root node.
100    * degree = 1 returns the child of the root node along the path to this Item.
101    * degree = 2 returns the grandchild of the root node along the path to this Item.
102    * And so on to degree = n, where n is the depth of this Item,
103    * which returns this Item itself. If degree > n is specified then an ItemNotFoundException
104    * is thrown.
105    * In level 2: Same behavior as level 1, but if multiple
106    * paths exist to this Item then the path used is the same
107    * one that is returned by getPath().
108    */

109   public Item getAncestor(int degree) throws ItemNotFoundException, AccessDeniedException, RepositoryException {
110     try {
111       log.debug("getAncestor(" + degree + ") " + ticket);
112       int n = getDepth() - degree;
113       if(n==0)
114         return this;
115       else if (n < 0)
116         throw new ItemNotFoundException("Workspace.getAncestor() ancestor's degree > depth of this item.");
117       else {
118         ItemImpl item = (ItemImpl)ticket.getNodesManager().getNodeByPath(location.getAncestorPath(n));
119         item.setTicket(ticket);
120         return item;
121       }
122     } catch (PathNotFoundException e) {
123       throw new ItemNotFoundException(e.getMessage(), e);
124     }
125   }
126
127
128   /**
129    * 6.2.4
130    * Returns the parent of this Item.
131    * In level 2: If this Item has multiple paths then this
132    * method returns the parent along the path returned by getPath().
133    */

134   public Node getParent() throws ItemNotFoundException, AccessDeniedException, RepositoryException {
135     try {
136       return (Node) getAncestor(getDepth() - 1);
137     } catch (PathNotFoundException e) {
138       throw new ItemNotFoundException(e.getMessage(), e);
139     }
140   }
141
142   /**
143    * 7.1.1
144    * Returns all the parents of this Item.
145    * In level 1: Returns the parent of this Item, wrapped in a NodeIterator.
146    */

147   public NodeIterator getParents() throws ItemNotFoundException, AccessDeniedException, RepositoryException {
148
149     ArrayList JavaDoc list = new ArrayList JavaDoc();
150     StringIterator paths = getPaths();
151     while(paths.hasNext()) {
152         String JavaDoc path = new ItemLocation(paths.nextString()).getParentPath();
153         Node node = ticket.getNodeByAbsPath(path);
154         list.add(node);
155     }
156     return new EntityCollection(list);
157
158   }
159
160   /**
161    * Returns the <code>Ticket</code> through which this <code>Item</code>
162    * was acquired.
163    * Every <code>Item</code> can ultimately be traced back through a series
164    * of method calls to the call <code>{@link Ticket#getRootNode}</code>,
165    * <code>{@link Ticket#getNodeByAbsPath}</code> or
166    * <code>{@link Ticket#getNodeByUUID}</code>. This method returns that
167    * <code>Ticket</code> object.
168    *
169    * @return the <code>Tickete</code> through which this <code>Item</code> was
170    * acquired.
171    */

172   public Ticket getTicket() {
173     return ticket;
174   }
175
176
177   /**
178    * 6.2.4
179    * Returns the depth below the root node of this Item (counting this Item itself):
180    * The root node returns 0.
181    * A property or child node of the root node returns 1.
182    * A property or child node of a child node of the root returns 2.
183    * And so on to this Item.
184    * In Level 2: Same behavior as level 1, but if multiple
185    * paths exist to this Item then the path used to determine
186    * the depth is the same one that is returned by getPath().
187    */

188   public int getDepth() {
189     return location.getDepth();
190   }
191
192   /**
193    * 6.5
194    * In some implementations, the identity of Node or Property Java
195    * objects may not correspond to the identity of nodes or properties
196    * as entities within the repository. For example, it may be possible
197    * for two Java objects A and B to both represent the same actual
198    * node in the repository such that any changes to A are reflected in
199    * subsequent reads from B, but that nonetheless, A.equals(B)==false.
200    * <p/>
201    * Returns true if this Item Java object represents the same
202    * repository item as the Java object otherItem.
203    */

204   public boolean isIdentical(Item otherItem) {
205     if (otherItem == null)
206       return false;
207
208     if (!this.getClass().getName().equals(otherItem.getClass().getName()))
209       return false;
210
211     return isItemIdentical(otherItem);
212   }
213
214
215   protected abstract boolean isItemIdentical(Item otherItem);
216
217   /* In level 1: Always returns true. */
218   public boolean isGranted(long permissions) throws UnsupportedRepositoryOperationException, RepositoryException {
219     return true;
220   }
221
222   /* level2 */
223   public Lock lock(boolean recurse, boolean shared, int lockType) throws UnsupportedRepositoryOperationException, AccessDeniedException, RepositoryException {
224     throw new UnsupportedRepositoryOperationException("Workspace.lock() is not supported by Level 1 of JCR.");
225   }
226
227   /* level2 */
228   public void unlock(Lock lock) throws UnsupportedRepositoryOperationException, AccessDeniedException {
229     throw new UnsupportedRepositoryOperationException("Workspace.unlock() is not supported by Level 1 of JCR.");
230   }
231
232   /* level2 */
233   public Lock[] getLocks() throws UnsupportedRepositoryOperationException {
234     throw new UnsupportedRepositoryOperationException("Workspace.getLocks() is not supported by Level 1 of JCR.");
235   }
236
237   /**
238    * Returns <code>true</code> if there is a lock on this node, false
239    * otherwise.
240    *
241    * @return a boolean.
242    */

243   public boolean hasLocks() {
244     return false;
245   }
246
247   public void setTicket(TicketImpl ticket) {
248     this.ticket = ticket;
249   }
250
251   void setLocation(ItemLocation loc) {
252     this.location = loc;
253   }
254 /*
255   public boolean equals(Object obj) {
256     if (!(obj instanceof ItemImpl))
257       return false;
258     return ((ItemImpl) obj).getPath() == this.getPath();
259   }
260 */

261 }
262
Popular Tags