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

templates.trace.ModelTraceApplicator.vm Maven / Gradle / Ivy

There is a newer version: 3.5.12
Show newest version
#* @vtlvariable name="ctx" type="org.kevoree.modeling.kotlin.generator.GenerationContext" *#
#* @vtlvariable name="helper" type="org.kevoree.modeling.kotlin.generator.ProcessorHelperClass" *#

#set($packName = ${helper.fqn($ctx, $ctx.getBasePackageForUtilitiesGeneration())})

package ${packName}.trace

import ${packName}.trace.ModelRemoveAllTrace
import ${packName}.trace.ModelAddTrace
import ${packName}.trace.ModelRemoveTrace
import ${packName}.trace.ModelTrace
import ${packName}.trace.ModelSetTrace
import ${packName}.trace.ModelAddAllTrace
import ${packName}.util.ActionType
import ${packName}.factory.MainFactory

/**
 * Created by duke on 25/07/13.
 */


public class ModelTraceApplicator(val targetModel: ${ctx.getKevoreeContainer().get()}) {

    var factory = MainFactory();
    var pendingObj: ${ctx.getKevoreeContainer().get()}? = null
    var pendingParent: ${ctx.getKevoreeContainer().get()}? = null
    var pendingParentRefName: String? = null
    var pendingObjPath: String? = null

    private fun tryClosePending(srcPath: String) {
        if(pendingObj != null && pendingObjPath != srcPath){
            pendingParent!!.reflexiveMutator(ActionType.ADD, pendingParentRefName!!, pendingObj)
            pendingObj = null
            pendingObjPath = null
            pendingParentRefName = null
            pendingParent = null
        }
    }

    public fun createOrAdd(previousPath: String?, target: ${ctx.getKevoreeContainer().get()}, refName: String, potentialTypeName: String?) {
        val targetElem: Any? = if(previousPath != null){
            targetModel.findByPath(previousPath)
        } else {
            null
        }
        if(targetElem != null){
            target.reflexiveMutator(ActionType.ADD, refName, targetElem)
        } else {
            //add to pending
            pendingObj = factory.create(potentialTypeName!!)
            pendingObjPath = previousPath;
            pendingParentRefName = refName;
            pendingParent = target;
        }
    }

    public fun applyTraceOnModel(traceSeq: TraceSequence) {
        for(trace in traceSeq.getTraces()){
            var target: ${ctx.getKevoreeContainer().get()} = targetModel;
            if(trace is ModelAddTrace){
                val castedTrace = trace as ModelAddTrace
                tryClosePending("#Fake#Path");
                if(trace.srcPath != ""){
                    target = targetModel.findByPath(castedTrace.srcPath) as ${ctx.getKevoreeContainer().get()};
                }
                createOrAdd(castedTrace.previousPath, target, castedTrace.refName, castedTrace.typeName)
            }
            if(trace is ModelAddAllTrace){
                val castedTrace = trace as ModelAddAllTrace
                tryClosePending("#Fake#Path");
                var i = 0
                for(path in castedTrace.previousPath!!){
                    createOrAdd(path, target, castedTrace.refName, castedTrace.typeName!!.get(i))
                    i++
                }
            }
            if(trace is ModelRemoveTrace){
                val castedTrace = trace as ModelRemoveTrace
                tryClosePending(trace.srcPath);
                if(trace.srcPath != ""){
                    target = targetModel.findByPath(castedTrace.srcPath) as ${ctx.getKevoreeContainer().get()};
                }
                target.reflexiveMutator(ActionType.REMOVE, castedTrace.refName, targetModel.findByPath(castedTrace.objPath))
            }
            if(trace is ModelRemoveAllTrace){
                val castedTrace = trace as ModelRemoveAllTrace
                tryClosePending(trace.srcPath);
                if(trace.srcPath != ""){
                    target = targetModel.findByPath(castedTrace.srcPath) as ${ctx.getKevoreeContainer().get()};
                }
                target.reflexiveMutator(ActionType.REMOVE_ALL, castedTrace.refName, null)
            }
            if(trace is ModelSetTrace){
                val castedTrace = trace as ModelSetTrace
                tryClosePending(trace.srcPath);
                if(trace.srcPath != "" && castedTrace.srcPath != pendingObjPath){
                    target = targetModel.findByPath(castedTrace.srcPath) as ${ctx.getKevoreeContainer().get()};
                } else {
                    if(castedTrace.srcPath == pendingObjPath && pendingObj != null){
                        target = pendingObj!!;
                    }
                }
                if(castedTrace.content != null){
                    target.reflexiveMutator(ActionType.SET, castedTrace.refName, castedTrace.content)
                } else {
                    var targetContentPath : Any? = if(castedTrace.objPath != null){targetModel.findByPath(castedTrace.objPath!!)} else {null};
                    if(targetContentPath != null){
                        target.reflexiveMutator(ActionType.SET, castedTrace.refName, targetContentPath)
                    } else {
                        if(castedTrace.typeName != null && castedTrace.typeName != ""){
                            createOrAdd(castedTrace.objPath, target, castedTrace.refName, castedTrace.typeName) //must create the pending element
                        } else {
                            target.reflexiveMutator(ActionType.SET, castedTrace.refName, targetContentPath) //case real null content
                        }
                    }
                }
            }
        }
        tryClosePending("#Fake#Path");
    }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy