KickJava   Java API By Example, From Geeks To Geeks.

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


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
14 /**
15  * SpecificObjectFinder implements a SimilarObjectFinder in such a way
16  * that a search for an object similar to tmpObj1 always returns tmpObj2,
17  * and nothing for other objects, where tmpObj1 and tmpObj2 are provided
18  * by the user. An example of the useage is provided by the method
19  * mergePersistentObjects(), wich merges two objects in the persistent
20  * cloud (i.e. the database).
21  *
22  * @author Rob van Maris (Finalist IT Group)
23  * @since MMBase-1.5
24  * @version $Id: SpecificObjectFinder.java,v 1.3 2003/03/07 08:50:03 pierre Exp $
25  */

26 public class SpecificObjectFinder implements SimilarObjectFinder {
27
28     // Base for unique transaction key.
29
private static long uniqueId = System.currentTimeMillis();
30
31     // The object searched.
32
private TmpObject tmpObj1;
33
34     // The object returned by the search.
35
private TmpObject tmpObj2;
36
37     /**
38      * Creates new SpecificObjectFinder.
39      * @param tmpObj1 The only object for which a similar object
40      * will be returned.
41      * @param tmpObj2 The only object that will be returned as
42      * similar to tmpObj1.
43      */

44     public SpecificObjectFinder(TmpObject tmpObj1, TmpObject tmpObj2) {
45         this.tmpObj1 = tmpObj1;
46         this.tmpObj2 = tmpObj2;
47     }
48
49     /**
50      * Initialize this instance. This implementation does nothing.
51      * @param params The initialization parameters, provided as
52      * name/value pairs (both String).
53      * @throws TransactionHandlerException if a failure occurred.
54      */

55     public void init(HashMap params) throws TransactionHandlerException {}
56
57     /**
58      * Searches for similar object. This implementation returns a list that
59      * contains tmpObj2 when the object to search for is tmpObj1, or an empty
60      * list when the object to search for is not tmpObj1.
61      * @return List of the similar objects found.
62      * @param transaction The transaction where the tmpObj belongs to.
63      * @param tmpObj The object to search for.
64      * @throws TransactionHandlerException If a failure occurred.
65      */

66     public List findSimilarObject(Transaction transaction, TmpObject tmpObj) throws TransactionHandlerException {
67         List results = new ArrayList();
68         if (tmpObj == tmpObj1) {
69             results.add(tmpObj2);
70         }
71         return results;
72     }
73
74     /**
75      * Merge two objects in the persistent cloud (the database).
76      * @param mmbaseId1 MMBase number of the first object. Afterward this
77      * object will be deleted.
78      * @param mmbaseId2 MMBase number of the second object. Afterward this
79      * object will hold the merged result.
80      * @param merger The merger to be used.
81      * @throws TransactionHandlerException If a failure occurred.
82      */

83     public static void mergePersistentObjects(
84         int mmbaseId1, int mmbaseId2, ObjectMerger merger)
85     throws TransactionHandlerException {
86
87         // Create user for transaction.
88
UserTransactionInfo uti = new UserTransactionInfo();
89         uti.user = new User("SpecificObjectFinder.java");
90
91         // Create transaction.
92
Transaction transaction
93             = Transaction.createTransaction(uti,
94             "SpecificObjectFinder" + (uniqueId++), true, 600);
95
96         // Access both objects.
97
TmpObject obj1 = transaction.accessObject("obj1", mmbaseId1);
98         TmpObject obj2 = transaction.accessObject("obj2", mmbaseId2);
99
100         // Merge obj1 and obj2.
101
transaction.mergeObjects(
102             obj1.getNode().getName(),
103             new SpecificObjectFinder(obj1, obj2),
104             merger);
105
106         // Commit transaction.
107
transaction.commit();
108     }
109
110 }
111
Popular Tags