001: /*
002: * argun 1.0
003: * Web 2.0 delivery framework
004: * Copyright (C) 2007 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.web.mda.db.model;
024:
025: import java.sql.Connection;
026: import java.sql.DatabaseMetaData;
027: import java.sql.SQLException;
028: import java.util.ArrayList;
029: import java.util.Collection;
030: import java.util.Iterator;
031:
032: import org.w3c.dom.Element;
033:
034: import biz.hammurapi.sql.SQLProcessor;
035: import biz.hammurapi.sql.metadata.GenerationPolicy;
036: import biz.hammurapi.util.CollectionVisitable;
037: import biz.hammurapi.util.VisitableBase;
038: import biz.hammurapi.util.Visitor;
039: import biz.hammurapi.web.mda.TemplateFactory;
040: import biz.hammurapi.web.metadata.MetadataEngine;
041: import biz.hammurapi.xml.dom.AbstractDomObject;
042: import biz.hammurapi.xml.dom.CompositeDomSerializer;
043: import biz.hammurapi.xml.dom.DomSerializable;
044:
045: /**
046: * Represents information from a particular database.
047: * @author Pavel
048: *
049: */
050: public class Database extends VisitableBase implements DomSerializable {
051:
052: private Collection catalogs = new ArrayList();
053: private GenerationPolicy generationPolicy;
054: private TemplateFactory templateFactory;
055:
056: /**
057: * Loads database object descriptors from engine and then augments them with
058: * JDBC metadata retrieved from metadataSource.
059: * @param engine
060: * @param metaDataSource
061: * @throws SQLException
062: */
063: public Database(MetadataEngine engine, String processorName,
064: SQLProcessor metadataSource,
065: GenerationPolicy generationPolicy) throws SQLException {
066: engine.getDbmCatalogByProcessor(processorName, catalogs,
067: Catalog.class);
068: Connection con = metadataSource.getConnection();
069: Iterator it = catalogs.iterator();
070: DatabaseMetaData metaData = con.getMetaData();
071: while (it.hasNext()) {
072: Catalog catalog = (Catalog) it.next();
073: catalog.setOwner(this );
074: catalog.loadMetaData(metaData);
075: }
076: metadataSource.releaseConnection(con);
077: this .generationPolicy = generationPolicy;
078: }
079:
080: protected void acceptChildren(Visitor visitor) {
081: new CollectionVisitable(catalogs, false).accept(visitor);
082: }
083:
084: public Collection getCatalogs() {
085: return catalogs;
086: }
087:
088: public void toDom(Element holder) {
089: CompositeDomSerializer.getThreadInstance().toDomSerializable(
090: catalogs).toDom(
091: AbstractDomObject.addElement(holder, "catalogs"));
092: }
093:
094: public GenerationPolicy getGenerationPolicy() {
095: return generationPolicy;
096: }
097:
098: public void setTemplateFactory(TemplateFactory templateFactory) {
099: this .templateFactory = templateFactory;
100: }
101:
102: public TemplateFactory getTemplateFactory() {
103: return templateFactory;
104: }
105: }
|