KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
9  * Created by The eXo Platform SARL .
10  *
11  * @author <a HREF="mailto:geaz@users.sourceforge.net">Gennady Azarenkov</a>
12  * @version $Id: NodesStorage.java,v 1.6 2004/11/02 18:36:33 geaz Exp $
13  */

14
15 import java.util.ArrayList JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.HashSet JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.Set JavaDoc;
22
23 import javax.jcr.PathNotFoundException;
24 import javax.jcr.RepositoryException;
25
26 import org.apache.commons.logging.Log;
27 import org.exoplatform.services.jcr.core.NodeChange;
28 import org.exoplatform.services.jcr.core.NodeData;
29 import org.exoplatform.services.jcr.core.ReferenceableNodeLocation;
30 import org.exoplatform.services.jcr.storage.RepositoryManager;
31 import org.exoplatform.services.jcr.storage.WorkspaceContainer;
32 import org.exoplatform.services.jcr.util.PathUtil;
33 import org.exoplatform.services.log.LogUtil;
34
35 public class NodesStorage {
36
37     // path -> NodeChange
38
private Map JavaDoc nodeChanges;
39     // UUID -> NodeChange (reference to nodeChanges' item)
40
private Map JavaDoc nodeReferences;
41     private RepositoryManager repositoryManager;
42     private WorkspaceContainer workspaceContainer;
43     protected Log log;
44
45     NodesStorage(RepositoryManager repositoryManager, WorkspaceContainer workspaceContainer) {
46       nodeChanges = new HashMap JavaDoc();
47       nodeReferences = new HashMap JavaDoc();
48       this.repositoryManager = repositoryManager;
49       this.workspaceContainer = workspaceContainer;
50       log = LogUtil.getLog("org.exoplatform.services.jcr");
51     }
52
53     void add(NodeImpl node) {
54
55         NodeChange change = new NodeChangeImpl(node, NodeChangeState.ADDED, node.getPath());
56         if(node != null)
57           nodeChanges.put(change.getPath(), change);
58
59        // If there is UUID add change to the nodeReferences as well
60
String JavaDoc uuid;
61        try {
62           uuid = node.getUUID();
63           nodeReferences.put(uuid, change);
64        } catch (Exception JavaDoc e) {
65        }
66
67     }
68
69     void addReference(String JavaDoc relatedUUID, String JavaDoc refPath) {
70        log.debug("NodesStorage.addReference() "+refPath+" added to "+relatedUUID);
71        // Is there such node?
72
NodeChange change = (NodeChange)nodeReferences.get(relatedUUID);
73        if(change == null) {
74
75           try {
76              ReferenceableNodeLocation loc = repositoryManager.getLocationByUUID(workspaceContainer.getName(), relatedUUID);
77              loc.addReferencedPath(refPath);
78              // Find node from workspaceContainer by realPath
79
// Unchanged or we need REF_ADDED?
80
change = new NodeChangeImpl(workspaceContainer.getNodeByPath(loc.getRealPath()), NodeChangeState.REF_ADDED, refPath);
81           } catch (PathNotFoundException e) {
82             log.error("NodesStorage.addReference node for "+relatedUUID+"('"+refPath+"') not found "+change);
83             return;
84           } catch (RepositoryException e) {
85             e.printStackTrace();
86             throw new RuntimeException JavaDoc("NodesStorage.addReference node for "+relatedUUID+" FAILED "+e);
87           }
88        }
89        nodeChanges.put(refPath, change);
90        nodeReferences.put(relatedUUID, change);
91
92     }
93
94     NodeChange getNodeChangeByPath(String JavaDoc path) {
95         return (NodeChange)nodeChanges.get(path);
96     }
97
98     NodeChange getNodeChangeByUUID(String JavaDoc UUID) {
99         return (NodeChange)nodeReferences.get(UUID);
100     }
101
102     void remove(String JavaDoc path) {
103         // try to remove from references as well
104
NodeChange change = (NodeChange)nodeChanges.get(path);
105         log.debug("NodesStorage.remove "+change);
106         if(change != null) {
107           try {
108             String JavaDoc uuid = change.getNode().getUUID();
109             nodeReferences.remove(uuid);
110           } catch (Exception JavaDoc e) {
111             // just not referenceable node
112
}
113         }
114         nodeChanges.remove(path);
115     }
116
117     void setState(String JavaDoc path, int state) {
118         NodeChange change = (NodeChange)nodeChanges.get(path);
119         if(change != null)
120          change.setState(state);
121     }
122
123     Set JavaDoc getKeys() {
124         return nodeChanges.keySet();
125     }
126
127
128   NodeData getNodeByUUID(String JavaDoc UUID) {
129
130     // try to get node from storage's ref by UUID
131
NodeChange nc = (NodeChange)nodeReferences.get(UUID);
132     if(nc != null) {
133 // if(nc.getState() == NodeChangeState.DELETED)
134
// return null;
135
if(nc.getState() != NodeChangeState.UNCHANGED)
136            return nc.getNode();
137         else { // UNCHANGED
138
try {
139              NodeData temp = workspaceContainer.getNodeByPath(nc.getPath());
140              return nc.refreshNode(temp);
141            } catch (Exception JavaDoc e) {
142              e.printStackTrace();
143              log.error("NodesStorage.getNodeByUUID can't refresh "+nc.getNode()+" NULL is returned!!!! Reason: "+e.getMessage());
144              return null;
145            }
146         }
147     }
148
149     // try to get node from workspace container
150
// try to get node location from repository ref by UUID
151
ReferenceableNodeLocation loc;
152     try {
153         loc = repositoryManager.getLocationByUUID(workspaceContainer.getName(), UUID);
154     } catch (PathNotFoundException e) {
155         log.debug("NodesStorage.getNodeByUUID: UUID '"+UUID+"' not found in RepositoryManager");
156         return null;
157     }
158
159     // find in workspace container by realPath
160
NodeData temp = null;
161     try {
162        temp = workspaceContainer.getNodeByPath(loc.getRealPath());
163     } catch (RepositoryException e) {
164        e.printStackTrace();
165        throw new RuntimeException JavaDoc("NodesStorage.getNodeByPath() for "+loc.getRealPath()+" FAILED "+e);
166     }
167
168     if (temp == null) {
169       log.error("NodesStorage.getNodeByUUID: UUID '"+UUID+"' not found in "+workspaceContainer.getName()+"probably error in container/repository impl!!!");
170       return null;
171     }
172     // Should we add to nodeChanges as well ??
173
nc = new NodeChangeImpl(temp, NodeChangeState.UNCHANGED, temp.getPath());
174     nodeChanges.put(loc.getRealPath(), nc);
175     nodeReferences.put(UUID, nc);
176     return nc.getNode();
177
178   }
179
180   NodeData getNodeByPath(String JavaDoc absPath) {
181     // try get From changes
182
NodeChange nc = (NodeChange)nodeChanges.get(absPath);
183     log.debug("NodesStorage.getNodeByPath(" + absPath + ") NodeChange :"+nc);
184
185     if (nc != null) {
186         // The Same codes as in getByUUID!!!
187
// if(nc.getState() == NodeChangeState.DELETED)
188
// return null;
189

190         if(nc.getState() != NodeChangeState.UNCHANGED)
191            return nc.getNode();
192         else { // UNCHANGED
193
try {
194
195               NodeData temp = workspaceContainer.getNodeByPath(nc.getPath());
196               return nc.refreshNode(temp);
197            } catch (Exception JavaDoc e) {
198              e.printStackTrace();
199              log.error("NodesStorage.getNodeByPath can't refresh "+nc.getNode()+" NULL is returned!!!! Reason: "+e.getMessage());
200              return null;
201            }
202         }
203     }
204
205     //try from workspaceManager by absPath
206
NodeImpl temp = null;
207
208     try {
209        temp = (NodeImpl)workspaceContainer.getNodeByPath(absPath);
210
211     } catch (RepositoryException e) {
212        e.printStackTrace();
213        throw new RuntimeException JavaDoc("NodesStorage.getNodeByPath() for "+absPath+" FAILED "+e);
214     }
215
216     if(temp != null) {
217        nc = new NodeChangeImpl(temp, NodeChangeState.UNCHANGED, temp.getPath());
218        log.debug("Node "+temp+" added to changes from container" );
219        nodeChanges.put(absPath, nc);
220        try {
221           String JavaDoc uuid = nc.getNode().getUUID();
222           nodeReferences.put(uuid, nc);
223        } catch (Exception JavaDoc e) {
224           // just not referenceable node
225
}
226        return nc.getNode();
227     }
228
229
230     // try from repository references
231
ReferenceableNodeLocation loc = null;
232     try {
233         loc = repositoryManager.getLocationByPath(workspaceContainer.getName(), absPath);
234     } catch (PathNotFoundException e) {
235         log.debug("NodesStorage.getNodeByPath: Path '"+absPath+"' not found in RepositoryManager");
236 // return null;
237
}
238
239
240     // find in workspace container by realPath
241
if(loc != null) {
242        try {
243           temp = (NodeImpl)workspaceContainer.getNodeByPath(loc.getRealPath());
244        } catch (RepositoryException e) {
245           e.printStackTrace();
246           throw new RuntimeException JavaDoc("NodesStorage.getNodeByPath() for "+absPath+" FAILED "+e);
247        }
248        if (temp == null) {
249          log.debug("NodesStorage.getNodeByPath: Path '"+loc.getRealPath()+"' not found in "+workspaceContainer.getName());
250          return null;
251        }
252
253        nc = new NodeChangeImpl(temp, NodeChangeState.UNCHANGED, temp.getPath());
254        log.debug("Node "+temp+" added to changes from container" );
255        nodeChanges.put(absPath, nc);
256        try {
257           String JavaDoc uuid = nc.getNode().getUUID();
258           nodeReferences.put(uuid, nc);
259        } catch (Exception JavaDoc e) {
260           // just not referenceable node
261
}
262        return nc.getNode();
263     } else
264        return null;
265
266
267   }
268
269   Set JavaDoc getChildren(String JavaDoc absPath) {
270
271     Set JavaDoc children = new HashSet JavaDoc();
272     List JavaDoc changes = getChildrenChanges(absPath, true);
273     for(int i=0; i<changes.size(); i++) {
274        NodeImpl node =(NodeImpl)((NodeChange)changes.get(i)).getNode();
275        String JavaDoc path = node.getPath();
276 // log.debug("NodesStorage getChild paths of " + absPath + " from changes. ="+path);
277
children.add(path);
278     }
279
280     try {
281        List JavaDoc nodes = workspaceContainer.getChildren(absPath);
282        for(int i=0; i<nodes.size(); i++) {
283 // log.debug("NodesStorage getChild paths of " + absPath + " from workspace container ="+nodes.get(i));
284
children.add((String JavaDoc)nodes.get(i));
285        }
286
287     } catch (RepositoryException e) {
288        e.printStackTrace();
289        throw new RuntimeException JavaDoc("NodesStorage.getChildren for "+absPath+" FAILED "+e);
290     }
291
292     return children;
293   }
294
295 /////////////////////////////////////////////
296

297   List JavaDoc getChildrenChanges(String JavaDoc path, boolean shallow) {
298     ArrayList JavaDoc list = new ArrayList JavaDoc();
299     Iterator JavaDoc entries = nodeChanges.keySet().iterator();
300     while (entries.hasNext()) {
301       String JavaDoc testPath = (String JavaDoc) entries.next();
302       if (PathUtil.isDescendant(testPath, path, shallow)) {
303         NodeImpl node = (NodeImpl)((NodeChange)nodeChanges.get(testPath)).getNode();
304 // if(
305
log.debug("NodesStorage.getChildrenChanges for " +path+ "=" + testPath+" "+node);
306         list.add(nodeChanges.get(testPath));
307       }
308     }
309     return list;
310   }
311
312   // Returns nodes
313
Set JavaDoc getChangedReferencedPaths(String JavaDoc uuid) {
314     Set JavaDoc list = new HashSet JavaDoc();
315     if(!nodeReferences.containsKey(uuid))
316        return list;
317
318     NodeImpl node = (NodeImpl)((NodeChange)nodeReferences.get(uuid)).getNode();
319     Iterator JavaDoc entries = nodeChanges.values().iterator();
320     while (entries.hasNext()) {
321       NodeImpl testNode = (NodeImpl)((NodeChange)entries.next()).getNode();
322       if(testNode.equals(node)) {
323          list.add(testNode.getPath());
324       }
325     }
326     return list;
327   }
328
329 }
330
Popular Tags