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.wcm.core;
18  
19  import javax.jcr.Item;
20  import javax.jcr.Node;
21  import javax.jcr.PathNotFoundException;
22  import javax.jcr.RepositoryException;
23  import javax.jcr.Session;
24  
25  import org.exoplatform.services.cms.link.NodeFinder;
26  import org.exoplatform.services.jcr.core.ManageableRepository;
27  import org.exoplatform.services.security.IdentityConstants;
28  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
29  
30  /**
31   * Created by The eXo Platform SAS
32   * Author : Nguyen Anh Vu
33   *              anhvurz90@gmail.com
34   * 5 Jul. 2011
35   */
36  public class ItemLocation {
37    
38    /** The repository. */
39    protected String repository;
40  
41    /** The workspace. */
42    protected String workspace;
43  
44    /** The path. */
45    protected String path;
46    
47    /** The UUID. */
48    protected String uuid;
49    
50    /** If session is system */
51    protected boolean isSystemSession;
52    
53    /**
54     * Instantiates a new item location.
55     */
56    public ItemLocation() {}
57    
58    /**
59     * Instantiates a new item location.
60     *
61     * @param repository the repository
62     * @param workspace the workspace
63     * @param path the path
64     * @param uuid the uuid
65     * @param isSystem if the node session is system 
66     */
67    public ItemLocation(final String repository, final String workspace, final String path, final String uuid, 
68        final boolean isSystem) {
69      this.repository = repository;
70      this.workspace = workspace;
71      this.path = path;
72      this.uuid = uuid;
73      this.isSystemSession = isSystem;
74    }
75    
76    /**
77     * Instantiates a new item location.
78     *
79     * @param repository the repository
80     * @param workspace the workspace
81     * @param path the path
82     * @param uuid the uuid
83     */
84    public ItemLocation(final String repository, final String workspace, final String path, final String uuid ) {
85      this(repository, workspace, path, uuid, false);
86    }
87    
88    /**
89     * Instantiates a new item location.
90     *
91     * @param repository the repository
92     * @param workspace the workspace
93     * @param path the path
94     */
95    public ItemLocation(final String repository, final String workspace, final String path) {
96      this(repository, workspace, path, null, false);
97    }
98  
99    /**
100    * Instantiates a new item location.
101    */
102   public ItemLocation(ItemLocation itemLocation) {
103     this(itemLocation.repository, itemLocation.workspace, itemLocation.path, itemLocation.uuid, itemLocation.isSystemSession);
104   }
105 
106   /**
107    * Gets the repository.
108    *
109    * @return the repository
110    */
111   public String getRepository() {
112     return repository;
113   }
114 
115   /**
116    * Sets the repository.
117    *
118    * @param repository the new repository
119    */
120   public void setRepository(String repository) {
121     this.repository = repository;
122   }
123 
124   /**
125    * Gets the workspace.
126    *
127    * @return the workspace
128    */
129   public String getWorkspace() {
130     return workspace;
131   }
132 
133   /**
134    * Sets the workspace.
135    *
136    * @param workspace the new workspace
137    */
138   public void setWorkspace(String workspace) {
139     this.workspace = workspace;
140   }
141 
142   /**
143    * Gets the path.
144    *
145    * @return the path
146    */
147   public String getPath() {
148     return path;
149   }
150 
151 
152   /**
153    * Sets the path.
154    *
155    * @param path the new path
156    */
157   public void setPath(String path) {
158     this.path = path;
159   }
160   
161   /**
162    * Sets the uuid.
163    *
164    * @param uuid the new uuid
165    */
166   public void setUUID(String uuid) {
167     this.uuid = uuid;
168   }
169 
170   /**
171    * Gets the uuid.
172    *
173    * @return the uuid
174    */
175   public String getUUID() {
176     return uuid;
177   }
178   
179   /**
180    * Sets the isSystemSession.
181    *
182    * @param value isSysstemSession
183    */
184   public void setSystemSession(boolean value) {
185     this.isSystemSession = value;
186   }
187 
188   /**
189    * Gets the isSystemSession.
190    *
191    * @return true if node session is system, false if not
192    */
193   public boolean isSystemSession() {
194     return this.isSystemSession;
195   }
196   
197   /**
198    * Get an ItemLocation object by an item
199    *
200    * @param item the item
201    *
202    * @return a ItemLocation object
203    */
204   public static final ItemLocation getItemLocationByItem(final Item item) {
205     Session session = null;
206     try {
207       session = item.getSession();
208       String repository = ((ManageableRepository)session.getRepository()).getConfiguration().getName();
209       String workspace = session.getWorkspace().getName();
210       String path = item.getPath();
211       String uuid = null;
212       try {
213         if (item instanceof Node)
214           uuid = ((Node)item).getUUID();
215       } catch (RepositoryException e) {
216         uuid = null;
217       }
218       boolean isSystemSession = IdentityConstants.SYSTEM.equals(session.getUserID());
219       return new ItemLocation(repository, workspace, path, uuid, isSystemSession);
220     } catch (RepositoryException e) {
221       return null;
222     }
223   }
224 
225   /**
226    * Get an item by a ItemLocation object.
227    *
228    * @param itemLocation the ItemLocation object
229    *
230    * @return an item
231    */
232   public static final Item getItemByLocation(final ItemLocation itemLocation) {
233     Session session = null;
234     try {
235       ManageableRepository repository = WCMCoreUtils.getRepository();
236       session = (itemLocation.isSystemSession ? 
237                         WCMCoreUtils.getSystemSessionProvider() : WCMCoreUtils.getUserSessionProvider())
238                                     .getSession(itemLocation.getWorkspace(), repository);
239       if (itemLocation.getUUID() != null)
240         return session.getNodeByUUID(itemLocation.getUUID());
241       else {
242         return WCMCoreUtils.getService(NodeFinder.class).getItem(session, itemLocation.getPath());
243       }
244     } catch(PathNotFoundException pne) {
245       return null;
246     } catch (Exception e) {
247       return null;
248     }
249   }
250 
251   
252   
253 }