from zope.interface import implementer
from persistent.list import PersistentList

from dace.interfaces import IEntity
from .object import Object
from dace.descriptors import SharedUniqueProperty, SharedMultipleProperty
from dace.util import getAllBusinessAction


class ActionCall(object):
    # il faut faire ici un delegation des attribut de l'action en question
    def __init__(self, action, obj):
        super(ActionCall, self).__init__()
        self.object = obj
        self.action = action
        self.title = self.action.title
        self.process = self.action.process

    @property
    def url(self):
        return self.action.url(self.object)

    @property
    def content(self):
        return self.action.content(self.object)


class ProcessSharedUniqueProperty(SharedUniqueProperty):
    def __get__(self, obj, objtype=None):
        if obj is None:
            return self

        res = super(ProcessSharedUniqueProperty, self).__get__(obj, objtype)
        if res is not None:
            return res.process

        return None


class ProcessSharedMultipleProperty(SharedMultipleProperty):
    def __get__(self, obj, objtype=None):
        if obj is None:
            return self

        res = super(ProcessSharedMultipleProperty, self).__get__(obj, objtype)
        return [e.process for e in res]


@implementer(IEntity)
class Entity(Object):

    creator = ProcessSharedUniqueProperty('creator', 'createds', True)
    involvers = ProcessSharedMultipleProperty('involvers', 'involveds', True)

    def __init__(self, **kwargs):
        super(Entity, self).__init__(**kwargs)
        self.state = PersistentList()
        self.__property__ = None

    def setstate(self, state):
        if not isinstance(state, (list, tuple)):
            state = [state]

        self.state = PersistentList()
        for s in state:
            self.state.append(s)

    @property
    def actions(self):
        allactions = getAllBusinessAction(self)
        return [ActionCall(a, self) for a in allactions]

    #les relations avec le processus sont des relations d'agregation multiple bidirectionnelles
    # il faut donc les difinir comme des properties


@@ -74,16 +74,114 @@
 tentList
+%0Afrom pyramid.threadlocal import get_current_registry%0A%0Afrom substanced.event import ObjectModified
 %0A%0Afrom d
@@ -1973,16 +1973,169 @@
 end(s)%0A%0A
+    def reindex(self):%0A        event = ObjectModified(self)%0A        registry = get_current_registry()%0A        registry.subscribers((event, self), None)%0A%0A
     @pro

add reindex method