001: /*
002:
003: This software is OSI Certified Open Source Software.
004: OSI Certified is a certification mark of the Open Source Initiative.
005:
006: The license (Mozilla version 1.0) can be read at the MMBase site.
007: See http://www.MMBase.org/license
008:
009: */
010: package org.mmbase.module.core;
011:
012: import java.util.*;
013: import org.mmbase.bridge.Field;
014: import org.mmbase.datatypes.*;
015: import org.mmbase.core.CoreField;
016: import org.mmbase.core.util.Fields;
017: import org.mmbase.util.logging.Logger;
018: import org.mmbase.util.logging.Logging;
019:
020: /**
021: * VirtualBuilder is a builder which creates 'virtual' nodes.
022: * This class is intended to facilitate practical creation of virtual
023: * builders by capturing events that migth otherwise lead to unexpected or
024: * faulty behavior.
025: *
026: * @author Pierre van Rooden
027: * @version $Id: VirtualBuilder.java,v 1.25 2008/02/03 17:33:57 nklasens Exp $
028: */
029: public class VirtualBuilder extends MMObjectBuilder {
030: private static final Logger log = Logging
031: .getLoggerInstance(VirtualBuilder.class);
032:
033: private static int counter = 0;
034:
035: /**
036: * Creates an instance of a Virtual builder.
037: * A builder instantiated with this constrcutor is not registered in MMBase
038: * and should only be used as a temporary parent for virtual nodes which
039: * do not have a long life span.
040: * @param m the MMbase cloud creating the node
041: */
042: public VirtualBuilder(MMBase m) {
043: this .mmb = m;
044: this .tableName = "virtualnodes_" + counter++;
045: this .description = "";
046: virtual = true;
047: }
048:
049: /**
050: * Creates an instance of a Virtual builder and registers it in MMBase.
051: * @param m the MMbase cloud creating the node
052: * @param tableName the name of the builder as known in the MMbase system
053: */
054: protected VirtualBuilder(MMBase m, String tableName) {
055: this .mmb = m;
056: this .tableName = tableName;
057: this .description = "";
058: virtual = true;
059: if (m.addBuilder(tableName, this ) != null) {
060: log.debug("Replaced virtual builder '" + tableName + "'");
061: } else {
062: log.debug("Created virtual builder '" + tableName + "'");
063: }
064: }
065:
066: /**
067: * Initializes this builder.
068: * No specifici cation is performed.
069: * This method overrides the default emthod in MMObhjectBuilder, which
070: * would otherwise attempt to access the database.
071: * @return Always true.
072: * @see #create
073: */
074: public boolean init() {
075: return true;
076: }
077:
078: /**
079: * Creates a new builder table in the current database.
080: * This method does not perform any action in a virtual builder, as there is
081: * no actual table associated with it.
082: */
083: public boolean create() {
084: return true;
085: }
086:
087: /**
088: * Insert a new object (content provided) in the cloud, including an entry for the object alias (if provided).
089: * This method does not perform any action in a virtual builder.
090: * @param owner The administrator creating the node
091: * @param node The object to insert
092: * @return -1 (the insert failed)
093: */
094: public int insert(String owner, MMObjectNode node) {
095: // no insert allowed on this builder, so signal -1
096: return -1;
097: }
098:
099: /**
100: * Get a new node, using this builder as its parent.
101: * The new node is a virtual node.
102: * @param owner The administrator creating the new node.
103: * @return A newly initialized <code>VirtualNode</code>.
104: */
105: public MMObjectNode getNewNode(String owner) {
106: VirtualNode node = new VirtualNode(this );
107: node.setValue("number", -1);
108: node.setValue("owner", owner);
109: node.setValue("otype", oType);
110: setDefaults(node);
111: return node;
112: }
113:
114: /**
115: * {@inheritDoc}
116: * The default behavior of a virtual node is to display the content of
117: * the 'name' field (if present).
118: * XXX: should be changed to something better
119: * @param node The node to display
120: * @return either the name field of the node or "no info"
121: */
122: public String getGUIIndicator(MMObjectNode node) {
123: String s = node.getStringValue("name");
124: if (s != null) {
125: return s;
126: } else {
127: return GUI_INDICATOR;
128: }
129: }
130:
131: /**
132: * Return a field's database state.
133: * The default behavior for a virtual node is to return <code>DBSTATE_VIRTUAL</code>.
134: * @param fieldName the requested field's name
135: * @return <code>DBSTATE_VIRTUAL</code>
136: */
137: public int getDBState(String fieldName) {
138: return Field.STATE_VIRTUAL;
139: }
140:
141: /**
142: * {@inheritDoc}
143: * Since virtual builders are generally not associated with a database,
144: * this method returns null.
145: * @param fieldName name of the field
146: * @param node
147: * @return <code>null</code>
148: */
149: protected String getShortedText(String fieldName, MMObjectNode node) {
150: return null;
151: }
152:
153: /**
154: * {@inheritDoc}
155: * Since virtual builders are generally not associated with a database,
156: * this method returns null.
157: * @param fieldName name of the field
158: * @param node
159: * @return <code>null</code>
160: */
161: protected byte[] getShortedByte(String fieldName, MMObjectNode node) {
162: return null;
163: }
164:
165: /**
166: * Get text from a blob field from a database.
167: * @since MMBase-1.8
168: */
169: public Map<String, CoreField> getFields(MMObjectNode node) {
170: Map<String, CoreField> res = new HashMap<String, CoreField>();
171: // determine fields and field types
172: Map<String, Object> values = node.getValues();
173: synchronized (values) {
174: Iterator<Map.Entry<String, Object>> i = values.entrySet()
175: .iterator();
176: while (i.hasNext()) {
177: Map.Entry<String, Object> entry = i.next();
178: String fieldName = entry.getKey();
179: Object value = entry.getValue();
180: if (value == null)
181: value = new Object();
182: DataType<? extends Object> fieldDataType = DataTypes
183: .createDataType("field", value.getClass());
184: int type = Fields.classToType(value.getClass());
185: CoreField fd = Fields.createField(fieldName, type,
186: Field.TYPE_UNKNOWN, Field.STATE_VIRTUAL,
187: fieldDataType);
188: fd.finish();
189: res.put(fieldName, fd);
190: }
191: }
192: return res;
193: }
194: }
|