001: /*
002: * $Id: AxionTablePropertiesMetaTableUpdater.java,v 1.2 2004/08/24 01:09:26 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2002 Axion Development Team. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above
011: * copyright notice, this list of conditions and the following
012: * disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The names "Tigris", "Axion", nor the names of its contributors may
020: * not be used to endorse or promote products derived from this
021: * software without specific prior written permission.
022: *
023: * 4. Products derived from this software may not be called "Axion", nor
024: * may "Tigris" or "Axion" appear in their names without specific prior
025: * written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
032: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
033: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
034: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
035: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
037: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038: * =======================================================================
039: */
040:
041: package org.axiondb.engine.metaupdaters;
042:
043: import java.util.ArrayList;
044: import java.util.Collections;
045: import java.util.Iterator;
046: import java.util.List;
047: import java.util.Map;
048: import java.util.Properties;
049:
050: import org.apache.commons.logging.Log;
051: import org.apache.commons.logging.LogFactory;
052: import org.axiondb.AxionCommand;
053: import org.axiondb.AxionException;
054: import org.axiondb.ColumnIdentifier;
055: import org.axiondb.Database;
056: import org.axiondb.ExternalTable;
057: import org.axiondb.Literal;
058: import org.axiondb.Row;
059: import org.axiondb.Table;
060: import org.axiondb.engine.TransactableTableImpl;
061: import org.axiondb.engine.commands.DeleteCommand;
062: import org.axiondb.engine.rows.SimpleRow;
063: import org.axiondb.event.BaseDatabaseModificationListener;
064: import org.axiondb.event.DatabaseModificationListener;
065: import org.axiondb.event.DatabaseModifiedEvent;
066: import org.axiondb.functions.FunctionIdentifier;
067:
068: /**
069: * Updates the <code>AXION_TABLE_PROPERTIES</code> meta table
070: *
071: * @version $Revision: 1.2 $ $Date: 2004/08/24 01:09:26 $
072: * @author Jonathan Giron
073: */
074: public class AxionTablePropertiesMetaTableUpdater extends
075: BaseDatabaseModificationListener implements
076: DatabaseModificationListener {
077: private static Log _log = LogFactory
078: .getLog(AxionTablePropertiesMetaTableUpdater.class);
079: private Database _db = null;
080:
081: public AxionTablePropertiesMetaTableUpdater(Database db) {
082: _db = db;
083: }
084:
085: public void tableAdded(DatabaseModifiedEvent e) {
086: Table tbl = e.getTable();
087: Table propsTable = null;
088:
089: try {
090: propsTable = _db.getTable("AXION_TABLE_PROPERTIES");
091: } catch (AxionException ex) {
092: _log
093: .error(
094: "Unable to locate system table AXION_TABLE_PROPERTIES for update",
095: ex);
096: }
097:
098: List rows = createRowsForAddedTable(tbl);
099: Iterator iter = rows.iterator();
100: while (iter.hasNext() && propsTable != null) {
101: try {
102: propsTable.addRow((Row) iter.next());
103: } catch (AxionException ex) {
104: _log.error("Unable to mention table in system tables",
105: ex);
106: }
107: }
108: }
109:
110: private List createRowsForAddedTable(Table table) {
111: List rowList = Collections.EMPTY_LIST;
112: if (table instanceof TransactableTableImpl) {
113: table = ((TransactableTableImpl) table).getTable();
114: }
115:
116: if (table instanceof ExternalTable) {
117: rowList = new ArrayList();
118:
119: final String tableName = table.getName();
120:
121: ExternalTable ffTable = (ExternalTable) table;
122: Properties p = ffTable.getTableProperties();
123: if (p != null) {
124: Iterator iter = p.entrySet().iterator();
125: while (iter.hasNext()) {
126: Map.Entry entry = (Map.Entry) iter.next();
127: String key = (String) entry.getKey();
128:
129: SimpleRow row = new SimpleRow(3);
130: row.set(0, tableName);
131: row.set(1, key);
132: row.set(2, entry.getValue());
133:
134: rowList.add(row);
135: }
136: }
137: }
138: return rowList;
139: }
140:
141: public void tableDropped(DatabaseModifiedEvent e) {
142: FunctionIdentifier fn = new FunctionIdentifier("=");
143: fn.addArgument(new ColumnIdentifier("TABLE_NAME"));
144: fn.addArgument(new Literal(e.getTable().getName()));
145: AxionCommand cmd = new DeleteCommand("AXION_TABLE_PROPERTIES",
146: fn);
147: try {
148: cmd.execute(_db);
149: } catch (AxionException ex) {
150: _log
151: .error(
152: "Unable to remove mention of table in system tables",
153: ex);
154: }
155: }
156: }
|