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.CubbyConstants.ATTR_CONVERSION_FAILURES;
20  
21  import java.util.List;
22  import java.util.Map;
23  
24  import javax.servlet.http.HttpServletRequest;
25  
26  import org.seasar.cubby.action.ActionErrors;
27  import org.seasar.cubby.action.FieldInfo;
28  import org.seasar.cubby.action.MessageInfo;
29  import org.seasar.cubby.internal.controller.ConversionFailure;
30  import org.seasar.cubby.internal.controller.ThreadContext;
31  import org.seasar.cubby.internal.util.RequestUtils;
32  
33  /**
34   * 要求パラメータをフォームオブジェクトへのバインドする時の型変換エラーを検証する入力検証ルールです。
35   * 
36   * @author baba
37   */
38  public class ConversionValidationRule implements ValidationRule {
39  
40  	/** リソースのキープレフィックス。 */
41  	private final String resourceKeyPrefix;
42  
43  	/**
44  	 * キーのプレフィックスなしでインスタンス化します。
45  	 */
46  	public ConversionValidationRule() {
47  		this(null);
48  	}
49  
50  	/**
51  	 * 指定されたプレフィックスをフィールド名のキーのプレフィックスとしてインスタンス化します。
52  	 * 
53  	 * @param resourceKeyPrefix
54  	 *            リソースのキープレフィックス
55  	 */
56  	public ConversionValidationRule(final String resourceKeyPrefix) {
57  		this.resourceKeyPrefix = resourceKeyPrefix;
58  	}
59  
60  	/**
61  	 * {@inheritDoc}
62  	 */
63  	public void apply(final Map<String, Object[]> params, final Object form,
64  			final ActionErrors errors) throws ValidationException {
65  		final ThreadContext currentContext = ThreadContext.getCurrentContext();
66  		final HttpServletRequest request = currentContext.getRequest();
67  		final List<ConversionFailure> conversionFailures = RequestUtils
68  				.getAttribute(request, ATTR_CONVERSION_FAILURES);
69  		if (conversionFailures != null && !conversionFailures.isEmpty()) {
70  			for (final ConversionFailure conversionFailure : conversionFailures) {
71  				final MessageInfo messageInfo = conversionFailure
72  						.getMessageInfo();
73  				final String fieldNameKey;
74  				if (resourceKeyPrefix == null) {
75  					fieldNameKey = conversionFailure.getFieldName();
76  				} else {
77  					fieldNameKey = resourceKeyPrefix
78  							+ conversionFailure.getFieldName();
79  				}
80  				final String message = messageInfo.toMessage(fieldNameKey);
81  				final FieldInfo[] fieldInfos = conversionFailure
82  						.getFieldInfos();
83  				errors.add(message, fieldInfos);
84  			}
85  		}
86  	}
87  
88  }