1
2
3
4
5
6
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
25
26 private static final Messages MESSAGES
27 = Messages.getMessages(NestingHandler.class);
28
29
30
31
32
33
34
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
46 String propertyName=fieldList.get(i);
47 CobolTypeDescriptor cobolTypeDescriptor=(CobolTypeDescriptor)fieldMap.get(propertyName);
48
49 if (i==0) {
50
51 currentLevel=cobolTypeDescriptor.getLevel();
52 }
53
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
63 currentCommareaBeanMappingDescriptor.addFieldMapping(propertyName, cobolTypeDescriptor.getName(),cobolTypeDescriptor);
64 break;
65 }
66 case -1 :{
67
68
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
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
85
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 }