KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > module > core > VirtualBuilder


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 package org.mmbase.module.core;
11
12 import java.util.*;
13 import org.mmbase.bridge.Field;
14 import org.mmbase.datatypes.*;
15 import org.mmbase.core.CoreField;
16 import org.mmbase.core.util.Fields;
17 import org.mmbase.util.logging.Logger;
18 import org.mmbase.util.logging.Logging;
19
20 /**
21  * VirtualBuilder is a builder which creates 'virtual' nodes.
22  * This class is intended to facilitate practical creation of virtual
23  * builders by capturing events that migth otherwise lead to unexpected or
24  * faulty behavior.
25  *
26  * @author Pierre van Rooden
27  * @version $Id: VirtualBuilder.java,v 1.22 2006/01/16 15:17:59 michiel Exp $
28  */

29 public class VirtualBuilder extends MMObjectBuilder {
30     private static final Logger log = Logging.getLoggerInstance(VirtualBuilder.class);
31
32     private static int counter = 0;
33     /**
34      * Creates an instance of a Virtual builder.
35      * A builder instantiated with this constrcutor is not registered in MMBase
36      * and should only be used as a temporary parent for virtual nodes which
37      * do not have a long life span.
38      * @param m the MMbase cloud creating the node
39      */

40     public VirtualBuilder(MMBase m) {
41         this.mmb = m;
42         this.tableName = "virtualnodes_" + counter++;
43         this.description = "";
44         virtual = true;
45     }
46
47     /**
48      * Creates an instance of a Virtual builder and registers it in MMBase.
49      * @param m the MMbase cloud creating the node
50      * @param tableName the name of the builder as known in the MMbase system
51      */

52     protected VirtualBuilder(MMBase m, String JavaDoc tableName) {
53         this.mmb = m;
54         this.tableName = tableName;
55         this.description = "";
56         virtual = true;
57         if (m.addBuilder(tableName, this) != null) {
58             log.debug("Replaced virtual builder '" + tableName + "'");
59         } else {
60             log.debug("Created virtual builder '" + tableName + "'");
61         }
62     }
63
64     /**
65      * Initializes this builder.
66      * No specifici cation is performed.
67      * This method overrides the default emthod in MMObhjectBuilder, which
68      * would otherwise attempt to access the database.
69      * @return Always true.
70      * @see #create
71      */

72     public boolean init() {
73         return true;
74     }
75
76     /**
77      * Creates a new builder table in the current database.
78      * This method does not perform any action in a virtual builder, as there is
79      * no actual table associated with it.
80      */

81     public boolean create() {
82         return true;
83     }
84
85     /**
86      * Insert a new object (content provided) in the cloud, including an entry for the object alias (if provided).
87      * This method does not perform any action in a virtual builder.
88      * @param owner The administrator creating the node
89      * @param node The object to insert
90      * @return -1 (the insert failed)
91      */

92     public int insert(String JavaDoc owner,MMObjectNode node) {
93         // no insert allowed on this builder, so signal -1
94
return -1;
95     }
96
97     /**
98      * Get a new node, using this builder as its parent.
99      * The new node is a virtual node.
100      * @param owner The administrator creating the new node.
101      * @return A newly initialized <code>VirtualNode</code>.
102      */

103     public MMObjectNode getNewNode(String JavaDoc owner) {
104         VirtualNode node = new VirtualNode(this);
105         node.setValue("number",-1);
106         node.setValue("owner",owner);
107         node.setValue("otype",oType);
108         setDefaults(node);
109         return node;
110     }
111
112    /**
113      * {@inheritDoc}
114      * The default behavior of a virtual node is to display the content of
115      * the 'name' field (if present).
116      * XXX: should be changed to something better
117      * @param node The node to display
118      * @return either the name field of the node or "no info"
119      */

120      public String JavaDoc getGUIIndicator(MMObjectNode node) {
121         String JavaDoc s= node.getStringValue("name");
122         if (s != null) {
123             return s;
124         } else {
125             return GUI_INDICATOR;
126         }
127     }
128
129     /**
130      * Return a field's database state.
131      * The default behavior for a virtual node is to return <code>DBSTATE_VIRTUAL</code>.
132      * @param fieldName the requested field's name
133      * @return <code>DBSTATE_VIRTUAL</code>
134      */

135     public int getDBState(String JavaDoc fieldName) {
136         return Field.STATE_VIRTUAL;
137     }
138
139     /**
140      * {@inheritDoc}
141      * Since virtual builders are generally not associated with a database,
142      * this method returns null.
143      * @param fieldName name of the field
144      * @param node
145      * @return <code>null</code>
146      */

147     protected String JavaDoc getShortedText(String JavaDoc fieldName, MMObjectNode node) {
148         return null;
149     }
150
151
152     /**
153      * {@inheritDoc}
154      * Since virtual builders are generally not associated with a database,
155      * this method returns null.
156      * @param fieldName name of the field
157      * @param node
158      * @return <code>null</code>
159      */

160     protected byte[] getShortedByte(String JavaDoc fieldName, MMObjectNode node) {
161         return null;
162     }
163
164
165     /**
166      * Get text from a blob field from a database.
167      * @since MMBase-1.8
168      */

169     public Map getFields(MMObjectNode node) {
170         Map res = new HashMap();
171         // determine fields and field types
172
Map values = node.getValues();
173         synchronized(values) {
174             Iterator i = values.entrySet().iterator();
175             while (i.hasNext()) {
176                 Map.Entry entry = (Map.Entry) i.next();
177                 String JavaDoc fieldName = (String JavaDoc) entry.getKey();
178                 Object JavaDoc value = entry.getValue();
179                 if (value == null) value = new Object JavaDoc();
180                 DataType fieldDataType = DataTypes.createDataType("field", value.getClass());
181                 int type = Fields.classToType(value.getClass());
182                 CoreField fd = Fields.createField(fieldName, type, Field.TYPE_UNKNOWN, Field.STATE_VIRTUAL, fieldDataType);
183                 fd.finish();
184                 res.put(fieldName, fd);
185             }
186         }
187         return res;
188     }
189 }
190
Popular Tags