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

# Source Code for Module Products.ZenModel.Collection

      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__="""Collection
     15
     16  Collection is a grouping of devices and components.
     17  """
     18
     19
     20  from Globals import InitializeClass
     21  from AccessControl import ClassSecurityInfo, Permissions
     22  from Globals import DTMLFile
     23  from Products.ZenRelations.RelSchema import *
     24  from ZenModelRM import ZenModelRM
     25  from Products.ZenWidgets import messaging
     26
     27
     28 -def manage_addCollection(context, id, REQUEST = None):
     29      ''' This is here so than zope will let us copy/paste/rename
     30      Collections.
     31      '''
     32      c = Collection(id)
     33      context._setObject(id, c)
     34      if REQUEST is not None:
     35          return REQUEST['RESPONSE'].redirect(context.absolute_url() +'/manage_main')
     36
     37  addCollection = DTMLFile('dtml/addCollection',globals())
     38
     39
     40 -class Collection(ZenModelRM):
     41      ''' Holds an assortment of devices and/or components.
     42      '''
     43
     44      meta_type = 'Collection'
     45
     46      _properties = (
     47          )
     48
     49      _relations =  (
     50          ('report',
     51              ToOne(ToManyCont, 'Products.ZenModel.MultiGraphReport', 'collections')),
     52          ('items',
     53              ToManyCont(ToOne, 'Products.ZenModel.CollectionItem', 'collection')),
     54          )
     55
     56      factory_type_information = (
     57      {
     58          'immediate_view' : 'editCollection',
     59          'actions'        :
     60          (
     61              { 'id'            : 'edit'
     62              , 'name'          : 'Collection'
     63              , 'action'        : 'editCollection'
     64              , 'permissions'   : ( Permissions.view, )
     65              },
     66          )
     67      },
     68      )
     69
     70      security = ClassSecurityInfo()
     71
     72
     73 -    def createCollectionItem(self, orgPath='', devId='', compPath='',
     74                              recurse=False, checkExists=False):
     75          ''' Create and insert a new CollectionItem based either on the
     76          orgPath or on devId/compPath.  Returns the new item.
     77          '''
     78          from CollectionItem import CollectionItem
     79          ci = CollectionItem(self.getUnusedId('items', 'Item'))
     80          if orgPath:
     81              ci.deviceOrganizer = orgPath
     82          else:
     83              ci.deviceId = devId
     84              ci.compPath = compPath
     85          ci.recurse = recurse
     86          ci.sequence = len(self.items())
     87          self.items._setObject(ci.id, ci)
     88          ci = self.items._getOb(ci.id)
     89          # This check happens after the _setObject so that ci has full
     90          # aq wrapper in case it needs it.
     91          if checkExists and not ci.getRepresentedItem():
     92              self.items._delObject(ci.id)
     93              ci = None
     94          return ci
     95
     96
     97      security.declareProtected('Manage DMD', 'manage_addCollectionItem')
     98 -    def manage_addCollectionItem(self, itemType,
     99              deviceIds=(), componentPaths=(), deviceClasses=(), systems=(),
    100              groups=(), locations=(), recurse=False, REQUEST=None):
    101          ''' Create a new CollectionItem and add to this collection
    102          '''
    103          count = 0
    104          if itemType == 'devcomp':
    105              if not deviceIds:
    106                  deviceIds = []
    107              if not componentPaths:
    108                  componentPaths = ['']
    109              for i, devId in enumerate(deviceIds):
    110                  for cPath in componentPaths:
    111                      ci = self.createCollectionItem(devId=devId, compPath=cPath,
    112                          recurse=False, checkExists=True)
    113                      if ci:
    114                          count += 1
    115          if itemType == 'deviceClass':
    116              for dClass in deviceClasses:
    117                  self.createCollectionItem(
    118                                      orgPath='/Devices' + dClass, recurse=recurse)
    119              count += 1
    120          if itemType == 'system':
    121              for system in systems:
    122                  self.createCollectionItem(
    123                                      orgPath='/Systems' + system, recurse=recurse)
    124                  count += 1
    125          if itemType == 'group':
    126              for group in groups:
    127                  self.createCollectionItem(
    128                                      orgPath='/Groups' + group, recurse=recurse)
    129                  count += 1
    130          if itemType == 'location':
    131              for loc in locations:
    132                  self.createCollectionItem(
    133                                      orgPath='/Locations' + loc, recurse=recurse)
    134                  count += 1
    135
    136          if REQUEST:
    137              messaging.IMessageSender(self).sendToBrowser(
    138                  'Items Added',
    139                  ' %s item%s added' % (count, count > 1 and 's' or '')
    140              )
    141              return self.callZenScreen(REQUEST)
    142
    143
    144      security.declareProtected('Manage DMD', 'manage_deleteCollectionItems')
    145 -    def manage_deleteCollectionItems(self, ids=(), REQUEST=None):
    146          ''' Delete collection items from this report
    147          '''
    148          for id in ids:
    149              self.items._delObject(id)
    150          self.manage_resequenceCollectionItems()
    151          if REQUEST:
    152              count = len(ids)
    153              messaging.IMessageSender(self).sendToBrowser(
    154                  'Items Deleted',
    155                  ' %s item%s deleted' % (count, count > 1 and 's' or '')
    156              )
    157              return self.callZenScreen(REQUEST)
    158
    159
    160      security.declareProtected('Manage DMD', 'manage_resequenceCollectionItems')
    161 -    def manage_resequenceCollectionItems(self, seqmap=(), origseq=(),
    162                                                                  REQUEST=None):
    163          """Reorder the sequence of the items.
    164          """
    165          from Products.ZenUtils.Utils import resequence
    166          return resequence(self, self.items(), seqmap, origseq, REQUEST)
    167
    168
    169      security.declareProtected('Manage DMD', 'getItems')
    170 -    def getItems(self):
    171          ''' Return an ordered list of CollectionItems
    172          '''
    173          import sys
    174          def cmpItems(a, b):
    175              try: a = int(a.sequence)
    176              except ValueError: a = sys.maxint
    177              try: b = int(b.sequence)
    178              except ValueError: b = sys.maxint
    179              return cmp(a, b)
    180          items =  self.items()[:]
    181          items.sort(cmpItems)
    182          return items
    183
    184
    185 -    def getNumItems(self):
    186          ''' Return the number of collection items
    187          '''
    188          return len(self.items())
    189
    190
    191 -    def getDevicesAndComponents(self):
    192          ''' Return a deduped list of devices and components represented
    193          by this collection's collectionitems
    194          '''
    195          things = []
    196          tset = set()
    197          for collectionItem in self.getItems():
    198              devsAndComps = collectionItem.getDevicesAndComponents()
    199              for devOrComp in devsAndComps:
    200                  tid = devOrComp.getPrimaryId()
    201                  if tid not in tset:
    202                      tset.add(tid)
    203                      things.append(devOrComp)
    204          return things
    205
    206  InitializeClass(Collection)
    207