View Javadoc

1   /*******************************************************************************
2    *  Copyright (c) 2005, 2006 Imola Informatica.
3    *  All rights reserved. This program and the accompanying materials
4    *  are made available under the terms of the LGPL License v2.1
5    *  which accompanies this distribution, and is available at
6    *  http://www.gnu.org/licenses/lgpl.html
7    *******************************************************************************/
8   package it.imolinfo.jbi4cics.messageformat;
9   
10  import it.imolinfo.jbi4cics.Logger;
11  import it.imolinfo.jbi4cics.LoggerFactory;
12  import it.imolinfo.jbi4cics.exception.FormatException;
13  import it.imolinfo.jbi4cics.jbi.Messages;
14  
15  import java.util.ArrayList;
16  import java.util.Arrays;
17  import java.util.Iterator;
18  import java.util.List;
19  import java.util.Map;
20  
21  import org.apache.commons.beanutils.DynaProperty;
22  import org.apache.commons.beanutils.WrapDynaClass;
23  import org.apache.commons.collections.map.ListOrderedMap;
24  
25  public class NoOpMappingDescriptor implements MappingDescriptor {
26      
27  	/**
28  	 * The logger for this class and its instances.
29  	 */
30  	  private static final Logger LOG
31  	          = LoggerFactory.getLogger(NoOpMappingDescriptor.class);
32  	  
33  	/**
34  	 * The responsible to translate localized messages.
35  	 */
36  	  private static final Messages MESSAGES
37  	          = Messages.getMessages(NoOpMappingDescriptor.class);
38  	
39    private Class beanClass;
40    
41    public NoOpMappingDescriptor() {
42      super();
43      // TODO Auto-generated constructor stub
44    }
45  
46    public void addFieldMapping(String propertyName, String fieldName, FieldDescriptor fieldDescriptor) throws FormatException {
47      throw new FormatException(MESSAGES.getString("CIC001600_Error_adding_field_mapping")); 
48    }
49  
50    public void addFieldMapping(String propertyName, Integer fieldIndex, FieldDescriptor fieldDescriptor) throws FormatException {
51      throw new FormatException(MESSAGES.getString("CIC001600_Error_adding_field_mapping")); 
52    }
53  
54    public Map<String, FieldDescriptor> getFieldMap() throws FormatException {
55      //TODO beanClass.
56      WrapDynaClass dynaClass=WrapDynaClass.createDynaClass(beanClass);
57      WrapDynaClass dynaSuperClass=WrapDynaClass.createDynaClass(beanClass.getSuperclass());
58      DynaProperty[] superClassProperties=dynaSuperClass.getDynaProperties();
59      DynaProperty[] properties=dynaClass.getDynaProperties();
60      LOG.debug("class property: "+Arrays.toString(properties));
61      LOG.debug("super class property: "+Arrays.toString(superClassProperties));
62      List<DynaProperty> superClassPropertyList=Arrays.asList(superClassProperties);
63      List<DynaProperty> classPropertyList=Arrays.asList(properties);
64  
65      List<DynaProperty> classPropertyListCopy=new ArrayList<DynaProperty>();
66      for (Iterator<DynaProperty> i=classPropertyList.iterator();i.hasNext();){
67        DynaProperty property=i.next();
68        
69        for (Iterator<DynaProperty> j=superClassPropertyList.iterator();j.hasNext();){
70          DynaProperty superClassProperty=j.next();
71          if (!(property.getName().equals(superClassProperty.getName()) && property.getType().equals(superClassProperty.getType()))){
72            classPropertyListCopy.add(property);
73          }
74        }
75      }
76      LOG.debug("remained properties: "+classPropertyListCopy);
77      Map<String, FieldDescriptor> fieldMap=new ListOrderedMap();
78      for (Iterator<DynaProperty> i=classPropertyListCopy.iterator();i.hasNext();){
79        final DynaProperty property=i.next();
80        LOG.debug("adding property: "+property.getName());
81        fieldMap.put(property.getName(),new FieldDescriptor(){
82          public Class getPreferredJavaType(){
83            return property.getType();
84          }
85        });
86      }
87      return fieldMap;
88    }
89  
90    public void setBeanClass(Class beanClass) {
91      this.beanClass=beanClass;
92    }
93  
94    public Class getBeanClass() {
95      return beanClass;
96    }
97  
98    public void setCodePage(String codePage) {
99      // TODO Auto-generated method stub
100     
101   }
102 
103 }