small improvement for $ variables
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / util / NthLevelConverter.java
1 /*
2  * Created on 11/08/2003
3  *
4  */
5 package com.quantum.util;
6
7 import java.util.Iterator;
8 import java.util.Vector;
9
10 import com.quantum.QuantumPlugin;
11
12 import org.eclipse.jface.preference.IPreferenceStore;
13
14 /**
15  * Class to clasify two levels of strings. It's called Nth Level but in fact it has only two levels :o) 
16  * @author panic
17  *
18  */
19 public class NthLevelConverter {
20         private final int DEFAULT_COLUMNS = 10;
21         private final int DEFAULT_INCREMENT = 10;
22         
23         private Vector upper = new Vector(DEFAULT_COLUMNS,DEFAULT_INCREMENT);
24
25         public void add(String id, String parentId, int level) {
26                 OneLevelConverter upperLine = new OneLevelConverter(id);
27                 if (level == 1) { // If it's an upper leaf
28                         if (!upper.contains(upperLine))
29                                 this.upper.add(upperLine);
30                 }
31                 else if (level == 0) { // It's a lower leaf
32                         if  (parentId == null) return; // No parent, what shall we do.
33                         OneLevelConverter upperFinder = new OneLevelConverter(parentId);
34                         // Search if its parent is already present in the upper level
35                         int ind = upper.indexOf(upperFinder);
36                         // If not, addIt and find its index
37                         if (ind < 0) {
38                                 this.upper.add(upperFinder);
39                                 ind = upper.indexOf(upperFinder);
40                         }
41                         OneLevelConverter upperItem = (OneLevelConverter) upper.get(ind);
42                         // If it has no children, we create a new Vector for the children
43                         if (upperItem.getLower() == null)
44                                 upperItem.setLower(new Vector(DEFAULT_COLUMNS,DEFAULT_INCREMENT));
45                         Vector children = upperItem.getLower();
46                         // Is the item already present in the children of its parent?
47                         int indChildren = children.indexOf(id);
48                         // If it's not, we add it
49                         if (indChildren < 0) {
50                                 OneLevelConverter leaf = new OneLevelConverter(id);
51                                 children.add(leaf);
52                         }               
53                 }
54                 
55         }
56         
57         
58         /**
59          * @return
60          */
61         public Vector getUpper() {
62                 return upper;
63         }
64         
65         public Vector getLevel(String id, int level){
66                 if (level == 1)
67                         return upper;
68                 else if (level == 0){
69                         if (id == null) return null;
70                         OneLevelConverter upperFinder = new OneLevelConverter(id);
71                         int ind = upper.indexOf(upperFinder);
72                         if (ind < 0) 
73                                 return null;
74                         else {
75                                 OneLevelConverter upperItem = (OneLevelConverter) upper.get(ind);
76                                 return upperItem.getLower();
77                         }                               
78                 }
79                 return null;
80         }
81         
82         public void convert(String ind){
83                 IPreferenceStore store = QuantumPlugin.getDefault().getPreferenceStore();
84
85                 String tableItem = QuantumUtil.trasposeEscape(store.getString("customCopyTableItem" + ind));
86                 String tableSeparator = QuantumUtil.trasposeEscape(store.getString("customCopyTableSeparator" + ind));
87                 String columnItem = QuantumUtil.trasposeEscape(store.getString("customCopyColumnItem" + ind));
88                 String columnSeparator = QuantumUtil.trasposeEscape(store.getString("customCopyColumnSeparator" + ind));
89
90                 for (Iterator iter = upper.iterator(); iter.hasNext();) {
91                         OneLevelConverter oneLevel = (OneLevelConverter) iter.next();
92                         String upperId = oneLevel.getId();
93                         Vector lower = oneLevel.getLower();
94                         if (lower != null) {
95                                 for (Iterator iterator = lower.iterator(); iterator.hasNext();) {
96                                         OneLevelConverter element = (OneLevelConverter) iterator.next();
97                                         String convLeaf =       columnItem + (iterator.hasNext() ? columnSeparator : "");
98                                          
99                                         convLeaf = StringUtil.substituteString(convLeaf, "${schema}", QuantumUtil.getSchemaName(upperId));
100                                         convLeaf = StringUtil.substituteString(convLeaf, "${qualified}", upperId);
101                                         convLeaf = StringUtil.substituteString(convLeaf, "${table}", QuantumUtil.getTableName(upperId));
102                                         convLeaf = StringUtil.substituteString(convLeaf, "${column}", element.getId());
103                                         
104                                         element.setResult(convLeaf);
105                                 }
106                         }
107                         // We have all the leafs converted, we calculate the parent conversion
108                         String convUpper = tableItem + (iter.hasNext() ? tableSeparator : "");
109                         convUpper = StringUtil.substituteString(convUpper, "${schema}", QuantumUtil.getSchemaName(upperId));
110                         convUpper = StringUtil.substituteString(convUpper, "${qualified}", upperId);
111                         convUpper = StringUtil.substituteString(convUpper, "${table}", QuantumUtil.getTableName(upperId));
112                         convUpper = StringUtil.substituteString(convUpper, "${column_list}", getColumnList(oneLevel));
113                         
114                         oneLevel.setResult(convUpper);
115                 }
116         }
117
118
119         /**
120          * Calculates the list of columns for an upper level converter. Has to be already converted.
121          * @param oneLevel
122          * @return
123          */
124         private String getColumnList(OneLevelConverter oneLevel) {
125
126                 String result = "";
127                 Vector lower = oneLevel.getLower();
128                 if (lower != null) {
129                         for (Iterator iterator = lower.iterator(); iterator.hasNext();) {
130                                 OneLevelConverter element = (OneLevelConverter) iterator.next();
131                                 result += element.getResult();
132                         }
133                 }
134                 return result;
135         }
136
137         public String getTableList() {
138                 String result = "";
139                 for (Iterator iter = upper.iterator(); iter.hasNext();) {
140                         OneLevelConverter oneLevel = (OneLevelConverter) iter.next();
141                         result += oneLevel.getResult();
142                 }
143                 return result;
144         }
145 }