1 package org.sapia.soto.state.util; 2 3 import org.apache.commons.beanutils.BeanUtils; 4 import org.apache.commons.lang.ClassUtils; 5 6 import org.sapia.soto.state.Context; 7 import org.sapia.soto.state.Result; 8 import org.sapia.soto.state.Step; 9 import org.sapia.soto.state.helpers.ScopeParser; 10 11 import java.lang.reflect.InvocationTargetException ; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 15 16 17 25 public class FormStep implements Step { 26 private List _params = new ArrayList (); 27 private String _className; 28 29 public FormStep() { 30 } 31 32 35 public String getName() { 36 return ClassUtils.getShortClassName(getClass()); 37 } 38 39 public void setClass(String className) { 40 _className = className; 41 } 42 43 46 public void execute(Result res) { 47 Object bean; 48 49 if (_className == null) { 50 bean = res.getContext().currentObject(); 51 assign(bean, res); 52 } else { 53 try { 54 bean = Class.forName(_className).newInstance(); 55 } catch (InstantiationException e) { 56 res.error(e); 57 58 return; 59 } catch (ClassNotFoundException e) { 60 res.error(e); 61 62 return; 63 } catch (IllegalAccessException e) { 64 res.error(e); 65 66 return; 67 } 68 69 assign(bean, res); 70 res.getContext().push(bean); 71 } 72 } 73 74 public Param createParam() { 75 Param p = new Param(); 76 _params.add(p); 77 78 return p; 79 } 80 81 private void assign(Object bean, Result res) { 82 Param p; 83 84 for (int i = 0; i < _params.size(); i++) { 85 p = (Param) _params.get(i); 86 87 try { 88 p.process(bean, res.getContext()); 89 } catch (InvocationTargetException e) { 90 res.error(e.getTargetException()); 91 92 return; 93 } catch (IllegalAccessException e) { 94 res.error(e); 95 96 return; 97 } 98 } 99 } 100 101 public static class Param { 102 static final String ON = "on"; 103 static final String TRUE = "true"; 104 static final String YES = "yes"; 105 static final Boolean BOOL_TRUE = new Boolean (true); 106 static final Boolean BOOL_FALSE = new Boolean (false); 107 String _from; 108 String _to; 109 String [] _scopes; 110 boolean _isBoolean; 111 112 public Param setBoolean(boolean bool) { 113 _isBoolean = bool; 114 115 return this; 116 } 117 118 public Param setFrom(String from) { 119 _from = from; 120 121 return this; 122 } 123 124 public Param setScopes(String scopes) { 125 _scopes = ScopeParser.parse(scopes); 126 127 return this; 128 } 129 130 public Param setTo(String to) { 131 _to = to; 132 133 return this; 134 } 135 136 void process(Object bean, Context ctx) 137 throws InvocationTargetException , IllegalAccessException , 138 IllegalStateException { 139 Object value; 140 141 if (_from == null) { 142 throw new IllegalStateException ("'from' attribute not specified"); 143 } 144 145 if (_to == null) { 146 throw new IllegalStateException ("'to' attribute not specified"); 147 } 148 149 if (_scopes == null) { 150 value = ctx.get(_from); 151 152 if (value != null) { 153 doAssign(bean, _to, value, _isBoolean); 154 } 155 } else { 156 value = ctx.get(_from, _scopes); 157 158 if (value != null) { 159 doAssign(bean, _to, value, _isBoolean); 160 } 161 } 162 } 163 164 private static void doAssign(Object bean, String to, Object val, boolean b) 165 throws InvocationTargetException , IllegalAccessException { 166 if (b) { 167 if (val != null) { 168 val = val.toString().toLowerCase(); 169 170 if (val.toString().equals(ON) || val.toString().equals(TRUE) || 171 val.toString().equals(YES)) { 172 BeanUtils.setProperty(bean, to, BOOL_TRUE); 173 } else { 174 BeanUtils.setProperty(bean, to, BOOL_FALSE); 175 } 176 } else { 177 BeanUtils.setProperty(bean, to, BOOL_FALSE); 178 } 179 } else { 180 BeanUtils.setProperty(bean, to, val); 181 } 182 } 183 } 184 } 185 | Popular Tags |