001: /**
002: * LibreSource
003: * Copyright (C) 2004-2008 Artenum SARL / INRIA
004: * http://www.libresource.org - contact@artenum.com
005: *
006: * This file is part of the LibreSource software,
007: * which can be used and distributed under license conditions.
008: * The license conditions are provided in the LICENSE.TXT file
009: * at the root path of the packaging that enclose this file.
010: * More information can be found at
011: * - http://dev.libresource.org/home/license
012: *
013: * Initial authors :
014: *
015: * Guillaume Bort / INRIA
016: * Francois Charoy / Universite Nancy 2
017: * Julien Forest / Artenum
018: * Claude Godart / Universite Henry Poincare
019: * Florent Jouille / INRIA
020: * Sebastien Jourdain / INRIA / Artenum
021: * Yves Lerumeur / Artenum
022: * Pascal Molli / Universite Henry Poincare
023: * Gerald Oster / INRIA
024: * Mariarosa Penzi / Artenum
025: * Gerard Sookahet / Artenum
026: * Raphael Tani / INRIA
027: *
028: * Contributors :
029: *
030: * Stephane Bagnier / Artenum
031: * Amadou Dia / Artenum-IUP Blois
032: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
033: */package org.libresource.wiki.ejb;
034:
035: import org.libresource.Libresource;
036: import org.libresource.LibresourceResourceBase;
037:
038: import org.libresource.wiki.EntryComparator;
039: import org.libresource.wiki.HistoryEntry;
040: import org.libresource.wiki.util.PageResourceUtil;
041:
042: import java.sql.Connection;
043: import java.sql.PreparedStatement;
044: import java.sql.ResultSet;
045: import java.sql.Statement;
046: import java.sql.Timestamp;
047:
048: import java.util.Date;
049: import java.util.TreeSet;
050:
051: import javax.ejb.CreateException;
052:
053: /**
054: * A libresource Wiki Page
055: *
056: * @libresource.resource name="Page" service="LibresourceWiki"
057: */
058: public abstract class PageResourceBean extends LibresourceResourceBase {
059: /**
060: * @ejb.create-method
061: */
062: public String ejbCreate(String name, String creator,
063: boolean versionable) throws CreateException {
064: setId(PageResourceUtil.generateGUID(this ));
065: setName(name);
066: setLastEdited(new Date());
067: setLastEditor(creator);
068: setVersionable(versionable);
069:
070: if (versionable) {
071: try {
072: saveState(creator, "initial");
073: } catch (Exception e) {
074: throw new CreateException(
075: "Cannot save initial state : " + e.getMessage());
076: }
077: }
078:
079: return null;
080: }
081:
082: // persistents fields
083:
084: /**
085: * @ejb.persistent-field
086: * @ejb.interface-method
087: * @ejb.value-object match="libresource"
088: * @ejb.transaction type="Supports"
089: */
090: public abstract String getId();
091:
092: /**
093: * @ejb.persistent-field
094: * @ejb.interface-method
095: * @ejb.transaction type="Mandatory"
096: */
097: public abstract void setId(String id);
098:
099: /**
100: * @ejb.interface-method
101: * @ejb.value-object match="libresource"
102: * @ejb.transaction type="Supports"
103: */
104: public String getShortResourceName() {
105: return getName();
106: }
107:
108: /**
109: * @ejb.persistent-field
110: * @ejb.interface-method
111: * @ejb.value-object match="libresource"
112: * @ejb.transaction type="Supports"
113: */
114: public abstract String getName();
115:
116: /**
117: * @ejb.persistent-field
118: * @ejb.interface-method
119: * @ejb.transaction type="Mandatory"
120: */
121: public abstract void setName(String name);
122:
123: /**
124: * @ejb.persistent-field
125: * @ejb.interface-method
126: * @ejb.value-object match="libresource"
127: * @ejb.transaction type="Supports"
128: */
129: public abstract String getContent();
130:
131: /**
132: * @ejb.persistent-field
133: * @ejb.interface-method
134: * @ejb.transaction type="Mandatory"
135: */
136: public abstract void setContent(String content);
137:
138: /**
139: * @ejb.persistent-field
140: * @ejb.interface-method
141: * @ejb.value-object match="libresource"
142: * @ejb.transaction type="Supports"
143: */
144: public abstract Date getLastEdited();
145:
146: /**
147: * @ejb.persistent-field
148: * @ejb.interface-method
149: * @ejb.transaction type="Mandatory"
150: */
151: public abstract void setLastEdited(Date lastEdited);
152:
153: /**
154: * @ejb.persistent-field
155: * @ejb.interface-method
156: * @ejb.value-object match="libresource"
157: * @ejb.transaction type="Supports"
158: */
159: public abstract String getLastEditor();
160:
161: /**
162: * @ejb.persistent-field
163: * @ejb.interface-method
164: * @ejb.transaction type="Mandatory"
165: */
166: public abstract void setLastEditor(String lastEditor);
167:
168: /**
169: * @ejb.persistent-field
170: * @ejb.interface-method
171: * @ejb.value-object match="libresource"
172: * @ejb.transaction type="Supports"
173: */
174: public abstract boolean getVersionable();
175:
176: /**
177: * @ejb.persistent-field
178: * @ejb.interface-method
179: * @ejb.transaction type="Mandatory"
180: */
181: public abstract void setVersionable(boolean versionable);
182:
183: // business methods
184:
185: /**
186: * @ejb.interface-method
187: * @ejb.transaction type="Mandatory"
188: */
189: public void initHistoryTable() throws Exception {
190: Connection conn = null;
191: Statement stmt = null;
192:
193: try {
194: conn = Libresource.getDatasource().getConnection();
195:
196: // if table does not exist
197: stmt = conn.createStatement();
198:
199: ResultSet rs = stmt
200: .executeQuery("SELECT tablename FROM pg_tables WHERE tablename='page_history_"
201: + getId() + "';");
202:
203: // create table
204: if (!rs.next()) {
205: stmt = conn.createStatement();
206: stmt.execute("CREATE TABLE page_history_" + getId()
207: + " (id_ SERIAL PRIMARY KEY,"
208: + " date_ TIMESTAMP," + " content_ VARCHAR,"
209: + " user_ VARCHAR," + " comment_ VARCHAR);");
210: }
211: } catch (Exception e) {
212: throw new Exception(
213: "Can't create history table for page : "
214: + e.getMessage());
215: } finally {
216: try {
217: if (stmt != null) {
218: stmt.close();
219: }
220: } catch (Exception e) {
221: }
222:
223: try {
224: if (conn != null) {
225: conn.close();
226: }
227: } catch (Exception e) {
228: }
229: }
230: }
231:
232: /**
233: * @ejb.interface-method
234: * @ejb.transaction type="Mandatory"
235: */
236: public void saveState(String user, String comment) throws Exception {
237: Connection conn = null;
238: PreparedStatement stmt = null;
239:
240: initHistoryTable();
241:
242: try {
243: conn = Libresource.getDatasource().getConnection();
244: stmt = conn
245: .prepareStatement("INSERT INTO page_history_"
246: + getId()
247: + "(date_, content_, user_, comment_) VALUES (?, ?, ?, ?)");
248: stmt.setTimestamp(1, new Timestamp(System
249: .currentTimeMillis()));
250: stmt.setString(2, getContent());
251: stmt.setString(3, user);
252: stmt.setString(4, comment);
253: stmt.executeUpdate();
254: } catch (Exception e) {
255: throw new Exception("Can't save state for page " + getId()
256: + " : " + e.getMessage());
257: } finally {
258: try {
259: if (stmt != null) {
260: stmt.close();
261: }
262: } catch (Exception e) {
263: }
264:
265: try {
266: if (conn != null) {
267: conn.close();
268: }
269: } catch (Exception e) {
270: }
271: }
272: }
273:
274: /**
275: * @ejb.interface-method
276: * @ejb.transaction type="Supports"
277: */
278: public TreeSet getHistory(int nbEntries) throws Exception {
279: Connection conn = null;
280: PreparedStatement stmt = null;
281:
282: try {
283: initHistoryTable();
284:
285: conn = Libresource.getDatasource().getConnection();
286:
287: String query = "SELECT * FROM page_history_" + getId()
288: + " ORDER BY id_ DESC";
289:
290: if (nbEntries != 0) {
291: query = query + " LIMIT " + nbEntries;
292: }
293:
294: stmt = conn.prepareStatement(query);
295:
296: ResultSet rs = stmt.executeQuery();
297: TreeSet entries = new TreeSet(new EntryComparator());
298:
299: while (rs.next()) {
300: HistoryEntry entry = new HistoryEntry(rs.getInt("id_"),
301: rs.getTimestamp("date_"), rs
302: .getString("content_"), rs
303: .getString("user_"), rs
304: .getString("comment_"));
305: entries.add(entry);
306: }
307:
308: return entries;
309: } finally {
310: try {
311: if (stmt != null) {
312: stmt.close();
313: }
314: } catch (Exception e) {
315: }
316:
317: try {
318: if (conn != null) {
319: conn.close();
320: }
321: } catch (Exception e) {
322: }
323: }
324: }
325:
326: /**
327: * @ejb.interface-method
328: * @ejb.transaction type="Supports"
329: */
330: public void importHistory(TreeSet history) throws Exception {
331: Connection conn = null;
332: PreparedStatement stmt = null;
333:
334: try {
335: initHistoryTable();
336:
337: conn = Libresource.getDatasource().getConnection();
338:
339: Object[] entries = history.toArray();
340:
341: // for (Iterator i = history.iterator(); i.hasNext(); ) {
342: for (int i = entries.length - 1; i > -1; i--) {
343: HistoryEntry entry = (HistoryEntry) entries[i];
344:
345: // HistoryEntry entry = (HistoryEntry) i.next();
346: stmt = conn
347: .prepareStatement("INSERT INTO page_history_"
348: + getId()
349: + "(date_, content_, user_, comment_) VALUES (?, ?, ?, ?)");
350: stmt.setTimestamp(1, new Timestamp(entry.getDate()
351: .getTime()));
352: stmt.setString(2, entry.getContent());
353: stmt.setString(3, entry.getUser());
354: stmt.setString(4, entry.getComment());
355: stmt.executeUpdate();
356: }
357: } finally {
358: try {
359: if (stmt != null) {
360: stmt.close();
361: }
362: } catch (Exception e) {
363: }
364:
365: try {
366: if (conn != null) {
367: conn.close();
368: }
369: } catch (Exception e) {
370: }
371: }
372: }
373:
374: /**
375: * @ejb.interface-method
376: * @ejb.transaction type="Supports"
377: */
378: public HistoryEntry getPageVersion(int version) throws Exception {
379: Connection conn = null;
380: PreparedStatement stmt = null;
381:
382: initHistoryTable();
383:
384: try {
385: conn = Libresource.getDatasource().getConnection();
386:
387: String query = "SELECT * FROM page_history_" + getId()
388: + " WHERE id_ ='" + version + "';";
389: stmt = conn.prepareStatement(query);
390:
391: ResultSet rs = stmt.executeQuery();
392:
393: if (rs.next()) {
394: return new HistoryEntry(rs.getInt("id_"), rs
395: .getDate("date_"), rs.getString("content_"), rs
396: .getString("user_"), rs.getString("comment_"));
397: }
398:
399: return null;
400: } finally {
401: try {
402: if (stmt != null) {
403: stmt.close();
404: }
405: } catch (Exception e) {
406: }
407:
408: try {
409: if (conn != null) {
410: conn.close();
411: }
412: } catch (Exception e) {
413: }
414: }
415: }
416:
417: /**
418: * @ejb.interface-method
419: * @ejb.transaction type="Mandatory"
420: */
421: public void cleanHistory() throws Exception {
422: initHistoryTable();
423:
424: Connection conn = null;
425: Statement stmt = null;
426:
427: try {
428: conn = Libresource.getDatasource().getConnection();
429:
430: stmt = conn.createStatement();
431: stmt.executeUpdate("DELETE FROM page_history_" + getId()
432: + ";");
433: } catch (Exception e) {
434: } finally {
435: try {
436: if (stmt != null) {
437: stmt.close();
438: }
439: } catch (Exception e) {
440: }
441:
442: try {
443: if (conn != null) {
444: conn.close();
445: }
446: } catch (Exception e) {
447: }
448: }
449: }
450: }
|