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.dxo.impl;
17  
18  import java.lang.reflect.Method;
19  import java.util.Map;
20  
21  import org.seasar.cubby.dxo.FormDxo;
22  import org.seasar.cubby.tags.FormTag;
23  import org.seasar.extension.dxo.annotation.AnnotationReader;
24  import org.seasar.extension.dxo.command.DxoCommand;
25  import org.seasar.extension.dxo.converter.Converter;
26  import org.seasar.extension.dxo.converter.ConverterFactory;
27  import org.seasar.framework.container.annotation.tiger.InitMethod;
28  import org.seasar.framework.util.ClassUtil;
29  
30  /**
31   * リクエストのパラメータとアクションのプロパティとを変換するDXOです。
32   * 
33   * @author baba
34   * @since 1.0.0
35   */
36  public class FormDxoImpl implements FormDxo {
37  
38  	/** {@link Converter コンバータ}のファクトリクラス。 */
39  	private ConverterFactory converterFactory;
40  
41  	/** Dxoからアノテーションを読み取るためのインタフェース。 */
42  	private AnnotationReader annotationReader;
43  
44  	/** リクエストのパラメータからフォームオブジェクトへ変換するコマンド。 */
45  	private DxoCommand objectArrayMapToBeanDxoCommand;
46  
47  	/** フォームオブジェクトからリクエストのパラメータへ変換するコマンド。 */
48  	private DxoCommand beanToStringArrayMapDxoCommand;
49  
50  	/**
51  	 * {@link Converter コンバータ}のファクトリクラスを設定します。
52  	 * 
53  	 * @param converterFactory
54  	 *            {@link Converter コンバータ}のファクトリクラス
55  	 */
56  	public void setConverterFactory(final ConverterFactory converterFactory) {
57  		this.converterFactory = converterFactory;
58  	}
59  
60  	/**
61  	 * Dxoからアノテーションを読み取るためのインタフェースを設定します。
62  	 * 
63  	 * @param annotationReader
64  	 *            Dxoからアノテーションを読み取るためのインタフェース
65  	 */
66  	public void setAnnotationReader(final AnnotationReader annotationReader) {
67  		this.annotationReader = annotationReader;
68  	}
69  
70  	/**
71  	 * このインスタンスを初期化します。
72  	 */
73  	@InitMethod
74  	public void initialize() {
75  		final Class<?> targetClass = this.getClass();
76  
77  		final Method objectArrayMapToBeanConvertMethod = ClassUtil.getMethod(
78  				targetClass, "convert", new Class<?>[] { Map.class,
79  						Object.class });
80  		this.objectArrayMapToBeanDxoCommand = createObjectArrayMapToBeanDxoCommand(
81  				targetClass, objectArrayMapToBeanConvertMethod);
82  
83  		final Method beanToStringArrayMapConvertMethod = ClassUtil.getMethod(
84  				targetClass, "convert", new Class<?>[] { Object.class,
85  						Map.class });
86  		this.beanToStringArrayMapDxoCommand = createBeanToStringArrayMapDxoCommand(
87  				targetClass, beanToStringArrayMapConvertMethod);
88  	}
89  
90  	/**
91  	 * リクエストパラメータの{@link Map}の値を変換してフォームオブジェクトへ設定します。
92  	 * 
93  	 * @param src
94  	 *            変換元
95  	 * @param dest
96  	 *            変換先
97  	 */
98  	public void convert(final Map<String, Object[]> src, final Object dest) {
99  		if (src != null) {
100 			objectArrayMapToBeanDxoCommand.execute(new Object[] { src, dest });
101 		}
102 	}
103 
104 	/**
105 	 * フォームオブジェクトの値を変換して{@link FormTag}で使用する{@link Map}へ設定します。
106 	 * 
107 	 * @param src
108 	 *            変換元
109 	 * @param dest
110 	 *            変換先
111 	 */
112 	public void convert(final Object src, final Map<String, String[]> dest) {
113 		if (src != null) {
114 			beanToStringArrayMapDxoCommand.execute(new Object[] { src, dest });
115 		}
116 	}
117 
118 	@SuppressWarnings("unchecked")
119 	private DxoCommand createObjectArrayMapToBeanDxoCommand(
120 			final Class dxoClass, final Method method) {
121 		return new ObjectArrayMapToBeanDxoCommand(dxoClass, method,
122 				converterFactory, annotationReader, Object.class);
123 	}
124 
125 	@SuppressWarnings("unchecked")
126 	private DxoCommand createBeanToStringArrayMapDxoCommand(
127 			final Class dxoClass, final Method method) {
128 		final String expression = annotationReader.getConversionRule(dxoClass,
129 				method);
130 		return new BeanToStringArrayMapDxoCommand(dxoClass, method,
131 				converterFactory, annotationReader, expression);
132 	}
133 
134 }