View Javadoc
1   /*
2    * Copyright 2012 Olivier Godineau
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * the License at http://www.apache.org/licenses/LICENSE-2.0
7    * 
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11   * License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  package olg.csv.bean.impl;
15  
16  import java.lang.reflect.InvocationTargetException;
17  import java.lang.reflect.Method;
18  
19  import olg.csv.base.Row;
20  import olg.csv.bean.IBeanProcessor;
21  import olg.csv.bean.IPropertyProcessor;
22  import olg.csv.bean.getter.AbstractGetter;
23  import olg.csv.bean.parser.AbstractParser;
24  import olg.csv.bean.parser.ParseException;
25  
26  /**
27   * In reading process, Class responsible of bean properties instantiation from
28   * list of Strings. This class seems to be the same as {@link BeanProcessor}
29   * class. But its motivation is different : We want to instantiate a
30   * characteristic of a bean while Transformer allows to product bean completed
31   * with its properties set by PropertyTransformers
32   * 
33   * @author Olivier Godineau
34   * 
35   * @param <B>
36   *            dedicated bean the Propertytransformer deals with
37   * 
38   */
39  public abstract class AbstractPropertyProcessor<B> implements IPropertyProcessor<B> {
40  
41  	/**
42  	 * The class of the bean this transformer deals with.
43  	 */
44  	protected Class<B> beanClass = null;
45  
46  	/**
47  	 * the method used to set the property of a &lt;B&gt; bean.
48  	 */
49  	protected Method method = null;
50  
51  	/**
52  	 * Returns the class of the bean this transformer deals with.
53  	 * 
54  	 * @return the bean class.
55  	 */
56  	public final Class<B> getBeanClass() {
57  		return beanClass;
58  	}
59  
60  	/*
61  	 * (non-Javadoc)
62  	 * 
63  	 * @see olg.csv.bean.impl.IPropertyProcessor#transform(olg.csv.base.Row, B)
64  	 */
65  	public abstract B transform(Row line, B bean);
66  
67  	/**
68  	 * allows to set field of a bean.
69  	 * 
70  	 * @param field
71  	 *            the value of the field
72  	 * @param bean
73  	 *            the bean to set its field
74  	 * @return the bean, if B argument is <code>null</code> return a new bean
75  	 *         with its field setted.
76  	 */
77  	protected final B invoke(Object field, B bean) {
78  		B retour = bean;
79  
80  		try { // On remplit le champ du bean
81  			if (retour == null) {
82  				retour = beanClass.newInstance();
83  			}
84  
85  			method.invoke(retour, new Object[]{field});
86  
87  		} catch (IllegalArgumentException e) {
88  			throw new ParseException("Error on method invocation  " + method.getName(), e);
89  		} catch (IllegalAccessException e) {
90  			throw new ParseException("Error on method invocation  " + method.getName(), e);
91  		} catch (InvocationTargetException e) {
92  			throw new ParseException("Error on method invocation  " + method.getName(), e);
93  		} catch (InstantiationException e) {
94  			throw new ParseException("Error on method invocation  " + method.getName(), e);
95  
96  		}
97  
98  		return retour;
99  	}
100 
101 	/**
102 	 * Returns a simple property processor which set a property of bean with the
103 	 * help of a getter and a parser. Typically the case when a property matches
104 	 * a string from a list of Strings.
105 	 * 
106 	 * @param getter
107 	 *            the getter. Allows to extract a sting corresponding to the
108 	 *            property to set. Must be not <code>null</code>.
109 	 * @param parser
110 	 *            the parser. Allows to instantiate the property from a String.
111 	 *            Must be not <code>null</code>.
112 	 * @param propertyName
113 	 *            the name of the property to set. Must be not <code>null</code>
114 	 *            .
115 	 * @param clazz
116 	 *            the bean class. Must be not <code>null</code>.
117 	 * @param <T>
118 	 *            the bean type
119 	 * @param <X>
120 	 *            the property type
121 	 * @return new instance of an basic property processor.
122 	 * 
123 	 * @see #transform(Row, Object)
124 	 */
125 
126 	public static final <T, X> IPropertyProcessor<T> getSimplePropertyTransformer(AbstractGetter getter,
127 			AbstractParser<X> parser, String propertyName, Class<T> clazz) {
128 		if (parser == null) {
129 			throw new IllegalArgumentException("getSimplePropertyTransformer: "
130 					+ "AbstractParser argument must be not null");
131 		}
132 		if (propertyName == null) {
133 			throw new IllegalArgumentException("getSimplePropertyTransformer: propertyName argument must be not null");
134 		}
135 		if (clazz == null) {
136 			throw new IllegalArgumentException("getSimplePropertyTransformer: Class argument must be not null");
137 		}
138 
139 		if (getter == null) {
140 			throw new IllegalArgumentException("getSimplePropertyTransformer: "
141 					+ "AbstractGetter argument must be not null");
142 		}
143 		return new PropertyProcessor<T>(getter, parser, propertyName, clazz);
144 	}
145 
146 	/**
147 	 * Returns a property processor which set a property of a bean with the help
148 	 * of a transformer. Typically the case where we instanciate a bean from a
149 	 * list of Strings we want to set as a property of our final bean
150 	 * 
151 	 * @param transformer
152 	 *            the transformer which products the value of the property to
153 	 *            set. Must not be <code>null</code>.
154 	 * @param beanClass
155 	 *            the class of the bean to return. Must not be <code>null</code>
156 	 *            .
157 	 * @param propertyName
158 	 *            the name of the property to set. Must not be <code>null</code>
159 	 *            .
160 	 * @param <T>
161 	 *            the bean type
162 	 * @param <P>
163 	 *            th property type
164 	 * @return new instance
165 	 */
166 	public static final <T, P> IPropertyProcessor<T> getComplexPropertyTransformer(IBeanProcessor<P> transformer,
167 			Class<T> beanClass, String propertyName) {
168 		if (transformer == null) {
169 			throw new IllegalArgumentException("Transformer argument must be not null");
170 		}
171 		if (beanClass == null) {
172 			throw new IllegalArgumentException("Class argument must be not null");
173 		}
174 		if (propertyName == null) {
175 			throw new IllegalArgumentException("propertyName argument must be not null");
176 		}
177 		return new ComplexPropertyProcessor<T>(transformer, beanClass, propertyName);
178 	}
179 
180 }