KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > bridge > implementation > VirtualNode


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10
11 package org.mmbase.bridge.implementation;
12
13 import java.io.InputStream JavaDoc;
14 import java.util.*;
15
16 import org.mmbase.bridge.*;
17 import org.mmbase.bridge.util.*;
18 import org.mmbase.module.core.MMObjectBuilder;
19 import org.mmbase.module.core.VirtualBuilder;
20 import org.mmbase.module.core.MMObjectNode;
21 import org.mmbase.module.core.MMBase;
22 import org.mmbase.core.util.Fields;
23 import org.mmbase.core.CoreField;
24 import org.mmbase.util.functions.*;
25 import org.mmbase.util.logging.Logger;
26 import org.mmbase.util.logging.Logging;
27 import org.w3c.dom.Document JavaDoc;
28 import org.w3c.dom.Element JavaDoc;
29
30 /**
31  * Implementation of Node. Simply wraps virtual node of core into an bridge Node. This class can be
32  * used even if you don't know the precise implementation of the Cloud object (in contradiction to {@link BasicNode}, and therefore has a public constructor
33  * {@link #VirtualNode(org.mmbase.module.core.VirtualNode, Cloud)}.
34  *
35  * @author Michiel Meeuwissen
36  * @version $Id: VirtualNode.java,v 1.19.2.1 2006/11/28 13:48:45 johannes Exp $
37  * @see org.mmbase.bridge.Node
38  * @see org.mmbase.module.core.VirtualNode
39  * @since MMBase-1.8
40  */

