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.typemapping.cobol;
11  
12  import it.imolinfo.jbi4cics.Logger;
13  import it.imolinfo.jbi4cics.LoggerFactory;
14  import it.imolinfo.jbi4cics.exception.FormatException;
15  import it.imolinfo.jbi4cics.messageformat.FieldDescriptor;
16  import it.imolinfo.jbi4cics.messageformat.commarea.CommareaBeanMappingDescriptor;
17  import org.apache.commons.lang.builder.EqualsBuilder;
18  import org.apache.commons.lang.builder.HashCodeBuilder;
19  import org.apache.commons.lang.builder.ReflectionToStringBuilder;
20  
21  /**
22   * @author raffaele
23   * @author <a href="mailto:mcimatti@imolinfo.it">Marco Cimatti</a>
24   */
25  public class CobolTypeDescriptor implements FieldDescriptor {
26  
27  
28      // Justification
29  
30  
31      public final static int STRING_JUSTIFICATION_LEFT = 0;
32  
33      public final static int STRING_JUSTIFICATION_RIGHT = 1;
34  
35      public final static int STRING_JUSTIFICATION_CENTER = 2;
36  
37  
38      // Zoned Sign Format
39  
40  
41      public final static int SIGN_FORMAT_LEADING = 0;
42  
43      public final static int SIGN_FORMAT_TRAILING = 1;
44  
45      public final static int SIGN_FORMAT_LEADING_SEPARATE = 2;
46  
47  
48      /**
49       * The logger for this class and its instances.
50       */
51      private static final Logger LOG
52              = LoggerFactory.getLogger(CobolTypeDescriptor.class);
53  
54      /**
55       * The field name as it appears in the commarea.
56       */
57      private String name;
58  
59      /**
60       * The Cobol type described by this instance.
61       */
62      private CobolType type;
63  
64  
65      // dati aggiuntivi stringa
66  
67  
68      // E' il default dell'host
69      private int justification = STRING_JUSTIFICATION_LEFT;
70  
71      private String padCharacter = " ";
72  
73      private int stringLength;
74  
75      private String codePage;
76  
77  
78      // dati aggiuntivi numeric
79  
80  
81      private boolean signed;
82  
83  
84      // dati aggiuntivi integer
85  
86  
87      private boolean bigEndian;
88  
89  
90      // dati agiuntivi decimal
91  
92  
93      private int integerPartLength;
94  
95      private int decimalPartLength;
96  
97  
98      // dati aggiuntivi zoned
99  
100 
101     private int zonedSignFormat = SIGN_FORMAT_LEADING;
102 
103 
104     // dati aggiuntivi per il nesting
105 
106 
107     private int level;
108 
109     private CommareaBeanMappingDescriptor nestedCommarea;
110 
111 
112     // dati aggiuntivi per occurs
113 
114 
115     private int occursSize;
116 
117 
118     /**
119      * Costruttore di default: sarebbe da usare solo nei test.
120      */
121     public CobolTypeDescriptor() {
122     }
123 
124     @Override
125     public String toString() {                                  // Overridden
126         return ReflectionToStringBuilder.toString(this);
127     }
128 
129     @Override
130     public boolean equals(Object obj) {                         // Overridden
131         CobolTypeDescriptor that;
132 
133         if (obj == this) {
134             return true;
135         }
136         if (!(obj instanceof CobolTypeDescriptor)) {
137             return false;
138         }
139         that = (CobolTypeDescriptor) obj;
140         return new EqualsBuilder().append(bigEndian, that.bigEndian)
141                 .append(codePage, that.codePage)
142                 .append(decimalPartLength, that.decimalPartLength)
143                 .append(integerPartLength, that.integerPartLength)
144                 .append(justification, that.justification)
145                 .append(level, that.level)
146                 .append(name, that.name)
147                 .append(nestedCommarea, that.nestedCommarea)
148                 .append(occursSize, that.occursSize)
149                 .append(padCharacter, that.padCharacter)
150                 .append(signed, that.signed)
151                 .append(stringLength, that.stringLength)
152                 .append(type, that.type)
153                 .append(zonedSignFormat, that.zonedSignFormat).isEquals();
154     }
155 
156     @Override
157     public int hashCode() {                                     // Overridden
158         return new HashCodeBuilder().append(bigEndian)
159                 .append(codePage)
160                 .append(decimalPartLength)
161                 .append(integerPartLength)
162                 .append(justification)
163                 .append(level)
164                 .append(name)
165                 .append(nestedCommarea)
166                 .append(occursSize)
167                 .append(padCharacter)
168                 .append(signed)
169                 .append(stringLength)
170                 .append(type)
171                 .append(zonedSignFormat).toHashCode();
172     }
173 
174     /**
175      * Returns the level.
176      *
177      * @return the level.
178      */
179     public int getLevel() {
180         return level;
181     }
182 
183     /**
184      * Sets the level.
185      *
186      * @param  level  the new level to set.
187      */
188     public void setLevel(int level) {
189         this.level = level;
190     }
191 
192     /**
193      * @return Returns the signed.
194      */
195     public boolean isSigned() {
196         return signed;
197     }
198 
199     /**
200      * @param signed
201      *            The signed to set.
202      */
203     public void setSigned(boolean signed) {
204         this.signed = signed;
205     }
206 
207     /**
208      * @return Returns the justification.
209      */
210     public int getJustification() {
211         return justification;
212     }
213 
214     /**
215      * @param justification
216      *            The justification to set.
217      */
218     public void setJustification(int justification) {
219         this.justification = justification;
220     }
221 
222     /**
223      * @return Returns the padCharacter.
224      */
225     public String getPadCharacter() {
226         return padCharacter;
227     }
228 
229     /**
230      * @param padCharacter
231      *            The padCharacter to set.
232      */
233     public void setPadCharacter(String padCharacter) {
234         this.padCharacter = padCharacter;
235     }
236 
237     /**
238      * @return Returns the decimalPartLength.
239      */
240     public int getDecimalPartLength() {
241         return decimalPartLength;
242     }
243 
244     /**
245      * @param decimalPartLength
246      *            The decimalPartLength to set.
247      */
248     public void setDecimalPartLength(int decimalPartLength) {
249         this.decimalPartLength = decimalPartLength;
250     }
251 
252     /**
253      * @return Returns the integerPartLength.
254      */
255     public int getIntegerPartLength() {
256         return integerPartLength;
257     }
258 
259     /**
260      * @param integerPartLength
261      *            The integerPartLength to set.
262      */
263     public void setIntegerPartLength(int integerPartLength) {
264         this.integerPartLength = integerPartLength;
265     }
266 
267     /**
268      * @return Returns the stringLength.
269      */
270     public int getStringLength() {
271         return stringLength;
272     }
273 
274     /**
275      * @param stringLength
276      *            The stringLength to set.
277      */
278     public void setStringLength(int stringLength) {
279         this.stringLength = stringLength;
280     }
281 
282     /**
283      * @return Returns the type.
284      */
285     public CobolType getType() {
286         return type;
287     }
288 
289     /**
290      * @param type
291      *            The type to set.
292      */
293     public void setType(CobolType type) {
294         this.type = type;
295     }
296 
297     /**
298      * @return Returns the virtualDecimalPoint.
299      */
300     public int getVirtualDecimalPoint() {
301         return getDecimalPartLength();
302     }
303 
304     /**
305      * Returns the buffered length.
306      *
307      * @return  the buffered length.
308      */
309     public int getBufferedLength() throws FormatException {
310         CobolType ct = getType();
311 
312         if (ct == null) {
313             LOG.error("CIC002110_Unknown_cobol_type", ct);
314             throw new FormatException("CIC002110_Unknown_cobol_type",
315                                       new Object[] { ct });
316         }
317         return ct.getBufferedLength(this);
318     }
319 
320     /**
321      * @return Returns the bigEndian.
322      */
323     public boolean isBigEndian() {
324         return bigEndian;
325     }
326 
327     /**
328      * @param bigEndian
329      *            The bigEndian to set.
330      */
331     public void setBigEndian(boolean bigEndian) {
332         this.bigEndian = bigEndian;
333     }
334 
335     /**
336      * Returns the code page.
337      *
338      * @return  the code page.
339      */
340     public String getCodePage() {
341         return codePage;
342     }
343 
344     /**
345      * Sets the code page.
346      *
347      * @param  codePage  the code page to set.
348      */
349     public void setCodePage(String codePage) {
350         CobolType ct = getType();
351 
352         this.codePage = codePage;
353         if ((ct == CobolType.NESTED_COMMAREA) || (ct == CobolType.OCCURS)) {
354             nestedCommarea.setCodePage(codePage);
355         }
356     }
357 
358     /**
359      * @return Returns the zonedSignFormat.
360      */
361     public int getZonedSignFormat() {
362         return zonedSignFormat;
363     }
364 
365     /**
366      * @param zonedSignFormat
367      *            The zonedSignFormat to set.
368      */
369     public void setZonedSignFormat(int zonedSignFormat) {
370         this.zonedSignFormat = zonedSignFormat;
371     }
372 
373     /**
374      * @return Returns the name.
375      */
376     public String getName() {
377         return name;
378     }
379 
380     /**
381      * @param name
382      *            The name to set.
383      */
384     public void setName(String name) {
385         this.name = name;
386     }
387 
388     public Class getPreferredJavaType() throws FormatException {
389         CobolType ct = getType();
390 
391         if (ct == null) {
392             LOG.error("CIC002111_Unexpected_cobol_type", ct);
393             throw new FormatException("CIC002111_Unexpected_cobol_type",
394                                       new Object[] { ct });
395         }
396         return ct.getPreferredJavaType(this);
397     }
398 
399     public CommareaBeanMappingDescriptor getNestedCommarea() {
400         return nestedCommarea;
401     }
402 
403     public void setNestedCommarea(CommareaBeanMappingDescriptor nestedCommarea) {
404         this.nestedCommarea = nestedCommarea;
405     }
406 
407     public int getOccursSize() {
408         return occursSize;
409     }
410 
411     public void setOccursSize(int occursSize) {
412         this.occursSize = occursSize;
413     }
414 }