View Javadoc
1   /**************************************************************************
2    Copyright 2005 Webstersmalley
3   
4    Licensed under the Apache License, Version 2.0 (the "License");
5    you may not use this file except in compliance with the License.
6    You may obtain a copy of the License at
7   
8    http://www.apache.org/licenses/LICENSE-2.0
9   
10   Unless required by applicable law or agreed to in writing, software
11   distributed under the License is distributed on an "AS IS" BASIS,
12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   See the License for the specific language governing permissions and
14   limitations under the License.
15   *************************************************************************/
16  /*
17   * Created on 08-Aug-2005
18   */
19  package com.webstersmalley.chessweb.web;
20  
21  import javax.servlet.ServletException;
22  import javax.servlet.http.HttpServletRequest;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.springframework.web.servlet.ModelAndView;
27  import org.springframework.web.servlet.mvc.SimpleFormController;
28  import org.springframework.web.servlet.view.RedirectView;
29  
30  import com.webstersmalley.chessweb.model.Board;
31  import com.webstersmalley.chessweb.model.InvalidMoveException;
32  import com.webstersmalley.chessweb.model.Move;
33  import com.webstersmalley.chessweb.model.Position;
34  
35  /***
36   * Controller for the move form.
37   * 
38   * @author Matthew Smalley
39   */
40  public final class MoveFormController extends SimpleFormController {
41      /*** Logger for this class and subclasses. */
42      private final Log logger = LogFactory.getLog(getClass());
43  
44      /*** The board. Instantiated by Spring */
45      private Board board;
46  
47      /***
48       * Handles the form submission.
49       * 
50       * @param command
51       *            the form command
52       * @return the model/view
53       * @throws ServletException
54       *             if the move is invalid etc.
55       * @overrides 
56       *   org.springframework.web.servlet.mvc.SimpleFormController.onSubmit
57       */
58      public ModelAndView onSubmit(final Object command) throws ServletException {
59          logger.info("Moving: " + command);
60          try {
61              board.movePiece((Move) command);
62          } catch (InvalidMoveException e) {
63              throw new ServletException(e);
64          }
65          return new ModelAndView(new RedirectView(getSuccessView()));
66      }
67  
68      /***
69       * Creates a form backing object (a Move).
70       * @param request the http request.
71       * @return the form backing object.
72       * @throws ServletException if there's any problems.
73       */
74      protected Object formBackingObject(final HttpServletRequest request)
75              throws ServletException {
76          Move move = new Move(new Position(0, 0), new Position(0, 0));
77          return move;
78      }
79  
80      /***
81       * @param board
82       *            The board to set.
83       */
84      public void setBoard(final Board board) {
85          this.board = board;
86      }
87  
88  }