View Javadoc
1   /*
2    * Copyright (C) 2003-2009 eXo Platform SAS.
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Affero General Public License
6    * as published by the Free Software Foundation; either version 3
7    * of the License, or (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, see<http://www.gnu.org/licenses/>.
16   */
17  package org.exoplatform.services.cms.link;
18  
19  import javax.jcr.AccessDeniedException;
20  import javax.jcr.InvalidItemStateException;
21  import javax.jcr.Item;
22  import javax.jcr.ItemExistsException;
23  import javax.jcr.ItemNotFoundException;
24  import javax.jcr.ItemVisitor;
25  import javax.jcr.Node;
26  import javax.jcr.Property;
27  import javax.jcr.ReferentialIntegrityException;
28  import javax.jcr.RepositoryException;
29  import javax.jcr.Session;
30  import javax.jcr.lock.LockException;
31  import javax.jcr.nodetype.ConstraintViolationException;
32  import javax.jcr.nodetype.NoSuchNodeTypeException;
33  import javax.jcr.version.VersionException;
34  
35  import org.exoplatform.services.jcr.ext.common.SessionProvider;
36  import org.exoplatform.services.wcm.core.ItemLocation;
37  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
38  
39  /**
40   * Created by The eXo Platform SAS
41   * Author : eXoPlatform
42   *          nicolas.filotto@exoplatform.com
43   * 1 avr. 2009
44   */
45  public abstract class ItemLinkAware implements Item {
46  
47    protected final ItemLocation itemLocation;
48    protected final String originalWorkspaceName;
49    protected final String virtualPath;
50  
51    protected ItemLinkAware(String originalWorkspaceName, String virtualPath, Item item) {
52      this.originalWorkspaceName = originalWorkspaceName;
53      this.itemLocation = ItemLocation.getItemLocationByItem(item);
54      if (!virtualPath.startsWith("/")) {
55        throw new IllegalArgumentException("The path '" + virtualPath +  "' must be an absolute path");
56      }
57      this.virtualPath = virtualPath;
58    }
59  
60    public static ItemLinkAware newInstance(String originalWorkspaceName, String originalAbsPath, Item item) {
61      if (item instanceof Node) {
62        return new NodeLinkAware(originalWorkspaceName, originalAbsPath, (Node) item);
63      } 
64      return new PropertyLinkAware(originalWorkspaceName, originalAbsPath, (Property) item);
65    }
66    
67    public Item getItem() {
68      return ItemLocation.getItemByLocation(itemLocation);
69    }
70    
71    public Session getItemSession() throws RepositoryException {
72      return getItem().getSession();
73    }
74    
75    public Session getOriginalSession() throws RepositoryException {
76      SessionProvider sessionProvider;
77      if (itemLocation.isSystemSession()) {
78        sessionProvider = WCMCoreUtils.getSystemSessionProvider();
79      } else {
80        sessionProvider = WCMCoreUtils.getUserSessionProvider();
81      }
82      return sessionProvider.getSession(originalWorkspaceName, WCMCoreUtils.getRepository());
83    }
84    
85    /**
86     * {@inheritDoc}
87     */
88    public void accept(ItemVisitor visitor) throws RepositoryException {
89      getItem().accept(visitor);
90    }
91  
92    /**
93     * {@inheritDoc}
94     */
95    public Item getAncestor(int depth) throws ItemNotFoundException,
96                                     AccessDeniedException,
97                                     RepositoryException {
98      return LinkUtils.getNodeFinder().getItem(getOriginalSession(), LinkUtils.getAncestorPath(virtualPath, depth));
99    }
100 
101   /**
102    * {@inheritDoc}
103    */
104   public int getDepth() throws RepositoryException {
105     return LinkUtils.getDepth(virtualPath);
106   }
107 
108   /**
109    * {@inheritDoc}
110    */
111   public String getName() throws RepositoryException {
112     return getItem().getName();
113   }
114 
115   /**
116    * {@inheritDoc}
117    */
118   public Node getParent() throws ItemNotFoundException, AccessDeniedException, RepositoryException {
119     return (Node) LinkUtils.getNodeFinder().getItem(getOriginalSession(), LinkUtils.getParentPath(virtualPath));
120   }
121 
122   /**
123    * {@inheritDoc}
124    */
125   public String getPath() throws RepositoryException {
126     return virtualPath;
127   }
128 
129   /**
130    * {@inheritDoc}
131    */
132   public Session getSession() throws RepositoryException {
133     return new SessionLinkAware(this);
134   }
135 
136   /**
137    * {@inheritDoc}
138    */
139   public boolean isModified() {
140     return getItem().isModified();
141   }
142 
143   /**
144    * {@inheritDoc}
145    */
146   public boolean isNew() {
147     return getItem().isNew();
148   }
149 
150   /**
151    * {@inheritDoc}
152    */
153   public boolean isNode() {
154     return getItem().isNode();
155   }
156 
157   /**
158    * {@inheritDoc}
159    */
160   public boolean isSame(Item otherItem) throws RepositoryException {
161     return getItem().isSame(otherItem);
162   }
163 
164   /**
165    * {@inheritDoc}
166    */
167   public void refresh(boolean keepChanges) throws InvalidItemStateException, RepositoryException {
168     getItem().refresh(keepChanges);
169   }
170 
171   /**
172    * {@inheritDoc}
173    */
174   public void remove() throws VersionException,
175                       LockException,
176                       ConstraintViolationException,
177                       RepositoryException {
178     getItem().remove();
179   }
180 
181   /**
182    * {@inheritDoc}
183    */
184   public void save() throws AccessDeniedException,
185                     ItemExistsException,
186                     ConstraintViolationException,
187                     InvalidItemStateException,
188                     ReferentialIntegrityException,
189                     VersionException,
190                     LockException,
191                     NoSuchNodeTypeException,
192                     RepositoryException {
193     getItem().save();
194   }
195 }