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

org.joinedworkz.spring.boot.PersistenceCartridge.xtend Maven / Gradle / Ivy

There is a newer version: 1.3.46
Show newest version
package org.joinedworkz.spring.boot

import java.util.Properties
import javax.inject.Inject
import javax.inject.Singleton
import org.joinedworkz.common.helper.CmnModelHelper
import org.joinedworkz.common.java.ClassGeneratorContext
import org.joinedworkz.core.facility.AbstractCartridge
import org.joinedworkz.core.facility.GeneratorContext
import org.joinedworkz.core.facility.Outlet
import org.joinedworkz.core.facility.Outlets
import org.joinedworkz.core.model.CmnComplexType
import org.joinedworkz.core.model.CmnFieldContainer
import org.joinedworkz.core.model.CmnModel
import org.joinedworkz.core.model.CmnObject
import org.joinedworkz.core.model.CmnRelation
import org.joinedworkz.core.model.CmnService
import org.joinedworkz.spring.boot.adapter.PersistenceAdapter
import org.joinedworkz.spring.boot.context.DataAccessServiceGeneratorContext
import org.joinedworkz.spring.boot.context.EntityMapperGeneratorContext
import org.joinedworkz.spring.boot.context.RepositoryGeneratorContext
import org.joinedworkz.spring.boot.generator.DDLGenerator
import org.joinedworkz.spring.boot.generator.DataAccessServiceGenerator
import org.joinedworkz.spring.boot.generator.EntityClassGenerator
import org.joinedworkz.spring.boot.generator.EntityMapperGenerator
import org.joinedworkz.spring.boot.generator.EntityObjectClassGenerator
import org.joinedworkz.spring.boot.generator.RelationDaoGenerator
import org.joinedworkz.spring.boot.generator.RelationEntityClassGenerator
import org.joinedworkz.spring.boot.generator.RepositoryGenerator
import org.joinedworkz.spring.boot.generator.ServiceDaoGenerator
import org.joinedworkz.spring.boot.generator.TupleConverterGenerator
import org.joinedworkz.spring.boot.helper.AdapterHelper
import org.joinedworkz.spring.boot.helper.PersistenceHelper
import org.joinedworkz.spring.boot.info.RelationInfo
import org.joinedworkz.spring.boot.helper.OutletHelper

@Singleton
class PersistenceCartridge extends AbstractCartridge {
    
    @Inject
    protected DDLGenerator ddlGenerator;
	
	@Inject
	protected EntityClassGenerator entityClassGenerator;
	
    @Inject
    protected RelationEntityClassGenerator relationEntityClassGenerator;
	
	@Inject
	protected RepositoryGenerator daoGenerator;
	
	@Inject
    protected DataAccessServiceGenerator dasGenerator;
    
    @Inject
    protected EntityMapperGenerator entityMapperGenerator;
    
    @Inject
    protected TupleConverterGenerator tupleConverterGenerator
    
    @Inject
    protected EntityObjectClassGenerator entityObjectClassGenerator;
	
	@Inject
    protected ServiceDaoGenerator serviceDaoGenerator;
	   
    @Inject
    protected RelationDaoGenerator relationDaoGenerator;
	
	@Inject
	protected extension PersistenceHelper jpaHelper
	
	@Inject
	protected extension AdapterHelper
	
	@Inject
	protected extension CmnModelHelper
	
	@Inject
    extension OutletHelper;
	
	override void prepare(CmnObject cmnObj) {
        if (cmnObj instanceof CmnModel) {
            getOrCreatePersistenceAdapter(cmnObj)
        }
    }
    
    override apply(CmnObject cmnObj, Outlets outlets) {
        apply(cmnObj, outlets, null)
    }
	
