KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
13 import org.mmbase.util.logging.Logger;
14 import org.mmbase.util.logging.Logging;
15
16 /**
17  * A Consultant object serves as the intermediary for communication between
18  * the thread that processes a transaction on one hand, and threads that
19  * handle interaction with the user on the other hand, to resolve ambiguity
20  * when more than one similar object is found to merge an object with.
21  *
22  * The processing thread should call: <ul>
23  * <li>consultUser() - first, to request the user to be donsulted.
24  * <li>duplicatesResolved() - after a call to consultUser() , to find out of it
25  * successfully resolved the ambiguity.
26  * <li>getChoice() - to get the index of the mergeResult, chosen by the user
27  * during the last succesfull call of consultUser().</ul>
28  *
29  * @author Erik Visser (Finalist IT Group)
30  * @author Rob van Maris (Finalist IT Group)
31  * @since MMBase-1.5
32  * @version $Id: Consultant.java,v 1.3 2003/03/07 08:50:02 pierre Exp $
33  */

34 public class Consultant {
35
36    /** Logger instance. */
37    private static Logger log
38    = Logging.getLoggerInstance(Consultant.class.getName());
39
40    /** When import finished.
41     */

42    public final static int IMPORT_STARTING = 0;
43
44    /** When import running.
45     */

46    public final static int IMPORT_RUNNING = 1;
47
48    /** When import finished.
49     */

50    public final static int IMPORT_FINISHED = 2;
51
52    /** When transaction time out.
53     */

54    public final static int IMPORT_TIMED_OUT = 3;
55
56    /** When transaction time out.
57     */

58    public final static int IMPORT_EXCEPTION = 4;
59
60    /** the original object */
61    private TmpObject originalObject;
62
63    /** the collection of mergeresults */
64    private List JavaDoc mergeResults;
65
66    /** the index of the user chosen mergeresult */
67    private int choice;
68
69    /** the index of the user chosen mergeresult */
70    private int importStatus;
71
72    /** switch that indicates the import mode chosen by the user
73     * true = interactive ; false = batch
74     */

75    private boolean interactive;
76
77    /** switch to detect if duplicates are found during a BATCH import */
78    private boolean duplicatesFound;
79
80    /** Creates new Consultant */
81    public Consultant() {
82       init();
83    }
84
85    /** init all fields */
86    public void init() {
87       originalObject = null;
88       mergeResults = null;
89       choice = 0;
90       importStatus = 0;
91       interactive = false;
92       duplicatesFound = false;
93    }
94
95    /** Consult the user for a mergeresult choice.
96     * After a call to this method, call duplicatesResolved() to examine
97     * if duplicates are resolved. If not the thread was interrupted.
98     * @param originalObject The original object.
99     * @param mergeResults The merge results to choose from.
100     */

101    public synchronized void consultUser(TmpObject originalObject,
102    List JavaDoc mergeResults) {
103
104       this.originalObject = originalObject;
105       this.mergeResults = mergeResults;
106       try {
107          while(!Thread.interrupted() && !duplicatesResolved()) {
108             wait();
109          }
110       } catch (InterruptedException JavaDoc e) {}
111    }
112
113    /** sets the user chosen import mode
114     * true if interactive false otherwise
115     * @param duplicatesFound Set true if duplicates are found. False otherwise.
116     */

117    public synchronized void setDuplicatesFound(boolean duplicatesFound) {
118       this.duplicatesFound = duplicatesFound;
119    }
120
121    /** Returns true if in a transaction duplicates are found.
122     * @return True if duplicates are found. False otherwise.
123     */

124    public synchronized boolean duplicatesFound() {
125       if (interactive) {
126          return mergeResults != null;
127       } else {
128          return duplicatesFound;
129       }
130    }
131
132    /** Returns true if the user has chosen a mergeresult.
133     * @return True if duplicates are resolved. False otherwise.
134     */

135    public synchronized boolean duplicatesResolved() {
136       return mergeResults == null;
137    }
138
139    /** Returns the index of the mergeresult the user has chosen.
140     * Call this method after method duplicatesResolved() returns true and
141     * before the next call to method consultUser(). Nou ok tis goed.
142     * @return The index of the mergeresult the user has chosen.
143     */

144    public synchronized int getChoice() {
145       return choice;
146    }
147
148    /** Sets the index of the mergeresult the user has chosen.
149     * Call this method after method duplicatesResolved() returns true and
150     * before the next call to method consultUser(). Nou ok tis goed.
151     * @param choice Sets the index of the mergeresult the user has chosen.
152     */

153    public synchronized void setChoice(int choice) {
154       this.choice = choice;
155       // to indicact ea choice is made
156
mergeResults = null;
157       originalObject = null;
158       // to wake up the consulting Thread
159
notifyAll();
160    }
161
162    /** Returns the original object.
163     * The consultant object holds an original object and a list of merge results.
164     *
165     * @return The original object.
166     */

167    public synchronized TmpObject getOriginalObject() {
168       return originalObject;
169    }
170
171    /** Returns the a list of merge results.
172     * The consultant object holds an original object and a list of merge results.
173     *
174     * @return The list of merge results.
175     */

176    public synchronized List JavaDoc getMergeResults() {
177       return mergeResults;
178    }
179
180    /** Sets the import status.
181     * Import status can be:
182     * <ul>
183     * <li>starting
184     * <li>running
185     * <li>finished
186     * <li>transaction timed out
187     * <li>exception occured
188     * </ul>
189     * See public static final int class variables.
190     * @param importStatus Set to a known import status.
191     * See public static final int class variables.
192     */

193    public synchronized void setImportStatus(int importStatus) {
194       this.importStatus = importStatus;
195    }
196
197    /** Returns true if import has finished.
198     * False otherwise.
199     * @return The actual import status.
200     */

201    public synchronized int getImportStatus() {
202       return importStatus;
203    }
204
205    /** Sets the user chosen import mode.
206     * True if interactive. False otherwise.
207     *
208     * @param interactive Set to true if user has chosen interactive importmode. Set false otherwise.
209     */

210    public synchronized void setInteractive(boolean interactive) {
211       this.interactive = interactive;
212    }
213
214    /** Returns true if user has chosen interactive import mode.
215     * False otherwise.
216     * @return True if import mode is interactive. False otherwise.
217     */

218    public synchronized boolean interactive() {
219       return interactive;
220    }
221
222    /**
223     * Add to log using info. Can be used from a jsp.
224     * @param source Name of the message source.
225     * @param message Text to add to log.
226     */

227    public static void logInfo(String JavaDoc source, String JavaDoc message) {
228       log.info("source: " + source + "message: " + message);
229    }
230
231    /**
232     * Add to log using error. Can be used from a jsp.
233     * @param source Name of the message source.
234     * @param message Text to add to log.
235     */

236    public static void logError(String JavaDoc source, String JavaDoc message) {
237       log.error("source: " + source + "message: " + message);
238    }
239
240    /**
241     * Add to log using debug. Can be used from a jsp.
242     * @param source Name of the message source.
243     * @param message Text to add to log.
244     */

245    public static void logDebug(String JavaDoc source, String JavaDoc message) {
246       if (log.isDebugEnabled()) {
247          log.debug("source: " + source + "message: " + message);
248       }
249    }
250
251    /**
252     * Add to log using trace. Can be used from a jsp.
253     * @param source Name of the message source.
254     * @param message Text to add to log.
255     */

256    public static void logTrace(String JavaDoc source, String JavaDoc message) {
257       if (log.isDebugEnabled()) {
258          log.trace("source: " + source + "message: " + message);
259       }
260    }
261 }
Popular Tags