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

com.xlrit.gears.application.GearsServletProcessApplication Maven / Gradle / Ivy

package com.xlrit.gears.application;

import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;

import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.CDI;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.camunda.bpm.application.PostDeploy;
import org.camunda.bpm.application.ProcessApplication;
import org.camunda.bpm.application.impl.ServletProcessApplication;
import org.camunda.bpm.engine.ProcessEngine;
import org.camunda.bpm.engine.impl.javax.el.ELResolver;
import org.camunda.bpm.engine.impl.variable.serializer.DefaultVariableSerializers;
import org.camunda.bpm.engine.impl.variable.serializer.VariableSerializers;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.tool.schema.TargetType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.xlrit.gears.base.execution.ExecutionFactory;
import com.xlrit.gears.base.form.ChoicesFactory;
import com.xlrit.gears.engine.GearsProcessApplication;
import com.xlrit.gears.engine.camunda.CdiElResolver;
import com.xlrit.gears.engine.camunda.GearsFormSerializer;
import com.xlrit.gears.engine.camunda.GearsObjectSerializer;
import com.xlrit.gears.engine.config.ConfigProperty;
import com.xlrit.gears.engine.form.CdiCompanionManager;
import com.xlrit.gears.engine.form.ChoicesFactoryImpl;
import com.xlrit.gears.engine.form.GearsFormEngine;
import com.xlrit.gears.engine.form.GearsFormEngineImpl;
import com.xlrit.gears.engine.meta.CdiMetaManager;
import com.xlrit.gears.engine.meta.MetaManager;

@ProcessApplication
public class GearsServletProcessApplication extends ServletProcessApplication implements GearsProcessApplication {
	private static final Logger LOG = LoggerFactory.getLogger(GearsServletProcessApplication.class);

	private final ELResolver elResolver;
	private final MetaManager metaManager;
	private final GearsFormEngine formEngine;
	private final VariableSerializers variableSerializers;

	@PersistenceContext
	private EntityManager em;


	@Inject @ConfigProperty("gears.persistence.schema.application.action")
	private String schemaAction;

	@Inject
	public GearsServletProcessApplication(ExecutionFactory executionFactory) {
		CDI cdi = CDI.current();
		ObjectMapper objectMapper = new ObjectMapper(); // TODO can this be shared with more components?
		LOG.info("Creating instance for CDI {}", cdi);

		this.elResolver = CdiElResolver.create(cdi);
		this.metaManager = CdiMetaManager.create(cdi);
		this.formEngine = new GearsFormEngineImpl(metaManager, CdiCompanionManager.create(cdi), executionFactory, objectMapper);
		this.variableSerializers =
			new DefaultVariableSerializers()
				.addSerializer(new GearsFormSerializer(formEngine, objectMapper))
				.addSerializer(new GearsObjectSerializer(metaManager, objectMapper))
				//.addSerializer(new GearsEntitySerializer(metaManager, objectMapper))
				//.addSerializer(new GearsListSerializer(metaManager, objectMapper))
		;
	}

	@PostDeploy
	public void postDeploy(ProcessEngine processEngine) {
		LOG.info("Process Application '{}' deployed to {}", getName(), processEngine);

		Map settings = new HashMap<>();
		settings.put(AvailableSettings.DATASOURCE,   processEngine.getProcessEngineConfiguration().getDataSource());
		settings.put(AvailableSettings.SHOW_SQL,     true);
		settings.put(AvailableSettings.HBM2DLL_CREATE_NAMESPACES, true);
		settings.put(AvailableSettings.HBM2DDL_FILTER_PROVIDER,   com.xlrit.gears.engine.persistence.ModuleSchemaFilterProvider.class);

		doExport(settings, schemaAction);
	}

	private static void doExport(Map settings, String schemaAction) {
		StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
			.applySettings(settings)
			.build();

		MetadataSources metadataSources = new MetadataSources(serviceRegistry);
		metadataSources.addResource("orm-common.xml");
		metadataSources.addResource("orm-app.xml");
		Metadata metadata = metadataSources.buildMetadata();

		SchemaExport schemaExport = new SchemaExport();
		schemaExport.execute(
			EnumSet.of(TargetType.DATABASE),
			SchemaExport.Action.parseCommandLineOption(schemaAction),
			metadata
		);
	}

	@Override
	public ELResolver getElResolver() {
		return elResolver;
	}

	@Override
	public VariableSerializers getVariableSerializers() {
		return variableSerializers;
	}

	@Override
	public MetaManager getMetaManager() {
		return metaManager;
	}

	@Override
	public GearsFormEngine getFormEngine() {
		return formEngine;
	}

	@Override
	public EntityManager getEntityManager() {
		return em;
	}

	@Produces
	public ChoicesFactory choicesFactory() {
		return new ChoicesFactoryImpl(metaManager);
	}
}