KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > workflow > util > WebWorkValidator


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.workflow.util;
6
7 import com.opensymphony.module.propertyset.PropertySet;
8
9 import com.opensymphony.workflow.*;
10
11 import webwork.action.Action;
12 import webwork.action.ActionContext;
13
14 import webwork.dispatcher.ActionResult;
15 import webwork.dispatcher.GenericDispatcher;
16
17 import java.lang.reflect.Method JavaDoc;
18
19 import java.security.Principal JavaDoc;
20
21 import java.util.*;
22
23
24 /**
25  * DOCUMENT ME!
26  *
27  * @author $author$
28  * @version $Revision: 1.2 $
29  */

30 public class WebWorkValidator implements Validator {
31     //~ Methods ////////////////////////////////////////////////////////////////
32

33     public void validate(Map transientVars, Map args, PropertySet ps) throws WorkflowException {
34         final WorkflowContext wfContext = (WorkflowContext) transientVars.get("context");
35
36         String JavaDoc actionName = (String JavaDoc) args.get("action.name");
37         GenericDispatcher gd = new GenericDispatcher(actionName);
38         gd.prepareContext();
39         ActionContext.setPrincipal(new Principal JavaDoc() {
40                 public String JavaDoc getName() {
41                     return wfContext.getCaller();
42                 }
43             });
44         ActionContext.setApplication(args);
45         ActionContext.setSession(ps.getProperties(""));
46         ActionContext.setLocale(Locale.getDefault());
47         ActionContext.setParameters(Collections.unmodifiableMap(transientVars));
48
49         boolean hasErrors = false;
50         InvalidInputException iie = new InvalidInputException();
51
52         try {
53             gd.executeAction();
54
55             ActionResult ar = gd.finish();
56             gd.finalizeContext();
57
58             List actions = ar.getActions();
59
60             for (Iterator iterator = actions.iterator(); iterator.hasNext();) {
61                 Action action = (Action) iterator.next();
62
63                 List errorMessages = getErrorMessages(action);
64
65                 for (Iterator iterator2 = errorMessages.iterator();
66                         iterator2.hasNext();) {
67                     String JavaDoc error = (String JavaDoc) iterator2.next();
68                     iie.addError(error);
69                     hasErrors = true;
70                 }
71
72                 Map errors = getErrors(action);
73
74                 for (Iterator iterator2 = errors.entrySet().iterator();
75                         iterator2.hasNext();) {
76                     Map.Entry entry = (Map.Entry) iterator2.next();
77                     String JavaDoc error = (String JavaDoc) entry.getKey();
78                     String JavaDoc message = (String JavaDoc) entry.getValue();
79                     iie.addError(error, message);
80                     hasErrors = true;
81                 }
82             }
83         } catch (Exception JavaDoc e) {
84             throw new WorkflowException("Could not execute action " + actionName, e);
85         }
86
87         if (hasErrors) {
88             throw iie;
89         }
90     }
91
92     private List getErrorMessages(Action action) {
93         try {
94             Method JavaDoc m = action.getClass().getMethod("getErrorMessages", new Class JavaDoc[] {
95                     
96                 });
97
98             return (List) m.invoke(action, new Object JavaDoc[] {});
99         } catch (Throwable JavaDoc t) {
100             return Collections.EMPTY_LIST;
101         }
102     }
103
104     private Map getErrors(Action action) {
105         try {
106             Method JavaDoc m = action.getClass().getMethod("getErrors", new Class JavaDoc[] {});
107
108             return (Map) m.invoke(action, new Object JavaDoc[] {});
109         } catch (Throwable JavaDoc t) {
110             return Collections.EMPTY_MAP;
111         }
112     }
113 }
114
Popular Tags