Back to index

plone3  3.1.7
atcttool.py
Go to the documentation of this file.
00001 from Products.ATContentTypes.interface import IATCTTool
00002 from Products.GenericSetup.utils import PropertyManagerHelpers
00003 from Products.GenericSetup.utils import XMLAdapterBase
00004 from Products.GenericSetup.utils import exportObjects
00005 from Products.GenericSetup.utils import importObjects
00006 from Products.CMFCore.utils import getToolByName
00007 
00008 def safeGetAttribute(node, attribute):
00009     """Get an attribute froma node, but return None if it does not exist.
00010     """
00011     if not node.hasAttribute(attribute):
00012         return None
00013     else:
00014         return node.getAttribute(attribute)
00015 
00016 
00017 class ATCTToolXMLAdapter(XMLAdapterBase, PropertyManagerHelpers):
00018     """Node in- and exporter for ATCTTool.
00019     """
00020     __used_for__ = IATCTTool
00021 
00022     def _exportNode(self):
00023         """Export the object as a DOM node.
00024         """
00025         node=self._doc.createElement('atcttool')
00026         node.appendChild(self._extractSettings())
00027         node.appendChild(self._extractProperties())
00028 
00029         self._logger.info('ATCTTool settings exported.')
00030         return node
00031 
00032     def _importNode(self, node):
00033         purge = self.environ.shouldPurge()
00034         if node.getAttribute('purge'):
00035             purge = self._convertToBoolean(node.getAttribute('purge'))
00036 
00037         if purge:
00038             self._purgeSettings()
00039             self._purgeProperties()
00040 
00041         self._initSettings(node)
00042         self._initProperties(node)
00043         self._logger.info('ATCTTool settings imported.')
00044 
00045     def _purgeSettings(self):
00046         # initialize topic tool
00047         self.context.topic_indexes = {}
00048         self.context.topic_metadata = {}
00049         self.context.allowed_portal_types = []
00050         self.context.createInitialIndexes()
00051         self.context.createInitialMetadata()
00052 
00053     def _initSettings(self, node):
00054         for child in node.childNodes:
00055             if child.nodeName=='topic_indexes':
00056                 for indexNode in child.childNodes:
00057                     if indexNode.nodeName=='index':
00058                         name=indexNode.getAttribute('name')
00059                         if indexNode.hasAttribute("remove"):
00060                             self.context.removeIndex(name)
00061                             continue
00062 
00063                         try:
00064                             self.context.getIndex(name)
00065                         except AttributeError:
00066                             self.context.addIndex(name)
00067 
00068                         description=safeGetAttribute(indexNode, 'description')
00069                         enabled=safeGetAttribute(indexNode, 'enabled')
00070                         if enabled is not None:
00071                             enabled=self._convertToBoolean(enabled)
00072                         friendlyName=safeGetAttribute(indexNode, 'friendlyName')
00073 
00074                         criteria = None
00075                         for critNode in indexNode.childNodes:
00076                             if critNode.nodeName == 'criteria':
00077                                 for textNode in critNode.childNodes:
00078                                     if textNode.nodeName != '#text' or \
00079                                         not textNode.nodeValue.strip():
00080                                         continue
00081                                     if criteria is None:
00082                                         criteria = []
00083                                     criteria.append(str(textNode.nodeValue))
00084                     
00085                         self.context.updateIndex(name,
00086                                               friendlyName=friendlyName,
00087                                               description=description,
00088                                               enabled=enabled,
00089                                               criteria=criteria)
00090                     
00091             if child.nodeName=='topic_metadata':
00092                 for metadataNode in child.childNodes:
00093                     if metadataNode.nodeName=='metadata':
00094                         name=metadataNode.getAttribute('name')
00095                         if metadataNode.hasAttribute("remove"):
00096                             self.context.removeMetadata(name)
00097                             continue
00098 
00099                         try:
00100                             self.context.getMetadata(name)
00101                         except AttributeError:
00102                             self.context.addMetadata(name)
00103 
00104                         description=safeGetAttribute(metadataNode, 'description')
00105                         enabled=safeGetAttribute(metadataNode, 'enabled')
00106                         if enabled is not None:
00107                             enabled=self._convertToBoolean(enabled)
00108                         friendlyName=safeGetAttribute(metadataNode, 'friendlyName')
00109                         self.context.updateMetadata(name,
00110                                                  friendlyName=friendlyName,
00111                                                  description=description,
00112                                                  enabled=enabled)
00113 
00114     def _extractSettings(self):
00115         fragment = self._doc.createDocumentFragment()
00116         # topic tool indexes
00117         indexes=self._doc.createElement('topic_indexes')
00118         for indexname in self.context.getIndexes():
00119             index = self.context.getIndex(indexname)
00120             child=self._doc.createElement('index')
00121             child.setAttribute('name', str(indexname))
00122             child.setAttribute('friendlyName', str(index.friendlyName))
00123             child.setAttribute('description', str(index.description))
00124             child.setAttribute('enabled', str(bool(index.enabled)))
00125             for criteria in index.criteria:
00126                 if criteria != 'criterion':
00127                     sub = self._doc.createElement('criteria')
00128                     sub.appendChild(self._doc.createTextNode(criteria))
00129                     child.appendChild(sub)
00130             indexes.appendChild(child)
00131         fragment.appendChild(indexes)
00132         # topic tool metadata
00133         metadata=self._doc.createElement('topic_metadata')
00134         for metaname in self.context.getAllMetadata():
00135             meta = self.context.getMetadata(metaname)
00136             child=self._doc.createElement('metadata')
00137             child.setAttribute('name', str(metaname))
00138             child.setAttribute('friendlyName', str(meta.friendlyName))
00139             child.setAttribute('description', str(meta.description))
00140             child.setAttribute('enabled', str(bool(meta.enabled)))
00141             metadata.appendChild(child)
00142         fragment.appendChild(metadata)
00143         
00144         return fragment
00145 
00146 def importATCTTool(context):
00147     """Import ATCT Tool configuration.
00148     """
00149     site = context.getSite()
00150     tool = getToolByName(site, 'portal_atct')
00151 
00152     importObjects(tool, '', context)
00153 
00154 def exportATCTTool(context):
00155     """Export ATCT Tool configuration.
00156     """
00157     site = context.getSite()
00158     tool = getToolByName(site, 'portal_atct', None)
00159     if tool is None:
00160         logger = context.getLogger("atcttool")
00161         logger.info("Nothing to export.")
00162         return
00163 
00164     exportObjects(tool, '', context)