All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jpattern.orm.JPOrm Maven / Gradle / Ivy

There is a newer version: 3.5.1
Show newest version
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.IDialect;
import com.jpattern.orm.exception.OrmConfigurationException;
import com.jpattern.orm.generator.AutoGeneratorStrategy;
import com.jpattern.orm.generator.IGeneratorStrategy;
import com.jpattern.orm.generator.IOrmPersistor;
import com.jpattern.orm.logger.OrmLogger;
import com.jpattern.orm.session.ISession;
import com.jpattern.orm.session.ISessionProvider;
import com.jpattern.orm.session.NullSessionProvider;
import com.jpattern.orm.session.Session;

/**
 * 
 * @author Francesco Cina'
 *
 * 26/ago/2011
 */
public class JPOrm implements IJPOrm {

	private static Integer JPORM_INSTANCES_COUNT = Integer.valueOf(0);
	private ISessionProvider sessionProvider;
	private IOrmClassToolMap ormClassToolMap = new OrmClassToolMap(this);
	private final ILogger logger = OrmLogger.getOrmLogger(this.getClass());
	private final IDialect dialect;
	private final IGeneratorStrategy generatorStrategy;
	private final Integer instanceCount;

	/**
	 * 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(ISessionProvider sessionProvider) {
		this( sessionProvider, new DefaultDialect());
	}

	/**
	 * Create a new instance of JPOrm.
	 * 
	 * @param sessionProvider
	 * @param dialect
	 */
	public JPOrm(ISessionProvider sessionProvider, IDialect dialect) {
		this( sessionProvider, dialect, new AutoGeneratorStrategy());
	}

	/**
	 * Create a new instance of JPOrm.
	 * 
	 * @param sessionProvider
	 * @param dialect
	 * @param generatorStrategy
	 */
	public JPOrm(ISessionProvider sessionProvider, IDialect dialect, IGeneratorStrategy generatorStrategy) {
		synchronized (JPORM_INSTANCES_COUNT) {
			this.instanceCount = JPORM_INSTANCES_COUNT++;
		}
		this.sessionProvider = sessionProvider;
		this.dialect = dialect;
		this.generatorStrategy = generatorStrategy;
	}

	@Override
	public final ISession session() {
		return new Session(this.ormClassToolMap, this.sessionProvider.getSessionStrategy() );
	}

	@Override
	public  void register(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).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(List> classes) throws OrmConfigurationException {
		for (final Class clazz : classes) {
			this.register(clazz);
		}
	}

	public ISessionProvider getSessionProvider() {
		return this.sessionProvider;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy