001: //=============================================================================
002: //=== Copyright (C) 2001-2007 Food and Agriculture Organization of the
003: //=== United Nations (FAO-UN), United Nations World Food Programme (WFP)
004: //=== and United Nations Environment Programme (UNEP)
005: //===
006: //=== This program is free software; you can redistribute it and/or modify
007: //=== it under the terms of the GNU General Public License as published by
008: //=== the Free Software Foundation; either version 2 of the License, or (at
009: //=== your option) any later version.
010: //===
011: //=== This program is distributed in the hope that it will be useful, but
012: //=== WITHOUT ANY WARRANTY; without even the implied warranty of
013: //=== MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: //=== General Public License for more details.
015: //===
016: //=== You should have received a copy of the GNU General Public License
017: //=== along with this program; if not, write to the Free Software
018: //=== Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
019: //===
020: //=== Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,
021: //=== Rome - Italy. email: geonetwork@osgeo.org
022: //==============================================================================
023:
024: package org.fao.geonet.kernel;
025:
026: import java.sql.SQLException;
027: import java.util.List;
028: import java.util.Vector;
029: import jeeves.constants.Jeeves;
030: import jeeves.resources.dbms.Dbms;
031: import jeeves.server.UserSession;
032: import jeeves.utils.Util;
033: import jeeves.utils.Xml;
034: import org.fao.geonet.util.ISODate;
035: import org.jdom.Element;
036:
037: //=============================================================================
038:
039: /** This class is responsible of reading and writing xml on the database. It
040: * works on tables like (id, data, lastChangeDate)
041: */
042:
043: public class XmlSerializer {
044: //--------------------------------------------------------------------------
045: //---
046: //--- API
047: //---
048: //--------------------------------------------------------------------------
049:
050: /** Retrieve the xml element which id matches the given one. The element is
051: * read from 'table' and the string read is converted into xml
052: */
053:
054: public static Element select(Dbms dbms, String table, String id)
055: throws Exception {
056: String query = "SELECT * FROM " + table + " WHERE id = ?";
057:
058: Element rec = dbms.select(query, new Integer(id)).getChild(
059: Jeeves.Elem.RECORD);
060:
061: if (rec == null)
062: return null;
063:
064: String xmlData = rec.getChildText("data");
065:
066: // if (!xmlData.startsWith("<?xml"))
067: // xmlData = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n" + xmlData;
068:
069: rec = Xml.loadString(xmlData, false);
070:
071: return (Element) rec.detach();
072: }
073:
074: //--------------------------------------------------------------------------
075:
076: public static String insert(Dbms dbms, String schema, Element xml,
077: int serial, String source, String uuid, int owner,
078: String groupOwner) throws SQLException {
079: return insert(dbms, schema, xml, serial, source, uuid, null,
080: null, "n", null, owner, groupOwner);
081: }
082:
083: //--------------------------------------------------------------------------
084:
085: public static String insert(Dbms dbms, String schema, Element xml,
086: int serial, String source, String uuid, String isTemplate,
087: String title, int owner, String groupOwner)
088: throws SQLException {
089: return insert(dbms, schema, xml, serial, source, uuid, null,
090: null, isTemplate, title, owner, groupOwner);
091: }
092:
093: //--------------------------------------------------------------------------
094:
095: public static String insert(Dbms dbms, String schema, Element xml,
096: int serial, String source, String uuid, String createDate,
097: String changeDate, String isTemplate, String title,
098: int owner, String groupOwner) throws SQLException {
099: String date = new ISODate().toString();
100:
101: if (createDate == null)
102: createDate = date;
103:
104: if (changeDate == null)
105: changeDate = date;
106:
107: fixCR(xml);
108:
109: StringBuffer fields = new StringBuffer(
110: "id, schemaId, data, createDate, changeDate, source, "
111: + "uuid, isTemplate, isHarvested, root, owner, groupOwner");
112: StringBuffer values = new StringBuffer(
113: "?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?");
114:
115: Vector args = new Vector();
116: args.add(new Integer(serial));
117: args.add(schema);
118: args.add(Xml.getString(xml));
119: args.add(createDate);
120: args.add(changeDate);
121: args.add(source);
122: args.add(uuid.toLowerCase());
123: args.add(isTemplate);
124: args.add("n");
125: args.add(xml.getQualifiedName());
126: args.add(owner);
127:
128: if (groupOwner != null)
129: args.add(new Integer(groupOwner));
130: else
131: args.add(null);
132:
133: if (title != null) {
134: fields.append(", title");
135: values.append(", ?");
136: args.add(title);
137: }
138:
139: String query = "INSERT INTO Metadata (" + fields + ") VALUES("
140: + values + ")";
141: dbms.execute(query, args.toArray());
142:
143: return Integer.toString(serial);
144: }
145:
146: //--------------------------------------------------------------------------
147: /** Updates an xml element into the database. The new data replaces the old one
148: */
149:
150: public static void update(Dbms dbms, String id, Element xml)
151: throws SQLException {
152: update(dbms, id, xml, null);
153: }
154:
155: //--------------------------------------------------------------------------
156:
157: public static void update(Dbms dbms, String id, Element xml,
158: String changeDate) throws SQLException {
159: String query = "UPDATE Metadata SET data=?, changeDate=?, root=? WHERE id=?";
160:
161: Vector args = new Vector();
162:
163: fixCR(xml);
164: args.add(Xml.getString(xml));
165:
166: if (changeDate == null)
167: args.add(new ISODate().toString());
168: else
169: args.add(changeDate);
170:
171: args.add(xml.getQualifiedName());
172: args.add(new Integer(id));
173:
174: dbms.execute(query, args.toArray());
175: }
176:
177: //--------------------------------------------------------------------------
178: /** Deletes an xml element given its id
179: */
180:
181: public static void delete(Dbms dbms, String table, String id)
182: throws SQLException {
183: String query = "DELETE FROM " + table + " WHERE id=" + id;
184:
185: dbms.execute(query);
186: }
187:
188: //--------------------------------------------------------------------------
189:
190: private static void fixCR(Element xml) {
191: List list = xml.getChildren();
192:
193: if (list.size() == 0) {
194: String text = xml.getText();
195:
196: xml.setText(Util.replaceString(text, "\r\n", "\n"));
197: }
198:
199: else
200: for (Object o : list)
201: fixCR((Element) o);
202: }
203:
204: //---------------------------------------------------------------------------
205:
206: // public static void dump(String name, String text)
207: // {
208: // System.out.print("name: "+name+", value:");
209: //
210: // char[] chars = text.toCharArray();
211: //
212: // for(char c: chars)
213: // {
214: // int i = c;
215: //
216: // if (i>=32) System.out.print(c);
217: // else System.out.print("["+ i +"]");
218: // }
219: //
220: // System.out.println("");
221: // }
222: }
223:
224: //=============================================================================
|