Package Products :: Package ZenModel :: Module CollectionItem |
[hide private]
[frames] | no frames]

# Source Code for Module Products.ZenModel.CollectionItem

      1  ##############################################################################
      2  #
      3  # Copyright (C) Zenoss, Inc. 2007, all rights reserved.
      4  #
      5  # This content is made available according to terms specified in
      6  # License.zenoss under the directory where your Zenoss product is installed.
      7  #
      8  ##############################################################################
      9
     10
     11  __doc__="""CollectionItem
     12
     13  Defines attributes for how a data source will be graphed
     14  and builds the nessesary rrd commands.
     15  """
     16
     17  from Globals import InitializeClass
     18  from Globals import DTMLFile
     19  from AccessControl import ClassSecurityInfo, Permissions
     20  from Products.ZenRelations.RelSchema import *
     21  from ZenModelRM import ZenModelRM
     22  from Products.ZenUtils.Utils import unused
     23  from Products.ZenUtils.deprecated import deprecated
     24
     25  @deprecated
     26 -def manage_addCollectionItem(context, id, deviceId, compPath, sequence,
     27                                                              REQUEST = None):
     28      ''' This is here so than zope will let us copy/paste/rename
     29      CollectionItems.
     30      '''
     31      unused(deviceId, compPath, sequence)
     32      ci = CollectionItem(id)
     33      context._setObject(id, ci)
     34      ci.deviceId = deviceId
     35      ci.compPath = compPath
     36      ci.sequence = sequence
     37      if REQUEST is not None:
     38          return REQUEST['RESPONSE'].redirect(context.absolute_url() +'/manage_main')
     39
     40  addCollectionItem = DTMLFile('dtml/addCollectionItem',globals())
     41
     42
     43 -class CollectionItem(ZenModelRM):
     44
     45      meta_type = 'CollectionItem'
     46
     47      sequence = 0
     48      deviceId = ''
     49      compPath = ''
     50      deviceOrganizer = ''
     51      recurse = False
     52
     53      _properties = (
     54          {'id':'sequence', 'type':'long', 'mode':'w'},
     55          {'id':'deviceId', 'type':'string', 'mode':'w'},
     56          {'id':'compPath', 'type':'string', 'mode':'w'},
     57          {'id':'deviceOrganizer', 'type':'string', 'mode':'w'},
     58          {'id':'recurse', 'type':'boolean', 'mode':'w'},
     59          )
     60
     61      _relations = (
     62          ('collection', ToOne(ToManyCont,'Products.ZenModel.Collection','collection_items')),
     63          )
     64
     65      factory_type_information = (
     66          {
     67              'immediate_view' : 'editCollectionItem',
     68              'actions'        :
     69              (
     70                  { 'id'            : 'edit'
     71                  , 'name'          : 'Collection Item'
     72                  , 'action'        : 'editCollectionItem'
     73                  , 'permissions'   : ( Permissions.view, )
     74                  },
     75              )
     76          },
     77      )
     78
     79      security = ClassSecurityInfo()
     80
     81 -    def __init__(self, id, deviceId='', compPath='', deviceOrganizer='',
     82                      recurse=False, sequence=0, title=None, buildRelations=True):
     83          ZenModelRM.__init__(self, id, title, buildRelations)
     84          self.deviceId = deviceId
     85          self.compPath = compPath
     86          self.deviceOrganizer = deviceOrganizer
     87          self.recurse = recurse
     88          self.sequence = sequence
     89
     90
     91 -    def getDesc(self, withLink=True):
     92          ''' Return a string that represents this item
     93          '''
     94          thing = self.getRepresentedItem()
     95          if not thing:
     96              desc = 'missing'
     97          elif self.deviceId:
     98              if self.compPath:
     99                  name = callable(thing.name) and thing.name() or thing.name
    100                  desc = '%s %s' % (self.deviceId, name)
    101              else:
    102                  desc = self.deviceId
    103              if withLink and thing:
    104                  desc = '<a href="%s">%s</a>' % (thing.getPrimaryUrlPath(),desc)
    105          else:
    106              if withLink and thing:
    107                  desc = '<a href="%s">%s</a>' % (thing.getPrimaryUrlPath(),
    108                      self.deviceOrganizer)
    109              else:
    110                  desc = self.deviceOrganizer
    111              if self.recurse:
    112                  desc += ' and suborganizers'
    113          return desc
    114
    115
    116 -    def getRepresentedItem(self):
    117          ''' Get the device organizer, component or device
    118          that this collection item represents
    119          '''
    120          thing = None
    121          if self.deviceId:
    122              thing = self.dmd.Devices.findDevice(self.deviceId)
    123              if self.compPath:
    124                  for part in self.compPath.split('/'):
    125                      if part:
    126                          thing = getattr(thing, part, None)
    127                          if not thing:
    128                              break
    129          elif self.deviceOrganizer:
    130              try:
    131                  thing = self.dmd.getObjByPath(self.deviceOrganizer.lstrip('/'))
    132              except KeyError:
    133                  thing = None
    134          return thing
    135
    136
    137 -    def getDevicesAndComponents(self):
    138          ''' Return a list of the devices and components referenced by this item
    139          '''
    140          thing = self.getRepresentedItem()
    141          if not thing:
    142              stuff = []
    143          elif self.deviceId:
    144              stuff = [thing]
    145          elif self.recurse:
    146              stuff = thing.getSubDevices(lambda d: d.monitorDevice())
    147          else:
    148              stuff = [ dev for dev in thing.devices() if dev.monitorDevice() ]
    149          if len(stuff) > 1:
    150              stuff.sort(key=lambda x: x.primarySortKey())
    151          return stuff
    152
    153
    154 -    def getNumDevicesAndComponents(self):
    155          ''' Return the number of devices and components matched by this item
    156          '''
    157          things = self.getDevicesAndComponents()
    158          return len(things)
    159
    160
    161
    162  InitializeClass(CollectionItem)
    163