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: * @see org.mandarax.sql.SQLFunction
034: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
035: * @version 3.4 <7 March 05>
036: * @since 1.6
037: * @deprecated from v 3.4 - support for new features such as validation will not be added to XKB, please use ZKB instead
038: */
039:
040: public class XMLAdapter4SQLFunctions extends AbstractXMLAdapter {
041: public static final String SQL_FUNCTION = "sql_function";
042: public static final String NAME = "name";
043: public static final String QUERY = "query";
044: public static final String STRUCTURE = "structure";
045: public static final String MAPPING = "mapping";
046:
047: /**
048: * Export an object, i.e., convert it to an element in the DOM.
049: * @param obj an object
050: * @param driver the generic driver
051: * @param cache a cache used in order to associate the same
052: * id with various occurences of the same object
053: * @exception an XKBException is thrown if export fails
054: */
055: public Element exportObject(Object obj, GenericDriver driver,
056: Map cache) throws XKBException {
057: check(obj, SQLFunction.class);
058: SQLFunction f = (SQLFunction) obj;
059: Element e = new Element(SQL_FUNCTION);
060: // add name and query
061: e.setAttribute(NAME, f.getName());
062: e.setAttribute(QUERY, f.getQuery());
063:
064: // add structure
065: Element es = new Element(STRUCTURE);
066: e.addContent(es);
067: exportChildren(f.getStructure(), es, GenericDriver.TYPE,
068: driver, cache);
069:
070: // add mapping (use object adapter)
071: Element em = new Element(MAPPING);
072: e.addContent(em);
073: Element eo = exportObject(f.getObjectRelationalMapping(),
074: GenericDriver.OBJECT, driver, cache);
075: em.addContent(eo);
076: // add data source
077: Element ed = exportObject(f.getDataSource(),
078: GenericDriver.DATA_SOURCE, driver, cache);
079: e.addContent(ed);
080:
081: return e;
082: }
083:
084: /**
085: * Build an object from an XML element.
086: * @param e an element
087: * @param driver the generic driver
088: * @param cache a cache used to identify objects that have the same id
089: * @param lfactory the logic factory used to create objects
090: * @exception an XKBException is thrown if import fails
091: */
092: public Object importObject(Element e, GenericDriver driver,
093: Map cache, LogicFactory lfactory) throws XKBException {
094: // get query and name
095: String query = e.getAttributeValue(QUERY);
096: String name = e.getAttributeValue(NAME);
097: // get structure
098: Element eStruct = e.getChild(STRUCTURE);
099: Class[] structure = (Class[]) importChildren(eStruct, null,
100: driver, cache, lfactory, Class.class);
101: // get mapping
102: Element eMap = e.getChild(MAPPING);
103: SQLObjectRelationalMapping map = (SQLObjectRelationalMapping) importChild(
104: eMap, GenericDriver.OBJECT, driver, cache, lfactory);
105:
106: // get data source
107: Element eDatasource = (Element) e.getChildren().get(2);
108: XMLAdapter adapter = driver.getAdapterByTagName(eDatasource
109: .getName());
110: DataSource ds = (DataSource) adapter.importObject(eDatasource,
111: driver, cache, lfactory);
112:
113: // build function
114: SQLFunction function = new SQLFunction();
115: function.setDataSource(ds);
116: function.setName(name);
117: function.setQuery(query);
118: function.setObjectRelationalMapping(map);
119: function.setStructure(structure);
120: return function;
121:
122: }
123:
124: /**
125: * Get the name of the associated tag (element).
126: * @return a string
127: */
128: public String getTagName() {
129: return SQL_FUNCTION;
130: }
131:
132: /**
133: * Get the kind of object the adapter can export/import.
134: * @return a string
135: */
136: public String getKindOfObject() {
137: return GenericDriver.SQL_FUNCTION;
138: }
139: }
|