001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.wso2.esb.persistence.dao;
021:
022: import org.hibernate.Criteria;
023: import org.hibernate.HibernateException;
024: import org.hibernate.Session;
025: import org.hibernate.Transaction;
026: import org.hibernate.criterion.Expression;
027: import org.wso2.esb.persistence.dataobject.RegistryEntryDO;
028: import org.wso2.esb.util.HibernateConfig;
029:
030: public class RegistryEntryDAO extends BaseDAO {
031:
032: public RegistryEntryDAO(HibernateConfig hbConfig) {
033: super (hbConfig);
034: }
035:
036: public void addRegistryEntry(RegistryEntryDO registryEntryDO) {
037: if (registryEntryDO != null
038: && registryEntryDO.getRegistryKey() != null) {
039: super .create(registryEntryDO);
040: } else {
041: throw new RuntimeException(
042: "Invalid registry entry for add " + registryEntryDO);
043: }
044: }
045:
046: public void updateRegistryEntry(RegistryEntryDO registryEntryDO) {
047: if (registryEntryDO != null
048: && registryEntryDO.getRegistryKey() != null) {
049: RegistryEntryDO storedEntry = getRegistryEntry(registryEntryDO
050: .getRegistryKey());
051: if (storedEntry != null) {
052: storedEntry.setExpiryTime(registryEntryDO
053: .getExpiryTime());
054: super .update(storedEntry);
055: }
056: } else {
057: throw new RuntimeException(
058: "Invalid registry entry for update "
059: + registryEntryDO);
060: }
061: }
062:
063: public void saveOrUpdateRegistryEntry(
064: RegistryEntryDO registryEntryDO) {
065: if (registryEntryDO != null
066: && registryEntryDO.getRegistryKey() != null) {
067: RegistryEntryDO storedEntry = getRegistryEntry(registryEntryDO
068: .getRegistryKey());
069: if (storedEntry != null) {
070: storedEntry.setExpiryTime(registryEntryDO
071: .getExpiryTime());
072: super .update(storedEntry);
073: } else {
074: super .create(registryEntryDO);
075: }
076: } else {
077: throw new RuntimeException(
078: "Invalid registry entry for save/update "
079: + registryEntryDO);
080: }
081: }
082:
083: public RegistryEntryDO getRegistryEntry(String key) {
084:
085: Session session = hbConfig.currentSession();
086: Transaction tx = null;
087: RegistryEntryDO registryEntryDO = null;
088: try {
089: tx = session.beginTransaction();
090: Criteria criteria = session
091: .createCriteria(RegistryEntryDO.class);
092: criteria.add(Expression.eq("registryKey", key.trim()));
093: registryEntryDO = (RegistryEntryDO) criteria.uniqueResult();
094: session.flush();
095: tx.commit();
096: } catch (HibernateException e) {
097: tx.rollback();
098: String msg = "Error getting registry key for key : " + key;
099: log.error(msg, e);
100: throw new RuntimeException(msg, e);
101: } finally {
102: hbConfig.closeSession();
103: }
104:
105: return registryEntryDO;
106: }
107:
108: public void deleteRegistryEntry(String key) {
109:
110: Session session = hbConfig.currentSession();
111: Transaction tx = null;
112: RegistryEntryDO registryEntryDO = null;
113: try {
114: tx = session.beginTransaction();
115: Criteria criteria = session
116: .createCriteria(RegistryEntryDO.class);
117: criteria.add(Expression.eq("registryKey", key.trim()));
118: registryEntryDO = (RegistryEntryDO) criteria.uniqueResult();
119:
120: if (registryEntryDO != null) {
121: session.delete(registryEntryDO);
122: }
123: tx.commit();
124: } catch (HibernateException e) {
125: tx.rollback();
126: String msg = "Error getting registry key for key : " + key;
127: log.error(msg, e);
128: throw new RuntimeException(msg, e);
129: } finally {
130: hbConfig.closeSession();
131: }
132: }
133: }
|