01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.bpmscript.jbi.component.db;
18:
19: import java.sql.SQLException;
20: import java.sql.Timestamp;
21: import java.util.List;
22:
23: import javax.jbi.messaging.MessageExchange;
24:
25: import org.bpmscript.BpmScriptException;
26: import org.bpmscript.jbi.component.IExchange;
27: import org.bpmscript.jbi.component.IExchangeCallback;
28: import org.bpmscript.jbi.component.IExchangeStore;
29: import org.hibernate.HibernateException;
30: import org.hibernate.LockMode;
31: import org.hibernate.Query;
32: import org.hibernate.Session;
33: import org.springframework.orm.hibernate3.HibernateCallback;
34: import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
35:
36: public class HibernateExchangeStore extends HibernateDaoSupport
37: implements IExchangeStore {
38:
39: private final transient org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
40: .getLog(getClass());
41:
42: @SuppressWarnings("unchecked")
43: public List<IExchange> findAllExchanges() throws BpmScriptException {
44: return getHibernateTemplate()
45: .find(
46: "from org.bpmscript.jbi.component.db.DbExchange exchange");
47: }
48:
49: public boolean remove(final String exchangeId,
50: final IExchangeCallback callback) throws BpmScriptException {
51:
52: Boolean result = (Boolean) getHibernateTemplate().execute(
53: new HibernateCallback() {
54:
55: public Object doInHibernate(Session session)
56: throws HibernateException, SQLException {
57: Query query = session
58: .createQuery("from org.bpmscript.jbi.component.db.DbExchange exchange where exchange.exchangeId = :exchangeId");
59: query.setString("exchangeId", exchangeId);
60: query.setLockMode("exchange", LockMode.UPGRADE);
61: List list = query.list();
62: if (list.size() == 0) {
63: return Boolean.FALSE;
64: } else {
65: DbExchange exchange = (DbExchange) list
66: .get(0);
67: try {
68: callback.execute(exchange);
69: session.delete(exchange);
70: return true;
71: } catch (Exception e) {
72: log.error(e, e);
73: return false;
74: }
75: }
76: }
77:
78: });
79: return result.booleanValue();
80:
81: }
82:
83: public void save(MessageExchange exchange)
84: throws BpmScriptException {
85: DbExchange dbExchange = new DbExchange();
86: dbExchange.setCreationDate(new Timestamp(System
87: .currentTimeMillis()));
88: dbExchange.setLastModified(new Timestamp(System
89: .currentTimeMillis()));
90: dbExchange.setExchangeId(exchange.getExchangeId());
91: dbExchange.setExchange(exchange);
92: getHibernateTemplate().save(dbExchange);
93: }
94:
95: }
|