2 * Created on 11/08/2003
5 package com.quantum.util;
7 import java.util.Iterator;
8 import java.util.Vector;
10 import com.quantum.QuantumPlugin;
12 import org.eclipse.jface.preference.IPreferenceStore;
15 * Class to clasify two levels of strings. It's called Nth Level but in fact it has only two levels :o)
19 public class NthLevelConverter {
20 private final int DEFAULT_COLUMNS = 10;
21 private final int DEFAULT_INCREMENT = 10;
23 private Vector upper = new Vector(DEFAULT_COLUMNS,DEFAULT_INCREMENT);
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);
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
38 this.upper.add(upperFinder);
39 ind = upper.indexOf(upperFinder);
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);
61 public Vector getUpper() {
65 public Vector getLevel(String id, int level){
69 if (id == null) return null;
70 OneLevelConverter upperFinder = new OneLevelConverter(id);
71 int ind = upper.indexOf(upperFinder);
75 OneLevelConverter upperItem = (OneLevelConverter) upper.get(ind);
76 return upperItem.getLower();
82 public void convert(String ind){
83 IPreferenceStore store = QuantumPlugin.getDefault().getPreferenceStore();
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));
90 for (Iterator iter = upper.iterator(); iter.hasNext();) {
91 OneLevelConverter oneLevel = (OneLevelConverter) iter.next();
92 String upperId = oneLevel.getId();
93 Vector lower = oneLevel.getLower();
95 for (Iterator iterator = lower.iterator(); iterator.hasNext();) {
96 OneLevelConverter element = (OneLevelConverter) iterator.next();
97 String convLeaf = columnItem + (iterator.hasNext() ? columnSeparator : "");
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());
104 element.setResult(convLeaf);
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));
114 oneLevel.setResult(convUpper);
120 * Calculates the list of columns for an upper level converter. Has to be already converted.
124 private String getColumnList(OneLevelConverter oneLevel) {
127 Vector lower = oneLevel.getLower();
129 for (Iterator iterator = lower.iterator(); iterator.hasNext();) {
130 OneLevelConverter element = (OneLevelConverter) iterator.next();
131 result += element.getResult();
137 public String getTableList() {
139 for (Iterator iter = upper.iterator(); iter.hasNext();) {
140 OneLevelConverter oneLevel = (OneLevelConverter) iter.next();
141 result += oneLevel.getResult();