001: package org.osbl.domain.logic;
002:
003: import org.osbl.domain.model.Domain;
004: import org.osbl.domain.model.DomainValue;
005: import org.osbl.domain.DomainRegistry;
006: import org.osbl.JTAHibernateEnvironment;
007: import org.hibernate.*;
008: import org.concern.controller.Transactions;
009: import org.conform.hibernate.HibernateEnvironment;
010:
011: import javax.transaction.TransactionManager;
012: import java.util.*;
013:
014: public class DefaultDomainLogic implements DomainLogic {
015: private SessionFactory sessionFactory;
016: private Transactions transactions;
017:
018: public Domain newDomain(String key, String type) {
019: try {
020: getTransactions().beginRequired();
021: Domain domain = new Domain();
022: domain.setKey(key);
023: domain.setType(type);
024: domain.setValues(new ArrayList<DomainValue>(2));
025:
026: Session session = getSession();
027: session.save(domain);
028: return domain;
029: } finally {
030: getTransactions().endRequired();
031: }
032: }
033:
034: public synchronized Collection<Domain> loadDomains() {
035: try {
036: getTransactions().beginRequired();
037: Session session = getSession();
038: List<Domain> list = (List<Domain>) session.createQuery(
039: "from Domain").list();
040:
041: List<Domain> domains = new ArrayList<Domain>(list.size());
042: for (Domain current : list) {
043: Domain domain = new Domain();
044: domain.setKey(current.getKey());
045: domain.setType(current.getType());
046: List<DomainValue> currentValues = current.getValues();
047: List<DomainValue> domainValues = new ArrayList<DomainValue>(
048: currentValues != null ? currentValues.size()
049: : 0);
050: if (currentValues != null) {
051: for (DomainValue currentValue : currentValues) {
052: domainValues.add(currentValue);
053: }
054: }
055: domain.setValues(domainValues);
056: domains.add(domain);
057: }
058: return domains;
059: } finally {
060: getTransactions().endRequired();
061: }
062: }
063:
064: public synchronized void updateDomain(Domain domain) {
065: try {
066: getTransactions().beginRequired();
067: Session session = getSession();
068: Domain loaded = (Domain) session.createQuery(
069: "from Domain where key = :key").setString("key",
070: domain.getKey()).uniqueResult();
071: if (loaded != null) {
072: domain.setId(loaded.getId());
073: session.merge(domain);
074: } else
075: session.save(domain);
076:
077: DomainRegistry.updateDomain(domain);
078: } finally {
079: getTransactions().endRequired();
080: }
081: }
082:
083: public void setSessionFactory(SessionFactory sessionFactory) {
084: this .sessionFactory = sessionFactory;
085: }
086:
087: public void setTransactionManager(
088: TransactionManager transactionManager) {
089: transactions = new Transactions(transactionManager);
090: }
091:
092: private Session getSession() {
093: return sessionFactory.getCurrentSession();
094: }
095:
096: protected Transactions getTransactions() {
097: JTAHibernateEnvironment environment = (JTAHibernateEnvironment) HibernateEnvironment
098: .getInstance();
099: if (environment != null)
100: return environment.getTransactions();
101:
102: return transactions;
103: }
104: }
|