KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > dataxslt > db > PageSetDB


1 /*
2   Copyright (C) 2003 Know Gate S.L. All rights reserved.
3                       C/Oña, 107 1º2 28050 Madrid (Spain)
4
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions
7   are met:
8
9   1. Redistributions of source code must retain the above copyright
10      notice, this list of conditions and the following disclaimer.
11
12   2. The end-user documentation included with the redistribution,
13      if any, must include the following acknowledgment:
14      "This product includes software parts from hipergate
15      (http://www.hipergate.org/)."
16      Alternately, this acknowledgment may appear in the software itself,
17      if and wherever such third-party acknowledgments normally appear.
18
19   3. The name hipergate must not be used to endorse or promote products
20      derived from this software without prior written permission.
21      Products derived from this software may not be called hipergate,
22      nor may hipergate appear in their name, without prior written
23      permission.
24
25   This library is distributed in the hope that it will be useful,
26   but WITHOUT ANY WARRANTY; without even the implied warranty of
27   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28
29   You should have received a copy of hipergate License with this code;
30   if not, visit http://www.hipergate.org or mail to info@hipergate.org
31 */

32
33 package com.knowgate.dataxslt.db;
34
35 import java.util.Date JavaDoc;
36
37 import java.io.File JavaDoc;
38
39 import java.sql.SQLException JavaDoc;
40 import java.sql.Statement JavaDoc;
41 import java.sql.PreparedStatement JavaDoc;
42 import java.sql.CallableStatement JavaDoc;
43 import java.sql.ResultSet JavaDoc;
44 import java.sql.Types JavaDoc;
45
46 import com.knowgate.debug.DebugFile;
47 import com.knowgate.jdc.JDCConnection;
48 import com.knowgate.dataobjs.DB;
49 import com.knowgate.dataobjs.DBBind;
50 import com.knowgate.dataobjs.DBPersist;
51 import com.knowgate.misc.Gadgets;
52
53 /**
54  *
55  * <p>PageSet database index</p>
56  * @author Sergio Montoro Ten
57  * @version 1.1
58  */

