View Javadoc

1   /*
2    * Copyright 2012 Vincent Demeester<vincent+shortbrain@demeester.fr>.
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
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, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package org.shortbrain.vaadin.container.annotation.reader;
17  
18  import java.lang.reflect.InvocationTargetException;
19  import java.util.HashMap;
20  import java.util.LinkedList;
21  import java.util.List;
22  import java.util.Map;
23  
24  import org.shortbrain.vaadin.container.annotation.Container;
25  import org.shortbrain.vaadin.container.annotation.ContainerType;
26  import org.shortbrain.vaadin.container.annotation.Property;
27  import org.shortbrain.vaadin.container.property.PropertyMetadata;
28  
29  /**
30   * Reader of Entity to generate containers metadatas
31   * 
32   * @author Vincent Demeester <vincent@demeester.fr>
33   * 
34   */
35  public class ContainerBeanAnnotationReader
36  		extends
37  		BeanAbstractAnnotationReader<Map<ContainerType, List<PropertyMetadata>>> {
38  
39  	private ContainerBeanAnnotationReader(Class<?> originalEntityClass,
40  			Class<?> entityClass) {
41  		super(originalEntityClass, entityClass);
42  		setMetadatas(new HashMap<ContainerType, List<PropertyMetadata>>());
43  	}
44  
45  	protected void process() throws IllegalAccessException,
46  			InvocationTargetException, NoSuchMethodException,
47  			InstantiationException, SecurityException, NoSuchFieldException {
48  		Container container = getBeanClass().getAnnotation(Container.class);
49  		for (Property property : container.properties()) {
50  			processProperty(property);
51  		}
52  	}
53  
54  	private void processProperty(Property property)
55  			throws IllegalAccessException, InvocationTargetException,
56  			NoSuchMethodException, InstantiationException, SecurityException,
57  			NoSuchFieldException {
58  		for (ContainerType containerType : property.types()) {
59  			if (!getMetadatas().containsKey(containerType)) {
60  				getMetadatas().put(containerType,
61  						new LinkedList<PropertyMetadata>());
62  			}
63  			String propertyName = property.name();
64  			String propertyAttribute = property.attribute();
65  			if (property.attribute().equals("")) {
66  				propertyAttribute = property.name();
67  			}
68  			Class<?> propertyClass = getPropertyType(propertyAttribute);
69  			PropertyMetadata propertyMetadata = new PropertyMetadata(
70  					propertyName, propertyClass, null, propertyAttribute);
71  			getMetadatas().get(containerType).add(propertyMetadata);
72  		}
73  	}
74  
75  	public static Map<ContainerType, List<PropertyMetadata>> getMetadataByContainerType(
76  			Class<?> originalEntityClass) throws IllegalAccessException,
77  			InvocationTargetException, NoSuchMethodException,
78  			InstantiationException, SecurityException, NoSuchFieldException {
79  		Map<ContainerType, List<PropertyMetadata>> ret = null;
80  		Class<?> entityClass = getAnnotatedClass(originalEntityClass,
81  				Container.class);
82  		if (entityClass != null) {
83  			ContainerBeanAnnotationReader reader = new ContainerBeanAnnotationReader(
84  					originalEntityClass, entityClass);
85  			reader.process();
86  			ret = reader.getMetadatas();
87  		}
88  		return ret;
89  	}
90  
91  }