KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > repository > serverimpl > EventHelper


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.repository.serverimpl;
17
18 import org.apache.xmlbeans.XmlObject;
19 import org.outerj.daisy.repository.RepositoryException;
20 import org.outerj.daisy.jdbcutil.JdbcHelper;
21
22 import java.sql.Connection JavaDoc;
23 import java.sql.SQLException JavaDoc;
24 import java.sql.PreparedStatement JavaDoc;
25 import java.io.CharArrayWriter JavaDoc;
26 import java.io.IOException JavaDoc;
27
28 public class EventHelper {
29     private LocalRepositoryManager.Context context;
30     private JdbcHelper jdbcHelper;
31
32     public EventHelper(LocalRepositoryManager.Context context, JdbcHelper jdbcHelper) {
33         this.context = context;
34         this.jdbcHelper = jdbcHelper;
35     }
36
37     /**
38      * Creates an event record, this event will then be picked up by the event dispatcher component
39      * which will forward it to JMS. The event record should usually be created in one database
40      * transaction together with the changes causing the event.
41      */

42     public void createEvent(XmlObject eventDescription, String JavaDoc eventName, Connection JavaDoc conn) throws SQLException JavaDoc, RepositoryException {
43         PreparedStatement JavaDoc stmt = null;
44         try {
45             stmt = conn.prepareStatement("insert into events(seqnr, message_type, message) values(?,?,?)");
46             stmt.setLong(1, context.getNextEventId());
47             stmt.setString(2, eventName);
48             stmt.setString(3, eventToString(eventDescription));
49             stmt.execute();
50             stmt.close();
51         } finally {
52             jdbcHelper.closeStatement(stmt);
53         }
54
55     }
56
57     private String JavaDoc eventToString(XmlObject xmlObject) throws RepositoryException {
58         CharArrayWriter JavaDoc writer = new CharArrayWriter JavaDoc(5000);
59         try {
60             xmlObject.save(writer);
61         } catch (IOException JavaDoc e) {
62             throw new RepositoryException("Error serializing event description.", e);
63         }
64         return writer.toString();
65     }
66
67 }
68
Popular Tags