# This file is part of the Coral project.
# Please read the copyright notice in the LICENSE file.

import coral
from coral import log
logger=log.getLogger("Manage10 Manager")

_cleanParameters={}

def cleanParameter(p):
    global _cleanParameters
    _cleanParameters[p]=1
    
def onModelChanged(dir):
    global _cleanParameters


    if dir!=coral.TRANSACTION_ORDINARY:
        return

    tr=coral.getTransactionManager().getCurrentTransaction()

    processed={}
    toupdate={}
    nottoupdate={}
    
    for c in tr:
        element=None
        if not isinstance(c,coral.ElementChangeSlotCommand):
            continue
        element = c.getSlot().getElement()
        if element.oclIsKindOf(coral.lang.UML14.Dependency) or element.oclIsKindOf(coral.lang.Manage10.ModelRelationship):
            nottoupdate[element]=1
        else:
            if element.oclIsKindOf(coral.lang.Manage10.ManagedModel):
                element=element.__parent__()
            if element:
                updateDependencies(element,processed,toupdate,nottoupdate)
            
    logger.debug("nottoupdate " + str(nottoupdate))
    for t in nottoupdate.keys():
        logger.debug(str(t) + " "+ str(t.name))
    for d in toupdate.keys():
        if not nottoupdate.has_key(d) and not _cleanParameters.has_key(d):
            if d.oclIsKindOf(coral.lang.UML14.Dependency):
                if toupdate[d] & 1:
                    setTaggedValue(d,"sourceHasChanged","True")
                if toupdate[d] & 2:
                    setTaggedValue(d,"targetHasChanged","True")
            elif d.oclIsKindOf(coral.lang.Manage10.ModelRelationship):
                if toupdate[d] & 1:
                    d.sourceHasChanged=True
                if toupdate[d] & 2:
                    d.targetHasChanged=True

    _cleanParameters={}

def getTaggedValue(e,name):
    for tv in e.taggedValue:
        if str(tv.name)==name:
            return str(tv.dataValue)
    return None

def setTaggedValue(e,name,value):
    for tv in e.taggedValue:
        if str(tv.name)==name:
            tv.dataValue=value
            return
    e.taggedValue.append(coral.lang.UML14.TaggedValue(name=name,dataValue=value))

def updateDependencies(element,processed,toupdate,nottoupdate):
      
    if not element:
        return

    if processed.has_key(element):
        return

    processed[element]=1

    if element.oclIsKindOf(coral.lang.UML14.ModelElement):
        for d in element.clientDependency:
            if getTaggedValue(d,"type")=="ManagedModelRelationship":
                if not toupdate.has_key(d):
                    toupdate[d]=1
                else:
                    toupdate[d]|=1

        for d in element.supplierDependency:
            if getTaggedValue(d,"type")=="ManagedModelRelationship":
                if not toupdate.has_key(d):
                    toupdate[d]=2
                else:
                    toupdate[d]|=2
                    
    elif element.oclIsKindOf(coral.lang.Manage10.ManagedModel):
        for d in element.sourceRelationship:
            if not toupdate.has_key(d):
                toupdate[d]=1
            else:
                toupdate[d]|=1

        for d in element.targetRelationship:
            if not toupdate.has_key(d):
                toupdate[d]=2
            else:
                toupdate[d]|=2

    elif element.oclIsKindOf(coral.lang.Manage10.Step):
        for d in element.outputParameter:
            if not toupdate.has_key(d):
                toupdate[d]=1
            else:
                toupdate[d]|=1

    if element.__parent__():
            updateDependencies(element.__parent__(),processed,toupdate,nottoupdate)
