KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > bridge > util > MapNode


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.util;
12
13 import java.util.*;
14 import java.io.InputStream JavaDoc;
15 import org.mmbase.bridge.*;
16 import org.mmbase.bridge.implementation.BasicFunctionValue;
17 import org.mmbase.bridge.implementation.BasicField;
18 import org.mmbase.core.CoreField;
19 import org.mmbase.core.util.Fields;
20 import org.mmbase.util.Casting;
21 import org.mmbase.util.SizeOf;
22 import org.mmbase.util.logging.*;
23 import org.mmbase.util.functions.*;
24 import org.w3c.dom.Document JavaDoc;
25 import org.w3c.dom.Element JavaDoc;
26
27 /**
28  * A bridge Node based on a Map. It can come in handy sometimes to be able to present any Map as an
29  * MMBase Node. E.g. because then it can be accessed in MMBase taglib using mm:field tags.
30
31  * @author Michiel Meeuwissen
32  * @version $Id: MapNode.java,v 1.6 2006/06/20 20:05:32 michiel Exp $
33  * @since MMBase-1.8
34  */

35
36 public class MapNode extends AbstractNode implements Node {
37
38     private static final Logger log = Logging.getLoggerInstance(MapNode.class);
39
40     /**
41      * This is normally, but not always, a VirtualBuilder. It is not for some builders which have
42      * besides real nodes also virtual nodes, like typedef (cluster nodes) and typerel (allowed relations because of inheritance).
43      */

44     final protected NodeManager nodeManager;
45     final protected Map values;
46     final protected Map sizes = new HashMap();
47     final protected Map originalValues = new HashMap();
48
49     /**
50      * This constructor explicitely specifies the node manager of the Node. This is used for {#getNodeManager} and {#getCloud}.
51      */

52     public MapNode(Map v, NodeManager nm) {
53         values = v;
54         originalValues.putAll(values);
55         nodeManager = nm;
56     }
57     /**
58      * A node with a 'virtual' nodemanager will be constructed. This virtual node manager will have
59      * fields which are guessed based on the keys and values of the given map.
60      */

61     public MapNode(Map v, Cloud cloud) {
62         this(v, createVirtualNodeManager(cloud, v));
63
64     }
65     /**
66      * This allows you to create a Node object even without having a Cloud object. 'Class' security
67      * is used to acquire a Cloud, because every bridge node must be associated with some Cloud
68      * object.
69      */

70     public MapNode(Map v) {
71         this(v, ContextProvider.getDefaultCloudContext().getCloud("mmbase", "class", null));
72     }
73
74     protected static NodeManager createVirtualNodeManager(Cloud cloud, final Map map) {
75         return new AbstractNodeManager(cloud) {
76             Map fieldTypes = new HashMap();
77             {
78                 Iterator i = map.entrySet().iterator();
79                 while (i.hasNext()) {
80                     Map.Entry entry = (Map.Entry) i.next();
81                     String JavaDoc fieldName = (String JavaDoc) entry.getKey();
82                     Object JavaDoc value = entry.getValue();
83                     CoreField fd = Fields.createField(fieldName, Fields.classToType(value == null ? Object JavaDoc.class : value.getClass()),
84                                                       Field.TYPE_UNKNOWN, Field.STATE_VIRTUAL, null);
85                     Field ft = new BasicField(fd, this);
86                     fieldTypes.put(fieldName, ft);
87                 }
88             }
89             protected Map getFieldTypes() {
90                 return fieldTypes;
91             }
92
93         };
94     }
95
96     public Cloud getCloud() {
97         return nodeManager.getCloud();
98     }
99
100     public NodeManager getNodeManager() {
101         return nodeManager;
102     }
103
104     public int getNumber() {
105         return Casting.toInt(values.get("number"));
106     }
107
108     public boolean isNew() {
109         return false;
110     }
111
112     public boolean isChanged(String JavaDoc fieldName) {
113         Object JavaDoc originalValue = originalValues.get(fieldName);
114         Object JavaDoc value = values.get(fieldName);
115         return originalValue == null ? value == null : originalValue.equals(value);
116     }
117     public boolean isChanged() {
118         return ! values.equals(originalValues);
119     }
120
121
122     protected void edit(int i) {
123         // always ok.
124
}
125     public Object JavaDoc getValueWithoutProcess(String JavaDoc fieldName) {
126         return values.get(fieldName);
127     }
128     public void setValueWithoutProcess(String JavaDoc fieldName, Object JavaDoc value) {
129         values.put(fieldName, value);
130     }
131     public void setValueWithoutChecks(String JavaDoc fieldName, Object JavaDoc value) {
132         values.put(fieldName, value);
133     }
134
135     public boolean isNull(String JavaDoc fieldName) {
136         return values.get(fieldName) == null;
137     }
138     protected void setSize(String JavaDoc fieldName, long size) {
139         sizes.put(fieldName, new Long JavaDoc(size));
140     }
141
142     public long getSize(String JavaDoc fieldName) {
143         Long JavaDoc size = (Long JavaDoc) sizes.get(fieldName);
144         if (size != null) {
145             return size.longValue();
146         } else {
147             int s = SizeOf.getByteSize(values.get(fieldName));
148             sizes.put(fieldName, new Long JavaDoc(s));
149             return s;
150         }
151     }
152
153     public void commit() {
154         throw new UnsupportedOperationException JavaDoc("Cannot commit map node");
155     }
156
157     public void cancel() {
158     }
159
160
161     public void delete(boolean deleteRelations) {
162         throw new UnsupportedOperationException JavaDoc("Cannot delete map node");
163     }
164
165     public String JavaDoc toString() {
166         return "Map Node" + values;
167     }
168
169     public void deleteRelations(String JavaDoc type) throws NotFoundException {
170     }
171
172     public RelationList getRelations(String JavaDoc role, NodeManager nodeManager, String JavaDoc searchDir) throws NotFoundException {
173         return BridgeCollections.EMPTY_RELATIONLIST;
174     }
175     public RelationList getRelations(String JavaDoc role, String JavaDoc nodeManager) throws NotFoundException {
176         return BridgeCollections.EMPTY_RELATIONLIST;
177     }
178
179
180     public boolean hasRelations() {
181         return false;
182     }
183
184     public int countRelatedNodes(NodeManager otherNodeManager, String JavaDoc role, String JavaDoc direction) {
185         return 0;
186
187     }
188
189     public NodeList getRelatedNodes(NodeManager nodeManager, String JavaDoc role, String JavaDoc searchDir) {
190         return BridgeCollections.EMPTY_NODELIST;
191     }
192
193     public int countRelatedNodes(String JavaDoc type) {
194         return 0;
195     }
196
197     public StringList getAliases() {
198         return BridgeCollections.EMPTY_STRINGLIST;
199     }
200
201     public void createAlias(String JavaDoc aliasName) {
202         throw new UnsupportedOperationException JavaDoc("Map nodes have no aliases");
203     }
204
205     public void deleteAlias(String JavaDoc aliasName) {
206         throw new UnsupportedOperationException JavaDoc("Map nodes have no aliases");
207     }
208
209     public Relation createRelation(Node destinationNode, RelationManager relationManager) {
210         throw new UnsupportedOperationException JavaDoc("Map nodes have no relations");
211     }
212
213
214     public void setContext(String JavaDoc context) {
215         throw new UnsupportedOperationException JavaDoc("Map nodes have no security context");
216     }
217
218     // javadoc inherited (from Node)
219
public String JavaDoc getContext() {
220         throw new UnsupportedOperationException JavaDoc("Virtual nodes have no security context");
221     }
222
223
224     // javadoc inherited (from Node)
225
public StringList getPossibleContexts() {
226         return BridgeCollections.EMPTY_STRINGLIST;
227     }
228
229     public boolean mayWrite() {
230         return true;
231     }
232
233     public boolean mayDelete() {
234         return false;
235     }
236
237     public boolean mayChangeContext() {
238         return false;
239     }
240
241     public Collection getFunctions() {
242         return nodeManager.getFunctions();
243     }
244
245
246     protected Function getNodeFunction(String JavaDoc functionName) {
247         return nodeManager.getFunction(functionName);
248     }
249 }
250
251
Popular Tags