1 package org.apache.slide.projector.processor.form; 2 3 import java.util.ArrayList ; 4 import java.util.HashMap ; 5 import java.util.Iterator ; 6 import java.util.List ; 7 import java.util.Map ; 8 import java.util.StringTokenizer ; 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 35 36 public class FormHandler implements Processor { 37 public final static String RESULT = "result"; 38 39 private final static String 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 parameter, Context context) throws Exception { 50 String trigger = null; 52 for ( Iterator i = parameter.entrySet().iterator(); i.hasNext(); ) { 53 Map.Entry entry = (Map.Entry )i.next(); 54 String key = (String )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 StringTokenizer tokenizer = new StringTokenizer (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 lastStep = tokenizer.nextToken(); 71 String targetStep = tokenizer.nextToken(); 72 String domain = tokenizer.nextToken(); 73 List involvedParamters = new ArrayList (); 74 while ( tokenizer.hasMoreTokens() ) { 75 involvedParamters.add(tokenizer.nextToken()); 76 } 77 Store store = context.getStore(Store.SESSION); 78 Map map; 80 MapValue mapResource = (MapValue)store.get(domain); 81 if ( mapResource == null ) { 82 map = new HashMap (); 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 Processor processor = ProcessorManager.getInstance().getProcessor(actionURI); 94 try { 95 ParameterDescriptor[] parameterDescriptors = processor.getParameterDescriptors(); 97 for ( int i = 0; i < parameterDescriptors.length; i++ ) { 98 String 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 map.put(ControlComposer.VALIDATE, BooleanValue.FALSE); 106 } 107 } catch ( ValidationException exception ) { 108 map.put(Process.STEP, new StringValue(lastStep)); 110 } 111 } 112 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 |