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.SQLFunction;
028: import org.mandarax.sql.SQLObjectRelationalMapping;
029: import org.mandarax.xkb.XKBException;
030:
031: /**
032: * An adapter class for sql functions.
033: * New is the support for the close connection property.
034: * @see org.mandarax.sql.SQLFunction
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 2.0
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 XMLAdapter4SQLFunctions2 extends AbstractXMLAdapter {
042: public static final String SQL_FUNCTION = "sql_function";
043: public static final String NAME = "name";
044: public static final String QUERY = "query";
045: public static final String STRUCTURE = "structure";
046: public static final String MAPPING = "mapping";
047: public static final String CLOSE_CONNECTION = "close_connection";
048:
049: /**
050: * Export an object, 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: public Element exportObject(Object obj, GenericDriver driver,
058: Map cache) throws XKBException {
059: check(obj, SQLFunction.class);
060: SQLFunction f = (SQLFunction) obj;
061: Element e = new Element(SQL_FUNCTION);
062: // add name and query
063: e.setAttribute(NAME, f.getName());
064: e.setAttribute(QUERY, f.getQuery());
065: e.setAttribute(CLOSE_CONNECTION, f.isCloseConnection() ? "true"
066: : "false");
067:
068: // add structure
069: Element es = new Element(STRUCTURE);
070: e.addContent(es);
071: exportChildren(f.getStructure(), es, GenericDriver.TYPE,
072: driver, cache);
073:
074: // add mapping (use object adapter)
075: Element em = new Element(MAPPING);
076: e.addContent(em);
077: Element eo = exportObject(f.getObjectRelationalMapping(),
078: GenericDriver.OBJECT, driver, cache);
079: em.addContent(eo);
080: // add data source
081: Element ed = exportObject(f.getDataSource(),
082: GenericDriver.DATA_SOURCE, driver, cache);
083: e.addContent(ed);
084:
085: return e;
086: }
087:
088: /**
089: * Build an object from an XML element.
090: * @param e an element
091: * @param driver the generic driver
092: * @param cache a cache used to identify objects that have the same id
093: * @param lfactory the logic factory used to create objects
094: * @exception an XKBException is thrown if import fails
095: */
096: public Object importObject(Element e, GenericDriver driver,
097: Map cache, LogicFactory lfactory) throws XKBException {
098: // get query and name
099: String query = e.getAttributeValue(QUERY);
100: String name = e.getAttributeValue(NAME);
101: // get structure
102: Element eStruct = e.getChild(STRUCTURE);
103: Class[] structure = (Class[]) importChildren(eStruct, null,
104: driver, cache, lfactory, Class.class);
105: // get mapping
106: Element eMap = e.getChild(MAPPING);
107: SQLObjectRelationalMapping map = (SQLObjectRelationalMapping) importChild(
108: eMap, GenericDriver.OBJECT, driver, cache, lfactory);
109:
110: // get data source
111: Element eDatasource = (Element) e.getChildren().get(2);
112: XMLAdapter adapter = driver.getAdapterByTagName(eDatasource
113: .getName());
114: DataSource ds = (DataSource) adapter.importObject(eDatasource,
115: driver, cache, lfactory);
116:
117: // build function
118: SQLFunction function = new SQLFunction();
119: function.setDataSource(ds);
120: function.setName(name);
121: function.setQuery(query);
122: function.setCloseConnection("true".equals(e
123: .getAttributeValue(CLOSE_CONNECTION)));
124: function.setObjectRelationalMapping(map);
125: function.setStructure(structure);
126: return function;
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_FUNCTION;
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_FUNCTION;
144: }
145: }
|