[cvs] expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/core/controller Controller.java

JCorporate Ltd jcorp at jcorp2.servlets.net
Mon Jul 19 16:50:05 PDT 2004


Update of /home/javacorp/.cvs/expresso/expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/core/controller
In directory jcorp2.servlets.net:/tmp/cvs-serv29095

Modified Files:
	Controller.java 
Log Message:
added struts validator integration. see @todo tags


Index: Controller.java
===================================================================
RCS file: /home/javacorp/.cvs/expresso/expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/core/controller/Controller.java,v
retrieving revision 1.92
retrieving revision 1.93
diff -C2 -d -r1.92 -r1.93
*** Controller.java	8 Jul 2004 06:00:01 -0000	1.92
--- Controller.java	19 Jul 2004 23:50:03 -0000	1.93
***************
*** 1073,1077 ****
              //If the state's form is blank then use the controller's form - if it's of ControllerForm type
              if (stateFormClass.equals("")) {
!                 if (controllerForm instanceof ControllerForm) {
                      return (StateForm) controllerForm;
                  } else {
--- 1073,1078 ----
              //If the state's form is blank then use the controller's form - if it's of ControllerForm type
              if (stateFormClass.equals("")) {
!               /** @todo change instanceof ControllerForm for instanceof DefaultForm  *RD* Mon Jul 27 2004 */
!                 if (controllerForm instanceof DefaultForm) {
                      return (StateForm) controllerForm;
                  } else {
***************
*** 1128,1131 ****
--- 1129,1141 ----
          newState = StringUtil.notNull(newState);
  
+         /** @todo  added to help struts validator to find the state to go back to
+          * if an error is found in the form  *RD* Mon Jul 27 2004 */
+         PersistentSession mySession = myRequest.getSession();
+         mySession.removePersistentAttribute("previousState");
+         mySession.setPersistentAttribute("previousState", newState);
+         mySession.removePersistentAttribute("previousController");
+         mySession.setPersistentAttribute("previousController",
+                                      this.getClass().getName());
+        /** @todo end addition  *RD* Mon Jul 27 2004 */
  
          if (log.isDebugEnabled()) {
***************
*** 1582,1585 ****
--- 1592,1600 ----
          long beginTimer = System.currentTimeMillis();
  
+         /** @todo to avoid nullPointerExceptions we initialize here the error collection  *RD* Mon Jul 27 2004 */
+         /** @todo add this line  *RD* Mon Jul 27 2004 */
+         ErrorCollection actionErrors = new ErrorCollection();
+         /** @todo end addition  *RD* Mon Jul 27 2004 */
+ 
          // Call newState directly if we were called from a dispatch in Transition.
          ControllerRequest queuedRequest = (ControllerRequest) request.getAttribute(
***************
*** 1603,1611 ****
  
              // TODO: Why are we repopulating the bean? *PP* Tue Jan 27 11:19:15 GMT 2004
!             try {
                  BeanUtils.populate(form, queuedRequest.getParameters());
              } catch (Exception e) {
                  throw new ServletException(e);
!             }
  
              ControllerResponse queuedResponse = null;
--- 1618,1627 ----
  
              // TODO: Why are we repopulating the bean? *PP* Tue Jan 27 11:19:15 GMT 2004
!             /** @todo I commented out the code and nothing seems broken. The form is already populated *RD* Mon Jul 27 2004 */
!             /*try {
                  BeanUtils.populate(form, queuedRequest.getParameters());
              } catch (Exception e) {
                  throw new ServletException(e);
!             }*/
  
              ControllerResponse queuedResponse = null;
***************
*** 1613,1617 ****
              try {
                  queuedRequest.setFormAttribute(mapping.getAttribute());
!                 queuedResponse = newState(queuedState, queuedRequest);
              } catch (Exception e) {
                  request.setAttribute(ExpressoConstants.NEWSTATE_EXCEPTION_KEY, e);
--- 1629,1670 ----
              try {
                  queuedRequest.setFormAttribute(mapping.getAttribute());
!                 /** @todo test if this action must be automatically validated.
!                  * If it's the case validate the form.  *RD* Mon Jul 27 2004 */
!                 /** @todo replace
!                  * queuedResponse = newState(queuedState, queuedRequest);
!                  * with all this  *RD* Mon Jul 27 2004 */
!                 /** @todo start addition  *RD* Mon Jul 27 2004 */
!                 if (mapping.getValidate()) {
!                   actionErrors = (ErrorCollection) form.validate(mapping, request);
!                 }
!                 /** @todo test if there are no validation errors  *RD* Mon Jul 27 2004 */
!                 if (actionErrors.size() == 0) {
!                   /** @todo no errors: forward to the handle state  *RD* Mon Jul 27 2004 */
!                   queuedResponse = newState(queuedState, queuedRequest);
!                 }
!                 else {
!                   /** @todo errors: forward control to the prompt state  *RD* Mon Jul 27 2004 */
!                   /** @todo retrieve the previous controller and states parameters from the session  *RD* Mon Jul 27 2004 */
!                   String previousController = request.getSession().getAttribute(
!                       "previousController").toString();
!                   String previousState = request.getSession().getAttribute(
!                       "previousState").toString();
!                   /** @todo test if the prompt state belongs to this same controller  *RD* Mon Jul 27 2004 */
!                   if (previousController.equals(this.getClass().getName())) {
!                     queuedResponse = newState(StringUtil.notNull(previousState),
!                                       queuedRequest);
!                     queuedResponse.saveErrors(actionErrors);
!                   }
!                   else {
!                     /** @todo if not transition to the new controller  *RD* Mon Jul 27 2004 */
!                     /** @todo I guess there must be a cleaner way to do this  *RD* Mon Jul 27 2004 */
!                     Transition t = new Transition();
!                     t.setState(StringUtil.notNull(previousState));
!                     t.setControllerObject(StringUtil.notNull(previousController));
!                     t.transition(queuedRequest, new ControllerResponse());
!                   }
! 
!                 }
!                 /** @todo end addition  *RD* Mon Jul 27 2004 */
              } catch (Exception e) {
                  request.setAttribute(ExpressoConstants.NEWSTATE_EXCEPTION_KEY, e);
***************
*** 1851,1854 ****
--- 1904,1913 ----
  
              // TODO: Why are we setting the form bean again here? What are doing here in this code? *PP* Tue Jan 27 11:35:22 GMT 2004
+             /* BUG BUG
+                     the test if form instanceof DefaultForm should be done before
+                     BeanUtils.populate() otherwise the DefaultForm never gets populated
+                     This also fixes Input.setDefaultValue(ControllerResponse) which wasn't working
+              *RD* Mon Jul 27 2004
+ 
              try {
                  BeanUtils.populate(form, req.getParameters());
***************
*** 1865,1869 ****
--- 1924,1950 ----
                      throw new ServletException(e);
                  }
+                          }*/
+             /** @todo corrected this  *RD* Mon Jul 27 2004 */
+             /** @todo  we populate the DefaultForm with the contents of the HTML form  *RD* Mon Jul 27 2004 */
+             if (form instanceof DefaultForm) {
+               try {
+                 ((DefaultForm) form).setUsingHashtableParameters(req.getParameters());
+               }
+               catch (Exception ex) {
+                 ex.printStackTrace();
+                 throw new ServletException(ex);
+               }
              }
+             /** @todo I commented out the code and nothing seems broken. The form is already populated *RD* Mon Jul 27 2004 */
+             /*else {
+                      try {
+                        BeanUtils.populate(form, req.getParameters());
+                      }
+                      catch (Exception e) {
+                        log.error("Error populating form", e);
+                        throw new ServletException(e);
+                      }
+             }*/
+ 
  
              // Remove the last controller response from the request
***************
*** 1883,1887 ****
              ControllerResponse res = null;
              try {
!                 res = newState(StringUtil.notNull(requestedState), req);
              } finally {
                  if (form != null && "session".equals(mapping.getScope())) {
--- 1964,2005 ----
              ControllerResponse res = null;
              try {
!             /** @todo test if this action must be automatically validated.
!              * If it's the case validate the form.  *RD* Mon Jul 27 2004 */
!             /** @todo replace
!              * res = newState(StringUtil.notNull(previousState), req);
!              * with all this  *RD* Mon Jul 27 2004 */
! 
!             /** @todo start addition  *RD* Mon Jul 27 2004 */
!             if (mapping.getValidate()) {
!               actionErrors = (ErrorCollection) form.validate(mapping, request);
!             }
!             /** @todo test if there are no validation errors  *RD* Mon Jul 27 2004 */
!             if (actionErrors.size() == 0) {
!               /** @todo no errors: forward to the handle state  *RD* Mon Jul 27 2004 */
!               res = newState(StringUtil.notNull(requestedState), req);
!             }
!             else {
!               /** @todo errors: forward control to the prompt state  *RD* Mon Jul 27 2004 */
!               /** @todo retrieve the previous controller and states parameters from the session  *RD* Mon Jul 27 2004 */
!               String previousController = request.getSession().getAttribute(
!                   "previousController").toString();
!               String previousState = request.getSession().getAttribute(
!                   "previousState").toString();
!               /** @todo test if the prompt state belongs to this same controller  *RD* Mon Jul 27 2004 */
!               if (previousController.equals(this.getClass().getName())) {
!                 res = newState(StringUtil.notNull(previousState), req);
!                 res.saveErrors(actionErrors);
!               }
!               else {
!                 /** @todo if not transition to the new controller  *RD* Mon Jul 27 2004 */
!                 /** @todo I guess there must be a cleaner way to do this  *RD* Mon Jul 27 2004 */
!                 Transition t = new Transition();
!                 t.setState(StringUtil.notNull(previousState));
!                 t.setControllerObject(StringUtil.notNull(previousController));
!                 t.transition(req, new ControllerResponse());
!               }
!             }
!             /** @todo end addition  *RD* Mon Jul 27 2004 */
! 
              } finally {
                  if (form != null && "session".equals(mapping.getScope())) {



More information about the cvs mailing list