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 org.mmbase.bridge.Field;
013: import org.mmbase.module.corebuilders.RelDef;
014:
015: import org.mmbase.util.logging.Logger;
016: import org.mmbase.util.logging.Logging;
017: import org.mmbase.util.Casting;
018:
019: /**
020: * @javadoc
021: *
022: * @author Rico Jansen
023: * @version $Id: TemporaryNodeManager.java,v 1.52 2007/05/14 14:46:40 michiel Exp $
024: */
025: public class TemporaryNodeManager {
026:
027: private static final Logger log = Logging
028: .getLoggerInstance(TemporaryNodeManager.class);
029:
030: /**
031: * Return value for setObjectField
032: */
033: public static final String UNKNOWN = "unknown";
034: /**
035: * @since MMBase-1.8
036: */
037: public static final String INVALID_VALUE = "invalid value";
038:
039: private final MMBase mmbase;
040:
041: /**
042: * @javadoc
043: */
044: TemporaryNodeManager(MMBase mmbase) {
045: this .mmbase = mmbase;
046: }
047:
048: /**
049: * @javadoc
050: */
051: public String createTmpNode(String type, String owner, String key) {
052: if (log.isDebugEnabled()) {
053: log.debug("createTmpNode : type=" + type + " owner="
054: + owner + " key=" + key);
055: }
056: // WTF!?
057: // if (owner.length() > 12) owner = owner.substring(0, 12);
058: MMObjectBuilder builder = mmbase.getBuilder(type);
059: MMObjectNode node;
060: if (builder != null) {
061: node = builder.getNewTmpNode(owner, getTmpKey(owner, key));
062: if (log.isDebugEnabled()) {
063: log.debug("New tmpnode " + node);
064: }
065: } else {
066: log.error("Can't find builder " + type);
067: }
068: return key;
069: }
070:
071: /**
072: * @javadoc
073: */
074: public String createTmpRelationNode(String role, String owner,
075: String key, String source, String destination)
076: throws Exception {
077: // decode type to a builder using reldef
078: RelDef reldef = mmbase.getRelDef();
079: int rnumber = reldef.getNumberByName(role, true);
080: if (rnumber == -1) {
081: throw new Exception("role " + role
082: + " is not a proper relation");
083: }
084: MMObjectBuilder builder = reldef.getBuilder(reldef
085: .getNode(rnumber));
086: String bulname = builder.getTableName();
087:
088: // Create node
089: createTmpNode(bulname, owner, key);
090: setObjectField(owner, key, "_snumber", getTmpKey(owner, source));
091: setObjectField(owner, key, "_dnumber", getTmpKey(owner,
092: destination));
093: setObjectField(owner, key, "rnumber", "" + rnumber);
094: return key;
095: }
096:
097: /**
098: * @javadoc
099: */
100: public String createTmpAlias(String name, String owner, String key,
101: String destination) {
102: MMObjectBuilder builder = mmbase.getOAlias();
103: String bulname = builder.getTableName();
104:
105: // Create alias node
106: createTmpNode(bulname, owner, key);
107: setObjectField(owner, key, "_destination", getTmpKey(owner,
108: destination));
109: setObjectField(owner, key, "name", name);
110: return key;
111: }
112:
113: /**
114: * @javadoc
115: */
116: public String deleteTmpNode(String owner, String key) {
117: MMObjectBuilder b = mmbase.getBuilder("object");
118: b.removeTmpNode(getTmpKey(owner, key));
119: if (log.isDebugEnabled()) {
120: log.debug("delete node " + getTmpKey(owner, key));
121: }
122: return key;
123: }
124:
125: /**
126: * @javadoc
127: */
128: public MMObjectNode getNode(String owner, String key) {
129: MMObjectBuilder bul = mmbase.getBuilder("object");
130: String tmpKey = getTmpKey(owner, key);
131: MMObjectNode node = bul.getTmpNode(tmpKey);
132: // fallback to normal nodes
133: if (node == null) {
134: log.debug("getNode tmp not node found " + key);
135: node = bul.getNode(key);
136: if (node == null)
137: throw new RuntimeException("Node not found !! (key = '"
138: + key + "' nor tmpKey = " + tmpKey + ")");
139: }
140: return node;
141: }
142:
143: /**
144: * @javadoc
145: */
146: public String getObject(String owner, String key, String dbkey) {
147: MMObjectNode node = MMObjectBuilder.getTmpNode(getTmpKey(owner,
148: key));
149: if (node == null) {
150: log.debug("getObject not tmp node found " + key);
151: MMObjectBuilder bul = mmbase.getBuilder("object");
152: node = bul.getNode(dbkey, false);
153: if (node == null) {
154: log.warn("Node not found in database " + dbkey);
155: } else {
156: MMObjectBuilder.putTmpNode(getTmpKey(owner, key), node);
157: }
158: }
159: if (node != null) {
160: return key;
161: } else {
162: return null;
163: }
164: }
165:
166: /**
167: * @javadoc
168: * @return An empty string if succesfull, the string {@link #UNKNOWN} if the field was not found in the node.
169: * The string {@link #INVALID_VALUE} if the value was not valid for the field's type.
170: */
171: public String setObjectField(String owner, String key,
172: String field, Object value) {
173: MMObjectNode node = getNode(owner, key);
174: if (node != null) {
175: int type = node.getDBType(field);
176: if (type >= 0) {
177: if (value instanceof String) {
178: String stringValue = (String) value;
179: switch (type) {
180: case Field.TYPE_XML:
181: case Field.TYPE_STRING:
182: node.setValue(field, stringValue);
183: break;
184: case Field.TYPE_NODE:
185: case Field.TYPE_INTEGER:
186: try {
187: int i = -1;
188: if (!stringValue.equals(""))
189: i = Integer.parseInt(stringValue);
190: node.setValue(field, i);
191: } catch (NumberFormatException x) {
192: log.debug("Value for field " + field
193: + " is not a number '"
194: + stringValue + "'");
195: return INVALID_VALUE;
196: }
197: break;
198: case Field.TYPE_BINARY:
199: log
200: .error("We don't support casts from String to Binary");
201: return INVALID_VALUE; // so, a String value is invalid for binaries.
202: case Field.TYPE_FLOAT:
203: try {
204: float f = -1;
205: if (!stringValue.equals(""))
206: f = Float.parseFloat(stringValue);
207: node.setValue(field, f);
208: } catch (NumberFormatException x) {
209: log
210: .debug("Value for field " + field
211: + " is not a number "
212: + stringValue);
213: return INVALID_VALUE;
214: }
215: break;
216: case Field.TYPE_DOUBLE:
217: try {
218: double d = -1;
219: if (!stringValue.equals(""))
220: d = Double.parseDouble(stringValue);
221: node.setValue(field, d);
222: } catch (NumberFormatException x) {
223: log
224: .debug("Value for field " + field
225: + " is not a number "
226: + stringValue);
227: return INVALID_VALUE;
228: }
229: break;
230: case Field.TYPE_LONG:
231: try {
232: long l = -1;
233: if (!stringValue.equals(""))
234: l = Long.parseLong(stringValue);
235: node.setValue(field, l);
236: } catch (NumberFormatException x) {
237: log
238: .debug("Value for field " + field
239: + " is not a number "
240: + stringValue);
241: return INVALID_VALUE;
242: }
243: break;
244: case Field.TYPE_DATETIME:
245: try {
246: node.setValue(field, Casting.toDate(value));
247: } catch (Exception e) {
248: return INVALID_VALUE;
249: }
250: break;
251: case Field.TYPE_BOOLEAN:
252: if (org.mmbase.datatypes.StringDataType.BOOLEAN_PATTERN
253: .matcher(stringValue).matches()) {
254: node.setValue(field, Casting
255: .toBoolean(value));
256: } else {
257: return INVALID_VALUE;
258: }
259: break;
260: default:
261: log.error("Unknown type for field " + field);
262: break;
263: }
264: } else {
265: node.setValue(field, value);
266: }
267: } else {
268: node.setValue(field, value);
269: log.warn("Invalid type for field " + field);
270: return UNKNOWN;
271: }
272: } else {
273: log.error("setObjectField(): Can't find node : " + key);
274: }
275: return "";
276: }
277:
278: /**
279: * @javadoc
280: */
281: public String getObjectField(String owner, String key, String field) {
282: String rtn;
283: MMObjectNode node = getNode(owner, key);
284: if (node == null) {
285: log.error("Node " + key + " not found!");
286: rtn = "";
287: } else {
288: rtn = node.getStringValue(field);
289: }
290: return rtn;
291: }
292:
293: /**
294: * @javadoc
295: */
296: private String getTmpKey(String owner, String key) {
297: return owner + "_" + key;
298: }
299: }
|