KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > applications > xmlimporter > TmpObject


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.applications.xmlimporter;
11
12 import java.util.*;
13 import org.mmbase.module.Module;
14 import org.mmbase.module.core.*;
15 import org.mmbase.module.corebuilders.*;
16 import org.mmbase.util.*;
17 import org.mmbase.util.logging.*;
18
19 /**
20  * A TmpObject represents a temporary object in a transaction.
21  * TmpObject instances can only be created by methods of Transaction, and have
22  * no meaning outside the context of the transaction in which they are created.
23  *
24  * @author Rob van Maris: Finalist IT Group
25  * @since MMBase-1.5
26  * @version $Id: TmpObject.java,v 1.8 2005/10/06 14:14:41 michiel Exp $
27  */

28 public class TmpObject {
29
30     // Node field names.
31
final static String JavaDoc NUMBER = "number";
32     final static String JavaDoc RNUMBER = "rnumber";
33     final static String JavaDoc SNUMBER = "snumber";
34     final static String JavaDoc DNUMBER = "dnumber";
35     final static String JavaDoc _SNUMBER = "_snumber";
36     final static String JavaDoc _DNUMBER = "_dnumber";
37
38     /** Logger instance. */
39     private static Logger log
40         = Logging.getLoggerInstance(TmpObject.class.getName());
41
42     /** The mmbase module. */
43     private static MMBase mmbase;
44
45     /** The temporary node manager. */
46     private static TemporaryNodeManagerInterface tmpNodeManager;
47
48     /** All user-related data. */
49     private UserTransactionInfo uti;
50
51     /** User specified id. */
52     private String JavaDoc id;
53
54     /** The MMObjectNode in the temporary cloud. */
55     private MMObjectNode node;
56
57     /** Relation flag, true if this a relation object, false otherwise. */
58     private boolean relationFlag;
59
60     /** Flag, true if this object is to be dropped when it has
61      * no relations on commit, false otherwise. */

62     private boolean disposeWhenNotReferenced = false;
63
64     /**
65      * Gets reference to the MMBase module.
66      * @return The MMBase module.
67      */

68     private static MMBase getMMBase() {
69         if (mmbase == null) {
70             mmbase=(MMBase) Module.getModule("MMBASEROOT");
71         }
72         return mmbase;
73     }
74
75     /**
76      * Gets reference to TemporaryNodeManager module.
77      * @return the TemporaryNodeManager module.
78      */

79     private static synchronized TemporaryNodeManagerInterface getTmpNodeManager() {
80         if (tmpNodeManager == null) {
81             tmpNodeManager = new TemporaryNodeManager(getMMBase());
82         }
83         return tmpNodeManager;
84     }
85
86     /**
87      * Creates new import TmpObject.
88      * @param uti transaction info for current user.
89      * @param objectId user-specified id for the new object
90      * (must be unique in this transaction context),
91      * or null for anonymous object.
92      * @param relationFlag relation flag: true if this is a relation, false
93      * otherwise.
94      * @param disposeWhenNotReferenced flag: true if this object is
95      * to be dropped when it has no relations on commit, false otherwise.
96      */

97     TmpObject(UserTransactionInfo uti, String JavaDoc objectId,
98     boolean relationFlag, boolean disposeWhenNotReferenced) {
99         this.uti = uti;
100         this.id = objectId;
101         this.node = getTmpNodeManager().getNode(uti.user.getName(), objectId);
102         this.relationFlag = relationFlag;
103         this.disposeWhenNotReferenced = disposeWhenNotReferenced;
104     }
105
106     /**
107      * Creates new access TmpObject.
108      * @param uti transaction info for current user.
109      * @param objectId user-specified id for the new object
110      * (must be unique in this transaction context),
111      * or null for anonymous object.
112      * @param mmbaseId the MMBase id.
113      */

114     TmpObject(UserTransactionInfo uti, String JavaDoc objectId, int mmbaseId) {
115         // Ironically, the mmbaseId argument is no longer needed.
116
this.uti = uti;
117         this.id = objectId;
118         this.node = getTmpNodeManager().getNode(uti.user.getName(), objectId);
119         this.relationFlag = (node.getBuilder() instanceof InsRel);
120     }
121
122     /**
123      * Gets field of the temporary node.
124      * @param name The field.
125      * @return The value of the field.
126      */

127     public Object JavaDoc getField(String JavaDoc name) {
128         return node.getValue(name);
129     }
130
131     /**
132      * Sets field of the temporary node represented by this TmpObject instance.
133      * If the value is a <code>String</code> and the type of the field is
134      * {@link org.mmbase.module.corebuilders.FieldDefs#TYPE_BYTE TYPE_BYTE},
135      * the string is decoded to bytes using Base64.
136      * @param name The field name.
137      * @param value The field value.
138      */

139     public void setField(String JavaDoc name, Object JavaDoc value) {
140        // Decode string for binary field to byte-array using Base64.
141
if (node.getDBType(name) == FieldDefs.TYPE_BYTE
142        && value instanceof String JavaDoc) {
143           String JavaDoc strValue = (String JavaDoc) value;
144           value = new Encode("BASE64").decodeBytes(strValue);
145        }
146        node.setValue(name, value);
147     }
148
149     /**
150      * Gets the relations of this object in the persistent cloud.
151      * Note that the relations returned are always of builder type 'InsRel',
152      * even if they are really from a derived builder such as AuthRel.
153      * @return All relations in the persistent cloud of the object in the
154      * persistent cloud represented by this TmpObject instance.
155      */

156     public Vector getRelationsInPersistentCloud() {
157         Vector relations;
158         int mmbaseId = getMMBaseId();
159         if (mmbaseId != -1) {
160             // Access object.
161
relations = getMMBase().getInsRel().getRelations_main(mmbaseId);
162             if (log.isDebugEnabled()) {
163                 log.debug("Relations in persistent cloud of " + this
164                 + ": " + relations);
165             }
166         } else {
167             // Not an access object, so it has no relations in persistent cloud.
168
relations = new Vector();
169         }
170         return relations;
171     }
172
173     /**
174      * Gets the temporary node corresponding to this object.
175      * @return The temporary node.
176      */

177     public MMObjectNode getNode() {
178         return node;
179     }
180
181     /**
182      * Key accessor.
183      * @return The key used internally by TemporaryNodeManager and
184      * TransactionManager.
185      */

186     public String JavaDoc getKey() {
187        // This reproduces the key used by the TemporaryNodeManager.
188
return uti.user.getName() + "_" + id;
189     }
190
191     /**
192      * Id accessor.
193      * @return The id specified by the user.
194      */

195     public String JavaDoc getId() {
196         return id;
197     }
198
199     /**
200      * Tests if this object is an accessed object
201      * (representing an object that already exists
202      * in the persistent cloud) or an input object.
203      * @return true if this is an access object, false otherwise.
204      */

205     public boolean isAccessObject() {
206         return getMMBaseId() != -1;
207     }
208
209     /**
210      * MMBaseId accessor (for access objects).
211      * @return The MMBase id, null if this is not an access object.
212      */

213     public int getMMBaseId() {
214         return node.getIntValue(NUMBER);
215     }
216
217     /**
218      * Tests if this object is a relation.
219      * @return true if this is a relation, false otherwise.
220      */

221     public boolean isRelation() {
222         return relationFlag;
223     }
224
225     /**
226      * DisposeWhenNotReferenced accessor.
227      * @return disposeWhenNotReferenced flag: true if this object is
228      * to be dropped when it has no relations on commit, false otherwise.
229      */

230     public boolean getDisposeWhenNotReferenced() {
231         return disposeWhenNotReferenced;
232     }
233
234     /**
235      * Tests if this node is the source node of a relation.
236      * @param tempRel Temporary relation node.
237      * @return true if the relation node has this node as source.
238      */

239     public boolean isSourceOf(TmpObject tempRel) {
240         MMObjectNode relation = tempRel.node;
241         String JavaDoc sourceId = relation.getStringValue(_SNUMBER);
242         if (!sourceId.equals("")) {
243             // Source is temporary node.
244
return getKey().equals(sourceId);
245         }
246         int sourceMmbaseId = relation.getIntValue(SNUMBER);
247         if (sourceMmbaseId != -1) {
248             // Source is persistent node.
249
return isAccessObject() && getMMBaseId() == sourceMmbaseId;
250         }
251         return false;
252     }
253
254     /**
255      * Tests if this node is the destination node of a relation.
256      * @param tempRel Temporary relation node.
257      * @return true if the relation node has this node as destination.
258      */

259     public boolean isDestinationOf(TmpObject tempRel) {
260         MMObjectNode relation = tempRel.node;
261         String JavaDoc destinationId = relation.getStringValue(_DNUMBER);
262         if (!destinationId.equals("")) {
263             // Destination is temporary node.
264
return getKey().equals(destinationId);
265         }
266         int destinationMmbaseId = relation.getIntValue(DNUMBER);
267         if (destinationMmbaseId != -1) {
268             // Destination is persistent node.
269
return isAccessObject() && getMMBaseId() == destinationMmbaseId;
270         }
271         return false;
272     }
273
274     /**
275      * Sets source for relation in temporary cloud.
276      * Requires this object to be a relation.
277      * @param tmpObj The temporary object to set as source. */

278     public void setSource(TmpObject tmpObj) {
279         setField(_SNUMBER, tmpObj.getKey());
280         setField(SNUMBER, Integer.toString(tmpObj.getMMBaseId()));
281     }
282
283     /**
284      * Sets destination for relation in temporary cloud.
285      * Requires this object to be a relation.
286      * @param tmpObj The temporary object to set as destination. */

287     public void setDestination(TmpObject tmpObj) {
288         setField(_DNUMBER, tmpObj.getKey());
289         setField(DNUMBER, Integer.toString(tmpObj.getMMBaseId()));
290     }
291
292     /**
293      * Copies source of relation in temporary cloud to this object.
294      * Requires this object to be a relation.
295      * @param tmpObj The temporary object to copy the source of. */

296     public void copySourceOf(TmpObject tmpObj) {
297         setField(_SNUMBER, tmpObj.getField(_SNUMBER));
298         setField(SNUMBER, tmpObj.getField(SNUMBER));
299     }
300
301     /**
302      * Copies destination of relation in temporary cloud to this object.
303      * Requires this object to be a relation.
304      * @param tmpObj The temporary object to copy the destination of. */

305     public void copyDestinationOf(TmpObject tmpObj) {
306         setField(_DNUMBER, tmpObj.getField(_DNUMBER));
307         setField(DNUMBER, tmpObj.getField(DNUMBER));
308     }
309
310     /**
311      * Compares value of specified field of this and another object.
312      * @param tmpObj The other object.
313      * @param name The field name.
314      * @return True if the fieldvalues of both objects are equal,
315      * false otherwise.
316      */

317     private boolean compareField(TmpObject tmpObj, String JavaDoc name) {
318         Object JavaDoc value = getField(name);
319         if (value != null) {
320             return value.equals(tmpObj.getField(name));
321         } else {
322             return tmpObj.getField(name) == null;
323         }
324     }
325
326     /**
327      * ToString() method, displays most important fields.
328      * @return String representation of this object.
329      */

330     public String JavaDoc toString() {
331         return "TmpObject(id=\"" + id + "\", user=\"" + uti.user.getName()
332         + "\", key=\"" + getKey() + "\", node={" + node
333         + "}, disposeWhenNotReferenced=" + disposeWhenNotReferenced + ")";
334     }
335
336     /**
337      * Displays XML representation of this object, such as the XML code
338      * necessary to create this object in a transaction.
339      * @return XML representation of this object.
340      */

341     public String JavaDoc toXML() {
342         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
343         if (isAccessObject()) {
344             sb.append("<accessObject id=\"" + getId()
345                 + "\" mmbaseId=\"" + getMMBaseId()
346                 + "\">\n");
347         } else {
348             sb.append("<createObject id=\"" + getId()
349                 + "\" type=\"" + getNode().getName()
350                 + "\" disposeWhenNotReferenced=\"" + disposeWhenNotReferenced
351                 + "\">\n");
352         }
353         Iterator i = node.getValues().entrySet().iterator();
354         while (i.hasNext()) {
355             Map.Entry entry = (Map.Entry) i.next();
356             String JavaDoc name = (String JavaDoc) entry.getKey();
357             String JavaDoc value = entry.getValue().toString();
358             if (!name.equals("otype") && !name.equals("owner")
359                 && !name.equals("number") && !name.equals("_number")) {
360                     sb.append("<setField name=\"" + name + "\">"
361                         + value + "</setField>\n");
362             }
363         }
364         if (isAccessObject()) {
365             sb.append("</accessObject>\n");
366         } else {
367             sb.append("</createObject>\n");
368         }
369
370         return sb.toString();
371     }
372
373 }
374
Popular Tags