001: /*
002: * $Author$
003: * $Id$
004: * This is free software, as software should be; you can redistribute
005: * it and/or modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008:
009: * See LICENSE.txt for the full license covering this software/program/code.
010: */
011:
012: package isql.commands;
013:
014: import isql.Command;
015: import util.PerlWrapper;
016: import isql.*;
017: import util.*;
018: import javax.swing.*;
019: import javax.swing.table.*;
020: import java.awt.*;
021: import java.util.*;
022:
023: /**
024: * The command object associated with our metadata, such as links for
025: * tables, sort options etc.
026: * I am wondering whether they should be separate classes, however,
027: * would like them to be in one place.
028: * @author rahul kumar <rahul_kumar@yahoo.com>
029: * @see XXX
030: */
031:
032: public class MetadataCommand implements Command {
033:
034: /** ctor/constructor.
035: */
036: public MetadataCommand() {
037: }
038:
039: public String[] getCommandList() {
040: return commands;
041: }
042:
043: public void execute(SQLForm form, String fword, String SQLString) {
044: _form = form;
045: SQLTabbedPane tp = _form.tp;
046: //SQLJDBC myjdbc = _form.myjdbc;
047:
048: Map htLinks = _form.getLinkMap();
049: Map htMaster = _form.getMasterMap();
050: Map htSorts = _form.getSortMap();
051:
052: if (fword.equals("help")) {
053: _form.tp.appendOutputArea(help(fword, SQLString));
054: _form.tp.makeOutputAreaVisible();
055: return;
056: }
057: if (fword.equals("settings")) {
058: _form.tp.appendOutputArea(settings(fword, SQLString));
059: _form.tp.makeOutputAreaVisible();
060: return;
061: }
062: if (fword.equals("link")) {
063: String ts = SQLString.substring(5);
064: int pos1 = ts.indexOf(':');
065: int pos2 = ts.indexOf(':', pos1 + 1);
066: if (htLinks == null)
067: htLinks = new HashMap();
068: String tname = ts.substring(0, pos1) + '.';
069: String master = ts.substring(pos2 + 1);
070: if ("..".equals(tname))
071: tname = "";
072: htLinks.put(tname + ts.substring(pos1 + 1, pos2), master);
073: if (SQLForm.DEBUG)
074: System.out.print(" Master:" + master);
075: String p[] = ArrayUtil.split(master, ':');
076: String key = p[1];
077: if (htMaster == null)
078: htMaster = new HashMap();
079: htMaster.put(key, master);
080: System.out.print(" key:" + key);
081: return;
082: }
083: if (fword.equals("sort")) {
084: String ts = SQLString.substring(5);
085: if (htSorts == null)
086: htSorts = new HashMap();
087: int pos1 = ts.indexOf(':');
088: // put table name and then sort
089: // order
090: htSorts.put(ts.substring(0, pos1), ts.substring(pos1 + 1));
091: return;
092: }
093:
094: }
095:
096: public String settings(String fword, String SQLString) {
097: StringBuffer sb = new StringBuffer(128);
098: Map params = _form.getParams();
099: sb.append("Attributes:\n");
100: sb = printMap(params, sb);
101: sb.append("Links:\n");
102: Map htLinks = _form.getLinkMap();
103: sb = printMap(htLinks, sb);
104: sb.append("Master:\n");
105: Map htMaster = _form.getMasterMap();
106: sb = printMap(htMaster, sb);
107: Map htSorts = _form.getSortMap();
108: sb.append("Sorts:\n");
109: sb = printMap(htSorts, sb);
110: sb.append("Filters:\n");
111: sb = printMap(_form.getFilterMap(), sb);
112:
113: sb.append("Abbreviations:\n");
114: sb = printMap(_form.getAbbreviations(), sb);
115: sb.append("Remembered Values:\n");
116: sb = printMap(_form.getRemembered(), sb);
117: return sb.toString();
118:
119: }
120:
121: private StringBuffer printMap(Map map, StringBuffer sb) {
122: if (map == null) {
123: sb.append('\t').append("No values.");
124: return sb;
125: }
126: Iterator it = map.keySet().iterator();
127: while (it.hasNext()) {
128: String key = (String) it.next();
129: Object value = (Object) map.get(key);
130: sb.append('\t').append(key).append(':').append(value)
131: .append('\n');
132: }
133: return sb;
134: }
135:
136: public String help(String fword, String SQLString) {
137: if (fword.trim().equals(SQLString.trim())) {
138: //Command[] commands = Commands.getCommands();
139: Command[] commands = _form.getCommandHandler()
140: .getCommands();
141: StringBuffer sb = new StringBuffer(128);
142:
143: for (int i = 0; i < commands.length; i++) {
144: Command com = commands[i];
145: String[] commandlist = com.getCommandList();
146: for (int j = 0; j < commandlist.length; j++) {
147: String key = commandlist[j];
148: sb.append(key).append(":\n\t");
149: sb.append(com.getUsage(key)).append('\n');
150: }
151: }
152: return sb.toString();
153: } else {
154: String w[] = ArrayUtil.split(SQLString, ' ');
155: String option = w[1];
156: Command com = _form.getCommandHandler().getCommandFor(
157: option);
158: if (com == null)
159: return "No command:" + option;
160: return com.getUsage(option);
161: }
162: } // help
163:
164: public String getUsage(String s) {
165: if (s.equals("sort"))
166: return "sort <table>:<sortorder>";
167: if (s.equals("link"))
168: return "link <table>:<column>:<link table>:<link column>:<link condition>";
169: if (s.equals("help"))
170: return "help ";
171: if (s.equals("settings"))
172: return "settings ";
173: return s + " not implemented";
174:
175: }
176:
177: String commands[] = { "link", "sort", "filter", "help", "settings" };
178: SQLForm _form;
179:
180: ////// START INSTANCE VARIABLES //////
181:
182: ////// START CONSTANTS AND CLASS LEVEL VARIABLES //////
183: static final String P = "MetadataCommand"; // used in exception strings
184:
185: } // end of class
|