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   
9   
10  package it.imolinfo.jbi4cics.messageformat.commarea;
11  
12  import it.imolinfo.jbi4cics.exception.FormatException;
13  import it.imolinfo.jbi4cics.messageformat.FieldDescriptor;
14  import it.imolinfo.jbi4cics.messageformat.MappingDescriptor;
15  import it.imolinfo.jbi4cics.typemapping.cobol.CobolTypeDescriptor;
16  import java.util.Collections;
17  import java.util.Map;
18  import org.apache.commons.collections.map.ListOrderedMap;
19  import org.apache.commons.lang.builder.ReflectionToStringBuilder;
20  
21  /**
22   * Questa classe fondamentalmente wrappa una mappa fra i nomi dei field nella
23   * commarea e i nomi delle properies di un java bean.
24   *
25   * @author raffaele
26   */
27  public class CommareaBeanMappingDescriptor implements MappingDescriptor {
28  
29      private Map<String, FieldDescriptor> fieldMap = new ListOrderedMap();
30  
31      /**
32       * The bean class.
33       */
34      private Class beanClass;
35  
36      /**
37       * Creates a new instance of this class.
38       */
39      public CommareaBeanMappingDescriptor() {
40      }
41  
42      /**
43       * Returns the bean class.
44       *
45       * @return  the bean class.
46       */
47      public Class getBeanClass() {
48          return beanClass;
49      }
50  
51      /**
52       * Sets the bean class.
53       *
54       * @param  beanClass  the bean class to set.
55       */
56      public void setBeanClass(Class beanClass) {
57          this.beanClass = beanClass;
58      }
59  
60      public void addFieldMapping(String propertyName, String fieldName,
61              FieldDescriptor fieldDescriptor) throws FormatException {
62          if (!(fieldDescriptor instanceof CobolTypeDescriptor)) {
63              throw new FormatException(
64                      "CIC001700_Expected_cobol_type_descriptor",
65                      new Object[] { fieldDescriptor.getClass() });
66          }
67          fieldMap.put(propertyName, fieldDescriptor);
68      }
69  
70      /**
71       * Not implementaed.
72       *
73       * @throws  FormatException  ever.
74       */
75      public void addFieldMapping(String propertyName, Integer fieldIndex,
76              FieldDescriptor fieldDescriptor) throws FormatException {
77          throw new FormatException("CIC001701_Error_adding_field_mapping");
78      }
79  
80      public int getBufferedLength() throws FormatException {
81          int size = 0;
82  
83          for (FieldDescriptor desc : fieldMap.values()) {
84              CobolTypeDescriptor cobolDesc = (CobolTypeDescriptor) desc;
85  
86              size += cobolDesc.getBufferedLength();
87          }
88          return size;
89      }
90  
91      public Map<String, FieldDescriptor> getFieldMap() {
92          return Collections.unmodifiableMap(fieldMap);
93      }
94  
95      public void setCodePage(String codePage) {
96          for (FieldDescriptor desc : fieldMap.values()) {
97              CobolTypeDescriptor cobolDesc = (CobolTypeDescriptor) desc;
98  
99              cobolDesc.setCodePage(codePage);
100         }
101     }
102 
103     @Override
104     public String toString() {                                  // Overridden
105         return ReflectionToStringBuilder.toString(this);
106     }
107 
108     @Override
109     public int hashCode() {                                     // Overridden
110         return fieldMap.hashCode();
111     }
112 
113     @Override
114     public boolean equals(Object obj) {                         // Overridden
115         if (obj instanceof CommareaBeanMappingDescriptor) {
116             CommareaBeanMappingDescriptor that
117                     = (CommareaBeanMappingDescriptor) obj;
118 
119             return fieldMap.equals(that.fieldMap);
120         }
121         return false;
122     }
123 }