01: /*
02: * Copyright 2004 Outerthought bvba and Schaubroeck nv
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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;
23: import java.sql.SQLException;
24: import java.sql.PreparedStatement;
25: import java.io.CharArrayWriter;
26: import java.io.IOException;
27:
28: public class EventHelper {
29: private LocalRepositoryManager.Context context;
30: private JdbcHelper jdbcHelper;
31:
32: public EventHelper(LocalRepositoryManager.Context context,
33: JdbcHelper jdbcHelper) {
34: this .context = context;
35: this .jdbcHelper = jdbcHelper;
36: }
37:
38: /**
39: * Creates an event record, this event will then be picked up by the event dispatcher component
40: * which will forward it to JMS. The event record should usually be created in one database
41: * transaction together with the changes causing the event.
42: */
43: public void createEvent(XmlObject eventDescription,
44: String eventName, Connection conn) throws SQLException,
45: RepositoryException {
46: PreparedStatement stmt = null;
47: try {
48: stmt = conn
49: .prepareStatement("insert into events(seqnr, message_type, message) values(?,?,?)");
50: stmt.setLong(1, context.getNextEventId());
51: stmt.setString(2, eventName);
52: stmt.setString(3, eventToString(eventDescription));
53: stmt.execute();
54: stmt.close();
55: } finally {
56: jdbcHelper.closeStatement(stmt);
57: }
58:
59: }
60:
61: private String eventToString(XmlObject xmlObject)
62: throws RepositoryException {
63: CharArrayWriter writer = new CharArrayWriter(5000);
64: try {
65: xmlObject.save(writer);
66: } catch (IOException e) {
67: throw new RepositoryException(
68: "Error serializing event description.", e);
69: }
70: return writer.toString();
71: }
72:
73: }
|