1 5 6 package org.exoplatform.services.jcr.impl.core; 7 8 import java.util.ArrayList ; 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 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 46 public ItemImpl(String 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 64 public String getPath() { 65 return location.getPath(); 66 } 67 68 74 81 89 public String getName() { 90 return location.getName(); 91 } 92 93 94 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 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 147 public NodeIterator getParents() throws ItemNotFoundException, AccessDeniedException, RepositoryException { 148 149 ArrayList list = new ArrayList (); 150 StringIterator paths = getPaths(); 151 while(paths.hasNext()) { 152 String 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 172 public Ticket getTicket() { 173 return ticket; 174 } 175 176 177 188 public int getDepth() { 189 return location.getDepth(); 190 } 191 192 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 218 public boolean isGranted(long permissions) throws UnsupportedRepositoryOperationException, RepositoryException { 219 return true; 220 } 221 222 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 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 233 public Lock[] getLocks() throws UnsupportedRepositoryOperationException { 234 throw new UnsupportedRepositoryOperationException("Workspace.getLocks() is not supported by Level 1 of JCR."); 235 } 236 237 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 261 } 262 | Popular Tags |