41 public class VirtualNode extends AbstractNode implements Node {
42
43     private static final Logger log = Logging.getLoggerInstance(VirtualNode.class);
44
45     final protected org.mmbase.module.core.VirtualNode noderef;
46
47     /**
48      * This is normally, but not always, a VirtualBuilder. It is not for some builders which have
49      * besides real nodes also virtual nodes, like typedef (cluster nodes) and typerel (allowed relations because of inheritance).
50      */

51     final protected NodeManager nodeManager;
52     final protected Cloud cloud;
53
54
55     protected VirtualNode(Cloud cloud, org.mmbase.module.core.VirtualNode node, NodeManager nm) {
56         this.cloud = cloud;
57         this.noderef = node;
58         nodeManager = nm;
59
60     }
61
62     public VirtualNode(org.mmbase.module.core.VirtualNode node, Cloud cloud) {
63         this(cloud, node, new VirtualNodeManager(node, cloud));
64     }
65
66     /**
67      * Makes a Node from a map of values. Sadly, this uses a local MMBase, so you can't use this with
68      * e.g. RMMCI, but I didn't feel like reimplementating Node completely..
69      */

70     public VirtualNode(Map values, Cloud cloud) {
71         this(getVirtualNode(values), cloud);
72     }
73
74     public String JavaDoc toString() {
75         return "BridgeVirtualNode " + noderef;
76     }
77
78     protected static org.mmbase.module.core.VirtualNode getVirtualNode(Map values) {
79         VirtualBuilder builder = new VirtualBuilder(MMBase.getMMBase());
80         org.mmbase.module.core.VirtualNode node = new org.mmbase.module.core.VirtualNode(builder);
81         Iterator i = values.entrySet().iterator();
82         while (i.hasNext()) {
83             Map.Entry entry = (Map.Entry) i.next();
84             String JavaDoc name = entry.getKey().toString();
85             Object JavaDoc value = entry.getValue();
86             node.storeValue(name, value);
87         }
88         return node;
89     }
90
91     /**
92      * Returns the MMObjectNode on which the VirtualNode was based
93      */

94     public org.mmbase.module.core.VirtualNode getNodeRef() {
95         return noderef;
96     }
97
98
99     public boolean isRelation() {
100         return false;
101     }
102
103     public Relation toRelation() {
104         return (Relation)this;
105     }
106
107     public boolean isNodeManager() {
108         return false;
109     }
110
111     public NodeManager toNodeManager() {
112         return (NodeManager)this;
113     }
114
115     public boolean isRelationManager() {
116         return false;
117     }
118
119     public RelationManager toRelationManager() {
120         return (RelationManager)this;
121     }
122
123     /**
124      * Obtains a reference to the underlying MMObjectNode.
125      * If the underlying node was deleted, this returns a virtual node with
126      * no info except the (original) node number.
127      * @return the underlying MMObjectNode
128      * @throws NotFoundException if no node was specified. This generally means the
129      */

130     protected final MMObjectNode getNode() {
131         return noderef;
132     }
133
134
135     public Cloud getCloud() {
136         return cloud;
137     }
138
139     public NodeManager getNodeManager() {
140         return nodeManager;
141     }
142
143     public int getNumber() {
144         return noderef.getNumber();
145     }
146
147
148     protected void edit(int action) {
149         throw new UnsupportedOperationException JavaDoc("Cannot edit virtual node");
150     }
151
152     public boolean isNull(String JavaDoc fieldName) {
153         return noderef.isNull(fieldName);
154     }
155
156     public void setSize(String JavaDoc fieldName, long size) {
157         noderef.setSize(fieldName, size);
158     }
159     public long getSize(String JavaDoc fieldName) {
160         return noderef.getSize(fieldName);
161     }
162
163     protected void setValueWithoutChecks(String JavaDoc fieldName, Object JavaDoc value) {
164         // cannot edit virtual node.
165
// should not come here..
166
getNode().setValue(fieldName, value);
167     }
168
169     public Object JavaDoc getValueWithoutProcess(String JavaDoc fieldName) {
170         Object JavaDoc result = getNode().getValue(fieldName);
171         return result;
172     }
173
174     public boolean getBooleanValue(String JavaDoc fieldName) {
175         Boolean JavaDoc result = Boolean.valueOf(noderef.getBooleanValue(fieldName));
176         return result.booleanValue();
177     }
178
179     public Date getDateValue(String JavaDoc fieldName) {
180         Date result = noderef.getDateValue(fieldName);
181         return result;
182     }
183
184     public List getListValue(String JavaDoc fieldName) {
185         List result = noderef.getListValue(fieldName);
186         return result;
187     }
188
189
190     /**
191      * Returns the Node value of a certain field, but in the case of a VirtualNode this can also occasionally be <code>null</code>
192      * because the node can have been deleted.
193      */

194     public Node getNodeValue(String JavaDoc fieldName) {
195         if (fieldName == null || fieldName.equals("number")) {
196             return this;
197         }
198         Node result = null;
199         MMObjectNode mmobjectNode = getNode().getNodeValue(fieldName);
200         if (mmobjectNode != null) {
201             MMObjectBuilder builder = mmobjectNode.getBuilder();
202             if (builder instanceof VirtualBuilder) {
203                 return new VirtualNode((org.mmbase.module.core.VirtualNode)mmobjectNode, cloud);
204             }
205                 
206             try {
207                 result = cloud.getNode(mmobjectNode.getNumber());
208             } catch (NotFoundException nfe) {
209                 // don't know when this happens, perhaps the node was deleted in the mean time?
210
log.debug(nfe.getMessage());
211                 return null;
212             }
213         }
214         return result;
215     }
216
217     public int getIntValue(String JavaDoc fieldName) {
218         Integer JavaDoc result = new Integer JavaDoc(getNode().getIntValue(fieldName));
219         return result.intValue();
220
221     }
222
223     public float getFloatValue(String JavaDoc fieldName) {
224         Float JavaDoc result = new Float JavaDoc(getNode().getFloatValue(fieldName));
225         return result.floatValue();
226     }
227
228     public long getLongValue(String JavaDoc fieldName) {
229         Long JavaDoc result = new Long JavaDoc(getNode().getLongValue(fieldName));
230         return result.longValue();
231     }
232
233     public double getDoubleValue(String JavaDoc fieldName) {
234         Double JavaDoc result = new Double JavaDoc(getNode().getDoubleValue(fieldName));
235         return result.doubleValue();
236     }
237
238     public byte[] getByteValue(String JavaDoc fieldName) {
239         byte[] result = getNode().getByteValue(fieldName);
240         return result;
241     }
242     public java.io.InputStream JavaDoc getInputStreamValue(String JavaDoc fieldName) {
243         java.io.InputStream JavaDoc result = getNode().getInputStreamValue(fieldName);
244         return result;
245     }
246
247     public String JavaDoc getStringValue(String JavaDoc fieldName) {
248         String JavaDoc result = getNode().getStringValue(fieldName);
249         return result;
250     }
251
252     public Document JavaDoc getXMLValue(String JavaDoc fieldName) {
253         Document JavaDoc result = getNode().getXMLValue(fieldName);
254         return result;
255     }
256
257
258     public Collection getFunctions() {
259         return getNode().getFunctions();
260     }
261
262     protected Function getNodeFunction(String JavaDoc functionName) {
263         return getNode().getFunction(functionName);
264     }
265
266     public Parameters createParameters(String JavaDoc functionName) {
267         return getNode().getFunction(functionName).createParameters();
268     }
269
270     protected FieldValue createFunctionValue(Object JavaDoc result) {
271         return new BasicFunctionValue(getCloud(), result);
272     }
273
274 }
275
Popular Tags