KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > beancoder > MgnlNode


1 package info.magnolia.beancoder;
2
3 import info.magnolia.cms.core.Content;
4 import info.magnolia.cms.core.ItemType;
5 import info.magnolia.cms.core.NodeData;
6 import info.magnolia.cms.util.ContentUtil;
7 import info.magnolia.cms.util.DateUtil;
8 import info.magnolia.cms.util.NodeDataUtil;
9 import openwfe.org.jcr.Item;
10 import openwfe.org.jcr.JcrException;
11 import openwfe.org.jcr.Node;
12 import openwfe.org.jcr.Property;
13
14 import javax.jcr.RepositoryException;
15 import java.io.InputStream JavaDoc;
16 import java.util.Date JavaDoc;
17 import java.util.Iterator JavaDoc;
18
19 /**
20  * Magnolia wrapper for a node.
21  */

22 public class MgnlNode implements Node {
23     Content mnode;
24
25     public MgnlNode(Content mnode) {
26         this.mnode = mnode;
27     }
28
29     public Iterator JavaDoc getProperties() throws JcrException {
30         return new MgnlPropertyIterator(mnode);
31     }
32
33     public Iterator JavaDoc getNodes() throws JcrException {
34         return new MgnlNodeIterator(mnode);
35     }
36
37     public Property setProperty(String JavaDoc string, Object JavaDoc object) throws JcrException {
38         Property property = getProperty(string);
39         try {
40             final NodeData nodeData = ((NodeData) property.getWrappedInstance());
41             if (object instanceof String JavaDoc)
42                 nodeData.setValue((String JavaDoc) object);
43             else if (object instanceof Date JavaDoc)
44                 nodeData.setValue(DateUtil.getUTCCalendarFromLocalDate((Date JavaDoc) object));
45             else if (object instanceof InputStream JavaDoc)
46                 nodeData.setValue((InputStream JavaDoc)object);
47             else if (object instanceof Double JavaDoc)
48                 nodeData.setValue(((Double JavaDoc)object).doubleValue());
49             else if (object instanceof Long JavaDoc)
50                 nodeData.setValue(((Long JavaDoc)object).longValue());
51             else if (object instanceof Boolean JavaDoc)
52                 nodeData.setValue(((Boolean JavaDoc)object).booleanValue());
53             else
54                 throw new JcrException("Does not support object of kind:"+object.getClass().getName());
55         } catch (RepositoryException e) {
56             throw new JcrException(e.getMessage(),e);
57         }
58         return property;
59     }
60
61     public String JavaDoc getPath() throws JcrException {
62         return mnode.getHandle();
63     }
64
65     public Object JavaDoc getWrappedInstance() throws JcrException {
66         return mnode;
67     }
68
69     public boolean hasProperty(String JavaDoc propertyName) throws JcrException {
70         try {
71             return mnode.hasNodeData(propertyName);
72         } catch (RepositoryException e) {
73             throw new JcrException(e.getMessage());
74         }
75     }
76
77     public Property getProperty(String JavaDoc propertyName) throws JcrException {
78         try {
79             return new MgnlProperty(this, NodeDataUtil.getOrCreate(mnode,propertyName));
80         } catch (RepositoryException e) {
81             throw new JcrException(e.getMessage());
82         }
83     }
84
85     public Property setProperty(String JavaDoc propertyName, String JavaDoc value) throws JcrException {
86         try {
87             NodeData nodeData = NodeDataUtil.getOrCreate(mnode,propertyName);
88             nodeData.setValue(value);
89             return new MgnlProperty(this,nodeData);
90         } catch (RepositoryException e) {
91             throw new JcrException(e.getMessage());
92         }
93     }
94
95     public Property setProperty(String JavaDoc propertyName, long value) throws JcrException {
96         try {
97             NodeData nodeData = NodeDataUtil.getOrCreate(mnode,propertyName);
98             nodeData.setValue(value);
99             return new MgnlProperty(this,nodeData);
100         } catch (RepositoryException e) {
101             throw new JcrException(e.getMessage());
102         }
103     }
104
105     public boolean hasNode(String JavaDoc relPath) throws JcrException {
106         try {
107             return mnode.hasContent(relPath);
108         } catch (RepositoryException e) {
109             throw new JcrException(e.getMessage());
110         }
111     }
112
113     public Node getNode(String JavaDoc relPath) throws JcrException {
114         try {
115             Content c = ContentUtil.getOrCreateContent(mnode,relPath, ItemType.CONTENTNODE);
116             return new MgnlNode(c);
117         } catch (RepositoryException e) {
118             throw new JcrException(e.getMessage());
119         }
120     }
121
122     public Node addNode(String JavaDoc newNodeName) throws JcrException {
123         try {
124             return new MgnlNode(mnode.createContent(newNodeName));
125         } catch (RepositoryException e) {
126             throw new JcrException(e.getMessage());
127         }
128     }
129
130     public String JavaDoc getName() throws JcrException {
131         return mnode.getName();
132     }
133
134     public Item getParent() throws JcrException {
135         try {
136             return new MgnlNode(mnode.getParent());
137         } catch (RepositoryException e) {
138             throw new JcrException(e.getMessage());
139         }
140     }
141
142     public boolean isNode() throws JcrException {
143         return true;
144     }
145
146     public void save() throws JcrException {
147         try {
148             mnode.save();
149         } catch (RepositoryException e) {
150             throw new JcrException(e.getMessage());
151         }
152     }
153
154     class MgnlNodeIterator implements Iterator JavaDoc {
155         Content node;
156         private Iterator JavaDoc internalIterator;
157
158         public MgnlNodeIterator(Content node) {
159             this.node = node;
160             this.internalIterator = node.getChildren().iterator();
161         }
162
163         public void remove() {
164             internalIterator.remove();
165         }
166
167         public boolean hasNext() {
168             return internalIterator.hasNext();
169         }
170
171         public Object JavaDoc next() {
172             return new MgnlNode((Content)internalIterator.next());
173         }
174     }
175
176     class MgnlPropertyIterator implements Iterator JavaDoc {
177         private Content mnode;
178         private Iterator JavaDoc internalIterator;
179
180         public MgnlPropertyIterator(Content mnode) {
181             this.mnode = mnode;
182             this.internalIterator = mnode.getNodeDataCollection().iterator();
183         }
184
185         public void remove() {
186             internalIterator.remove();
187         }
188
189         public boolean hasNext() {
190             return internalIterator.hasNext();
191         }
192
193         public Object JavaDoc next() {
194             return new MgnlProperty(new MgnlNode(mnode),(NodeData)internalIterator.next());
195         }
196     }
197
198     /**
199      * @see openwfe.org.jcr.Node#setProperty(java.lang.String, java.lang.String, int)
200      */

201     public Property setProperty(String JavaDoc propertyName, String JavaDoc value, int noideaofwhatisthis) throws JcrException {
202         // @todo added to make this compile with openwfe-jcr 1.7.2pre8 "second edition".
203
// looks like two different jars have been deployed as openwfe-jcr-1.7.2pre8.jar to the repo, so build fails for
204
// anybody who didn't download the first copy (this method has been added only to the second release)
205
// please avoid this: use snapshots or increment the release number
206
// ... and please, also deploy sources to the repo using "mvn source:jar deploy"
207
return setProperty(propertyName, value);
208     }
209
210     public void remove() throws JcrException {
211         try {
212             this.mnode.delete();
213         } catch (RepositoryException e) {
214             throw new JcrException(e.getMessage(),e);
215         }
216
217     }
218 }
219
Popular Tags