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

# Source Code for Module Products.ZenModel.ManufacturerRoot

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