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
18
19 package com.webstersmalley.chessweb.model;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.springframework.validation.Errors;
24 import org.springframework.validation.Validator;
25
26 /***
27 * Validator class for a new move.
28 *
29 * @author Matthew Smalley
30 */
31 public final class MoveValidator implements Validator {
32
33 /*** Logger for the class. */
34 private final Log logger = LogFactory.getLog(getClass());
35
36 /*** The all important board. * */
37 private Board board;
38
39 /***
40 * @return Returns the board.
41 */
42 public Board getBoard() {
43 return board;
44 }
45
46 /***
47 * @param board
48 * The board to set.
49 */
50 public void setBoard(final Board board) {
51 this.board = board;
52 }
53
54 /***
55 * Checks which classes are supported by this validator. Only Move classes
56 * are supported.
57 *
58 * @param clazz
59 * the class to test.
60 * @return whether the class is supported.
61 */
62 public boolean supports(final Class clazz) {
63 return clazz.equals(Move.class);
64 }
65
66 /***
67 * Checks whether the move is valid or not.
68 *
69 * @param command
70 * the command object.
71 * @param errors
72 * the collection of errors.
73 */
74 public void validate(final Object command, final Errors errors) {
75 logger.info("Validating: " + command);
76 Move move = (Move) command;
77 if (move.getSource().equals(move.getDestination())) {
78 logger.info("Rejecting: source and destination are the same");
79 errors.rejectValue("destination",
80 "error.source-and-destination-same",
81 "Source and destination are the same.");
82 return;
83 }
84 Piece piece = null;
85 try {
86 piece = board.getAt(move.getSource());
87 } catch (NullPointerException e) {
88 logger.info("Rejecting: bogus notation");
89 errors.rejectValue("source", "error.bogus-notation",
90 "Error with the notation.");
91 return;
92 }
93 if (piece == null) {
94 logger.info("Rejecting: no piece at the source");
95 errors.rejectValue("source", "error.no-piece-at-source",
96 "No piece at the source.");
97 return;
98 }
99 try {
100 piece.validateMove(move, board);
101 } catch (InvalidMoveException e) {
102 logger.info("Error validating move: ", e);
103 errors.rejectValue("destination", "error.move-error", e
104 .getMessage());
105 }
106 }
107
108 }