KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > jcr > impl > storage > filesystem > nodedata > BaseNodeContainer


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.storage.filesystem.nodedata;
7
8 import org.apache.commons.logging.Log;
9 import org.exoplatform.services.jcr.core.NodeData;
10 import org.exoplatform.services.jcr.impl.core.NodeImpl;
11 import org.exoplatform.services.jcr.impl.core.PropertyImpl;
12 import org.exoplatform.services.jcr.util.PathUtil;
13 import org.exoplatform.services.log.LogUtil;
14
15 import javax.jcr.DateValue;
16 import javax.jcr.PathNotFoundException;
17 import javax.jcr.PropertyType;
18 import javax.jcr.StringValue;
19 import java.io.File JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Calendar JavaDoc;
22 import java.util.Date JavaDoc;
23 import java.util.List JavaDoc;
24
25 /**
26  * Created by The eXo Platform SARL .
27  *
28  * @author <a HREF="mailto:geaz@users.sourceforge.net">Gennady Azarenkov</a>
29  * @version $Id: BaseNodeContainer.java,v 1.7 2004/09/16 15:26:54 geaz Exp $
30  */

31
32 public abstract class BaseNodeContainer implements NodeContainer {
33
34   protected static NodeContainerResolver resolver = new NodeContainerResolver();
35
36   protected File JavaDoc storage;
37   protected String JavaDoc containerPath;
38   protected String JavaDoc nodeType;
39   protected ArrayList JavaDoc rootProperties;
40   protected Log log;
41
42   BaseNodeContainer() {
43     this.log = LogUtil.getLog("org.exoplatform.services.jcr");
44   }
45
46   // jcrPath of container!!!
47
BaseNodeContainer(String JavaDoc jcrPath, String JavaDoc type, File JavaDoc storage) {
48     this();
49     this.containerPath = jcrPath;
50     this.storage = storage;
51     this.nodeType = type;
52   }
53
54   public NodeData getRootNode() {
55     try {
56       return new NodeImpl(containerPath, getRootProps());
57     } catch (Exception JavaDoc e) {
58       throw new RuntimeException JavaDoc("getRootNode failed. Reason: " + e.getMessage());
59     }
60   }
61
62   public NodeData getNodeById(String JavaDoc UUID) {
63     throw new RuntimeException JavaDoc("getNodeById does not supported ! ");
64   }
65
66
67   public final String JavaDoc getNodeType() {
68     return this.nodeType;
69   }
70
71   public final String JavaDoc getContainerPath() {
72     return containerPath;
73   }
74
75   public final File JavaDoc getStorage() {
76     return storage;
77   }
78
79   public final String JavaDoc parseRelPath(String JavaDoc jcrPath) throws PathNotFoundException {
80     if (jcrPath.length() < containerPath.length())
81       throw new PathNotFoundException("ParseRelPath failed. Path <" + jcrPath + ">. is too short.");
82     else if (jcrPath.length() == containerPath.length())
83       return "";
84     if (jcrPath.startsWith(containerPath))
85       return jcrPath.substring(containerPath.length(), jcrPath.length());
86     throw new PathNotFoundException("ParseRelPath failed. Unexpected Path <" + jcrPath + ">.");
87   }
88
89   protected String JavaDoc getJcrPath(String JavaDoc relPath) {
90     return containerPath + relPath;
91   }
92
93   protected String JavaDoc getParentRelPath(String JavaDoc relPath) {
94
95     try {
96
97       if (PathUtil.getDepth(relPath) <= 1)
98         return "";
99       else
100         return PathUtil.getAncestorPath(relPath, 1);
101     } catch (Exception JavaDoc e) {
102       return null;
103     }
104
105   }
106
107   protected PropertyImpl getRootProperty(String JavaDoc relPath) {
108 //System.out.println("Prop relPath -- "+relPath+" '"+getParentRelPath(relPath)+"'");
109
try {
110
111 // String name = PathUtil.getName(jcrPath);
112

113       if (relPath.equals("/jcr:primaryType"))
114         return new PropertyImpl(getJcrPath("/jcr:primaryType"), new StringValue(nodeType),
115             PropertyType.STRING);
116       if (relPath.equals("/jcr:created") || relPath.equals("/jcr:lastModified")) {
117         long cr = storage.lastModified();
118         Calendar JavaDoc cal = Calendar.getInstance();
119         cal.setTime(new Date JavaDoc(cr));
120         return new PropertyImpl(getJcrPath(relPath), new DateValue(cal),
121             PropertyType.DATE);
122       }
123
124     } catch (Exception JavaDoc e) {
125       return null;
126     }
127
128     return null;
129   }
130
131   protected List JavaDoc getRootProps() {
132     ArrayList JavaDoc properties = new ArrayList JavaDoc();
133     properties.add(getRootProperty("/jcr:primaryType"));
134     properties.add(getRootProperty("/jcr:lastModified"));
135     properties.add(getRootProperty("/jcr:created"));
136     return properties;
137   }
138
139 }
140
Popular Tags