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.core.ejb;
034:
035: import org.libresource.Libresource;
036: import org.libresource.LibresourceEvent;
037: import org.libresource.LibresourceResourceBase;
038:
039: import org.libresource.core.TimelineItem;
040: import org.libresource.core.interfaces.LibresourceCoreService;
041: import org.libresource.core.util.ProjectResourceUtil;
042:
043: import java.net.URI;
044:
045: import java.sql.Connection;
046: import java.sql.PreparedStatement;
047: import java.sql.ResultSet;
048: import java.sql.Timestamp;
049:
050: import java.util.Date;
051: import java.util.HashSet;
052: import java.util.Vector;
053:
054: import javax.ejb.CreateException;
055:
056: /**
057: * @libresource.resource name="Timeline" service="LibresourceCore"
058: *
059: * @ejb.finder
060: * signature= "java.util.Collection findByURIInterest(java.lang.String uri)"
061: * query ="SELECT OBJECT(t) FROM TimelineResource t WHERE t.rootURI LIKE ?1"
062: * transaction-type="Supports"
063: */
064: public abstract class TimelineResourceBean extends
065: LibresourceResourceBase {
066: // business method
067: private static int maxEntries = 100;
068:
069: /**
070: * @ejb.create-method
071: */
072: public String ejbCreate(String name, String rootURI)
073: throws CreateException {
074: setId(ProjectResourceUtil.generateGUID(this ));
075: setName(name);
076: setRootURI(rootURI);
077:
078: try {
079: LibresourceCoreService libresourceCoreService = (LibresourceCoreService) Libresource
080: .getService("LibresourceCore");
081: setLoggedEvents(libresourceCoreService
082: .getDefaultLoggedEvents());
083: } catch (Exception e) {
084: throw new CreateException();
085: }
086:
087: return null;
088: }
089:
090: public void ejbPostCreate(String name, String rootURI)
091: throws CreateException {
092: Connection conn = null;
093: PreparedStatement stmt = null;
094:
095: try {
096: conn = Libresource.getDatasource().getConnection();
097: stmt = conn
098: .prepareStatement("CREATE TABLE timeline_"
099: + getId()
100: + " (date_ TIMESTAMP, from_ VARCHAR, type_ VARCHAR, service_ VARCHAR, resource_ VARCHAR, by_ VARCHAR, args_ VARCHAR);");
101: stmt.execute();
102: } catch (Exception e) {
103: throw new CreateException(
104: "Can't create log table for timeline : "
105: + e.getMessage());
106: } finally {
107: try {
108: if (stmt != null) {
109: stmt.close();
110: }
111: } catch (Exception e) {
112: }
113:
114: try {
115: if (conn != null) {
116: conn.close();
117: }
118: } catch (Exception e) {
119: }
120: }
121: }
122:
123: // persistents fields
124:
125: /**
126: * @ejb.persistent-field
127: * @ejb.interface-method
128: * @ejb.value-object match="libresource"
129: * @ejb.transaction type="Supports"
130: */
131: public abstract String getId();
132:
133: /**
134: * @ejb.persistent-field
135: * @ejb.interface-method
136: * @ejb.transaction type="Mandatory"
137: */
138: public abstract void setId(String id);
139:
140: /**
141: * @ejb.interface-method
142: * @ejb.value-object match="libresource"
143: * @ejb.transaction type="Supports"
144: */
145: public String getShortResourceName() {
146: return getName();
147: }
148:
149: /**
150: * @ejb.persistent-field
151: * @ejb.interface-method
152: * @ejb.value-object match="libresource"
153: * @ejb.transaction type="Supports"
154: */
155: public abstract String getName();
156:
157: /**
158: * @ejb.persistent-field
159: * @ejb.interface-method
160: * @ejb.transaction type="Mandatory"
161: */
162: public abstract void setName(String name);
163:
164: /**
165: * @ejb.persistent-field
166: * @ejb.interface-method
167: * @ejb.value-object match="libresource"
168: * @ejb.transaction type="Supports"
169: */
170: public abstract String getRootURI();
171:
172: /**
173: * @ejb.persistent-field
174: * @ejb.interface-method
175: * @ejb.transaction type="Mandatory"
176: */
177: public abstract void setRootURI(String rootURI);
178:
179: /**
180: * @ejb.persistent-field
181: * @ejb.interface-method
182: * @ejb.value-object match="libresource"
183: * @ejb.transaction type="Supports"
184: */
185: public abstract HashSet getLoggedEvents();
186:
187: /**
188: * @ejb.persistent-field
189: * @ejb.interface-method
190: * @ejb.transaction type="Mandatory"
191: */
192: public abstract void setLoggedEvents(HashSet events);
193:
194: /**
195: * @ejb.persistent-field
196: * @ejb.interface-method
197: * @ejb.value-object match="libresource"
198: * @ejb.transaction type="Supports"
199: */
200: public abstract boolean getPublicEvents();
201:
202: /**
203: * @ejb.persistent-field
204: * @ejb.interface-method
205: * @ejb.transaction type="Mandatory"
206: */
207: public abstract void setPublicEvents(boolean publicEvents);
208:
209: // business methods
210:
211: /**
212: * @ejb.interface-method
213: * @ejb.transaction type="Required"
214: */
215: public void addLoggedEvents(String event) {
216: HashSet events = getLoggedEvents();
217: events.add(event);
218: setLoggedEvents(events);
219: }
220:
221: /**
222: * @ejb.interface-method
223: * @ejb.transaction type="Required"
224: */
225: public void removeLoggedEvents(String event) {
226: HashSet events = getLoggedEvents();
227: events.remove(event);
228: setLoggedEvents(events);
229: }
230:
231: /**
232: * @ejb.interface-method
233: * @ejb.transaction type="Required"
234: */
235: public void log(LibresourceEvent event) throws Exception {
236: Connection conn = null;
237: PreparedStatement stmt = null;
238:
239: try {
240: conn = Libresource.getDatasource().getConnection();
241: stmt = conn
242: .prepareStatement("INSERT INTO timeline_"
243: + getId()
244: + " (date_, from_, type_, service_, resource_, by_, args_) VALUES(?, ?, ?, ?, ?, ?, ?)");
245: stmt.setTimestamp(1, new Timestamp(System
246: .currentTimeMillis()));
247: stmt.setString(2, event.getFromResource().getPath());
248: stmt.setString(3, event.getEventType());
249: stmt.setString(4, event.getServiceName());
250: stmt.setString(5, event.getResourceType());
251: stmt.setString(6, event.getThrowedBy().getPath());
252: stmt.setString(7, event.getArgs());
253: stmt.executeUpdate();
254: conn
255: .createStatement()
256: .executeUpdate(
257: "DELETE FROM timeline_"
258: + getId()
259: + " WHERE date_ NOT IN (SELECT date_ from timeline_"
260: + getId()
261: + " ORDER BY date_ DESC LIMIT "
262: + maxEntries + ");");
263: } catch (Exception e) {
264: ctx.setRollbackOnly();
265: } finally {
266: try {
267: if (stmt != null) {
268: stmt.close();
269: }
270: } catch (Exception e) {
271: }
272:
273: try {
274: if (conn != null) {
275: conn.close();
276: }
277: } catch (Exception e) {
278: }
279: }
280: }
281:
282: /**
283: * @ejb.interface-method
284: * @ejb.transaction type="Mandatory"
285: */
286: public void clean() throws Exception {
287: Connection conn = null;
288: PreparedStatement stmt = null;
289:
290: try {
291: conn = Libresource.getDatasource().getConnection();
292: stmt = conn.prepareStatement("DELETE FROM timeline_"
293: + getId());
294: stmt.execute();
295: } finally {
296: try {
297: if (stmt != null) {
298: stmt.close();
299: }
300: } catch (Exception e) {
301: }
302:
303: try {
304: if (conn != null) {
305: conn.close();
306: }
307: } catch (Exception e) {
308: }
309: }
310: }
311:
312: /**
313: * @ejb.interface-method
314: * @ejb.transaction type="Supports"
315: */
316: public Vector getDates() throws Exception {
317: Connection conn = null;
318: PreparedStatement stmt = null;
319: ResultSet rs = null;
320:
321: try {
322: conn = Libresource.getDatasource().getConnection();
323: stmt = conn
324: .prepareStatement("SELECT DISTINCT(date_trunc('day',date_)) AS ladate FROM timeline_"
325: + getId() + " ORDER BY ladate DESC;");
326: rs = stmt.executeQuery();
327:
328: Vector dates = new Vector();
329:
330: while (rs.next()) {
331: dates.add(rs.getDate(1));
332: }
333:
334: return dates;
335: } finally {
336: try {
337: if (stmt != null) {
338: stmt.close();
339: }
340: } catch (Exception e) {
341: }
342:
343: try {
344: if (conn != null) {
345: conn.close();
346: }
347: } catch (Exception e) {
348: }
349: }
350: }
351:
352: /**
353: * @ejb.interface-method
354: * @ejb.transaction type="Supports"
355: */
356: public Vector getItems(Date date) throws Exception {
357: Connection conn = null;
358: PreparedStatement stmt = null;
359: ResultSet rs = null;
360:
361: try {
362: conn = Libresource.getDatasource().getConnection();
363: stmt = conn
364: .prepareStatement("SELECT * FROM timeline_"
365: + getId()
366: + " WHERE date_trunc('day',date_) = ? ORDER BY date_ DESC;");
367: stmt.setDate(1, new java.sql.Date(date.getTime()));
368:
369: rs = stmt.executeQuery();
370:
371: Vector items = new Vector();
372:
373: while (rs.next()) {
374: String from = rs.getString("from_");
375: String by = rs.getString("by_");
376:
377: try {
378: from = Libresource.getShortName(new URI(rs
379: .getString("from_")));
380: } catch (Exception e) {
381: //
382: }
383:
384: try {
385: by = Libresource.getShortName(new URI(rs
386: .getString("by_")));
387: } catch (Exception e) {
388: //
389: }
390:
391: items.add(new TimelineItem(rs.getString("type_"),
392: new URI(rs.getString("from_")), from, by, rs
393: .getTimestamp("date_"), rs
394: .getString("args_")));
395: }
396:
397: return items;
398: } finally {
399: try {
400: if (stmt != null) {
401: stmt.close();
402: }
403: } catch (Exception e) {
404: }
405:
406: try {
407: if (conn != null) {
408: conn.close();
409: }
410: } catch (Exception e) {
411: }
412: }
413: }
414: }
|