KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.apache.slide.projector.processor.form;
2
3 import java.util.HashMap JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.List JavaDoc;
6 import java.util.Map JavaDoc;
7
8 import org.apache.slide.projector.Context;
9 import org.apache.slide.projector.Information;
10 import org.apache.slide.projector.ProcessException;
11 import org.apache.slide.projector.Processor;
12 import org.apache.slide.projector.Result;
13 import org.apache.slide.projector.Store;
14 import org.apache.slide.projector.URI;
15 import org.apache.slide.projector.descriptor.ParameterDescriptor;
16 import org.apache.slide.projector.descriptor.ResultDescriptor;
17 import org.apache.slide.projector.descriptor.URIValueDescriptor;
18 import org.apache.slide.projector.engine.ProcessorManager;
19 import org.apache.slide.projector.i18n.ErrorMessage;
20 import org.apache.slide.projector.i18n.ParameterMessage;
21 import org.apache.slide.projector.processor.process.Process;
22 import org.apache.slide.projector.store.FormStore;
23 import org.apache.slide.projector.value.BooleanValue;
24 import org.apache.slide.projector.value.MapValue;
25 import org.apache.slide.projector.value.NullValue;
26 import org.apache.slide.projector.value.StringValue;
27 import org.apache.slide.projector.value.URIValue;
28 import org.apache.slide.projector.value.Value;
29
30 public class Test implements Processor {
31     private final static URI FORM_PROCESSOR = new URIValue("formGenerator");
32     private final static URI RESULT_RENDERER = new URIValue("formResult");
33     private final static String JavaDoc PROCESSOR = "processor";
34
35     private final static String JavaDoc FORM_STEP = "form";
36     private final static String JavaDoc PROCESS_STEP = "process";
37     private final static String JavaDoc RESULT_STEP = "result";
38     
39     private final static ParameterDescriptor[] parameterDescriptors = new ParameterDescriptor[] {
40             new ParameterDescriptor(PROCESSOR, new ParameterMessage("test/parameter/processor"), new URIValueDescriptor(), NullValue.NULL),
41     };
42     
43     public Result process(Map JavaDoc parameter, Context context) throws Exception JavaDoc {
44         URI processorUri = ProcessorManager.getInstance().getURI(this);
45         context.setBookmark(processorUri);
46         Value uri = (Value)parameter.get(PROCESSOR);
47         if ( uri == null || uri == NullValue.NULL ) {
48             uri = (URI)context.getStore(Store.SESSION).get(PROCESSOR);
49         }
50         if ( uri == null ) {
51             throw new ProcessException(new ErrorMessage("test/noProcessorSpecified"));
52         }
53         context.setProcess((URI)uri);
54         context.getStore(Store.SESSION).put(PROCESSOR, uri);
55         Value stepResource = (Value)context.getStore(Store.FORM).get(Process.STEP);
56         String JavaDoc step;
57         if ( stepResource == null ) {
58             step = FORM_STEP;
59         } else {
60             step = stepResource.toString();
61         }
62         context.setStep(step);
63         Processor formProcessor = ProcessorManager.getInstance().getProcessor(FORM_PROCESSOR);
64         Processor resultRenderer = ProcessorManager.getInstance().getProcessor(RESULT_RENDERER);
65         Processor processor = ProcessorManager.getInstance().getProcessor((URI)uri);
66         parameter.put(Control.ACTION, uri);
67         parameter.put(ControlComposer.LOCALE, ProcessorManager.getInstance().process(ProcessorManager.LOCALE_RESOLVER, context.getStore(Store.REQUEST_HEADER).get("accept-language"), context));
68         parameter.put(FormGenerator.TARGET_STEP, PROCESS_STEP);
69         Result result = null;
70         if ( step.equals(FORM_STEP) ) {
71             result = formProcessor.process(parameter, context);
72         } else if ( step.equals(PROCESS_STEP) ) {
73             parameter.putAll(((MapValue)((FormStore)context.getStore(Store.FORM)).getDomain()).getMap());
74             Result processorResult = ProcessorManager.process(processor, parameter, context);
75             if ( hasErrors(context.getInformations()) ) {
76                 context.setStep(FORM_STEP);
77                 ((FormStore)context.getStore(Store.FORM)).put(ControlComposer.VALIDATE, BooleanValue.TRUE);
78                 result = formProcessor.process(parameter, context);
79             } else {
80                 Map JavaDoc resultParameters = new HashMap JavaDoc();
81                 resultParameters.put("state", new StringValue(processorResult.getState()));
82                 result = ProcessorManager.process(resultRenderer, resultParameters, context);
83                 ((FormStore)context.getStore(Store.FORM)).clear();
84             }
85         } else {
86             ((FormStore)context.getStore(Store.FORM)).clear();
87             return Result.OK;
88         }
89         return result;
90     }
91
92     protected boolean hasErrors(List JavaDoc informations) {
93         for ( Iterator JavaDoc i = informations.iterator(); i.hasNext(); ) {
94             Information info = (Information)i.next();
95             if ( info.getSeverity() == Information.ERROR ) {
96                 return true;
97             }
98         }
99         return false;
100     }
101
102     public ParameterDescriptor[] getParameterDescriptors() {
103         return parameterDescriptors;
104     }
105
106     public ResultDescriptor getResultDescriptor() {
107         return null;
108     }
109 }
Popular Tags