1 package net.sourceforge.formview.util; 2 3 import java.util.Collection ; 4 import java.util.HashMap ; 5 import java.util.Iterator ; 6 import java.util.Map ; 7 8 import javax.servlet.ServletContext ; 9 import javax.servlet.ServletRequest ; 10 import javax.servlet.http.HttpServletRequest ; 11 12 import org.apache.struts.action.ActionErrors; 13 import org.apache.struts.action.ActionMessages; 14 15 import net.sourceforge.formview.FieldView; 16 import net.sourceforge.formview.FormView; 17 import net.sourceforge.formview.FormViewConstants; 18 import net.sourceforge.formview.FormViewResources; 19 import net.sourceforge.formview.permission.IPermissionsAdapter; 20 import net.sourceforge.formview.permission.RolesPermissionsAdapter; 21 22 28 public class WEBFormViewUtil { 29 30 public static boolean contextPathInitialized = false; 31 32 37 public static void saveFormViewResources(ServletContext context,FormViewResources resources) { 38 context.setAttribute(FormViewConstants.SERVLETCONTEXT_FORMVIEW_RESSOURCES_KEY , resources); 39 } 40 41 48 protected static FormViewResources getFormViewResources(ServletContext context) { 49 FormViewResources resources = (FormViewResources)context.getAttribute(FormViewConstants.SERVLETCONTEXT_FORMVIEW_RESSOURCES_KEY); 50 if (resources == null) { 51 throw new IllegalArgumentException ("Impossible to get FormViewResources into servlet context. Check if you have register FormViewResources into WEB Servlet Context with key \"net.sourceforge.formview.SERVLETCONTEXT_FORMVIEW_RESSOURCES_KEY\" (If you are in Struts check if you have intiliaze Plugin FormViewPlugin correctly)."); 52 } 53 return resources; 54 } 55 56 public static void saveState(ServletRequest request, ServletContext context, String state) { 57 saveState(request, context, state, FormViewConstants.MAP_FORMVIEW_DEFAULT_KEY); 58 } 59 60 66 public static void saveState(ServletRequest request, ServletContext context, String state, String subFormId) { 67 if (state != null && state.length() > 0) { 68 request.setAttribute(getREQUEST_FORMSTATE_KEY(subFormId), state); 70 } 71 initializeContextPath(request, context); 72 } 73 74 75 public static void saveFormView(ServletRequest request, ServletContext context, String formName) { 76 saveFormView(request, context, formName, FormViewConstants.MAP_FORMVIEW_DEFAULT_KEY); 77 } 78 79 86 public static void saveFormView(ServletRequest request, ServletContext context, String formName, String subFormId) { 87 FormView formView = getFormViewSaved(request, subFormId); 88 if (formView == null) { 89 FormViewResources resources = getFormViewResources(context); 90 String formState = (String )request.getAttribute(getREQUEST_FORMSTATE_KEY(subFormId)); 92 if (formName != null && formName.length() > 0) { 94 formView = resources.getFormView(formName, formState); 95 if (formView != null) { 96 Map formViewSavedMap = getFormViewSavedMap(request); 98 if (formViewSavedMap == null) { 99 formViewSavedMap = new HashMap (); 100 request.setAttribute(FormViewConstants.REQUEST_FORMVIEW_KEY, formViewSavedMap); 101 } 102 formViewSavedMap.put(subFormId, formView); 104 } 105 } 106 } 107 initializeContextPath(request, context); 108 109 } 110 111 public static FormView getFormViewSaved(ServletRequest request) { 112 return getFormViewSaved(request, FormViewConstants.MAP_FORMVIEW_DEFAULT_KEY); 113 } 114 115 public static FormView getFormViewSaved(ServletRequest request, String subFormId) { 116 Map formViewSavedMap = getFormViewSavedMap(request); 117 if (formViewSavedMap != null) 118 return (FormView)formViewSavedMap.get(subFormId); 119 return null; 120 } 121 122 public static Map getFormViewSavedMap(ServletRequest request) { 123 return (Map )request.getAttribute(FormViewConstants.REQUEST_FORMVIEW_KEY); 124 } 125 126 public static void saveFieldView(ServletRequest request, ServletContext context, String formName, FieldView fieldView) { 127 saveFieldView(request, context, formName, fieldView, FormViewConstants.MAP_FORMVIEW_DEFAULT_KEY); 128 } 129 130 131 public static void saveFieldView(ServletRequest request, ServletContext context, String formName, FieldView fieldView, String subFormId) { 132 FormView formView = getFormViewSaved(request, subFormId); 133 if (formView != null) { 134 formView.mergeFieldView(fieldView); 135 } 136 } 137 138 public static void savePermissionAdapter(ServletRequest request, IPermissionsAdapter permissionsAdapter) { 139 request.setAttribute(FormViewConstants.REQUEST_FORMPERMISSION_KEY, permissionsAdapter); 141 } 142 143 public static String processHtmlContent(ServletRequest request, ServletContext context, String htmlContent) 144 { 145 return processHtmlContent(request, context, htmlContent, FormViewConstants.MAP_FORMVIEW_DEFAULT_KEY); 146 } 147 148 149 public static String processHtmlContent(ServletRequest request, ServletContext context, String htmlContent, String subFormId) { 150 FormView form = getFormViewSaved(request, subFormId); 151 String state = (String )request.getAttribute(getREQUEST_FORMSTATE_KEY(subFormId)); 152 153 IPermissionsAdapter permissionsAdapter = (IPermissionsAdapter)request.getAttribute(FormViewConstants.REQUEST_FORMPERMISSION_KEY); 155 if (permissionsAdapter == null) { 156 if (request instanceof HttpServletRequest ) 157 permissionsAdapter = new RolesPermissionsAdapter((HttpServletRequest )request); 158 } 159 FormViewResources resources = getFormViewResources(context); 160 return resources.processHtmlContent(form, state, permissionsAdapter, htmlContent); 161 } 162 163 169 public static void addContextValue(ServletContext context, String key, String value) { 170 FormViewResources resources = getFormViewResources(context); 171 resources.addContextValue(key, value); 172 } 173 174 private static void initializeContextPath(ServletRequest request, ServletContext context) { 175 if (!contextPathInitialized) { 176 if (request instanceof HttpServletRequest ) { 177 HttpServletRequest httpRequest = (HttpServletRequest )request; 178 addContextValue(context, "contextPath", httpRequest.getContextPath()); 180 } 181 } 182 contextPathInitialized = true; 183 } 184 185 191 192 public static String getREQUEST_FORMSTATE_KEY(String subFormId) { 193 if (subFormId != null && subFormId.length() > 0) { 194 return FormViewConstants.REQUEST_FORMSTATE_KEY + "_" + subFormId; 195 } 196 return FormViewConstants.REQUEST_FORMSTATE_KEY; 197 } 198 199 public static void mergeFormViewWithStrutsActionErrors(ServletRequest request, ServletContext context) { 200 ActionMessages errors = (ActionMessages)request.getAttribute("org.apache.struts.action.ERROR"); 201 if (errors != null && !errors.isEmpty()) { 202 Map formViewSavedMap = getFormViewSavedMap(request); 203 if (formViewSavedMap != null) { 204 FormViewResources resources = getFormViewResources(context); 206 String errorBehaviour = resources.getErrorBehaviour(); 207 208 Collection formViewList = formViewSavedMap.values(); 210 for (Iterator iter = formViewList.iterator(); iter.hasNext();) { 211 FormView formView = (FormView) iter.next(); 212 mergeFormViewWithStrutsActionErrors(request, formView, errorBehaviour, errors); 213 } 214 } 215 } 216 } 217 218 public static void mergeFormViewWithStrutsActionErrors(ServletRequest request, FormView formView, String errorBehaviour, ActionMessages errors) { 219 if (formView != null && errors != null) { 221 boolean isFirstFieldNameWithErrorInitialized = false; 224 for (Iterator iter = errors.properties(); iter.hasNext();) { 225 String currentFieldName = (String ) iter.next(); 226 if (currentFieldName != null && 227 !currentFieldName.equals(ActionMessages.GLOBAL_MESSAGE) && 228 !currentFieldName.equals(ActionErrors.GLOBAL_ERROR) && 229 !currentFieldName.equals("")) { 230 FieldView fieldViewWithError = formView.getField(currentFieldName); 232 if (fieldViewWithError == null) { 233 fieldViewWithError = new FieldView(); 234 fieldViewWithError.setProperty(currentFieldName); 235 formView.addField(fieldViewWithError); 236 } 237 fieldViewWithError.setBehaviour(errorBehaviour); 238 239 if (!isFirstFieldNameWithErrorInitialized) { 240 isFirstFieldNameWithErrorInitialized = true; 241 saveFirstFieldWithError(currentFieldName, new Integer (0), request); 242 } 243 } 244 } 245 } 246 } 247 248 public static void setMergeFormViewWithStrutsActionErrors(ServletRequest request, boolean merge) { 249 request.setAttribute(FormViewConstants.MERGE_WITH_STRUTS_ACTIONERRORS , new Boolean (merge)); 250 } 251 252 public static boolean isMergeFormViewWithStrutsActionErrors(ServletRequest request) { 253 Boolean merge = (Boolean )request.getAttribute(FormViewConstants.MERGE_WITH_STRUTS_ACTIONERRORS); 254 if (merge != null) 255 return merge.booleanValue(); 256 return true; 257 } 258 259 public static void saveFirstFieldWithError(String fieldNameWithError, HttpServletRequest request) { 260 saveFirstFieldWithError(fieldNameWithError, new Integer (0), request); 261 } 262 263 public static void saveFirstFieldWithError(String fieldNameWithError, Integer fieldIndexWithError, ServletRequest request) { 264 if (request.getAttribute("firstFieldNameWithError") == null) { 266 request.setAttribute("firstFieldNameWithError", fieldNameWithError); 267 request.setAttribute("firstFieldIndexWithError", fieldIndexWithError); 268 } 269 } 270 } 271 | Popular Tags |