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

# Source Code for Module Products.ZenModel.ManufacturerRoot

      1  ###########################################################################
      2  #
      3  # This program is part of Zenoss Core, an open source monitoring platform.
      4  # Copyright (C) 2007, 2009 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 or (at your
      8  # option) any later version as published by the Free Software Foundation.
      9  #
     10  # For complete information please visit: http://www.zenoss.com/oss/
     11  #
     12  ###########################################################################
     13
     14  __doc__="""ManufacturerRoot
     15
     16  The Manufacturer classification class.  default identifiers and screens,
     17  live here.
     18
     19  $Id: ManufacturerRoot.py,v 1.10 2004/04/22 02:14:12 edahl Exp $"""
     20
     21  __version__ = "$Revision: 1.10 $"[11:-2]
     22
     23  import logging
     24  log = logging.getLogger('zen')
     25
     26  import transaction
     27
     28  from Globals import InitializeClass
     29  from Acquisition import aq_base
     30  from AccessControl import Permissions as permissions
     31  from Products.ZenModel.ZenossSecurity import *
     32
     33  from Products.ZenRelations.PrimaryPathObjectManager import \
     34      PrimaryPathBTreeFolder2
     35
     36  from ZenModelItem import ZenModelItem
     37  from ZenPacker import ZenPacker
     38  from Products.ZenUtils.Search import \
     39      makeCaseSensitiveKeywordIndex, makeCaseInsensitiveFieldIndex
     40  from Products.ManagableIndex import FieldIndex
     41
     42 -def manage_addManufacturerRoot(context, REQUEST=None):
     43      """make a Manufacturer class"""
     44      m = ManufacturerRoot()
     45      context._setObject(m.getId(), m)
     46      m = context._getOb(m.dmdRootName)
     47      if REQUEST is not None:
     48          REQUEST['RESPONSE'].redirect(context.absolute_url() + '/manage_main')
     49
     50
     51  #addManufacturerRoot = DTMLFile('dtml/addManufacturerRoot',globals())
     52
     53
     54 -class ManufacturerRoot(ZenModelItem, PrimaryPathBTreeFolder2, ZenPacker):
     55      """
     56      The root organizer for manufacturers.  May become a BtreeFolder2 at
     57      some point (to scale better).  Has interface to manage Manufacturers
     58      and the products that they create.
     59      """
     60      dmdRootName = "Manufacturers"
     61      meta_type = "ManufacturerRoot"
     62      sub_classes = ('Manufacturer',)
     63      default_catalog = "productSearch"
     64
     65      # Screen action bindings (and tab definitions)
     66      factory_type_information = (
     67          {
     68              'id'             : 'Manufacturer',
     69              'meta_type'      : 'Manufacturer',
     70              'description'    : """Arbitrary device grouping class""",
     71              'icon'           : 'Manufacturer_icon.gif',
     72              'product'        : 'ZenModel',
     73              'factory'        : 'manage_addManufacturer',
     74              'immediate_view' : 'viewManufacturers',
     75              'actions'        :
     76              (
     77                  { 'id'            : 'overview'
     78                  , 'name'          : 'Overview'
     79                  , 'action'        : 'viewManufacturers'
     80                  , 'permissions'   : (
     81                    permissions.view, )
     82                  },
     83              )
     84            },
     85          )
     86
     87
     88 -    def __init__(self, id=None):
     89          if not id: id = self.dmdRootName
     90          super(ManufacturerRoot, self).__init__(id)
     91          PrimaryPathBTreeFolder2.__init__(self, id)
     92          self.createCatalog()
     93          self.buildzProperties()
     94
     95
     96 -    def manage_addManufacturer(self, manufacturerName=None, REQUEST=None):
     97          """Add a manufacturer from UI code.
     98          """
     99          if manufacturerName:
    100              self.createManufacturer(manufacturerName)
    101          if REQUEST: return self.callZenScreen(REQUEST)
    102
    103
    104 -    def manage_deleteManufacturers(self, ids=None, REQUEST=None):
    105          """Delete a list of manufacturers from UI.
    106          """
    107          if not ids: return self.callZenScreen(REQUEST)
    108          for id in ids: self._delObject(id)
    109          if REQUEST: return self.callZenScreen(REQUEST)
    110
    111
    112 -    def createManufacturer(self, manufacturerName=None):
    113          """Return and create if nessesary manufacturerName.
    114          """
    115          from Products.ZenModel.Manufacturer import manage_addManufacturer
    116          if manufacturerName and not self.has_key(manufacturerName):
    117              logging.info("Creating Manufacturer %s" % manufacturerName)
    118              manage_addManufacturer(self, manufacturerName)
    119          if manufacturerName:
    120              return self._getOb(manufacturerName)
    121          return None
    122
    123
    124 -    def getManufacturer(self, manufacturerName):
    125          """
    126          Return manufacturerName.  If it doesn't exist, create it.
    127          """
    128          manufacturerName = self.prepId(manufacturerName)
    129          if self.has_key(manufacturerName):
    130              return self._getOb(manufacturerName)
    131          else:
    132              for m in self.objectValues(spec="Manufacturer"):
    133                  if m.matches(manufacturerName):
    134                      return m
    135
    136          return self.createManufacturer(manufacturerName)
    137
    138
    139 -    def getManufacturerNames(self):
    140          """return list of all companies"""
    141          return self.objectIds(spec=("Manufacturer"))
    142
    143
    144 -    def getProductNames(self, mname, type=None):
    145          """return a list of all products this Manufacturer makes"""
    146          productFilter = dict(getManufacturerName=mname)
    147          if type == "OS":
    148              productFilter['meta_type'] = "SoftwareClass"
    149              productFilter['isOS'] = True
    150          elif type:
    151              productFilter['meta_type'] = type
    152
    153          cat = getattr(self, self.default_catalog)
    154          return sorted(['']+[entry.id for entry in cat(productFilter)])
    155
    156
    157 -    def findProduct(self, query):
    158          """Find a product by is productKey.
    159          """
    160          cat = getattr(self, self.default_catalog)
    161          brains = cat({'productKeys': query})
    162          if not brains: return None
    163          try:
    164              return self.getObjByPath(brains[0].getPrimaryId)
    165          except KeyError:
    166              log.warn("bad path '%s' index '%s'",
    167                          brains[0].getPrimaryId, self.default_catalog)
    168
    169
    170 -    def createHardwareProduct(self,prodName,manufacturer="Unknown",**kwargs):
    171          """Return and create if nessesary a HardwareClass object.
    172          """
    173          from Products.ZenModel.HardwareClass import HardwareClass
    174          return self._getProduct(prodName, manufacturer, HardwareClass, **kwargs)
    175
    176
    177 -    def createSoftwareProduct(self, prodName, manufacturer="Unknown", isOS=False, **kwargs):
    178          """Return and create if nesseary a SoftwareClass object.
    179          """
    180          from Products.ZenModel.SoftwareClass import SoftwareClass
    181          prod = self._getProduct(prodName, manufacturer, SoftwareClass, **kwargs)
    182          prod.isOS = isOS
    183          return prod
    184
    185
    186 -    def _getProduct(self, prodName, manufacturer, factory, **kwargs):
    187          prod = self.findProduct(prodName)
    188          if prod:
    189              # Product already exists. Return it.
    190              return prod
    191
    192          # Product doesn't already exist. Create it.
    193          prodid = self.prepId(prodName)
    194          manufobj = self.getManufacturer(manufacturer)
    195          prod = manufobj.products._getOb(prodid, None)
    196          if prod is None:
    197              prod = factory(prodid, prodName=prodName, **kwargs)
    198              manufobj.products._setObject(prodid, prod)
    199              prod = manufobj.products._getOb(prodid)
    200          return prod
    201
    202
    203 -    def getProductsGen(self):
    204          """Return a generator that gets all products.
    205          """
    206          for manuf in self.values(spec="Manufacturer"):
    207              for prod in manuf.products.objectValuesGen():
    208                  yield prod
    209
    210
    211 -    def reIndex(self):
    212          """Go through all devices in this tree and reindex them."""
    213          zcat = self._getOb(self.default_catalog)
    214          zcat.manage_catalogClear()
    215          for prod in self.getProductsGen():
    216              prod.index_object()
    217
    218
    219 -    def createCatalog(self):
    220          """Create a catalog for EventClassRecord searching"""
    221          from Products.ZCatalog.ZCatalog import manage_addZCatalog
    222
    223          # XXX update to use ManagableIndex
    224          manage_addZCatalog(self, self.default_catalog, self.default_catalog)
    225          zcat = self._getOb(self.default_catalog)
    226          cat = zcat._catalog
    227          cat.addIndex('productKeys',
    228              makeCaseSensitiveKeywordIndex('productKeys'))
    229          cat.addIndex('meta_type',
    230              makeCaseInsensitiveFieldIndex('meta_type'))
    231          cat.addIndex('getManufacturerName',
    232              makeCaseInsensitiveFieldIndex('getManufacturerName'))
    233          cat.addIndex('isOS', FieldIndex('isOS'))
    234          zcat.addColumn('getPrimaryId')
    235          zcat.addColumn('id')
    236
    237
    238 -    def exportXml(self, ofile, ignorerels=[], root=False):
    239          """Return an xml based representation of a RelationshipManager
    240          <object id='/Devices/Servers/Windows/dhcp160.confmon.loc'
    241              module='Products.Confmon.IpInterface' class='IpInterface'>
    242              <property id='name'>jim</property>
    243              <toone></toone>
    244              <tomany></tomany>
    245              <tomanycont></tomanycont>
    246          </object>
    247          """
    248          modname = self.__class__.__module__
    249          classname = self.__class__.__name__
    250          id = root and self.getPrimaryId() or self.id
    251          stag = "<object id='%s' module='%s' class='%s'>\n" % (
    252                      id , modname, classname)
    253          ofile.write(stag)
    254          for obj in self.objectValues():
    255              if getattr(aq_base(obj), 'exportXml', False):
    256                  obj.exportXml(ofile, ignorerels)
    257          ofile.write("</object>\n")
    258
    259
    260 -    def buildzProperties(self):
    261          if getattr(aq_base(self), "zDeviceClass", False): return
    262          self._setProperty("zDeviceClass", "")
    263          self._setProperty("zDeviceGroup", "")
    264          self._setProperty("zSystem", "")
    265
    266
    267  InitializeClass(ManufacturerRoot)
    268