001: package edu.indiana.lib.twinpeaks.util;
002:
003: import java.lang.*;
004: import java.util.*;
005:
006: import org.w3c.dom.*;
007: import org.xml.sax.*;
008:
009: public class StatusUtils {
010:
011: private static org.apache.commons.logging.Log _log = LogUtils
012: .getLog(StatusUtils.class);
013:
014: /**
015: * Set up initial status information (done before LOGON)
016: */
017: public static void initialize(SessionContext sessionContext,
018: String targets) {
019: StringTokenizer parser = new StringTokenizer(targets);
020: ArrayList dbList = new ArrayList();
021: HashMap targetMap = getNewStatusMap(sessionContext);
022:
023: /*
024: * Establish the DB list and initial (pre-LOGON) status
025: */
026: while (parser.hasMoreTokens()) {
027: String db = parser.nextToken();
028: HashMap emptyMap = new HashMap();
029:
030: /*
031: * Empty status entry
032: */
033: emptyMap.put("STATUS", "INACTIVE");
034: emptyMap.put("STATUS_MESSAGE", "<none>");
035:
036: emptyMap.put("HITS", "0");
037: emptyMap.put("ESTIMATE", "0");
038: emptyMap.put("MERGED", "0");
039: /*
040: * Save
041: */
042: dbList.add(db);
043: targetMap.put(db, emptyMap);
044: }
045:
046: sessionContext.put("TARGETS", dbList);
047: sessionContext.putInt("active", 0);
048:
049: sessionContext.put("STATUS", "INACTIVE");
050: sessionContext.put("STATUS_MESSAGE", "<none>");
051: }
052:
053: /**
054: * Get an iterator into the system status map
055: * @param sessionContext Active SessionContext
056: * @return Status map Iterator
057: */
058: public static Iterator getStatusMapEntrySetIterator(
059: SessionContext sessionContext) {
060: HashMap statusMap = (HashMap) sessionContext
061: .get("searchStatus");
062: Set entrySet = Collections.EMPTY_SET;
063:
064: if (statusMap != null) {
065: entrySet = statusMap.entrySet();
066: }
067: return entrySet.iterator();
068: }
069:
070: /**
071: * Get the status entry for a specified target database
072: * @param sessionContext Active SessionContext
073: * @param target Database name
074: * @return Status Map for this target (null if none)
075: */
076: public static HashMap getStatusMapForTarget(
077: SessionContext sessionContext, String target) {
078: HashMap statusMap = (HashMap) sessionContext
079: .get("searchStatus");
080:
081: _log.debug("Map for target " + target + " is "
082: + statusMap.get(target));
083: return (statusMap == null) ? null : (HashMap) statusMap
084: .get(target);
085: }
086:
087: /**
088: * Create a new status map
089: * @param sessionContext Active SessionContext
090: * @return Status Map for this target
091: */
092: public static HashMap getNewStatusMap(SessionContext sessionContext) {
093: HashMap statusMap = new HashMap();
094:
095: sessionContext.remove("searchStatus");
096: sessionContext.put("searchStatus", statusMap);
097:
098: return statusMap;
099: }
100:
101: /**
102: * Set global status (effects all target databases)
103: * @param sessionContext Active SessionContext
104: * @param status One of ERROR | DONE
105: * @param message Status text
106: */
107: public static void setGlobalStatus(SessionContext sessionContext,
108: String status, String message) {
109: /*
110: * Set global status
111: */
112: sessionContext.put("STATUS", status);
113: sessionContext.put("STATUS_MESSAGE", message);
114: /*
115: * Per-target status
116: */
117: for (Iterator iterator = StatusUtils
118: .getStatusMapEntrySetIterator(sessionContext); iterator
119: .hasNext();) {
120: Map.Entry entry = (Map.Entry) iterator.next();
121: HashMap targetMap = (HashMap) entry.getValue();
122:
123: targetMap.put("STATUS", status);
124: targetMap.put("STATUS_MESSAGE", message);
125: }
126: }
127:
128: /**
129: * Set global error status (effects all target databases)
130: * @param sessionContext Active SessionContext
131: * @param error Error number
132: * @param message Expanded error text (null to omit expanded message)
133: */
134: public static void setGlobalError(SessionContext sessionContext,
135: String error, String message) {
136: String statusMessage = "Error " + error;
137:
138: if (!StringUtils.isNull(message)) {
139: statusMessage += ": " + message;
140: }
141: setGlobalStatus(sessionContext, "ERROR", statusMessage);
142: }
143:
144: /**
145: * Set all status value to "search complete" (effects all target databases)
146: * @param sessionContext Active SessionContext
147: */
148: public static void setAllComplete(SessionContext sessionContext) {
149: setGlobalStatus(sessionContext, "DONE", "Search complete");
150: }
151:
152: /**
153: * Update the hit count for this target (database)
154: * @param sessionContext Active SessionContext
155: * @param target Database name
156: * @return Updated hit count
157: */
158: public static int updateHits(SessionContext sessionContext,
159: String target) {
160: Map targetMap;
161: String hits;
162: int total, estimate;
163:
164: if (StringUtils.isNull(target)) {
165: throw new SearchException("No target database to update");
166: }
167:
168: if ((targetMap = getStatusMapForTarget(sessionContext, target)) == null) {
169: throw new SearchException(
170: "No status map for target database " + target);
171: }
172: /*
173: * Update total hits from this search source
174: */
175: hits = (String) targetMap.get("HITS");
176: total = Integer.parseInt(hits) + 1;
177:
178: targetMap.put("HITS", String.valueOf(total));
179: /*
180: * Have we collected all available results?
181: */
182: estimate = Integer.parseInt((String) targetMap.get("ESTIMATE"));
183: if (estimate == total) {
184: int active = sessionContext.getInt("active");
185: /*
186: * If this is the last active source, mark everything DONE
187: */
188: if (--active <= 0) {
189: setAllComplete(sessionContext);
190: } else { /*
191: * Just this source is finished
192: */
193: targetMap.put("STATUS", "DONE");
194: targetMap.put("STATUS_MESSAGE", "Search complete");
195: }
196: sessionContext.putInt("active", active);
197: }
198: return total;
199: }
200:
201: /**
202: * Fetch the estimated hiots for a specified target (database)
203: * @param sessionContext Active SessionContext
204: * @param target Database name
205: * @return Updated hit count
206: */
207: public static int getEstimatedHits(SessionContext sessionContext,
208: String target) {
209: Map targetMap;
210: String estimate;
211:
212: if ((targetMap = getStatusMapForTarget(sessionContext, target)) == null) {
213: throw new SearchException(
214: "No status map for target database " + target);
215: }
216:
217: estimate = (String) targetMap.get("ESTIMATE");
218: return Integer.parseInt(estimate);
219: }
220: }
|