Package Products :: Package ZenRelations :: Module PrimaryPathObjectManager |
[hide private]
[frames] | no frames]

# Source Code for Module Products.ZenRelations.PrimaryPathObjectManager

      1  ###########################################################################
      2  #
      3  # This program is part of Zenoss Core, an open source monitoring platform.
      4  # Copyright (C) 2007, Zenoss Inc.
      5  #
      6  # This program is free software; you can redistribute it and/or modify it
      7  # under the terms of the GNU General Public License version 2 as published by
      8  # the Free Software Foundation.
      9  #
     10  # For complete information please visit: http://www.zenoss.com/oss/
     11  #
     12  ###########################################################################
     13
     14  __doc__="""PrimaryPathObjectManager
     15
     16  $Id: RelationshipBase.py,v 1.26 2003/10/03 16:16:01 edahl Exp $"""
     17
     18  __version__ = "$Revision: 1.26 $"[11:-2]
     19
     20  import logging
     21  log = logging.getLogger("zen.PrimaryPathObjectManager")
     22
     23  # base classes for PrimaryPathObjectManager
     24  from RelCopySupport import RelCopyContainer
     25  from Acquisition import Implicit, aq_base
     26  from OFS.ObjectManager import ObjectManager
     27  from AccessControl.Role import RoleManager
     28  import App.Undo
     29
     30  from ZItem import ZItem
     31
     32
     33  from Products.ZenUtils.Utils import getObjByPath
     34
     35  from Products.BTreeFolder2.BTreeFolder2 import BTreeFolder2
     36
     37
     38
     39 -class PrimaryPathManager(ZItem, Implicit, RoleManager):
     40
     41 -    def getPrimaryPath(self, fromNode=None):
     42          """
     43          Return the primary path of this object by following __primary_parent__
     44          """
     45          ppath = []
     46          obj = aq_base(self)
     47          while True:
     48              ppath.append(obj.id)
     49              parent = getattr(obj, "__primary_parent__", None)
     50              if parent is None: break
     51              obj = parent
     52          ppath.reverse()
     53          basepath = getattr(obj, "zPrimaryBasePath", [])
     54          for i in range(len(basepath)-1,-1,-1): ppath.insert(0,basepath[i])
     55          try:
     56              idx = ppath.index(fromNode)
     57              ppath = ppath[idx+1:]
     58          except ValueError: pass
     59          return tuple(ppath)
     60
     61
     62 -    def getPrimaryId(self, fromNode=None):
     63          """Return the primary path in the form /zport/dmd/xyz"""
     64          pid = "/".join(self.getPrimaryPath(fromNode))
     65          if fromNode: pid = "/" + pid
     66          return pid
     67
     68
     69 -    def getPrimaryUrlPath(self, full=False):
     70          """Return the primary path as an absolute url"""
     71          objaq = self.primaryAq()
     72          if full: return objaq.absolute_url()
     73          return objaq.absolute_url_path()
     74
     75
     76
     77 -    def primaryAq(self):
     78          """Return self with is acquisition path set to primary path"""
     79          app = self.getPhysicalRoot()
     80          return getObjByPath(app, self.getPrimaryPath())
     81
     82
     83 -    def getPrimaryParent(self):
     84          """Return our parent object by our primary path"""
     85          return self.__primary_parent__.primaryAq()
     86
     87
     88 -class PrimaryPathObjectManager(
     89              RelCopyContainer,
     90              ObjectManager,
     91              PrimaryPathManager,
     92              App.Undo.UndoSupport,
     93              ):
     94      """
     95      PrimaryPathObjectManager with basic Zope persistent classes.
     96      """
     97      manage_options = (ObjectManager.manage_options +
     98                        RoleManager.manage_options +
     99                        ZItem.manage_options)
    100
    101 -    def _setObject(self, id, obj, roles=None, user=None, set_owner=1):
    102          """Track __primary_parent__ when we are set into an object"""
    103          obj.__primary_parent__ = aq_base(self)
    104          return ObjectManager._setObject(self, id, obj, roles, user, set_owner)
    105
    106
    107 -    def _delObject(self, id, dp=1):
    108          """When deleted clear __primary_parent__."""
    109          obj = self._getOb(id, None)
    110          if obj is None:
    111              # Added this check because we are seeing stack traces in the UI.
    112              # We aren't 100% sure what is causing the object to disappear from
    113              # the ObjectManager. It could be that a different user had already
    114              # deleted it or that a single user had two brower tabs open. Ian saw
    115              # a case were the references on an object were wrong (getPrimaryId
    116              # pointed to the wrong location) but I'm not sure that is what is
    117              # causing this problem. -EAD
    118              log.warning(
    119              "Tried to delete object id '%s' but didn't find it on %s",
    120              id, self.getPrimaryId())
    121              return
    122          ObjectManager._delObject(self, id, dp)
    123          obj.__primary_parent__ = None
    124
    125
    126 -class PrimaryPathBTreeFolder2(BTreeFolder2):
    127      """
    128      BTreeFolder2 PrimaryPathObjectManager.
    129      """
    130 -    def _setObject(self, id, obj, roles=None, user=None, set_owner=1):
    131          """Track __primary_parent__ when we are set into an object"""
    132          obj.__primary_parent__ = aq_base(self)
    133          return ObjectManager._setObject(self, id, obj, roles, user, set_owner)
    134
    135
    136 -    def _delObject(self, id, dp=1):
    137          """When deleted clear __primary_parent__."""
    138          obj = self._getOb(id)
    139          ObjectManager._delObject(self, id, dp)
    140          obj.__primary_parent__ = None
    141