com.jpattern.orm.JPOrm Maven / Gradle / Ivy
package com.jpattern.orm;
import java.util.List;
import com.jpattern.logger.ILogger;
import com.jpattern.orm.classmapper.ClassMapperBuilder;
import com.jpattern.orm.classmapper.ClassTableMapBuilder;
import com.jpattern.orm.classmapper.IClassMapper;
import com.jpattern.orm.classmapper.TableMap;
import com.jpattern.orm.classtool.IOrmClassTool;
import com.jpattern.orm.classtool.IOrmClassToolMap;
import com.jpattern.orm.classtool.NullOrmClassToolMap;
import com.jpattern.orm.classtool.OrmClassTool;
import com.jpattern.orm.classtool.OrmClassToolMap;
import com.jpattern.orm.crud.IOrmCRUDQuery;
import com.jpattern.orm.crud.OrmCRUDQueryGenerator;
import com.jpattern.orm.dialect.DefaultDialect;
import com.jpattern.orm.dialect.Dialect;
import com.jpattern.orm.exception.OrmConfigurationException;
import com.jpattern.orm.generator.GeneratorStrategy;
import com.jpattern.orm.generator.IOrmPersistor;
import com.jpattern.orm.generator.ResultSetMethodHelper;
import com.jpattern.orm.generator.reflection.ReflectionGeneratorStrategy;
import com.jpattern.orm.generator.wrapper.TypeWrapper;
import com.jpattern.orm.logger.OrmLogger;
import com.jpattern.orm.session.Session;
import com.jpattern.orm.session.SessionProvider;
import com.jpattern.orm.session.NullSessionProvider;
import com.jpattern.orm.session.OrmSession;
/**
*
* @author Francesco Cina'
*
* 26/ago/2011
*/
public class JPOrm implements JPO {
private static Integer JPORM_INSTANCES_COUNT = Integer.valueOf(0);
private SessionProvider sessionProvider;
private IOrmClassToolMap ormClassToolMap = new OrmClassToolMap(this);
private final ILogger logger = OrmLogger.getOrmLogger(this.getClass());
private final Dialect dialect;
private final GeneratorStrategy generatorStrategy;
private final Integer instanceCount;
private final ResultSetMethodHelper resultSetMethodHelper = new ResultSetMethodHelper();
/**
* Create a new instance of JPOrm using the DefaultDialect (compatible with Oracle and H2 dbs).
* This instance can be obtained everywhere in the application using
* the static method get(String jpOrmName)
*
* @param sessionProvider
*/
public JPOrm(final SessionProvider sessionProvider) {
this( sessionProvider, new DefaultDialect());
}
/**
* Create a new instance of JPOrm.
*
* @param sessionProvider
* @param dialect
*/
public JPOrm(final SessionProvider sessionProvider, final Dialect dialect) {
this( sessionProvider, dialect, new ReflectionGeneratorStrategy());
}
/**
* Create a new instance of JPOrm.
*
* @param sessionProvider
* @param dialect
* @param generatorStrategy
*/
public JPOrm(final SessionProvider sessionProvider, final Dialect dialect, final GeneratorStrategy generatorStrategy) {
synchronized (JPORM_INSTANCES_COUNT) {
this.instanceCount = JPORM_INSTANCES_COUNT++;
}
this.sessionProvider = sessionProvider;
this.dialect = dialect;
this.generatorStrategy = generatorStrategy;
}
@Override
public final Session session() {
return new OrmSession(this.ormClassToolMap, this.sessionProvider.getSessionStrategy() );
}
@Override
public void register(final Class clazz) throws OrmConfigurationException {
this.logger.info("register", "register new class: " + clazz.getName());
try {
final TableMap tableMap = new TableMap();
new ClassTableMapBuilder(clazz, tableMap).updateTableMap();
final IClassMapper classMapper = new ClassMapperBuilder(clazz, tableMap).generate();
final IOrmPersistor ormPersistor = this.generatorStrategy.getGenerator(classMapper, "" + this.instanceCount, this.resultSetMethodHelper).generate();
final IOrmCRUDQuery ormCRUDQuery = new OrmCRUDQueryGenerator(this.dialect, classMapper).generate();
final IOrmClassTool ormClassTool = new OrmClassTool(classMapper, ormPersistor, ormCRUDQuery);
this.ormClassToolMap.put(clazz, ormClassTool);
} catch (final Exception e) {
throw new OrmConfigurationException(e);
}
}
@Override
public void destory() {
this.sessionProvider = new NullSessionProvider();
this.ormClassToolMap = new NullOrmClassToolMap();
}
@Override
public void register(final List> classes) throws OrmConfigurationException {
for (final Class> clazz : classes) {
this.register(clazz);
}
}
public SessionProvider getSessionProvider() {
return this.sessionProvider;
}
public ResultSetMethodHelper getResultSetMethodHelper() {
return this.resultSetMethodHelper;
}
@Override
public void register(final TypeWrapper, ?> typeWrapper) throws OrmConfigurationException {
try {
this.resultSetMethodHelper.addWrappers(typeWrapper);
} catch (Exception e) {
throw new OrmConfigurationException(e);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy