001: /*
002: * Bossa Workflow System
003: *
004: * $Id: BossaBrowser.java,v 1.8 2004/03/18 21:45:40 gdvieira Exp $
005: *
006: * Copyright (C) 2003,2004 OpenBR Sistemas S/C Ltda.
007: *
008: * This file is part of Bossa.
009: *
010: * Bossa is free software; you can redistribute it and/or modify it
011: * under the terms of version 2 of the GNU General Public License as
012: * published by the Free Software Foundation.
013: *
014: * This program is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * General Public License for more details.
018: *
019: * You should have received a copy of the GNU General Public
020: * License along with this program; if not, write to the
021: * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
022: * Boston, MA 02111-1307, USA.
023: */
024:
025: package com.bigbross.bossa.examples;
026:
027: import java.io.BufferedReader;
028: import java.io.InputStreamReader;
029: import java.text.DateFormat;
030: import java.util.Date;
031: import java.util.HashMap;
032: import java.util.Iterator;
033: import java.util.List;
034: import java.util.Map;
035: import java.util.StringTokenizer;
036:
037: import com.bigbross.bossa.Bossa;
038: import com.bigbross.bossa.BossaFactory;
039: import com.bigbross.bossa.history.Historian;
040: import com.bigbross.bossa.notify.Event;
041: import com.bigbross.bossa.resource.Resource;
042: import com.bigbross.bossa.resource.ResourceManager;
043: import com.bigbross.bossa.wfnet.Activity;
044: import com.bigbross.bossa.wfnet.Case;
045: import com.bigbross.bossa.wfnet.CaseType;
046: import com.bigbross.bossa.wfnet.CaseTypeManager;
047: import com.bigbross.bossa.wfnet.WorkItem;
048: import com.bigbross.bossa.work.WorkManager;
049:
050: /**
051: * This class implements a crude command line browser to the
052: * API of the bossa engine. <p>
053: */
054: public class BossaBrowser {
055:
056: Bossa bossa;
057: CaseTypeManager caseTypeManager;
058: ResourceManager resourceManager;
059: WorkManager workManager;
060: Historian historian;
061: HashMap cases;
062: List lastCaseList;
063: List lastWorkItemList;
064: List lastActivitiesList;
065: List lastResourceList;
066: List lastCaseTypeResourceList;
067:
068: public BossaBrowser() throws Exception {
069: BossaFactory factory = new BossaFactory();
070: factory.setStateDir("build/BossaBrowserState");
071: bossa = factory.createBossa();
072: caseTypeManager = bossa.getCaseTypeManager();
073: resourceManager = bossa.getResourceManager();
074: workManager = bossa.getWorkManager();
075: historian = bossa.getHistorian();
076:
077: cases = new HashMap();
078: lastCaseList = null;
079: lastWorkItemList = null;
080: lastActivitiesList = null;
081: lastResourceList = null;
082: lastCaseTypeResourceList = null;
083: }
084:
085: private Resource retrieveResource(int listId) {
086: Resource r;
087: if (listId < 100) {
088: r = (Resource) lastResourceList.get(listId);
089: } else {
090: r = (Resource) lastCaseTypeResourceList.get(listId - 100);
091: }
092: return r;
093: }
094:
095: private void printHistory(List l) {
096: DateFormat df = DateFormat.getDateTimeInstance(
097: DateFormat.SHORT, DateFormat.SHORT);
098: System.out.println(" Date Time T Id {Attributes}");
099: System.out.println("-------------------------------------");
100: for (int i = 0; i < l.size(); i++) {
101: Event e = (Event) l.get(i);
102: System.out.println(" " + df.format(e.getTime()) + " "
103: + e.getType() + " " + e.getId() + " "
104: + e.getAttributes());
105: }
106: }
107:
108: public void run() throws Exception {
109:
110: BufferedReader in = new BufferedReader(new InputStreamReader(
111: System.in));
112:
113: System.out.println();
114: System.out.println("## WFNet Browser ##");
115: System.out.println("Enter command (q to quit, ? for help):");
116: System.out.print("> ");
117: System.out.flush();
118:
119: String line;
120: StringTokenizer tokenizer;
121: String operation;
122: while (!"q".equals(line = in.readLine())) {
123: tokenizer = new StringTokenizer(line);
124: if (tokenizer.hasMoreTokens()) {
125: operation = tokenizer.nextToken();
126: } else {
127: operation = "";
128: }
129: if (operation.equals("?")) {
130: String options = "? This help message.\n"
131: + "s Takes a snapshot.\n"
132: + "q Quits the browser.\n"
133: + "-----------------------------------------------------------\n"
134: + "l List case types.\n"
135: + "g <file> Register a case type.\n"
136: + "r <id> Remove a case type.\n"
137: + "c <id> List cases of a case type.\n"
138: + "w <id> List work itens of a case type.\n"
139: + "a <id> List activities of a case type.\n"
140: + "t <id> List resources of a case type.\n"
141: + "y <listId> List resources of a case.\n"
142: + "m <listId> <place>=<n> ... Change state of a case.\n"
143: + "-----------------------------------------------------------\n"
144: + "lr List all resources.\n"
145: + "gr <id> Register a resource.\n"
146: + "rr <listId> Remove a resource.\n"
147: + "dr <listId> Detail a resource.\n"
148: + "ir <listId> <listId> Include a resource.\n"
149: + "er <listId> <listId> Exclude a resource.\n"
150: + "cr <listId> <listId> Cancel include or exclude.\n"
151: + "ct <listId> <listId> Contains resource?\n"
152: + "-----------------------------------------------------------\n"
153: + "wr <listId> List work itens of a resource.\n"
154: + "ar <listId> List activities of a resource.\n"
155: + "-----------------------------------------------------------\n"
156: + "o <listId> <resListId> Open a work item.\n"
157: + "cl <listId> Close an activity.\n"
158: + "ca <listId> Cancel an activity.\n"
159: + "f <listId> <resListId> Fire a work item.\n"
160: + "vs <caseType> <case> <id> <value> Declare a case attribute.\n"
161: + "vl <caseType> <case> List case attributes.\n"
162: + "-----------------------------------------------------------\n"
163: + "h Display all history.\n"
164: + "ht <id> Display a case type history.\n"
165: + "hc <listId> Display a case history.\n"
166: + "hr <listId> Display a resource history.\n"
167: + "hx Export and purge all history.\n";
168: System.out.print(options);
169: } else if (operation.equals("s")) {
170: bossa.takeSnapshot();
171: System.out.println("ok.");
172: } else if (operation.equals("l")) {
173: System.out.println(" ctID");
174: System.out
175: .println("-------------------------------------");
176: List l = caseTypeManager.getCaseTypes();
177: for (int i = 0; i < l.size(); i++) {
178: System.out.println(" "
179: + ((CaseType) l.get(i)).getId());
180: }
181: } else if (operation.equals("g")) {
182: String file = tokenizer.nextToken();
183: caseTypeManager.registerCaseType(file);
184: System.out.println("ok.");
185: } else if (operation.equals("r")) {
186: String id = tokenizer.nextToken();
187: caseTypeManager.removeCaseType(id);
188: System.out.println("ok.");
189: } else if (operation.equals("c")) {
190: System.out.println("\tcID\tmarking");
191: System.out
192: .println("-------------------------------------");
193: String id = tokenizer.nextToken();
194: lastCaseList = caseTypeManager.getCaseType(id)
195: .getCases();
196: for (int i = 0; i < lastCaseList.size(); i++) {
197: Case caze = (Case) lastCaseList.get(i);
198: System.out.println(i + ":\t" + caze.getId() + "\t"
199: + caze.getState());
200: }
201: } else if (operation.equals("w")) {
202: System.out.println("\tctID\tcID\twiID");
203: System.out
204: .println("-------------------------------------");
205: String id = tokenizer.nextToken();
206: lastWorkItemList = caseTypeManager.getCaseType(id)
207: .getWorkItems(true);
208: for (int i = 0; i < lastWorkItemList.size(); i++) {
209: WorkItem wi = (WorkItem) lastWorkItemList.get(i);
210: System.out.println(i + ":\t" + id + "\t\t"
211: + wi.getCase().getId() + "\t\t"
212: + wi.getId());
213: }
214: } else if (operation.equals("a")) {
215: System.out.println("\tctID\tcID\taID\ttransition");
216: System.out
217: .println("-------------------------------------");
218: String id = tokenizer.nextToken();
219: lastActivitiesList = caseTypeManager.getCaseType(id)
220: .getActivities();
221: for (int i = 0; i < lastActivitiesList.size(); i++) {
222: Activity a = (Activity) lastActivitiesList.get(i);
223: System.out.println(i + ":\t" + id + "\t\t"
224: + a.getCase().getId() + "\t\t" + a.getId()
225: + "\t\t" + a.getWorkItemId());
226: }
227: } else if (operation.equals("t")) {
228: System.out.println("\trID\tgroup?");
229: System.out
230: .println("-------------------------------------");
231: String id = tokenizer.nextToken();
232: lastCaseTypeResourceList = caseTypeManager.getCaseType(
233: id).getResources();
234: for (int i = 0; i < lastCaseTypeResourceList.size(); i++) {
235: Resource r = (Resource) lastCaseTypeResourceList
236: .get(i);
237: System.out.println(i + 100 + ":\t" + r.getId()
238: + "\t" + (r.isGroup() ? "yes" : "no"));
239: }
240: } else if (operation.equals("y")) {
241: System.out.println("\trID\tgroup?");
242: System.out
243: .println("-------------------------------------");
244: int listId = Integer.parseInt(tokenizer.nextToken());
245: lastCaseTypeResourceList = ((Case) lastCaseList
246: .get(listId)).getResources();
247: for (int i = 0; i < lastCaseTypeResourceList.size(); i++) {
248: Resource r = (Resource) lastCaseTypeResourceList
249: .get(i);
250: System.out.println(i + 100 + ":\t" + r.getId()
251: + "\t" + (r.isGroup() ? "yes" : "no"));
252: }
253: } else if (operation.equals("m")) {
254: int listId = Integer.parseInt(tokenizer.nextToken());
255: Case caze = (Case) lastCaseList.get(listId);
256: HashMap newState = new HashMap();
257: while (tokenizer.hasMoreTokens()) {
258: StringTokenizer t = new StringTokenizer(tokenizer
259: .nextToken(), "=");
260: String placeId = t.nextToken();
261: int value = Integer.parseInt(t.nextToken());
262: newState.put(placeId, new Integer(value));
263: }
264: caze.setState(newState);
265: System.out.println("ok.");
266: } else if (operation.equals("lr")) {
267: System.out.println("\trID\tgroup?");
268: System.out
269: .println("-------------------------------------");
270: lastResourceList = resourceManager.getResources();
271: for (int i = 0; i < lastResourceList.size(); i++) {
272: Resource r = (Resource) lastResourceList.get(i);
273: System.out.println(i + ":\t" + r.getId() + "\t"
274: + (r.isGroup() ? "yes" : "no"));
275: }
276: } else if (operation.equals("gr")) {
277: String id = tokenizer.nextToken();
278: resourceManager.createResource(id);
279: System.out.println("ok.");
280: } else if (operation.equals("rr")) {
281: int listId = Integer.parseInt(tokenizer.nextToken());
282: Resource r = (Resource) lastResourceList.get(listId);
283: resourceManager.removeResource(r);
284: System.out.println("ok.");
285: } else if (operation.equals("dr")) {
286: int listId = Integer.parseInt(tokenizer.nextToken());
287: Resource r = retrieveResource(listId);
288: System.out.println("Includes:");
289: Iterator i = r.getIncludes().iterator();
290: while (i.hasNext()) {
291: System.out.println(" "
292: + ((Resource) i.next()).getId());
293: }
294: System.out.println("Excludes:");
295: i = r.getExcludes().iterator();
296: while (i.hasNext()) {
297: System.out.println(" "
298: + ((Resource) i.next()).getId());
299: }
300: } else if (operation.equals("ir")) {
301: int hostId = Integer.parseInt(tokenizer.nextToken());
302: int resId = Integer.parseInt(tokenizer.nextToken());
303: Resource host = retrieveResource(hostId);
304: Resource r = retrieveResource(resId);
305: System.out.println(host.include(r) ? "ok." : "NOT ok.");
306: } else if (operation.equals("er")) {
307: int hostId = Integer.parseInt(tokenizer.nextToken());
308: int resId = Integer.parseInt(tokenizer.nextToken());
309: Resource host = retrieveResource(hostId);
310: Resource r = retrieveResource(resId);
311: System.out.println(host.exclude(r) ? "ok." : "NOT ok.");
312: } else if (operation.equals("cr")) {
313: int hostId = Integer.parseInt(tokenizer.nextToken());
314: int resId = Integer.parseInt(tokenizer.nextToken());
315: Resource host = retrieveResource(hostId);
316: Resource r = retrieveResource(resId);
317: host.remove(r);
318: System.out.println("ok.");
319: } else if (operation.equals("ct")) {
320: int hostId = Integer.parseInt(tokenizer.nextToken());
321: int resId = Integer.parseInt(tokenizer.nextToken());
322: Resource host = retrieveResource(hostId);
323: Resource r = retrieveResource(resId);
324: System.out.println(host.contains(r) ? "contains."
325: : "NOT contains.");
326: } else if (operation.equals("wr")) {
327: System.out.println("\tctID\tcID\twiID");
328: System.out
329: .println("-------------------------------------");
330: int resId = Integer.parseInt(tokenizer.nextToken());
331: Resource resource = (Resource) lastResourceList
332: .get(resId);
333: lastWorkItemList = workManager.getWorkItems(resource,
334: true);
335: for (int i = 0; i < lastWorkItemList.size(); i++) {
336: WorkItem wi = (WorkItem) lastWorkItemList.get(i);
337: System.out.println(i + ":\t"
338: + wi.getCaseType().getId() + "\t\t"
339: + wi.getCase().getId() + "\t\t"
340: + wi.getId());
341: }
342: } else if (operation.equals("ar")) {
343: System.out.println("\tctID\tcID\taID\ttransition");
344: System.out
345: .println("-------------------------------------");
346: int resId = Integer.parseInt(tokenizer.nextToken());
347: Resource resource = (Resource) lastResourceList
348: .get(resId);
349: lastActivitiesList = workManager
350: .getActivities(resource);
351: for (int i = 0; i < lastActivitiesList.size(); i++) {
352: Activity a = (Activity) lastActivitiesList.get(i);
353: System.out.println(i + ":\t"
354: + a.getCaseType().getId() + "\t\t"
355: + a.getCase().getId() + "\t\t" + a.getId()
356: + "\t\t" + a.getWorkItemId());
357: }
358: } else if (operation.equals("o")) {
359: int listId = Integer.parseInt(tokenizer.nextToken());
360: int resId = Integer.parseInt(tokenizer.nextToken());
361: WorkItem wi = (WorkItem) lastWorkItemList.get(listId);
362: Resource res = (Resource) lastResourceList.get(resId);
363: Activity a = wi.open(res);
364: System.out.println("ok. Activity: " + a.getId());
365: } else if (operation.equals("cl")) {
366: int listId = Integer.parseInt(tokenizer.nextToken());
367: Activity a = (Activity) lastActivitiesList.get(listId);
368: HashMap attributes = (HashMap) cases.get(a
369: .getCaseType().getId()
370: + a.getCase().getId());
371: boolean result = a.close(attributes);
372: System.out.println("ok. Success=" + result);
373: } else if (operation.equals("ca")) {
374: int listId = Integer.parseInt(tokenizer.nextToken());
375: Activity a = (Activity) lastActivitiesList.get(listId);
376: boolean result = a.cancel();
377: System.out.println("ok. Success=" + result);
378: } else if (operation.equals("f")) {
379: int listId = Integer.parseInt(tokenizer.nextToken());
380: int resId = Integer.parseInt(tokenizer.nextToken());
381: WorkItem wi = (WorkItem) lastWorkItemList.get(listId);
382: Resource res = (Resource) lastResourceList.get(resId);
383: HashMap attributes = (HashMap) cases.get(wi
384: .getCaseType().getId()
385: + wi.getCase().getId());
386: Activity a = wi.open(res);
387: boolean result = a.close(attributes);
388: System.out.println("ok. Success=" + result);
389: } else if (operation.equals("vs")) {
390: String caseTypeId = tokenizer.nextToken();
391: String caseId = tokenizer.nextToken();
392: String id = tokenizer.nextToken();
393: String valueStr = tokenizer.nextToken();
394: Object value;
395: try {
396: value = new Integer(valueStr);
397: } catch (NumberFormatException e) {
398: value = valueStr;
399: }
400: HashMap attributes = (HashMap) cases.get(caseTypeId
401: + caseId);
402: if (attributes == null) {
403: attributes = new HashMap();
404: cases.put(caseTypeId + caseId, attributes);
405: }
406: attributes.put(id, value);
407: System.out.println("ok.");
408: } else if (operation.equals("vl")) {
409: String caseTypeId = tokenizer.nextToken();
410: String caseId = tokenizer.nextToken();
411: HashMap attributes = (HashMap) cases.get(caseTypeId
412: + caseId);
413: if (attributes != null) {
414: Iterator it = attributes.entrySet().iterator();
415: while (it.hasNext()) {
416: Map.Entry attribute = (Map.Entry) it.next();
417: System.out.println(attribute.getKey()
418: .toString()
419: + " == " + attribute.getValue());
420: }
421: }
422: } else if (operation.equals("h")) {
423: List l = historian.getHistory();
424: printHistory(l);
425: } else if (operation.equals("ht")) {
426: String id = tokenizer.nextToken();
427: List l = historian.getCaseTypeHistory(id);
428: printHistory(l);
429: } else if (operation.equals("hc")) {
430: int listId = Integer.parseInt(tokenizer.nextToken());
431: Case caze = (Case) lastCaseList.get(listId);
432: List l = historian.getCaseHistory(caze.getCaseType()
433: .getId(), caze.getId());
434: printHistory(l);
435: } else if (operation.equals("hr")) {
436: int listId = Integer.parseInt(tokenizer.nextToken());
437: Resource resource = retrieveResource(listId);
438: List l = historian.getResourceHistory(resource.getId());
439: printHistory(l);
440: } else if (operation.equals("hx")) {
441: Date now = new Date();
442: historian.exportHistory(now, "history.xml");
443: historian.purgeHistory(now);
444: System.out.println("History written to history.xml.");
445: } else if (operation.equals("")) {
446: } else {
447: System.out.println("Invalid command.");
448: }
449: System.out.print("> ");
450: System.out.flush();
451: }
452: }
453:
454: public static void main(String[] args) throws Exception {
455: new BossaBrowser().run();
456: }
457: }
|