59
60 public class PageSetDB extends DBPersist {
61
62   public PageSetDB() {
63     super(DB.k_pagesets, "PageSetDB");
64   }
65
66   public PageSetDB(JDCConnection oConn,String JavaDoc sPageSetGUID) throws SQLException JavaDoc {
67     super(DB.k_pagesets, "PageSetDB");
68     Object JavaDoc aPageSet[] = {sPageSetGUID};
69     if (!load(oConn,aPageSet))
70       throw new SQLException JavaDoc ("Could not find PageSet " + sPageSetGUID + " at " + DB.k_pagesets);
71   }
72
73   // ----------------------------------------------------------
74

75   public boolean load(JDCConnection oConn, Object JavaDoc aPK[]) throws SQLException JavaDoc {
76     // Rutina especial de carga con procedimiento almacenado para maxima velocidad en el rendering
77

78     ResultSet JavaDoc oRSet;
79     PreparedStatement JavaDoc oStmt;
80     CallableStatement JavaDoc oCall;
81     String JavaDoc sNmPageSet;
82     Object JavaDoc sField;
83     boolean bRetVal;
84
85     if (DebugFile.trace) {
86       DebugFile.writeln("Begin PageSetDB.load([Connection], {" + aPK[0] + "}");
87       DebugFile.incIdent();
88     }
89
90     if (oConn.getDataBaseProduct()==JDCConnection.DBMS_ORACLE ||
91         oConn.getDataBaseProduct()==JDCConnection.DBMS_MSSQL ||
92         oConn.getDataBaseProduct()==JDCConnection.DBMS_MYSQL ) {
93
94       if (DebugFile.trace)
95         DebugFile.writeln("Connection.prepareCall({ call k_sp_read_pageset ('" + aPK[0] + "',?,?,?,?,?,?,?,?,?,?,?,?,?) }");
96
97       oCall = oConn.prepareCall("{ call k_sp_read_pageset (?,?,?,?,?,?,?,?,?,?,?,?,?,?) }");
98
99       clear();
100
101       oCall.setObject(1, aPK[0], Types.CHAR); // gu_pageset
102
oCall.registerOutParameter(2, Types.CHAR); // gu_microsite
103
oCall.registerOutParameter(3, Types.VARCHAR); // nm_microsite
104
oCall.registerOutParameter(4, Types.CHAR); // gu_workarea
105
oCall.registerOutParameter(5, Types.VARCHAR); // nm_pageset
106
oCall.registerOutParameter(6, Types.VARCHAR); // vs_stamp
107
oCall.registerOutParameter(7, Types.CHAR); // id_language
108
oCall.registerOutParameter(8, Types.TIMESTAMP); // dt_modified
109
oCall.registerOutParameter(9, Types.VARCHAR); // path_data
110
oCall.registerOutParameter(10, Types.VARCHAR); // id_status
111
oCall.registerOutParameter(11, Types.VARCHAR); // path_metadata
112
oCall.registerOutParameter(12, Types.VARCHAR); // tx_comments
113
oCall.registerOutParameter(13, Types.CHAR); // gu_company
114
oCall.registerOutParameter(14, Types.CHAR); // gu_project
115

116       if (DebugFile.trace) DebugFile.writeln("CallableStatement.execute()");
117
118       oCall.execute();
119
120       sNmPageSet = oCall.getString(5);
121       bRetVal = (null!=sNmPageSet);
122
123       put (DB.gu_pageset, aPK[0]);
124
125       if (bRetVal) {
126         put (DB.gu_microsite, oCall.getString(2).trim());
127         put (DB.nm_microsite, oCall.getString(3));
128         put (DB.gu_workarea, oCall.getString(4).trim());
129         put (DB.nm_pageset, oCall.getString(5));
130
131         sField = oCall.getObject(6);
132         if (!oCall.wasNull()) put(DB.vs_stamp, (String JavaDoc) sField);
133
134         sField = oCall.getObject(7);
135         if (!oCall.wasNull()) put(DB.id_language, ((String JavaDoc) sField).trim());
136
137         sField = oCall.getObject(8);
138         if (!oCall.wasNull()) put(DB.dt_modified, oCall.getDate(8));
139
140         sField = oCall.getObject(9);
141         if (!oCall.wasNull()) put(DB.path_data, (String JavaDoc) sField);
142
143         sField = oCall.getObject(10);
144         if (!oCall.wasNull()) put(DB.id_status, (String JavaDoc) sField);
145
146         sField = oCall.getObject(11);
147         if (!oCall.wasNull()) put(DB.path_metadata, (String JavaDoc) sField);
148
149         sField = oCall.getObject(12);
150         if (!oCall.wasNull()) put(DB.tx_comments, (String JavaDoc) sField);
151
152         sField = oCall.getObject(13);
153         if (!oCall.wasNull()) put(DB.gu_company, sField.toString().trim());
154
155         sField = oCall.getObject(14);
156         if (!oCall.wasNull()) put(DB.gu_project, sField.toString().trim());
157       } // fi (bRetVal)
158

159       oCall.close();
160     }
161     else {
162
163       if (DebugFile.trace)
164         DebugFile.writeln("Connection.prepareStatement(SELECT m." + DB.gu_microsite + ",m." + DB.nm_microsite +
165                                      ",p." + DB.gu_workarea + ",p." + DB.nm_pageset + ",p." + DB.vs_stamp +
166                                      ",p." + DB.id_language + ",p." + DB.dt_modified + ",p." + DB.path_data +
167                                      ",p." + DB.id_status + ",m." + DB.path_metadata + ",p." + DB.tx_comments +
168                                      ",p." + DB.gu_company + ",p." + DB.gu_project +
169                                      " FROM " + DB.k_pagesets + " p LEFT OUTER JOIN " + DB.k_microsites +
170                                      " m ON p." + DB.gu_microsite + "=m." + DB.gu_microsite + " WHERE p." + DB.gu_pageset + "=" + aPK[0] + ")");
171
172       oStmt = oConn.prepareStatement("SELECT m." + DB.gu_microsite + ",m." + DB.nm_microsite +
173                                      ",p." + DB.gu_workarea + ",p." + DB.nm_pageset + ",p." + DB.vs_stamp +
174                                      ",p." + DB.id_language + ",p." + DB.dt_modified + ",p." + DB.path_data +
175                                      ",p." + DB.id_status + ",m." + DB.path_metadata + ",p." + DB.tx_comments +
176                                      ",p." + DB.gu_company + ",p." + DB.gu_project +
177                                      " FROM " + DB.k_pagesets+ " p LEFT OUTER JOIN " + DB.k_microsites +
178                                      " m ON p." + DB.gu_microsite + "=m." + DB.gu_microsite + " WHERE p." + DB.gu_pageset + "=?",
179                                      ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
180
181       oStmt.setObject(1, aPK[0], Types.CHAR);
182       oRSet = oStmt.executeQuery();
183
184       bRetVal = oRSet.next();
185
186       put (DB.gu_pageset, aPK[0]);
187
188       if (bRetVal) {
189         put (DB.gu_microsite, oRSet.getString(1));
190         put (DB.nm_microsite, oRSet.getString(2));
191         put (DB.gu_workarea, oRSet.getString(3));
192         put (DB.nm_pageset, oRSet.getString(4));
193
194         sField = oRSet.getObject(5);
195         if (!oRSet.wasNull()) put(DB.vs_stamp, (String JavaDoc) sField);
196
197         sField = oRSet.getObject(6);
198         if (!oRSet.wasNull()) put(DB.id_language, (String JavaDoc) sField);
199
200         sField = oRSet.getObject(7);
201         if (!oRSet.wasNull()) put(DB.dt_modified, oRSet.getTimestamp(7));
202
203         sField = oRSet.getObject(8);
204         if (!oRSet.wasNull()) put(DB.path_data, (String JavaDoc) sField);
205
206         sField = oRSet.getObject(9);
207         if (!oRSet.wasNull()) put(DB.id_status, (String JavaDoc) sField);
208
209         sField = oRSet.getObject(10);
210         if (!oRSet.wasNull()) put(DB.path_metadata, (String JavaDoc) sField);
211
212         sField = oRSet.getObject(11);
213         if (!oRSet.wasNull()) put(DB.tx_comments, (String JavaDoc) sField);
214
215         sField = oRSet.getObject(12);
216         if (!oRSet.wasNull()) put(DB.gu_company, (String JavaDoc) sField);
217
218         sField = oRSet.getObject(13);
219         if (!oRSet.wasNull()) put(DB.gu_project, (String JavaDoc) sField);
220       }
221       oRSet.close();
222       oStmt.close();
223     }
224
225     if (DebugFile.trace) {
226       DebugFile.decIdent();
227       DebugFile.writeln("End PageSetDB.load() : " + String.valueOf(bRetVal));
228     }
229
230     return bRetVal;
231   } // load
232

233   // ----------------------------------------------------------
234

235   public boolean store(JDCConnection oConn) throws SQLException JavaDoc {
236     java.sql.Timestamp JavaDoc dtNow = new java.sql.Timestamp JavaDoc(DBBind.getTime());
237
238     if (!AllVals.containsKey(DB.gu_pageset))
239       put(DB.gu_pageset, Gadgets.generateUUID());
240
241     // Forzar la fecha de modificación del registro
242
replace(DB.dt_modified, dtNow);
243
244     return super.store(oConn);
245   } // store
246

247   // ----------------------------------------------------------
248

249   public boolean delete(JDCConnection oConn) throws SQLException JavaDoc {
250     boolean bRetVal;
251     File JavaDoc oXFil;
252     Statement JavaDoc oStmt;
253
254     if (DebugFile.trace) {
255       DebugFile.writeln("Begin PageSetDB.delete([Connection])");
256       DebugFile.incIdent();
257     }
258
259     if (exists(oConn)) {
260       if (DebugFile.trace) DebugFile.writeln("PageSet " + getString(DB.gu_pageset) + " exists");
261
262       if (!isNull(DB.path_data)) {
263           oXFil = new File JavaDoc(getString(DB.path_data));
264           if (oXFil.exists()) {
265             if (DebugFile.trace) DebugFile.writeln("File.delete(" + getString(DB.path_data) + ")");
266             oXFil.delete();
267           }
268           oXFil = null;
269       } // fi (path_data)
270

271       if (DBBind.exists(oConn, DB.k_list_jobs, "U")) {
272         oStmt = oConn.createStatement();
273         if (DebugFile.trace) DebugFile.writeln("Connection.execute(" + "DELETE FROM " + DB.k_list_jobs + " WHERE " + DB.gu_pageset + "='" + getString(DB.gu_pageset) + "'" + ")");
274         oStmt.execute("DELETE FROM " + DB.k_list_jobs + " WHERE " + DB.gu_pageset + "='" + getString(DB.gu_pageset) + "'");
275         oStmt.close();
276       }
277
278       if (DBBind.exists(oConn, DB.k_pageset_answers, "U")) {
279         oStmt = oConn.createStatement();
280         oStmt.execute("DELETE FROM " + DB.k_pageset_answers + " WHERE " + DB.gu_pageset + "='" + getString(DB.gu_pageset) + "'");
281         oStmt.execute("DELETE FROM " + DB.k_pageset_datasheets + " WHERE " + DB.gu_pageset + "='" + getString(DB.gu_pageset) + "'");
282         oStmt.execute("DELETE FROM " + DB.k_datasheets_lookup + " WHERE " + DB.gu_owner + "='" + getString(DB.gu_pageset) + "'");
283         oStmt.close();
284       }
285
286       oStmt = oConn.createStatement();
287       oStmt.execute("DELETE FROM " + DB.k_pageset_pages + " WHERE " + DB.gu_pageset + "='" + getString(DB.gu_pageset) + "'");
288       oStmt.close();
289
290       bRetVal = super.delete(oConn);
291     }
292     else
293       bRetVal = false;
294
295     if (DebugFile.trace) {
296       DebugFile.decIdent();
297       DebugFile.writeln("End PageSetDB.delete()" + String.valueOf(bRetVal));
298     }
299
300     return bRetVal;
301   } // delete
302

303   // ----------------------------------------------------------
304

305   public boolean existsFile() {
306     if (!isNull(DB.path_data))
307       return new File JavaDoc(getString(DB.path_data)).exists();
308     else
309       return false;
310   } // existsFile
311

312   // ----------------------------------------------------------
313

314   public void setPage(JDCConnection oConn, String JavaDoc sIdPage, int iPgPage, String JavaDoc sTlPage, String JavaDoc sPathPage) throws SQLException JavaDoc {
315     PreparedStatement JavaDoc oStmt;
316     ResultSet JavaDoc oRSet;
317     String JavaDoc sGuPage;
318     String JavaDoc sSQL;
319
320     if (DebugFile.trace) {
321       DebugFile.writeln("Begin PageSetDB.setPage([Connection], " + sIdPage + "," + String.valueOf(iPgPage) + "," + sTlPage + "," + sPathPage + ")");
322       DebugFile.incIdent();
323     }
324
325     sSQL = "SELECT " + DB.gu_page + " FROM " + DB.k_pageset_pages + " WHERE " + DB.gu_pageset + "=? AND " + DB.pg_page + "=?";
326
327     if (DebugFile.trace) DebugFile.writeln("Connection.prepareStatement(" + sSQL + ")");
328
329     oStmt = oConn.prepareStatement(sSQL);
330     oStmt.setString(1, getString(DB.gu_pageset));
331     oStmt.setInt(2, iPgPage);
332     oRSet = oStmt.executeQuery();
333     if (oRSet.next())
334       sGuPage = oRSet.getString(1);
335     else
336       sGuPage = null;
337     oRSet.close();
338     oStmt.close();
339
340     if (null==sGuPage) {
341
342       if (DebugFile.trace) DebugFile.writeln("Connection.prepareStatement(" + "INSERT INTO " + DB.k_pageset_pages + " (" + DB.gu_page + "," + DB.pg_page + "," + DB.gu_pageset + "," + DB.dt_modified + "," + DB.tl_page + "," + DB.path_page + ") VALUES ('" + sIdPage + "'," + String.valueOf(iPgPage) + ",'" + getString(DB.gu_pageset) + "'," + DBBind.escape(new Date JavaDoc(), "ts") + ",'" + sTlPage + "','" + sPathPage + "')" + ")");
343
344       sSQL = "INSERT INTO " + DB.k_pageset_pages + " (" + DB.gu_page + "," + DB.pg_page + "," + DB.gu_pageset + "," + DB.dt_modified + "," + DB.tl_page + "," + DB.path_page + ") VALUES (?,?,?," + DBBind.escape(new Date JavaDoc(), "ts") + ",?,?)";
345
346       oStmt = oConn.prepareStatement(sSQL);
347       oStmt.setString(1, sIdPage);
348       oStmt.setInt(2, iPgPage);
349       oStmt.setString(3, getString(DB.gu_pageset));
350       oStmt.setString(4, sTlPage);
351       oStmt.setString(5, sPathPage);
352       if (DebugFile.trace) DebugFile.writeln("PreparedStatement.execute()");
353       oStmt.execute();
354       oStmt.close();
355     }
356     else {
357       sSQL = "UPDATE " + DB.k_pageset_pages + " SET " + DB.dt_modified + "=" + DBBind.escape(new Date JavaDoc(), "ts") + "," + DB.tl_page + "=?," + DB.path_page + "=? WHERE " + DB.gu_page + "=?";
358       if (DebugFile.trace) DebugFile.writeln("Connection.prepareStatement)" + sSQL + ")");
359
360       oStmt = oConn.prepareStatement(sSQL);
361       oStmt.setString(1, sTlPage);
362       oStmt.setString(2, sPathPage);
363       oStmt.setString(3, sGuPage);
364       if (DebugFile.trace) DebugFile.writeln("PreparedStatement.executeUpdate()");
365       oStmt.executeUpdate();
366       oStmt.close();
367     }
368
369     if (DebugFile.trace) {
370       DebugFile.decIdent();
371       DebugFile.writeln("End PageSetDB.setPage()");
372     }
373   } // setPage
374

375   // **********************************************************
376
// Metodos Estáticos
377

378   /**
379    * <p>Get relative path to XML data file</p>
380    * Relative path is readed from field path_data of table k_pagesets.<br>
381    * For example: domains/2049/workareas/c0a801ccf90ee54e40100000ffd3af4f/apps/Mailwire/data/Basic (Dec 8 2003 11.00.01 PM).xml<br>
382    * @param oConn Database Connection
383    * @param sPageSetGUID GUID of PageSet witch data file relative path is to be retrieved
384    * @return Relative path to XML metadata file or <b>null</b> if no Microsite with such GUID was found at k_microsites table.
385    * @throws SQLException
386    */

387   public static String JavaDoc filePath(JDCConnection oConn, String JavaDoc sPageSetGUID) throws SQLException JavaDoc {
388
389     if (DebugFile.trace) {
390       DebugFile.writeln("Begin PageSetDB.filePath([Connection], " + sPageSetGUID + ")");
391       DebugFile.incIdent();
392     }
393     PreparedStatement JavaDoc oStmt;
394     ResultSet JavaDoc oRSet;
395     String JavaDoc sFilePath;
396
397     oStmt = oConn.prepareStatement("SELECT " + DB.path_data + " FROM " + DB.k_pagesets + " WHERE " + DB.gu_pageset + "=?", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
398     oStmt.setString(1, sPageSetGUID);
399     oRSet = oStmt.executeQuery();
400     if (oRSet.next())
401       sFilePath = oRSet.getString(1);
402     else
403       sFilePath = null;
404     oRSet.close();
405     oStmt.close();
406
407     if (DebugFile.trace) {
408       DebugFile.decIdent();
409       DebugFile.writeln("End PageSetDB.filePath() : " + (sFilePath!=null ? sFilePath : "null"));
410     }
411     return sFilePath;
412   } // filePath
413

414   // ----------------------------------------------------------
415

416   public static boolean delete(JDCConnection oConn, String JavaDoc sPageSetGUID) throws SQLException JavaDoc {
417     PageSetDB oPGDB = new PageSetDB(oConn, sPageSetGUID);
418     return oPGDB.delete(oConn);
419   } // delete
420

421   // **********************************************************
422
// * Variables estáticas
423

424   public static final short ClassId = 71;
425 }
426
Popular Tags