1 15 16 package com.jdon.strutsutil; 17 18 import org.apache.struts.action.ActionMapping; 19 import org.apache.struts.action.ActionForm; 20 import org.apache.struts.action.ActionErrors; 21 import javax.servlet.http.HttpServletRequest ; 22 23 import javax.servlet.http.HttpSession ; 24 import com.jdon.controller.events.EventModel; 25 import org.apache.commons.beanutils.PropertyUtils; 26 import com.jdon.util.Debug; 27 import com.jdon.controller.events.EventSupport; 28 import java.security.Principal ; 29 import com.jdon.controller.model.ModelIF; 30 import com.jdon.model.ModelForm; 31 import org.apache.struts.config.ModuleConfig; 32 import org.apache.struts.config.FormBeanConfig; 33 import org.apache.struts.util.ModuleUtils; 34 import org.apache.struts.action.ActionMessages; 35 import org.apache.struts.action.ActionMessage; 36 37 45 public final class FormBeanUtil { 46 47 public final static String module = FormBeanUtil.class.getName(); 48 49 private final static ModuleUtils moduleUtils = ModuleUtils.getInstance(); 50 51 public final static String FORWARD_SUCCESS_NAME = "success"; 52 53 public final static String FORWARD_FAILURE_NAME = "failure"; 54 55 61 public static void saveActionForm(ActionForm form, ActionMapping mapping, 62 HttpServletRequest request) { 63 if ((form != null) && (mapping.getAttribute() != null)) { 64 if ("request".equals(mapping.getScope())) { 65 request.setAttribute(mapping.getAttribute(), form); 66 } else { 67 HttpSession session = request.getSession(); 68 session.setAttribute(mapping.getAttribute(), form); 69 request.setAttribute(mapping.getAttribute(), form); 70 } 71 } 72 } 73 74 80 public static ActionForm loadActionForm(ActionMapping mapping, 81 HttpServletRequest request) { 82 if ("request".equals(mapping.getScope())) { 83 return (ActionForm) request.getAttribute(mapping.getAttribute()); 84 } else { 85 HttpSession session = request.getSession(); 86 return (ActionForm) session.getAttribute(mapping.getAttribute()); 87 } 88 } 89 90 95 public static ActionForm lookupActionForm(HttpServletRequest request, 96 String formName) { 97 ActionForm actionForm = null; 98 actionForm = (ActionForm) request.getAttribute(formName); 99 if (actionForm == null) { 100 HttpSession session = request.getSession(); 101 actionForm = (ActionForm) session.getAttribute(formName); 102 } 103 return actionForm; 104 } 105 106 111 public static void removeActionForm(ActionMapping mapping, 112 HttpServletRequest request) { 113 if (mapping.getAttribute() != null) { 114 if ("request".equals(mapping.getScope())) 115 request.removeAttribute(mapping.getAttribute()); 116 else { 117 HttpSession session = request.getSession(); 118 session.removeAttribute(mapping.getAttribute()); 119 request.removeAttribute(mapping.getAttribute()); 120 } 121 } 122 } 123 124 public static String getFormName(ActionMapping mapping) throws Exception { 125 String formName = "NoFormName Error!"; 126 if (mapping.getName() != null) 127 formName = mapping.getName(); 128 else if ((mapping.getAttribute() != null)) 129 formName = mapping.getAttribute(); 130 else 131 throw new Exception ("not found the actionForm name in action configure"); 132 return formName; 133 } 134 135 public static ModelForm getModelForm(ActionMapping actionMapping, 136 ActionForm actionForm, HttpServletRequest request) throws Exception { 137 138 if (actionForm == null) { 139 String msg = " not found the actionForm name in action configure"; 140 Debug.logError(msg); 141 throw new Exception (msg); 142 } 144 145 ModelForm modelForm = null; 146 try { 147 modelForm = (ModelForm) actionForm; 148 } catch (ClassCastException e) { 149 String msg = "your class:" + actionForm.getClass().getName() 150 + " isn't the subclass of com.jdon.model.ModelForm"; 151 Debug.logVerbose(msg, module); 152 throw new Exception (msg); 153 } 154 155 return modelForm; 156 } 157 158 167 private static ModelForm createModelFormNow(ActionMapping actionMapping, 168 ActionForm actionForm, HttpServletRequest request) throws Exception { 169 170 Debug.logVerbose( 171 "[JdonFramework] not found a existed ModelForm, create it now", 172 module); 173 ModuleConfig moduleConfig = moduleUtils.getModuleConfig(request, 174 request.getSession().getServletContext()); 175 ModelForm form = null; 176 String formName = null; 177 String formClass = null; 178 try { 179 formName = getFormName(actionMapping); 180 FormBeanConfig formConfig = moduleConfig.findFormBeanConfig(formName); 181 if (formConfig == null) { 182 throw new Exception (" not found config for " + formName); 183 } 184 formClass = formConfig.getType(); 185 186 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 187 form = (ModelForm) classLoader.loadClass(formClass).newInstance(); 188 189 String action = request.getParameter("action"); 190 if (action == null) 191 action = request.getParameter("method"); 192 form.setAction(action); 193 194 request.setAttribute(formName, form); 195 } catch (Exception ex) { 196 Debug.logError("[JdonFramework] formName:" + formName 197 + "formClass create error :" + formClass + ex, module); 198 } 199 return form; 200 } 201 202 public static boolean validateAction(String actionName, 203 ActionMapping mapping) { 204 boolean res = true; 205 int result = actionTransfer(actionName); if (result == 0) 207 res = false; 208 209 if (mapping.findForward(actionName) == null) res = false; 211 212 return res; 213 214 } 215 216 public static String getName(HttpServletRequest request) throws Exception { 217 Principal principal = request.getUserPrincipal(); 218 if (principal == null) { 219 Debug.logError("[JdonFramework] No Principal", module); 220 throw new Exception (" No Principal"); 221 } 222 return principal.getName(); 223 } 224 225 public static ActionErrors notNull(Object object, String errorsInfo) { 226 ActionErrors errors = new ActionErrors(); 227 String Id = (String ) object; 228 if (object == null) { 229 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage( 230 errorsInfo)); 231 } 232 return errors; 233 } 234 235 239 public static EventModel createEvent(ModelForm form, ModelIF model) 240 throws Exception { 241 EventModel em = new EventModel(); 242 try { 243 PropertyUtils.copyProperties(model, form); 244 em.setModelIF(model); 245 String action = form.getAction(); 246 em.setActionName(action); 247 em.setActionType(FormBeanUtil.actionTransfer(action)); 248 } catch (Exception ex) { 249 Debug.logError("[JdonFramework]create Event error:" + ex, module); 250 throw new Exception (ex); 251 } 252 return em; 253 } 254 255 public static int actionTransfer(String actionName) { 256 if (actionName.equalsIgnoreCase(ModelForm.CREATE_STR)) 257 return EventSupport.CREATE; 258 else if (actionName.equalsIgnoreCase(ModelForm.EDIT_STR)) 259 return EventSupport.EDIT; 260 else if (actionName.equalsIgnoreCase(ModelForm.UPDATE_STR)) 261 return EventSupport.EDIT; 262 else if (actionName.equalsIgnoreCase(ModelForm.DELETE_STR)) 263 return EventSupport.DELETE; 264 else 265 return 0; 266 } 267 268 } 269 | Popular Tags |