KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.File JavaDoc;
14 import org.mmbase.applications.xmlimporter.ObjectMerger;
15
16 /**
17  * Extended from Transaction for interactive handling of dulpicates.
18  * @since MMBase-1.5
19  * @version $Id: InteractiveTransaction.java,v 1.3 2003/03/07 08:50:02 pierre Exp $
20  */

21 public class InteractiveTransaction extends Transaction {
22
23    /**
24     * Creates new InteractiveTransaction.
25     * @param timeOut if the transactions is not finished after the timeout
26     * (in seconds) the transaction is cancelled.
27     * @param uti transaction info for current user.
28     * @param key TransactionManager key for this transaction.
29     * @param id TransactionHandler id for this transactions.
30     * @param commitOnClose - The user-specified commit-on-close setting.
31     * True if this transaction is to be committed
32     * when the user leaves it's context, false otherwise.
33     * @param reportFile The file to use as duplicates file.
34     * @param consultant The intermediate import object. Used to set and get status from and set and get objects to and from.
35     */

36    protected InteractiveTransaction(UserTransactionInfo uti, String JavaDoc key,
37    String JavaDoc id, boolean commitOnClose, long timeOut, File JavaDoc reportFile,
38    Consultant consultant) {
39       super(uti, key, id, commitOnClose, timeOut, reportFile, consultant);
40    }
41
42    /**
43     * Handles sitiuations where more then one similar objects are found to
44     * merge with. This implementation confronts the user with the merge results.
45     * The user can choose which result is preferred. The preferred result is
46     * used to merge with.
47     *
48     * @param tempObj the original object
49     * @param similarObjects the similar objects
50     * @param merger the merger
51     * @return True if duplicates are resolved.
52     * Throws TransactionHandlerException When failing to resolve
53     * the duplicates by consulting the user.
54     * @throws TransactionHandlerException When a failure occurred.
55     */

56    protected boolean handleDuplicates(TmpObject tempObj, List similarObjects,
57    ObjectMerger merger) throws TransactionHandlerException {
58       Iterator iter = similarObjects.iterator();
59       List mergeResults = new ArrayList();
60       while (iter.hasNext() ) {
61          TmpObject similarObject = (TmpObject)iter.next();
62          mergeResults.add(caculateMerge(similarObject, tempObj, merger));
63       }
64
65       int choice = consult(tempObj, mergeResults);
66
67       merge((TmpObject)similarObjects.get(choice), tempObj, merger);
68       return true;
69    }
70
71    /**
72     * Handles sitiuations where more then one similar objects are found to
73     * merge with. This implementation confronts the user with the merge results.
74     * The user can choose which result is preferred. The preferred result is
75     * used to merge with.
76     *
77     * @param tempObj1 the original object
78     * @param mergeResults the mergeResults
79     * @return the index number of the chosen mergeResult
80     * @throws TransactionHandlerException If the situation could not be handled.
81     */

82    int consult(TmpObject originalObject, List mergeResults)
83    throws TransactionHandlerException {
84
85       // Consult user.
86
consultant.consultUser(originalObject, mergeResults);
87
88       // Test if duplicates were actually resolved.
89
if (!consultant.duplicatesResolved()) {
90          throw new TransactionHandlerException(
91          "Failed to resolve duplicates by consulting user.");
92       }
93
94       // Get users choice.
95
return consultant.getChoice();
96    }
97 }
98
99
Popular Tags