001: package net.sourceforge.jaxor;
002:
003: import net.sourceforge.jaxor.api.*;
004: import net.sourceforge.jaxor.db.SingleConnectionTransaction;
005: import net.sourceforge.jaxor.impl.InstanceCacheImpl;
006: import net.sourceforge.jaxor.impl.InstanceFactoryImpl;
007: import net.sourceforge.jaxor.impl.QueryResultImpl;
008: import net.sourceforge.jaxor.impl.UnitOfWorkImpl;
009: import net.sourceforge.jaxor.impl.MapperRegistryImpl;
010: import net.sourceforge.jaxor.util.MethodCache;
011: import net.sourceforge.jaxor.util.SystemException;
012:
013: import java.sql.Connection;
014: import java.sql.ResultSet;
015: import java.sql.SQLException;
016: import java.util.List;
017:
018: public class JaxorContextImpl implements JaxorContext {
019:
020: private UnitOfWork _uow;
021: private QueryCache _queryCache;
022: private InstanceCache _cache;
023: private JaxorTransaction _transaction = null;
024: private final InstanceFactory _instanceFactory;
025: private MapperRegistry _mapperRegistry;
026: private String _user;
027:
028: public JaxorContextImpl(Connection conn) {
029: this (new SingleConnectionTransaction(conn));
030: }
031:
032: public JaxorContextImpl(ConnectionFactory fact) {
033: this (new SingleConnectionTransaction(fact));
034: }
035:
036: public JaxorContextImpl(JaxorTransaction trans) {
037: this (trans, new InstanceCacheImpl(), new MethodCache(),
038: new UnitOfWorkImpl());
039: }
040:
041: public JaxorContextImpl(JaxorTransaction conn, InstanceCache cache,
042: QueryCache queryCache, UnitOfWork uow) {
043: this (conn, cache, queryCache, uow, new InstanceFactoryImpl(),
044: new MapperRegistryImpl());
045: }
046:
047: public JaxorContextImpl(JaxorTransaction conn, InstanceCache cache,
048: QueryCache queryCache, UnitOfWork uow,
049: InstanceFactory factory, MapperRegistry reg) {
050: _transaction = conn;
051: _cache = cache;
052: _queryCache = queryCache;
053: _uow = uow;
054: _instanceFactory = factory;
055: _mapperRegistry = reg;
056: }
057:
058: public String getUser() {
059: return _user;
060: }
061:
062: public void setUser(String user) {
063: _user = user;
064: }
065:
066: public InstanceFactory getInstanceFactory() {
067: return _instanceFactory;
068: }
069:
070: public EntityInterface load(ResultSet rs, MetaRow clzz) {
071: EntityInterface result = createEntity(clzz);
072: try {
073: result.getFields().load(rs);
074: } catch (SQLException e) {
075: throw new SystemException(e);
076: }
077: EntityInterface cache = cache(result);
078: if (cache == result || cache == null) {// need to do a null check in case we are using a null proxy
079: result.setJaxorContext(this );
080: if (result instanceof LifeCycleListener)
081: ((LifeCycleListener) result).afterLoad();
082: result.registerLoad();
083: }
084: return cache;
085: }
086:
087: public EntityInterface createEntity(MetaRow clzz) {
088: EntityInterface entity = _instanceFactory.createEntity(clzz
089: .getImplClass());
090: entity.setMetaRow(clzz);
091: return entity;
092: }
093:
094: private EntityInterface cache(EntityInterface result) {
095: return _cache.updateCache(result);
096: }
097:
098: public QueryResult query(String sql, MetaRow implClass) {
099: return query(sql, new QueryParams(), implClass);
100: }
101:
102: public QueryResult query(final String sql, final QueryParams args,
103: final MetaRow _meta) {
104: return new QueryResultImpl(_meta, sql, args, this );
105: }
106:
107: public void flush() {
108: _queryCache.clear();
109: if (_uow.size() > 0) {
110: Connection connection = this .getConnection();
111: try {
112: _uow.flush(connection);
113: } finally {
114: try {
115: connection.close();
116: } catch (SQLException e) {
117: e.printStackTrace();
118: }
119: }
120: }
121: }
122:
123: public void commit() {
124: flush();
125: _transaction.commit();
126: }
127:
128: public void end() {
129: if (_transaction != null)
130: _transaction.end();
131: }
132:
133: public Connection getConnection() {
134: Connection conn = _transaction.getConnection();
135: if (conn == null)
136: throw new NullPointerException(
137: "JaxorTransaction returned a null connection");
138: return conn;
139: }
140:
141: public InstanceCache getCache() {
142: return _cache;
143: }
144:
145: public QueryCache getQueryCache() {
146: return _queryCache;
147: }
148:
149: public UnitOfWork getUnitOfWork() {
150: return _uow;
151: }
152:
153: public void setCache(InstanceCache cache) {
154: _cache = cache;
155: }
156:
157: public void setQueryCache(QueryCache cache) {
158: _queryCache = cache;
159: }
160:
161: public void setUnitOfWork(UnitOfWork work) {
162: _uow = work;
163: }
164:
165: public void setTransaction(JaxorTransaction fact) {
166: _transaction = fact;
167: }
168:
169: public JaxorTransaction getTransaction() {
170: return _transaction;
171: }
172:
173: public MapperRegistry getMapperRegistry() {
174: return _mapperRegistry;
175: }
176:
177: public void registerNew(EntityInterface entity) {
178: if (entity instanceof LifeCycleListener)
179: ((LifeCycleListener) entity).beforeCreate();
180: entity.setJaxorContext(this );
181: _uow.registerNew(entity);
182: if (entity instanceof LifeCycleListener)
183: ((LifeCycleListener) entity).afterCreate();
184: cache(entity);
185: }
186:
187: public FinderAdapter getFinder(Class finderClass) {
188: return getInstanceFactory().createFinder(finderClass, this );
189: }
190:
191: public Object createListImpl(List list, Class listImplClass) {
192: return getInstanceFactory().createListImpl(list, listImplClass);
193: }
194:
195: public void registerUpdate(EntityInterface entity) {
196: _uow.registerUpdate(entity);
197: }
198:
199: public void registerDelete(EntityInterface entity) {
200: _uow.registerDelete(entity);
201: }
202: }
|