01: /*
02:
03: This software is OSI Certified Open Source Software.
04: OSI Certified is a certification mark of the Open Source Initiative.
05:
06: The license (Mozilla version 1.0) can be read at the MMBase site.
07: See http://www.MMBase.org/license
08:
09: */
10:
11: package org.mmbase.bridge.implementation;
12:
13: import org.mmbase.bridge.*;
14: import org.mmbase.bridge.util.MapNode;
15: import org.mmbase.module.core.VirtualNode;
16: import org.mmbase.module.core.MMObjectNode;
17: import java.util.*;
18:
19: /**
20: * This implementation of the Field Value interface is used by getFunctionValue of Node. This
21: * represents the result of a `function' on a node and it (therefore) is a unmodifiable.
22: *
23: * @author Michiel Meeuwissen
24: * @version $Id: BasicFunctionValue.java,v 1.22 2008/02/03 17:33:57 nklasens Exp $
25: * @since MMBase-1.6
26: */
27: public class BasicFunctionValue extends
28: org.mmbase.bridge.util.AbstractFieldValue {
29:
30: private final Object value;
31:
32: /**
33: * Constructor for a function value returned by a Node.
34: * @since MMBase-1.8
35: * @param node the node that called the function
36: * @param value the function value
37: */
38: BasicFunctionValue(Node node, Object value) {
39: super (node, null);
40: this .value = convert(value, null);
41: }
42:
43: /**
44: * Constructor for a function value returned by a Module or NodeManager.
45: * @since MMBase-1.8
46: * @param cloud the cloud under which the call was run, used to instantiate NodeList values
47: * @param value the function value
48: */
49: BasicFunctionValue(Cloud cloud, Object value) {
50: super (null, cloud);
51: Object v = value;
52: if (v instanceof List) { // might be a collection of MMObjectNodes
53: List<Node> list = (List<Node>) v;
54: if (list.size() > 0) {
55: Object first = list.get(0);
56: if (first instanceof MMObjectNode
57: || first instanceof Node) { // if List of MMObjectNodes, make NodeList
58: if (cloud == null) {
59: if (first instanceof Node) {
60: cloud = ((Node) first).getCloud();
61: } else {
62: cloud = ContextProvider
63: .getDefaultCloudContext().getCloud(
64: "mmbase", "class", null);
65: }
66: // throw new IllegalStateException("Cloud is unknown, cannot convert MMObjectNode to Node");
67: }
68: NodeList l = cloud.createNodeList();
69: v = l;
70: l.addAll(list);
71: }
72: }
73: }
74: this .value = convert(v, cloud);
75: }
76:
77: protected static Object convert(Object o, Cloud cloud) {
78: if (o instanceof VirtualNode) {
79: VirtualNode vn = (VirtualNode) o;
80: return new MapNode(vn.getValues(), cloud);
81: } else if (o instanceof MMObjectNode) {
82: MMObjectNode mn = (MMObjectNode) o;
83: return cloud.getNode(mn.getNumber());
84: }
85: return o;
86: }
87:
88: @Override
89: public Object get() {
90: return value;
91: }
92:
93: }
|