KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > projector > processor > form > FormHandler


1 package org.apache.slide.projector.processor.form;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.List JavaDoc;
7 import java.util.Map JavaDoc;
8 import java.util.StringTokenizer JavaDoc;
9
10 import org.apache.slide.projector.ContentType;
11 import org.apache.slide.projector.Context;
12 import org.apache.slide.projector.Processor;
13 import org.apache.slide.projector.Result;
14 import org.apache.slide.projector.Store;
15 import org.apache.slide.projector.URI;
16 import org.apache.slide.projector.descriptor.ParameterDescriptor;
17 import org.apache.slide.projector.descriptor.ResultDescriptor;
18 import org.apache.slide.projector.descriptor.ResultEntryDescriptor;
19 import org.apache.slide.projector.descriptor.StateDescriptor;
20 import org.apache.slide.projector.descriptor.StringValueDescriptor;
21 import org.apache.slide.projector.descriptor.ValidationException;
22 import org.apache.slide.projector.engine.ProcessorManager;
23 import org.apache.slide.projector.i18n.ErrorMessage;
24 import org.apache.slide.projector.i18n.ParameterMessage;
25 import org.apache.slide.projector.processor.SimpleProcessor;
26 import org.apache.slide.projector.processor.process.Process;
27 import org.apache.slide.projector.value.BooleanValue;
28 import org.apache.slide.projector.value.MapValue;
29 import org.apache.slide.projector.value.StringValue;
30 import org.apache.slide.projector.value.URIValue;
31
32 /**
33  * @version $Revision: 1.4 $
34  */

35
36 public class FormHandler implements Processor {
37     public final static String JavaDoc RESULT = "result";
38
39     private final static String JavaDoc INSTRUCTION_IDENTIFIER = "instruction:";
40     
41     private final static ParameterDescriptor[] parameterDescriptors = new ParameterDescriptor[0];
42
43     private final static ResultDescriptor resultDescriptor = new ResultDescriptor(
44             new StateDescriptor[] { StateDescriptor.OK_DESCRIPTOR },
45             new ResultEntryDescriptor[] {
46                 new ResultEntryDescriptor(SimpleProcessor.OUTPUT, new ParameterMessage("trigger/output"), ContentType.DYNAMIC, true)
47             });
48
49     public Result process(Map JavaDoc parameter, Context context) throws Exception JavaDoc {
50         // Lookup valid instruction
51
String JavaDoc trigger = null;
52         for ( Iterator JavaDoc i = parameter.entrySet().iterator(); i.hasNext(); ) {
53             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)i.next();
54             String JavaDoc key = (String JavaDoc)entry.getKey();
55             if ( key.startsWith(Form.TRIGGER_IDENTIFIER) ) {
56                 if ( key.indexOf('.') > 0 ) {
57                     trigger = key.substring(0, key.indexOf('.'));
58                     break;
59                 }
60             }
61         }
62         if ( trigger == null ) {
63             throw new ValidationException(new ErrorMessage("trigger/triggerParameterMissing"));
64         }
65         // Decode instruction
66
StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(StringValueDescriptor.ANY.valueOf(parameter.get(INSTRUCTION_IDENTIFIER+trigger), context).toString(), ";");
67         URI actionURI = new URIValue(tokenizer.nextToken());
68         boolean validate = Boolean.valueOf(tokenizer.nextToken()).booleanValue();
69         boolean wizard = Boolean.valueOf(tokenizer.nextToken()).booleanValue();
70         String JavaDoc lastStep = tokenizer.nextToken();
71         String JavaDoc targetStep = tokenizer.nextToken();
72         String JavaDoc domain = tokenizer.nextToken();
73         List JavaDoc involvedParamters = new ArrayList JavaDoc();
74         while ( tokenizer.hasMoreTokens() ) {
75             involvedParamters.add(tokenizer.nextToken());
76         }
77         Store store = context.getStore(Store.SESSION);
78         // 1. Store all (request) parameters into requested domain
79
Map JavaDoc map;
80         MapValue mapResource = (MapValue)store.get(domain);
81         if ( mapResource == null ) {
82             map = new HashMap JavaDoc();
83             mapResource = new MapValue(map);
84             store.put(domain, mapResource);
85         } else {
86             map = mapResource.getMap();
87         }
88         map.put(ControlComposer.VALIDATE, new BooleanValue(validate));
89         map.put(Process.STEP, new StringValue(targetStep));
90         map.putAll(parameter);
91         if ( validate ) {
92             // 2. Validate parameters
93
Processor processor = ProcessorManager.getInstance().getProcessor(actionURI);
94             try {
95                 // Validate only given parameters to enable wizard like forms
96
ParameterDescriptor[] parameterDescriptors = processor.getParameterDescriptors();
97                 for ( int i = 0; i < parameterDescriptors.length; i++ ) {
98                     String JavaDoc parameterName = parameterDescriptors[i].getName();
99                     if ( involvedParamters.contains(parameterName) ) {
100                         map.put(parameterName, ProcessorManager.prepareValue(parameterDescriptors[i], parameter.get(parameterName), context));
101                     }
102                 }
103                 if ( wizard ) {
104                     // Don't validate target step form
105
map.put(ControlComposer.VALIDATE, BooleanValue.FALSE);
106                 }
107             } catch ( ValidationException exception ) {
108                 // 3. Go back to form step if validation failes
109
map.put(Process.STEP, new StringValue(lastStep));
110             }
111         }
112         // 4. Launch target step if validation is successfull
113
Processor formProcess = ProcessorManager.getInstance().getProcessor(new URIValue(domain));
114         map.remove(RESULT);
115         Result formResult = formProcess.process(parameter, context);
116         map.put(RESULT, formResult);
117         Processor bookmark = ProcessorManager.getInstance().getProcessor(context.getBookmark());
118         return bookmark.process(parameter, context);
119     }
120
121     public ParameterDescriptor[] getParameterDescriptors() {
122         return parameterDescriptors;
123     }
124
125     public ResultDescriptor getResultDescriptor() {
126         return resultDescriptor;
127     }
128 }
Popular Tags