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.extension.dxo.annotation.AnnotationReader;
23  import org.seasar.extension.dxo.annotation.AnnotationReaderFactory;
24  import org.seasar.extension.dxo.command.DxoCommand;
25  import org.seasar.extension.dxo.converter.ConverterFactory;
26  import org.seasar.framework.container.annotation.tiger.InitMethod;
27  import org.seasar.framework.util.ClassUtil;
28  
29  /**
30   * 
31   * @author baba
32   *
33   */
34  public class FormDxoImpl implements FormDxo {
35  
36  	private ConverterFactory converterFactory;
37  
38  	private AnnotationReaderFactory annotationReaderFactory;
39  
40  	private DxoCommand objectArrayMapToBeanDxoCommand;
41  
42  	private DxoCommand beanToStringArrayMapDxoCommand;
43  
44  	public void setConverterFactory(final ConverterFactory converterFactory) {
45  		this.converterFactory = converterFactory;
46  	}
47  
48  	public void setAnnotationReaderFactory(
49  			final AnnotationReaderFactory annotationReaderFactory) {
50  		this.annotationReaderFactory = annotationReaderFactory;
51  	}
52  
53  	@InitMethod
54  	public void initialize() {
55  		final Class<?> targetClass = this.getClass();
56  
57  		final Method objectArrayMapToBeanConvertMethod = ClassUtil.getMethod(
58  				targetClass, "convert", new Class<?>[] { Map.class,
59  						Object.class });
60  		this.objectArrayMapToBeanDxoCommand = createObjectArrayMapToBeanDxoCommand(
61  				targetClass, objectArrayMapToBeanConvertMethod);
62  
63  		final Method beanToStringArrayMapConvertMethod = ClassUtil.getMethod(
64  				targetClass, "convert", new Class<?>[] { Object.class,
65  						Map.class });
66  		this.beanToStringArrayMapDxoCommand = createBeanToStringArrayMapDxoCommand(
67  				targetClass, beanToStringArrayMapConvertMethod);
68  	}
69  
70  	public void convert(final Map<String, Object[]> src, final Object dest) {
71  		if (src != null) {
72  			objectArrayMapToBeanDxoCommand.execute(new Object[] { src, dest });
73  		}
74  	}
75  
76  	public void convert(final Object src, final Map<String, String[]> dest) {
77  		if (src != null) {
78  			beanToStringArrayMapDxoCommand.execute(new Object[] { src, dest });
79  		}
80  	}
81  
82  	@SuppressWarnings("unchecked")
83  	public DxoCommand createObjectArrayMapToBeanDxoCommand(
84  			final Class dxoClass, final Method method) {
85  		final AnnotationReader annotationReader = annotationReaderFactory
86  				.getAnnotationReader();
87  		return new ObjectArrayMapToBeanDxoCommand(dxoClass, method,
88  				converterFactory, annotationReader, Object.class);
89  	}
90  
91  	@SuppressWarnings("unchecked")
92  	public DxoCommand createBeanToStringArrayMapDxoCommand(
93  			final Class dxoClass, final Method method) {
94  		final AnnotationReader annotationReader = annotationReaderFactory
95  				.getAnnotationReader();
96  		final String expression = annotationReader.getConversionRule(dxoClass,
97  				method);
98  		return new BeanToStringArrayMapDxoCommand(dxoClass, method,
99  				converterFactory, annotationReader, expression);
100 	}
101 
102 }