JaxorContextImpl.java :  » Database-ORM » jaxor-3.5 » net » sourceforge » jaxor » Java Open Source

Java Open Source » Database ORM » jaxor 3.5 
jaxor 3.5 » net » sourceforge » jaxor » JaxorContextImpl.java
package net.sourceforge.jaxor;

import net.sourceforge.jaxor.api.*;
import net.sourceforge.jaxor.db.SingleConnectionTransaction;
import net.sourceforge.jaxor.impl.InstanceCacheImpl;
import net.sourceforge.jaxor.impl.InstanceFactoryImpl;
import net.sourceforge.jaxor.impl.QueryResultImpl;
import net.sourceforge.jaxor.impl.UnitOfWorkImpl;
import net.sourceforge.jaxor.impl.MapperRegistryImpl;
import net.sourceforge.jaxor.util.MethodCache;
import net.sourceforge.jaxor.util.SystemException;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class JaxorContextImpl implements JaxorContext {

    private UnitOfWork _uow;
    private QueryCache _queryCache;
    private InstanceCache _cache;
    private JaxorTransaction _transaction = null;
    private final InstanceFactory _instanceFactory;
    private MapperRegistry _mapperRegistry;
    private String _user;

    public JaxorContextImpl(Connection conn) {
        this(new SingleConnectionTransaction(conn));
    }

    public JaxorContextImpl(ConnectionFactory fact) {
        this(new SingleConnectionTransaction(fact));
    }

    public JaxorContextImpl(JaxorTransaction trans) {
        this(trans, new InstanceCacheImpl(), new MethodCache(), new UnitOfWorkImpl());
    }

    public JaxorContextImpl(JaxorTransaction conn, InstanceCache cache, QueryCache queryCache, UnitOfWork uow) {
        this(conn, cache, queryCache, uow, new InstanceFactoryImpl(), new MapperRegistryImpl());
    }

    public JaxorContextImpl(JaxorTransaction conn, InstanceCache cache, QueryCache queryCache, UnitOfWork uow, InstanceFactory factory, MapperRegistry reg) {
        _transaction = conn;
        _cache = cache;
        _queryCache = queryCache;
        _uow = uow;
        _instanceFactory = factory;
        _mapperRegistry = reg;
    }


    public String getUser() {
        return _user;
    }

    public void setUser(String user) {
        _user = user;
    }

    public InstanceFactory getInstanceFactory() {
        return _instanceFactory;
    }

    public EntityInterface load(ResultSet rs, MetaRow clzz) {
        EntityInterface result = createEntity(clzz);
        try {
            result.getFields().load(rs);
        } catch (SQLException e) {
            throw new SystemException(e);
        }
        EntityInterface cache = cache(result);
        if (cache == result || cache == null) {// need to do a null check in case we are using a null proxy
            result.setJaxorContext(this);
            if (result instanceof LifeCycleListener)
                ((LifeCycleListener) result).afterLoad();
            result.registerLoad();
        }
        return cache;
    }

    public EntityInterface createEntity(MetaRow clzz) {
        EntityInterface entity = _instanceFactory.createEntity(clzz.getImplClass());
        entity.setMetaRow(clzz);
        return entity;
    }

    private EntityInterface cache(EntityInterface result) {
        return _cache.updateCache(result);
    }

    public QueryResult query(String sql, MetaRow implClass) {
        return query(sql, new QueryParams(), implClass);
    }

    public QueryResult query(final String sql, final QueryParams args, final MetaRow _meta) {
        return new QueryResultImpl(_meta, sql, args, this);
    }

    public void flush() {
        _queryCache.clear();
        if (_uow.size() > 0){
            Connection connection = this.getConnection();
            try {
                _uow.flush(connection);
            } finally {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void commit() {
        flush();
        _transaction.commit();
    }

    public void end() {
        if (_transaction != null)
            _transaction.end();
    }

    public Connection getConnection() {
        Connection conn = _transaction.getConnection();
        if (conn == null)
            throw new NullPointerException("JaxorTransaction returned a null connection");
        return conn;
    }

    public InstanceCache getCache() {
        return _cache;
    }

    public QueryCache getQueryCache() {
        return _queryCache;
    }

    public UnitOfWork getUnitOfWork() {
        return _uow;
    }

    public void setCache(InstanceCache cache) {
        _cache = cache;
    }

    public void setQueryCache(QueryCache cache) {
        _queryCache = cache;
    }

    public void setUnitOfWork(UnitOfWork work) {
        _uow = work;
    }

    public void setTransaction(JaxorTransaction fact) {
        _transaction = fact;
    }

    public JaxorTransaction getTransaction() {
        return _transaction;
    }

    public MapperRegistry getMapperRegistry() {
        return _mapperRegistry;
    }

    public void registerNew(EntityInterface entity) {
        if (entity instanceof LifeCycleListener)
            ((LifeCycleListener) entity).beforeCreate();
        entity.setJaxorContext(this);
        _uow.registerNew(entity);
        if (entity instanceof LifeCycleListener)
            ((LifeCycleListener) entity).afterCreate();
        cache(entity);
    }

    public FinderAdapter getFinder(Class finderClass) {
        return getInstanceFactory().createFinder(finderClass, this);
    }

    public Object createListImpl(List list, Class listImplClass) {
        return getInstanceFactory().createListImpl(list, listImplClass);
    }

    public void registerUpdate(EntityInterface entity) {
        _uow.registerUpdate(entity);
    }

    public void registerDelete(EntityInterface entity) {
        _uow.registerDelete(entity);
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.