View Javadoc

1   /*
2    * Copyright 2004-2009 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.ActionContext;
25  import org.seasar.cubby.action.ActionErrors;
26  import org.seasar.cubby.action.ActionResult;
27  import org.seasar.cubby.action.Validation;
28  
29  /**
30   * {@link Validation} アノテーションで指定されたエラーページへ遷移する {@link ValidationFailBehaviour}
31   * です。
32   * 
33   * @author baba
34   */
35  class ErrorPageValidationFailBehaviour implements ValidationFailBehaviour,
36  		Serializable {
37  
38  	/** シリアルバージョンUID。 */
39  	private static final long serialVersionUID = 1L;
40  
41  	/** メッセージ。 */
42  	private final String errorMessage;
43  
44  	/** フィールド名。 */
45  	private final String[] fieldNames;
46  
47  	/**
48  	 * インスタンス化します。
49  	 */
50  	public ErrorPageValidationFailBehaviour() {
51  		this(null);
52  	}
53  
54  	/**
55  	 * インスタンス化します。
56  	 * 
57  	 * @param errorMessage
58  	 *            メッセージ
59  	 * @param fieldNames
60  	 *            フィールド名
61  	 */
62  	public ErrorPageValidationFailBehaviour(final String errorMessage,
63  			final String... fieldNames) {
64  		this.errorMessage = errorMessage;
65  		this.fieldNames = fieldNames;
66  	}
67  
68  	/**
69  	 * {@inheritDoc}
70  	 */
71  	public ActionResult getValidationErrorActionResult(
72  			final ActionContext actionContext) {
73  		if (errorMessage != null && errorMessage.length() > 0) {
74  			final ActionErrors actionErrors = actionContext.getActionErrors();
75  			actionErrors.add(errorMessage, fieldNames);
76  		}
77  		final Object action = actionContext.getAction();
78  		final Method actionMethod = actionContext.getActionMethod();
79  		final Validation validation = getValidation(actionMethod);
80  		final ValidationRules validationRules = getValidationRules(action,
81  				validation.rules());
82  		return validationRules.fail(validation.errorPage());
83  	}
84  
85  }