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 javax.inject.Inject
import javax.inject.Singleton
import org.joinedworkz.common.java.ClassGeneratorContext
import org.joinedworkz.core.facility.AbstractCartridge
import org.joinedworkz.core.facility.Outlet
import org.joinedworkz.core.facility.Outlets
import org.joinedworkz.core.model.CmnComplexType
import org.joinedworkz.core.model.CmnModel
import org.joinedworkz.core.model.CmnObject
import org.joinedworkz.core.facility.GeneratorContext
import org.joinedworkz.core.model.CmnRelation
import java.util.List
import java.util.Set
import java.util.Map
import java.util.Collection
import org.joinedworkz.core.model.CmnFieldContainer
import org.joinedworkz.core.model.CmnService
import org.joinedworkz.spring.boot.generator.EntityClassGenerator
import org.joinedworkz.spring.boot.generator.RelationEntityClassGenerator
import org.joinedworkz.spring.boot.generator.DDLGenerator
import org.joinedworkz.spring.boot.helper.PersistenceHelper
import org.joinedworkz.spring.boot.helper.AdapterHelper
import org.joinedworkz.spring.boot.generator.ServiceDaoGenerator
import org.joinedworkz.spring.boot.generator.RelationDaoGenerator
import org.joinedworkz.spring.boot.adapter.PersistenceAdapter
import org.joinedworkz.spring.boot.info.RelationInfo
import org.joinedworkz.spring.boot.generator.RepositoryGenerator
import org.joinedworkz.spring.boot.generator.DataAccessServiceGenerator
import org.joinedworkz.spring.boot.context.RepositoryGeneratorContext
import org.joinedworkz.spring.boot.context.DataAccessServiceGeneratorContext
import org.joinedworkz.spring.boot.generator.EntityMapperGenerator
import org.joinedworkz.spring.boot.context.EntityMapperGeneratorContext
import org.joinedworkz.common.helper.CmnModelHelper
import org.joinedworkz.spring.boot.generator.EntityObjectClassGenerator
import org.joinedworkz.spring.boot.generator.TupleConverterGenerator

@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
	
	override void prepare(CmnObject cmnObj) {
        if (cmnObj instanceof CmnModel) {
            getOrCreatePersistenceAdapter(cmnObj)
            
        }
    }
	
	override apply(CmnObject cmnObj, Outlets outlets) {
	    
		val generatedJavaSourceSlot = outlets.get("generatedJavaSource")
		val generatedConfigurationOutlet = outlets.get("generatedConfiguration")
		if (cmnObj instanceof CmnModel) {
		    val persistenceAdapter = getOrCreatePersistenceAdapter(cmnObj)
			for (modelElement : cmnObj.modelElements) {
				modelElement.generateModelElement(persistenceAdapter, generatedJavaSourceSlot)
			}
			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, generatedJavaSourceSlot))
		        }
		        val otherModelTypes = complexTypes.filter[!isEntity && hasFieldOfTypeEntity]
		        for (otherModelType : otherModelTypes) {
		        	entityObjectClassGenerator.generateClass(new ClassGeneratorContext(otherModelType, generatedJavaSourceSlot))
		        }
		        if (!returnTypesInOperationsWithNativeSql.empty) {
		        	tupleConverterGenerator.generateClass(new ClassGeneratorContext(cmnObj, returnTypesInOperationsWithNativeSql, generatedJavaSourceSlot));
		        }

			}
			for (relationInfo : persistenceAdapter.relationInfos) {
			    val relation = relationInfo.relation
			    if (relation.relationHasOwnDao) {
                    relationEntityClassGenerator.generateClass(createEntityGeneratorContext(relation, relationInfo, generatedJavaSourceSlot))
                    relationDaoGenerator.generateClass(createDaoGeneratorContext(relation, generatedJavaSourceSlot))
                }
            }
			    
			ddlGenerator.generateConfigurationFile(cmnObj, new GeneratorContext(generatedConfigurationOutlet))
		}
	}
	
	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) {
	   
		if (obj instanceof CmnComplexType) {
			if (obj.isEntity) {
				entityClassGenerator.generateClass(createGeneratorContext(obj,outlet))
			    if (!obj.abstract) {
    			     daoGenerator.generateClass(createDaoGeneratorContext(obj, outlet))
    			     dasGenerator.generateClass(createDataAccessServiceGeneratorContext(obj, outlet))
			    }
			}
		} else if (obj instanceof CmnService) {
		    if (obj.isDao) {
                serviceDaoGenerator.generateClass(createDaoGeneratorContext(obj, outlet))
            }
		}
	}
	
	def  ClassGeneratorContext createGeneratorContext(T classObject, Outlet outlet) {
		return new ClassGeneratorContext(classObject,outlet)
	}
	
	 def ClassEntityGeneratorContext createEntityGeneratorContext(CmnRelation classObject, RelationInfo relationInfo, Outlet outlet) {
        return new ClassEntityGeneratorContext(classObject, relationInfo, outlet)  
    }

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy