"""this contains the cube-specific entities' classes

:organization: Logilab
:copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2.
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
"""
from cubicweb.entities import AnyEntity
from cubicweb.interfaces import IPrevNext

class TestInstance(AnyEntity):
    __regid__ = 'TestInstance'
    __implements__ = AnyEntity.__implements__ + (IPrevNext,)
    fetch_attrs = ('name',)

    @property
    def version(self):
        """project item interface"""
        return self.for_version[0]

    @property
    def project(self):
        """project item interface"""
        return self.version.project

    def after_deletion_path(self):
        """return (path, parameters) which should be used as redirect
        information when this entity is being deleted
        """
        return self.version.rest_path(), {}

    # IPrevNext/IBreadCrumbs interfaces #######################################

    def previous_entity(self):
        rql = ('TestInstance X ORDERBY X DESC LIMIT 1 '
               'WHERE X for_version V, V eid %(v)s, X eid < %(x)s')
        rset = self._cw.execute(rql, {'v': self.version.eid, 'x': self.eid})
        if rset:
            return rset.get_entity(0)

    def next_entity(self):
        rql = ('TestInstance X ORDERBY X ASC LIMIT 1 '
               'WHERE X for_version V, V eid %(v)s, X eid > %(x)s')
        rset = self._cw.execute(rql, {'v': self.version.eid, 'x': self.eid})
        if rset:
            return rset.get_entity(0)

    def breadcrumbs(self, view=None, recurs=False):
        breadcrumbs = self.version.breadcrumbs(view, True)
        url = '%s/%s' % (self.version.absolute_url(), 'tests')
        breadcrumbs += [(url, self._cw._('tests')), self]
        return breadcrumbs

