1    package com.instantbank.lettertemplate.editor.web;
2    
3    import java.util.ArrayList;
4    import com.instantbank.common.utilcomponents.LetterTemplateGlobals;
5    
6    /**
7     *  Auxiliary class that manages the information related to variables.
8     *
9     * @author InstantBank (Rodrigo Lopez)
10    * @created September 2002
11    */
12   public class VariablesManager {
13   
14     public static final long DEFAULT_STRING_FORMAT = 1;
15   
16     public static final long DEFAULT_NUMBER_FORMAT = 2;
17   
18     public static final long DEFAULT_DATE_FORMAT = 3;
19   
20     /**
21      *  Variables related information: ArrayList of ArrayList of < code, name,
22      *  type, dateOffsetType>
23      */
24     private ArrayList theVars;
25   
26     /**
27      *  Code's position inside a variable's slot.
28      */
29     public static final int CODE = 0;
30   
31     /**
32      *  Name's position inside a variable's slot.
33      */
34     public static final int NAME = 1;
35   
36     /**
37      *  Type's position inside a variable's slot.
38      */
39     public static final int TYPE = 2;
40   
41     /**
42      *  Offset type's position inside a variable slot.
43      */
44     public static final int OFFSET = 3;
45   
46   
47     /**
48      *  VariablesManager constructor.
49      *
50      * @param varsInfo The variables info.
51      */
52     public VariablesManager(ArrayList varsInfo) {
53       theVars = varsInfo;
54     }
55   
56   
57     /**
58      *  Searches a variable --by its name-- inside the {@link #theVars variable
59      *  information structure}.
60      *
61      * @param name Variable's name.
62      * @return       <ul>
63      *        <li> The slot where the name is found.
64      *        <li> Null if the name is not found.
65      *      </ul>
66      */
67     public ArrayList assocName(String name) {
68       if(theVars == null) {
69         return null;
70       }
71   
72       for(int i = 0; i < theVars.size(); i++) {
73         ArrayList tmp = (ArrayList)theVars.get(i);
74         if(tmp.indexOf(name) != -1) {
75           return tmp;
76         }
77       }
78       return null;
79     }
80   
81   
82     /**
83      *  Searches a variable --by its code-- inside the {@link #theVars variable
84      *  information structure}.
85      *
86      * @param code The variable's code.
87      * @return       <ul>
88      *        <li> The slot where the name is found.
89      *        <li> Null if the name is not found.
90      *      </ul>
91      */
92     public ArrayList assocCode(Long code) {
93       if(theVars == null) {
94         return null;
95       }
96   
97       for(int i = 0; i < theVars.size(); i++) {
98         ArrayList tmp = (ArrayList)theVars.get(i);
99         if(tmp.indexOf(code) != -1) {
100          return tmp;
101        }
102      }
103      return null;
104    }
105  
106  
107    /**
108     *  Delivers the code of a variable, after its name.
109     *
110     * @param name
111     * @return The code, or "null" if it doesn't exist.
112     */
113    public Long getCode(String name) {
114      ArrayList a = assocName(name);
115      return a != null ? (Long)a.get(CODE) : null;
116    }
117  
118  
119    /**
120     *  Delivers the variable's name, after its code.
121     *
122     * @param code
123     * @return The name of the variable, or "null" if the variable doesn't
124     *      exist.
125     */
126    public String getName(Long code) {
127      ArrayList a = assocCode(code);
128      return a != null ? (String)a.get(NAME) : null;
129    }
130  
131  
132    /**
133     *  Delivers an internal name for the variable.
134     *
135     * @param varCode The code of the variable.
136     * @param formatCode The code of the format.
137     * @return The internal name or null if the variable doesn't exist.
138     * The name is formed as:<tt>f<formatCode>_v<varCode></tt>
139     */
140    public String buildNameWithFormat(Long varCode, Long formatCode) {
141  
142      return "f" + formatCode + "_v" + varCode;
143    }
144  
145  
146    /**
147     *  Delivers the type of the variable, after its name.
148     *
149     * @param name
150     * @return The type "D", "N" , "S" or null if the variable doesn't exist.
151     */
152    public String getType(String name) {
153      ArrayList a = assocName(name);
154      return a != null ? (String)a.get(TYPE) : null;
155    }
156  
157  
158    /**
159     *  Delivers the type of the variable, after its code.
160     *
161     * @param code
162     * @return The type "D", "N" , "S" or null if the variable doesn't exist.
163     */
164    public String getType(Long code) {
165      ArrayList a = assocCode(code);
166      return a != null ? (String)a.get(TYPE) : null;
167    }
168  
169  
170    /**
171     * Delivers the type of offset for Date variables: "no", "workable",
172     * "chronological"
173     *
174     * @param code Description of the Parameter
175     * @return The offsetType value
176     */
177    public String getOffsetType(Long code) {
178      ArrayList a = assocCode(code);
179      return a != null ? (String)a.get(OFFSET) : null;
180    }
181  
182  
183    /**
184     *  Test if a variable code corresponds to the undefined value.
185     *
186     * @param code
187     * @return The varUndef value
188     */
189    public boolean isVarUndef(long code) {
190      return code == LetterTemplateGlobals.UNDEF;
191    }
192  
193  }
194