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.commarea;
9   
10  import it.imolinfo.jbi4cics.exception.FormatException;
11  import it.imolinfo.jbi4cics.jbi.Messages;
12  import it.imolinfo.jbi4cics.messageformat.FieldDescriptor;
13  import it.imolinfo.jbi4cics.typemapping.cobol.CobolType;
14  import it.imolinfo.jbi4cics.typemapping.cobol.CobolTypeDescriptor;
15  
16  import java.util.ArrayList;
17  import java.util.List;
18  import java.util.Map;
19  import java.util.Stack;
20  
21  public class NestingHandler {
22  
23    /**
24     * The responsible to translate localized messages.
25     */
26     private static final Messages MESSAGES
27            = Messages.getMessages(NestingHandler.class);
28  
29    /**
30     * questo metodo prende in ingresso una commarea piatta come
31     * ritornata dal parser e crea una definizione con i dovuti nesting
32     * @param commareaBeanMappingDescriptor
33     * @return
34     * @throws FormatException
35     */
36    public static CommareaBeanMappingDescriptor handleNesting(CommareaBeanMappingDescriptor commareaBeanMappingDescriptor) throws FormatException{
37      CommareaBeanMappingDescriptor result=new CommareaBeanMappingDescriptor();
38      CommareaBeanMappingDescriptor currentCommareaBeanMappingDescriptor=result;
39      Integer currentLevel=0;
40      Stack<CommareaBeanMappingDescriptor> commareaStack=new Stack<CommareaBeanMappingDescriptor>();
41      Stack<Integer> levelStack=new Stack<Integer>();
42      Map<String,FieldDescriptor> fieldMap=commareaBeanMappingDescriptor.getFieldMap();
43      List<String> fieldList=new ArrayList<String>(fieldMap.keySet());
44      for (int i=0;i<fieldList.size();i++){
45        //coversione sicura
46        String propertyName=fieldList.get(i);
47        CobolTypeDescriptor cobolTypeDescriptor=(CobolTypeDescriptor)fieldMap.get(propertyName);
48  
49        if (i==0) {
50          //trattamento speciale per il primo perche' setta il livello di riferimento
51          currentLevel=cobolTypeDescriptor.getLevel();
52        }
53        // se siamo alla fine si aggiunge il campo attuale nel mapping corrente e si prosegue
54        if (i==(fieldList.size()-1)){
55          currentCommareaBeanMappingDescriptor.addFieldMapping(propertyName, cobolTypeDescriptor.getName(),cobolTypeDescriptor);
56          continue;
57        }
58  
59        Integer nextLevel=((CobolTypeDescriptor)fieldMap.get(fieldList.get(i+1))).getLevel();
60        switch (currentLevel.compareTo(nextLevel)) {
61          case 0 : {
62            //se il prossimo ha lo stesso livello allora si copia il campo attuale nel mapping corrente e si prosegue
63            currentCommareaBeanMappingDescriptor.addFieldMapping(propertyName, cobolTypeDescriptor.getName(),cobolTypeDescriptor);
64            break;
65          }
66          case -1 :{
67            //se il prossimo campo ha un livello maggiore allora inizia un nesting
68            //controllo che il campo attuale sia di tipo nesting
69            if (cobolTypeDescriptor.getType()!=CobolType.NESTED_COMMAREA && cobolTypeDescriptor.getType()!=CobolType.OCCURS){
70              throw new FormatException(MESSAGES.
71                      getString("CIC001704_Not_nested_field", new Object[] {
72                          cobolTypeDescriptor.getName(), currentLevel, nextLevel}));
73            }
74            //si copia il campo attuale nel mapping corrente e si prosegue e si mette livello e commarea nello stack, si istanzia il nuovo livello e una nuova commare descriptor
75            currentCommareaBeanMappingDescriptor.addFieldMapping(propertyName, cobolTypeDescriptor.getName(),cobolTypeDescriptor);
76            levelStack.push(currentLevel);
77            currentLevel=nextLevel;
78            commareaStack.push(currentCommareaBeanMappingDescriptor);
79            currentCommareaBeanMappingDescriptor=new CommareaBeanMappingDescriptor();
80            cobolTypeDescriptor.setNestedCommarea(currentCommareaBeanMappingDescriptor);
81            break;
82          }
83          case 1 : {
84            //se il prossimo campo ha un livello minore allora finisce un nestin
85            //si copia il campo attuale nel mapping corrente, e si fa il pop dei precednti livelli e commarea descriptor
86            currentCommareaBeanMappingDescriptor.addFieldMapping(propertyName, cobolTypeDescriptor.getName(),cobolTypeDescriptor);
87            currentLevel=levelStack.pop();
88            currentCommareaBeanMappingDescriptor=commareaStack.pop();
89            break;
90          }
91          default : {
92            throw new FormatException(MESSAGES.getString("CIC001705_Unreachable_code"));
93          }
94        }
95      }
96  
97      return result;
98    }
99  }