View Javadoc

1   /*
2    * Copyright 2004-2008 the Seasar Foundation and the Others.
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,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.cubby.validator;
17  
18  import static org.seasar.cubby.validator.ValidationUtils.getValidation;
19  import static org.seasar.cubby.validator.ValidationUtils.getValidationRules;
20  
21  import java.io.Serializable;
22  import java.lang.reflect.Method;
23  
24  import org.seasar.cubby.action.Action;
25  import org.seasar.cubby.action.ActionResult;
26  import org.seasar.cubby.action.Validation;
27  
28  class ErrorPageValidationFailBehaviour implements
29  		ValidationFailBehaviour, Serializable {
30  
31  	/** シリアルバージョンUID。 */
32  	private static final long serialVersionUID = 1L;
33  
34  	/** メッセージ。 */
35  	private final String errorMessage;
36  
37  	/** フィールド名。 */
38  	private final String[] fieldNames;
39  
40  	/**
41  	 * インスタンス化します。
42  	 */
43  	public ErrorPageValidationFailBehaviour() {
44  		this(null);
45  	}
46  
47  	/**
48  	 * インスタンス化します。
49  	 * 
50  	 * @param errorMessage
51  	 *            メッセージ
52  	 * @param fieldNames
53  	 *            フィールド名
54  	 */
55  	public ErrorPageValidationFailBehaviour(final String errorMessage,
56  			final String... fieldNames) {
57  		this.errorMessage = errorMessage;
58  		this.fieldNames = fieldNames;
59  	}
60  
61  	/**
62  	 * {@inheritDoc}
63  	 */
64  	public ActionResult getActionResult(final Action action, final Method method) {
65  		if (errorMessage != null && errorMessage.length() > 0) {
66  			action.getErrors().add(errorMessage, fieldNames);
67  		}
68  		final String errorPage;
69  		final Validation validation = getValidation(method);
70  		if (validation == null) {
71  			errorPage = null;
72  		} else {
73  			errorPage = validation.errorPage();
74  		}
75  
76  		final ValidationRules validationRules = getValidationRules(action,
77  				validation.rules());
78  		return validationRules.fail(errorPage);
79  	}
80  
81  }