1 16 17 package org.apache.struts.chain; 18 19 20 import java.util.HashMap ; 21 import java.util.Iterator ; 22 import java.util.Map ; 23 import org.apache.commons.beanutils.BeanUtils; 24 import org.apache.commons.chain.Command; 25 import org.apache.commons.chain.Context; 26 import org.apache.commons.chain.web.WebContext; 27 import org.apache.struts.Globals; 28 import org.apache.struts.action.ActionForm; 29 import org.apache.struts.config.ActionConfig; 30 31 32 38 39 public abstract class AbstractPopulateActionForm implements Command { 40 41 42 44 45 private String actionConfigKey = Constants.ACTION_CONFIG_KEY; 46 private String actionFormKey = Constants.ACTION_FORM_KEY; 47 private String cancelKey = Constants.CANCEL_KEY; 48 49 50 52 53 58 public String getActionConfigKey() { 59 60 return (this.actionConfigKey); 61 62 } 63 64 65 72 public void setActionConfigKey(String actionConfigKey) { 73 74 this.actionConfigKey = actionConfigKey; 75 76 } 77 78 79 84 public String getActionFormKey() { 85 86 return (this.actionFormKey); 87 88 } 89 90 91 98 public void setActionFormKey(String actionFormKey) { 99 100 this.actionFormKey = actionFormKey; 101 102 } 103 104 105 109 public String getCancelKey() { 110 111 return (this.cancelKey); 112 113 } 114 115 116 122 public void setCancelKey(String cancelKey) { 123 124 this.cancelKey = cancelKey; 125 126 } 127 128 129 131 132 139 public boolean execute(Context context) throws Exception { 140 141 ActionForm actionForm = (ActionForm) 143 context.get(getActionFormKey()); 144 if (actionForm == null) { 145 return (false); 146 } 147 148 ActionConfig actionConfig = (ActionConfig) 150 context.get(getActionConfigKey()); 151 152 153 reset(context, actionConfig, actionForm); 154 155 populate(context, actionConfig, actionForm); 156 157 handleCancel(context, actionConfig, actionForm); 158 159 return (false); 160 161 } 162 163 164 166 173 protected abstract void reset(Context context, 174 ActionConfig actionConfig, 175 ActionForm actionForm); 176 177 178 193 protected void populate(Context context, 194 ActionConfig actionConfig, 195 ActionForm actionForm) throws Exception 196 { 197 WebContext wcontext = (WebContext) context; 198 Map paramValues = wcontext.getParamValues(); 199 Map parameters = new HashMap (); 200 201 String prefix = actionConfig.getPrefix(); 202 String suffix = actionConfig.getSuffix(); 203 204 Iterator keys = paramValues.keySet().iterator(); 205 while (keys.hasNext()) { 206 String name = (String ) keys.next(); 207 String stripped = name; 208 if (prefix != null) { 209 if (!stripped.startsWith(prefix)) { 210 continue; 211 } 212 stripped = stripped.substring(prefix.length()); 213 } 214 if (suffix != null) { 215 if (!stripped.endsWith(suffix)) { 216 continue; 217 } 218 stripped = 219 stripped.substring(0, stripped.length() - suffix.length()); 220 } 221 parameters.put(stripped, paramValues.get(name)); 222 } 223 BeanUtils.populate(actionForm, parameters); 224 } 225 226 227 protected void handleCancel(Context context, 228 ActionConfig actionConfig, 229 ActionForm actionForm) throws Exception 230 { 231 WebContext wcontext = (WebContext) context; 232 Map paramValues = wcontext.getParamValues(); 233 234 if ((paramValues.get(org.apache.struts.taglib.html.Constants.CANCEL_PROPERTY) != null) || 236 (paramValues.get(org.apache.struts.taglib.html.Constants.CANCEL_PROPERTY_X) != null)) { 237 context.put(getCancelKey(), Boolean.TRUE); 238 wcontext.getRequestScope().put(Globals.CANCEL_KEY, Boolean.TRUE); 239 } else { 240 context.put(getCancelKey(), Boolean.FALSE); 241 } 242 } 243 } 244 | Popular Tags |