001: package org.mandarax.xkb.framework;
002:
003: /**
004: * Copyright (C) 1999-2004 Jens Dietrich (mailto:mandarax@jbdietrich.com)
005: *
006: * This library 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 library 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:
021: import java.util.Map;
022:
023: import javax.sql.DataSource;
024:
025: import org.jdom.Element;
026: import org.mandarax.kernel.LogicFactory;
027: import org.mandarax.sql.SQLPredicate;
028: import org.mandarax.sql.SQLTypeMapping;
029: import org.mandarax.xkb.XKBException;
030:
031: /**
032: * An adapter class for sql predicates. Predicates are exported with a unique id
033: * that will allow other objects to share them.
034: * @see org.mandarax.sql.SQLPredicate
035: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
036: * @version 3.4 <7 March 05>
037: * @since 1.6
038: * @deprecated from v 3.4 - support for new features such as validation will not be added to XKB, please use ZKB instead
039: */
040:
041: public class XMLAdapter4SQLPredicates extends CachedXMLAdapter {
042: public static final String SQL_PREDICATE = "sql_predicate";
043: public static final String ID = "id";
044: public static final String NAME = "name";
045: public static final String QUERY = "query";
046: public static final String STRUCTURE = "structure";
047: public static final String MAPPING = "mapping";
048:
049: /**
050: * Export a sql predicate, i.e., convert it to an element in the DOM.
051: * @param obj an object
052: * @param driver the generic driver
053: * @param cache a cache used in order to associate the same
054: * id with various occurences of the same object
055: * @exception an XKBException is thrown if export fails
056: */
057: protected Element _exportObject(Object obj, GenericDriver driver,
058: Map cache) throws XKBException {
059: check(obj, SQLPredicate.class);
060:
061: SQLPredicate p = (SQLPredicate) obj;
062: Element e = new Element(SQL_PREDICATE);
063:
064: // add name and query
065: e.setAttribute(NAME, p.getName());
066: e.setAttribute(QUERY, p.getQuery());
067:
068: // add data source
069: Element ed = exportObject(p.getDataSource(),
070: GenericDriver.DATA_SOURCE, driver, cache);
071: e.addContent(ed);
072:
073: // add structure
074: Element es = new Element(STRUCTURE);
075: e.addContent(es);
076: exportChildren(p.getStructure(), es, GenericDriver.TYPE,
077: driver, cache);
078:
079: // add mapping (use object adapter)
080: Element em = new Element(MAPPING);
081: e.addContent(em);
082: Element eo = exportObject(p.getTypeMapping(),
083: GenericDriver.OBJECT, driver, cache);
084: em.addContent(eo);
085:
086: return e;
087: }
088:
089: /**
090: * Build an object from an XML element.
091: * @param e an element
092: * @param driver the generic driver
093: * @param cache a cache used to identify objects that have the same id
094: * @param lfactory the logic factory used to create objects
095: * @exception an XKBException is thrown if export fails
096: */
097: protected Object _importObject(Element e, GenericDriver driver,
098: Map cache, LogicFactory lfactory) throws XKBException {
099:
100: // get query and name
101: String name = e.getAttributeValue(NAME);
102: String query = e.getAttributeValue(QUERY);
103:
104: // get data source
105: DataSource datasource = (DataSource) this .importChild(e,
106: GenericDriver.DATA_SOURCE, driver, cache, lfactory);
107:
108: // get structure
109: Element eStruct = e.getChild(STRUCTURE);
110: Class[] structure = (Class[]) importChildren(eStruct, null,
111: driver, cache, lfactory, Class.class);
112:
113: // get type mapping
114: Element eMap = e.getChild(MAPPING);
115: SQLTypeMapping map = (SQLTypeMapping) importChild(eMap,
116: GenericDriver.OBJECT, driver, cache, lfactory);
117:
118: // build SQL predicate
119: SQLPredicate p = new SQLPredicate();
120: p.setDataSource(datasource);
121: p.setName(name);
122:
123: p.setQuery(query);
124: p.setStructure(structure);
125:
126: return p;
127:
128: }
129:
130: /**
131: * Get the name of the associated tag (element).
132: * @return a string
133: */
134: public String getTagName() {
135: return SQL_PREDICATE;
136: }
137:
138: /**
139: * Get the kind of object the adapter can export/import.
140: * @return a string
141: */
142: public String getKindOfObject() {
143: return GenericDriver.SQL_PREDICATE;
144: }
145: }
|