1 package jimm.datavision.test.mock.source; 2 import jimm.datavision.Report; 3 import jimm.datavision.Selectable; 4 import jimm.datavision.source.*; 5 import jimm.util.XMLWriter; 6 import java.util.*; 7 import java.sql.Types ; 8 9 public class MockDataSource extends DataSource { 10 11 protected static final String DATABASE_NAME = "dv_example"; 12 13 protected Map tables; 14 protected List tablesUsedInReport; 15 protected List columns; 16 protected String name; 17 18 public MockDataSource(Report r) { 19 super(r, new Query(r)); 20 tables = new HashMap(); 21 tablesUsedInReport = new ArrayList(); 22 name = DATABASE_NAME; 23 24 createOfficeTable(); 25 createJobsTable(); 26 createAggregateTestTable(); 27 createAllCapsTable(); 28 } 29 30 protected void createOfficeTable() { 31 Table t = new Table(this, "office"); 32 tables.put(t.getName(), t); 33 tablesUsedInReport.add(t); 34 35 addColumn(t, "id", Types.INTEGER); 36 addColumn(t, "name", Types.VARCHAR); 37 addColumn(t, "abbrev", Types.VARCHAR); 38 addColumn(t, "fax", Types.VARCHAR); 39 addColumn(t, "email", Types.VARCHAR); 40 addColumn(t, "visible", Types.BOOLEAN); 41 } 42 43 protected void createJobsTable() { 44 Table t = new Table(this, "jobs"); 45 tables.put(t.getName(), t); 46 tablesUsedInReport.add(t); 47 addColumn(t, "ID", Types.INTEGER); 48 addColumn(t, "title", Types.VARCHAR); 49 addColumn(t, "fk_office_id", Types.INTEGER); 50 addColumn(t, "company", Types.VARCHAR); 51 addColumn(t, "location", Types.VARCHAR); 52 addColumn(t, "description", Types.VARCHAR); 53 addColumn(t, "visible", Types.BOOLEAN); 54 addColumn(t, "post_date", Types.DATE); 55 addColumn(t, "hourly rate", Types.INTEGER); 56 } 57 58 protected void createAggregateTestTable() { 59 Table t = new Table(this, "aggregate_test"); 60 tables.put(t.getName(), t); 61 addColumn(t, "col1", Types.VARCHAR); 62 addColumn(t, "col2", Types.VARCHAR); 63 addColumn(t, "col3", Types.VARCHAR); 64 addColumn(t, "value", Types.INTEGER); 65 } 66 67 protected void createAllCapsTable() { 68 Table t = new Table(this, "ALL_CAPS"); 69 tables.put(t.getName(), t); 70 addColumn(t, "COL1", Types.INTEGER); 71 addColumn(t, "COL2", Types.VARCHAR); 72 } 73 74 protected void addColumn(Table table, String name, int type) { 75 Column col = new Column(table.getName() + '.' + name, name, type); 76 table.addColumn(col); 77 } 78 79 public boolean canJoinTables() { return true; } 80 81 public boolean isSQLGenerated() { return true; } 82 83 public boolean isConnectionEditable() { return true; } 84 85 public boolean areRecordsSelectable() { return true; } 86 87 public boolean areRecordsSortable() { return true; } 88 89 public boolean canGroupRecords() { return true; } 90 91 public DataCursor execute() throws Exception { 92 return new MockDataCursor(getQuery()); 93 } 94 95 public int indexOfSelectable(Selectable sel) { 96 return MockDataCursor.indexOfSelectable(sel); 97 } 98 99 100 public Column findColumn(Object id) { 101 String str = id.toString(); 102 int pos = str.lastIndexOf('.'); 103 if (pos == -1) return null; 104 String tableName = str.substring(0, pos); 105 Table t = findTable(tableName); 106 return t == null ? null : t.findColumn(id); 107 } 108 109 113 protected Table findTable(String tableName) { 114 Table t = (Table)tables.get(tableName); 116 if (t != null) 117 return t; 118 119 String schemaName = null; 120 int pos = tableName.indexOf('.'); 121 if (pos >= 0) { 122 schemaName = tableName.substring(0, pos); 123 tableName = tableName.substring(pos + 1); 124 } 125 126 if (!getReport().caseSensitiveDatabaseNames()) { 127 if (schemaName != null) schemaName = schemaName.toLowerCase(); 128 tableName = tableName.toLowerCase(); 129 } 130 131 if (schemaName != null) { 133 if ((t = findTableWithId(schemaName + '.' + tableName)) != null) 134 return t; 135 } 136 137 if (name != null && !name.equals(schemaName)) { 140 if ((t = findTableWithId(name + '.' + tableName)) != null) 141 return t; 142 } 143 144 if ((t = findTableWithId(tableName)) != null) 146 return t; 147 148 return null; 149 } 150 151 155 protected Table findTableWithId(String id) { 156 boolean caseSensitive = getReport().caseSensitiveDatabaseNames(); 157 if (!caseSensitive) 158 id = id.toLowerCase(); 159 160 for (Iterator iter = tables.keySet().iterator(); iter.hasNext(); ) { 161 String key = (String )iter.next(); 162 if (caseSensitive) 163 key = key.toLowerCase(); 164 if (key.equals(id)) 165 return (Table)tables.get(key); 166 } 167 168 return null; 169 } 170 171 public Iterator tables() { return tables.values().iterator(); } 172 173 public Iterator tablesUsedInReport() { return tablesUsedInReport.iterator(); } 174 175 public Iterator columns() { 176 return new ColumnIterator(tables.values().iterator()); 177 } 178 179 protected void doWriteXML(XMLWriter out) { } 180 181 } 182 | Popular Tags |