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

Source Code for Module ZenModel.ManufacturerRoot

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