View Javadoc

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