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

templates.PersistentFactory.vm Maven / Gradle / Ivy

There is a newer version: 3.5.12
Show newest version
#* @vtlvariable name="formatedFactoryName" type="java.lang.String" *#
#* @vtlvariable name="ctx" type="org.kevoree.modeling.kotlin.generator.GenerationContext" *#
#* @vtlvariable name="FQNPackBaseImpl" type="java.lang.String" *#
#* @vtlvariable name="packElem" type="java.lang.String" *#
#* @vtlvariable name="getterMapName" type="java.lang.String" *#
#* @vtlvariable name="modelVersion" type="java.lang.String" *#
#* @vtlvariable name="ePackage" type="org.eclipse.emf.ecore.EPackage" *#
#* @vtlvariable name="ProcessorHelper" type="org.kevoree.modeling.kotlin.generator.ProcessorHelper" *#

package ${FQNPackBaseImpl};

import ${packElem}.${formatedFactoryName};

#foreach( $cls in ${ProcessorHelper.getEClassInEPackage($ePackage)} )
import ${packElem}.${cls.getName()};
#end

import org.mapdb.*;

open class Persistent${formatedFactoryName} (val basedir : java.io.File) : ${formatedFactoryName}, ${getterMapName} {
{
    if(!basedir.exists()){basedir.mkdirs()}
    if(!basedir.isDirectory()){
        throw java.lang.UnsupportedOperationException("Basedir param must be a directory")
    }
}

    override fun getVersion() = "${modelVersion}"

#foreach( $cls in ${ProcessorHelper.getEClassInEPackage($ePackage)} )
    override fun create${cls.getName()}() : ${cls.getName()} {return ${cls.getName()}Persistent(this)}
    fun create${cls.getName()}(id:String) : ${cls.getName()} {
        val v = ${cls.getName()}Persistent(this)
#if(${cls.getEIDAttribute()})
        v.set${cls.getEIDAttribute().getName().substring(0,1).toUpperCase()}${cls.getEIDAttribute().getName().substring(1)}(id)
#else
        v.setGenerated_KMF_ID(id)
#end
        return v
    }

#end



    fun createEntity( _type : String, id : String) : Any? {
        when(_type) {
#foreach( $cls in ${ProcessorHelper.getEClassInEPackage($ePackage)} )
            "${ProcessorHelper.fqn($ctx, $cls)}" -> {
                val v =  ${cls.getName()}Persistent(this)
#if(${cls.getEIDAttribute()})
                v.set${cls.getEIDAttribute().getName().substring(0,1).toUpperCase()}${cls.getEIDAttribute().getName().substring(1)}(id)
#else
                v.setGenerated_KMF_ID(id)
#end
                return v
            }
#end
            else -> {System.out.println("No constructor found for type:" + _type);return null}
        }
    }



    private val dbs = java.util.HashMap()

    //Generate DB Method
#foreach( $cls in ${ProcessorHelper.getEClassInEPackage($ePackage)} )

    private final val dbkey_${cls.getName()} : String = "${cls.getName()}"
    private fun get${cls.getName()}db() : org.mapdb.DB {
        var res = dbs.get(dbkey_${cls.getName()})
        if(res == null){
            res = DBMaker.newFileDB(java.io.File(basedir.getAbsolutePath()+java.io.File.separator+ "${ProcessorHelper.fqn($ctx, $cls)}_entity"))!!.journalDisable()!!.cacheLRUEnable()!!.cacheSize(5000)!!.closeOnJvmShutdown()!!.make()
            dbs.put(dbkey_${cls.getName()},res!!)
        }
        return res!!
    }

    override fun get${cls.getName()}Entity() : MutableMap {
        return get${cls.getName()}db().getTreeMap("default")!!
    }

    fun incrementAndGet${cls.getName()}Index() : Long {
        val keyinc = Atomic.getLong(get${cls.getName()}db(), "${cls.getName()}_id");
        return keyinc!!.incrementAndGet();
    }

#foreach($ref in $cls.getEAllReferences())
    private final val dbkey_${cls.getName()}_${ref.getName()}Relation : String = "${cls.getName()}_${ref.getName()}"
    private fun get${cls.getName()}_${ref.getName()}db() : org.mapdb.DB {
        var res = dbs.get(dbkey_${cls.getName()}_${ref.getName()}Relation)
        if(res == null){
            res = DBMaker.newFileDB(java.io.File(basedir.getAbsolutePath()+java.io.File.separator+"${ProcessorHelper.fqn($ctx, $cls)}_${ref.getName()}"))!!.journalDisable()!!.cacheLRUEnable()!!.cacheSize(5000)!!.closeOnJvmShutdown()!!.make()
            dbs.put(dbkey_${cls.getName()}_${ref.getName()}Relation,res!!)
        }
        return res!!
    }

    override fun get${cls.getName()}_${ref.getName()}Relation() : java.util.NavigableSet> {
       return get${cls.getName()}_${ref.getName()}db().getTreeSet("default")!!
    }

    fun incrementAndGet${cls.getName()}_${ref.getName()}Index() : Long {
        val keyinc = Atomic.getLong(get${cls.getName()}_${ref.getName()}db(), "${cls.getName()}_${ref.getName()}_id");
        return keyinc!!.incrementAndGet();
    }
#end
#end
}













© 2015 - 2025 Weber Informatics LLC | Privacy Policy