	override apply(CmnObject cmnObj, Outlets outlets, Properties properties) {
	    
		if (cmnObj instanceof CmnModel) {
		    
    		val generatedJavaSourceSlot = outlets.get("generatedJavaSource")
    		val generatedConfigurationOutlet = outlets.get("generatedConfiguration")
    		
    		val generatedMapperOutlet = getGeneratedDtoAndMapperOutlet(outlets, cmnObj, properties)
    		
		    val persistenceAdapter = getOrCreatePersistenceAdapter(cmnObj)
			for (modelElement : cmnObj.modelElements) {
				modelElement.generateModelElement(persistenceAdapter, generatedJavaSourceSlot, properties)
			}
			val complexTypes = cmnObj.modelElements.filter(CmnComplexType)
			val operationsWithNativeSql = complexTypes.filter[isEntity].flatMap[operations.filter[hasNativeSql]]
			val returnTypesInOperationsWithNativeSql = operationsWithNativeSql.map[returnType].filterNull.toSet
			
			if (!complexTypes.empty) {
    			val mappedTypes = complexTypes.filter[isEntity || isMappedType || (!isEntity && hasFieldOfTypeEntity)]
		        for (mappedType : mappedTypes) {
	        		entityMapperGenerator.generateClass(createEntityMapperGeneratorContext(mappedType, false, generatedMapperOutlet, properties))
		        }
		        val otherModelTypes = complexTypes.filter[!isEntity && hasFieldOfTypeEntity]
		        for (otherModelType : otherModelTypes) {
		        	entityObjectClassGenerator.generateClass(new ClassGeneratorContext(otherModelType, generatedJavaSourceSlot, properties))
		        }
		        if (!returnTypesInOperationsWithNativeSql.empty) {
		        	tupleConverterGenerator.generateClass(new ClassGeneratorContext(cmnObj, returnTypesInOperationsWithNativeSql, generatedJavaSourceSlot, properties));
		        }

			}
			for (relationInfo : persistenceAdapter.relationInfos) {
			    val relation = relationInfo.relation
			    if (relation.relationHasOwnDao) {
                    relationEntityClassGenerator.generateClass(createEntityGeneratorContext(relation, relationInfo, generatedJavaSourceSlot, properties))
                    relationDaoGenerator.generateClass(createDaoGeneratorContext(relation, generatedJavaSourceSlot, properties))
                }
            }
			    
			ddlGenerator.generateConfigurationFile(cmnObj, new GeneratorContext(generatedConfigurationOutlet, properties))
		}
	}
	
	def boolean isMappedType(CmnObject modelElement) {
        if (modelElement instanceof CmnComplexType) {
            return modelElement.fields.exists[sourceField !== null]
        }
        return false
    }
	
	def void generateModelElement(CmnObject obj, PersistenceAdapter persistenceAdapter, Outlet outlet, Properties properties) {
	   
		if (obj instanceof CmnComplexType) {
			if (obj.isEntity) {
				entityClassGenerator.generateClass(createGeneratorContext(obj,outlet, properties))
			    if (!obj.abstract) {
    			     daoGenerator.generateClass(createDaoGeneratorContext(obj, outlet, properties))
    			     dasGenerator.generateClass(createDataAccessServiceGeneratorContext(obj, outlet, properties))
			    }
			}
		} else if (obj instanceof CmnService) {
		    if (obj.isDao) {
                serviceDaoGenerator.generateClass(createDaoGeneratorContext(obj, outlet, properties))
            }
		}
	}
	
	def  ClassGeneratorContext createGeneratorContext(T classObject, Outlet outlet, Properties properties) {
		return new ClassGeneratorContext(classObject, outlet, properties)
	}
	
	 def ClassEntityGeneratorContext createEntityGeneratorContext(CmnRelation classObject, RelationInfo relationInfo, Outlet outlet, Properties properties) {
        return new ClassEntityGeneratorContext(classObject, relationInfo, outlet, properties)  
    }

	
	def  RepositoryGeneratorContext createDaoGeneratorContext(T classObject, Outlet outlet, Properties properties) {
        return new RepositoryGeneratorContext(classObject, outlet, properties, jpaHelper)
    }
    
    def  DataAccessServiceGeneratorContext createDataAccessServiceGeneratorContext(T classObject, Outlet outlet, Properties properties) {
    	val entityMapperClassShort = entityMapperClass(classObject as CmnComplexType).lastSegment.toFirstLower
        return new DataAccessServiceGeneratorContext(classObject, entityMapperClassShort, outlet, properties, jpaHelper)
    }
    
    def  EntityMapperGeneratorContext createEntityMapperGeneratorContext(CmnComplexType classObject, boolean entityMapper, Outlet outlet, Properties properties) {
        return new EntityMapperGeneratorContext(classObject, entityMapper, outlet, properties, jpaHelper)
    }
    
    def createDaoGeneratorContext(CmnService classObject, Outlet outlet, Properties properties) {
        return new ServiceDaoGeneratorContext(classObject, outlet, properties, jpaHelper)
    }
    
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy