View Javadoc
1   /*
2    * Copyright (C) 2003-2008 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 java.util.ArrayList;
20  import java.util.List;
21  
22  import javax.jcr.Item;
23  import javax.jcr.Node;
24  import javax.jcr.NodeIterator;
25  import javax.jcr.Session;
26  
27  import org.exoplatform.services.cms.link.ItemLinkAware;
28  import org.exoplatform.services.jcr.core.ManageableRepository;
29  import org.exoplatform.services.jcr.ext.common.SessionProvider;
30  import org.exoplatform.services.log.ExoLogger;
31  import org.exoplatform.services.log.Log;
32  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
33  
34  /**
35   * Created by The eXo Platform SAS.
36   *
37   * @author : Hoa.Pham
38   * hoa.pham@exoplatform.com
39   * Jun 20, 2008
40   */
41  public class NodeLocation extends ItemLocation {
42  
43    /** The log. */
44    private static final Log LOG = ExoLogger.getLogger(NodeLocation.class.getName());
45    
46    private List<NodeLocation> children_;
47  
48    public List<NodeLocation> getChildren() {
49      return children_;
50    }
51  
52    public void setChildren(List<NodeLocation> children) {
53      children_ = children;
54    }
55  
56    public boolean hasChildren() {
57      return children_ != null && children_.size() > 0;
58    }
59    /**
60     * Instantiates a new node location.
61     */
62    public NodeLocation() {
63      super();
64    }
65  
66    /**
67     * Instantiates a new node location.
68     *
69     * @param repository the repository
70     * @param workspace the workspace
71     * @param path the path
72     * @param uuid the uuid
73     * @param isSystem if the node session is system
74     */
75    public NodeLocation(final String repository, final String workspace, final String path, final String uuid,
76        final boolean isSystem) {
77      super(repository, workspace, path, uuid, isSystem);
78    }
79  
80    /**
81     * Instantiates a new node location.
82     *
83     * @param repository the repository
84     * @param workspace the workspace
85     * @param path the path
86     * @param uuid the uuid
87     */
88    public NodeLocation(final String repository, final String workspace, final String path, final String uuid ) {
89      super(repository, workspace, path, uuid, false);
90    }
91  
92    /**
93     * Instantiates a new node location.
94     *
95     * @param repository the repository
96     * @param workspace the workspace
97     * @param path the path
98     */
99    public NodeLocation(final String repository, final String workspace, final String path) {
100     super(repository, workspace, path, null, false);
101   }
102 
103   /**
104    * Instantiates a new node location.
105    *
106    */
107   public NodeLocation(ItemLocation location) {
108     super(location);
109   }
110 
111   /**
112    * Get an NodeLocation object by an expression.
113    *
114    * @param exp the expression with pattern repository:workspace:path
115    *
116    * @return a NodeLocation object
117    */
118   public static final NodeLocation getNodeLocationByExpression(final String exp) {
119     String[] temp = split(exp, ":");
120     if (temp.length >= 3 && temp[2].indexOf("/") == 0) {
121       String repository = temp[0];
122       String workspace = temp[1];
123       String nodepath = exp.substring(repository.length() + workspace.length() + 2);
124       return new NodeLocation(repository, workspace, nodepath);
125     }
126     throw new IllegalArgumentException("Invalid expression: " + exp
127         + ". An valid expression has pattern repository:workspace:path");
128   }
129 
130   /**
131    * Get an NodeLocation object by a node.
132    *
133    * @param node the node
134    *
135    * @return a NodeLocation object
136    */
137   public static final NodeLocation getNodeLocationByNode(final Node node) {
138     try {
139       ItemLocation itemLocation = ItemLocation.getItemLocationByItem(node);
140       return new NodeLocation(itemLocation);
141     } catch (Exception e) {
142       return null;
143     }
144   }
145 
146   /**
147    * Get a node by a NodeLocation object.
148    *
149    * @param nodeLocation the NodeLocation object
150    *
151    * @return a node
152    */
153   public static final Node getNodeByLocation(final NodeLocation nodeLocation) {
154     try {
155       Item item = ItemLocation.getItemByLocation(nodeLocation);
156       return (Node)item;
157     } catch (Exception e) {
158       return null;
159     }
160   }
161 
162   /**
163    * gets a Node list from a NodeLocation list
164    * @param locationList NodeLocation list
165    * @return the Node list
166    */
167   @SuppressWarnings("unchecked")
168   public static final List getNodeListByLocationList(final List locationList) {
169     List ret = new ArrayList();
170     try {
171       ManageableRepository repository = WCMCoreUtils.getRepository();
172       SessionProvider systemSessionProvider = WCMCoreUtils.getSystemSessionProvider();
173       SessionProvider sessionProvider = WCMCoreUtils.getUserSessionProvider();
174       String systemWorkspace = repository.getConfiguration().getSystemWorkspaceName();
175       Session session = null;
176       Node node;
177       for (Object obj : locationList)
178         if (obj instanceof NodeLocation) {
179           node = null;
180           try {
181            NodeLocation location = (NodeLocation)obj;
182            session = (systemWorkspace.equals(location.getWorkspace()) || location.isSystemSession)?
183                       systemSessionProvider.getSession(location.getWorkspace(), repository) :
184                       sessionProvider.getSession(location.getWorkspace(), repository);
185 
186            node = location.getUUID() != null ? session.getNodeByUUID(location.getUUID()) :
187                                           (Node)session.getItem(location.getPath());
188            ret.add(node);
189           } catch (Exception e) {
190             if (LOG.isDebugEnabled()) {
191               LOG.debug(e.getMessage(), e);
192             }
193           }
194         } else {
195           ret.add(obj);
196         }
197     } catch (Exception e) {
198       return ret;
199     }
200     return ret;
201   }
202 
203   /**
204    * returns the list of node location from the node list
205    * @param nodeList the node list
206    * @return node location list
207    */
208   @SuppressWarnings("unchecked")
209   public static final List getLocationsByNodeList(final List nodeList) {
210     List ret = new ArrayList();
211     for(Object obj : nodeList)
212       if (obj instanceof ItemLinkAware) {
213         ret.add(obj);
214       } else if (obj instanceof Node) {
215         NodeLocation location = getNodeLocationByNode((Node)obj);
216         if (location != null)
217           ret.add(location);
218       } else {
219         ret.add(obj);
220       }
221     return ret;
222   }
223 
224   /**
225    * returns the list of node location from the node iterator
226    * @param nodeIterator the Node iterator
227    * @return node location list
228    */
229   public static final List<NodeLocation> getLocationsByIterator(final NodeIterator nodeIterator) {
230     List<NodeLocation> ret = new ArrayList<NodeLocation>();
231     while (nodeIterator.hasNext()) {
232       NodeLocation location = getNodeLocationByNode(nodeIterator.nextNode());
233       if (location != null)
234         ret.add(location);
235     }
236     return ret;
237   }
238 
239 
240   /**
241    * Get a node by an expression.
242    *
243    * @param expression the expression
244    * @return a node
245    */
246   public static final Node getNodeByExpression(final String expression) {
247     return getNodeByLocation(getNodeLocationByExpression(expression));
248   }
249 
250   /**
251    * Get node's expression by a node.
252    *
253    * @param node the node to get expression
254    * @return The node's expression
255    */
256   public static final String getExpressionByNode(final Node node) {
257     NodeLocation location = NodeLocation.getNodeLocationByNode(node);
258     return mergeString(location.getRepository(), location.getWorkspace(), location.getPath());
259   }
260 
261   /**
262    * Get node's expression by a NodeLocation.
263    *
264    * @param location location
265    * @return The node's expression
266    */
267   public static final String getExpressionByNodeLocation(final NodeLocation location) {
268     return mergeString(location.getRepository(), location.getWorkspace(), location.getPath());
269   }
270 
271   /* (non-Javadoc)
272    * @see java.lang.Object#toString()
273    */
274   public String toString() {
275     return mergeString(repository, workspace, path);
276   }
277 
278   /**
279    * Get the merged string
280    *
281    * @param repository: The node's repository
282    * @param workspace: The node's workspace
283    * @param path: The node's path
284    * @return A merged string of the parameters
285    */
286   private static String mergeString(String repository, String workspace, String path) {
287     StringBuffer buffer = new StringBuffer();
288     buffer.append(repository)
289           .append(":")
290           .append(workspace)
291           .append(":")
292           .append(path);
293     return buffer.toString();
294   }
295 
296   public boolean equals(Object obj) {
297     if (obj == null || !(obj instanceof NodeLocation)) return false;
298     NodeLocation location2 = (NodeLocation)obj;
299     return equalsString(this.repository, location2.getRepository()) &&
300                 equalsString(this.getWorkspace(), location2.getWorkspace()) &&
301                 (equalsString(this.getPath(), location2.getPath()) ||
302                     equalsString(this.getUUID(), location2.getUUID()));
303   }
304 
305   public boolean equalsString(String st1, String st2) {
306     if (st1 == null && st2 == null) return true;
307     if (st1 == null || st2 == null) return false;
308     return st1.equals(st2);
309   }
310 
311   public int hashCode() {
312     return (repository == null ? 0 : repository.hashCode()) +
313            (workspace == null ? 0 : workspace.hashCode()) +
314            (uuid == null ? 0 : uuid.hashCode()) +
315            (path == null ? 0 : path.hashCode());
316   }
317 
318   private static final String[] split(String s, String ch) {
319     int maxLength = 3;
320     String[] ss = new String[maxLength];
321     int prev = 0;
322     int i=0;
323     while(true) {
324       int next = s.indexOf(ch, prev);
325       if (next == -1 || i == maxLength - 1) {
326         ss[i] = s.substring(prev);
327         break;
328       }
329       ss[i++] = s.substring(prev, next);
330       prev = next+1;
331     }
332 
333     return ss;
334   }
335 }