Back to index

plone3  3.1.7
exportimport.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2005 Zope Corporation and Contributors. All Rights Reserved.
00004 #
00005 # This software is subject to the provisions of the Zope Public License,
00006 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
00007 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00008 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00009 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00010 # FOR A PARTICULAR PURPOSE.
00011 #
00012 ##############################################################################
00013 """ZCatalog export / import support.
00014 
00015 $Id: exportimport.py 76398 2007-06-06 11:28:46Z jens $
00016 """
00017 
00018 from zope.component import adapts
00019 from zope.component import queryMultiAdapter
00020 
00021 from Products.GenericSetup.interfaces import INode
00022 from Products.GenericSetup.interfaces import ISetupEnviron
00023 from Products.GenericSetup.utils import ObjectManagerHelpers
00024 from Products.GenericSetup.utils import PropertyManagerHelpers
00025 from Products.GenericSetup.utils import XMLAdapterBase
00026 
00027 from Products.ZCatalog.interfaces import IZCatalog
00028 
00029 
00030 class _extra:
00031 
00032     pass
00033 
00034 
00035 class ZCatalogXMLAdapter(XMLAdapterBase, ObjectManagerHelpers,
00036                          PropertyManagerHelpers):
00037 
00038     """XML im- and exporter for ZCatalog.
00039     """
00040 
00041     adapts(IZCatalog, ISetupEnviron)
00042 
00043     _LOGGER_ID = 'catalog'
00044 
00045     name = 'catalog'
00046 
00047     def _exportNode(self):
00048         """Export the object as a DOM node.
00049         """
00050         node = self._getObjectNode('object')
00051         node.appendChild(self._extractProperties())
00052         node.appendChild(self._extractObjects())
00053         node.appendChild(self._extractIndexes())
00054         node.appendChild(self._extractColumns())
00055 
00056         self._logger.info('Catalog exported.')
00057         return node
00058 
00059     def _importNode(self, node):
00060         """Import the object from the DOM node.
00061         """
00062         if self.environ.shouldPurge():
00063             self._purgeProperties()
00064             self._purgeObjects()
00065             self._purgeIndexes()
00066             self._purgeColumns()
00067 
00068         self._initProperties(node)
00069         self._initObjects(node)
00070         self._initIndexes(node)
00071         self._initColumns(node)
00072 
00073         self._logger.info('Catalog imported.')
00074 
00075     def _extractIndexes(self):
00076         fragment = self._doc.createDocumentFragment()
00077         indexes = self.context.getIndexObjects()[:]
00078         indexes.sort(lambda x,y: cmp(x.getId(), y.getId()))
00079         for idx in indexes:
00080             exporter = queryMultiAdapter((idx, self.environ), INode)
00081             if exporter:
00082                 fragment.appendChild(exporter.node)
00083         return fragment
00084 
00085     def _purgeIndexes(self):
00086         for idx_id in self.context.indexes():
00087             self.context.delIndex(idx_id)
00088 
00089     def _initIndexes(self, node):
00090         for child in node.childNodes:
00091             if child.nodeName != 'index':
00092                 continue
00093             if child.hasAttribute('deprecated'):
00094                 continue
00095             zcatalog = self.context
00096 
00097             idx_id = str(child.getAttribute('name'))
00098             if child.hasAttribute('remove'):
00099                 # Remove index if it is there; then continue to the next
00100                 # index.  Removing a non existing index should not cause an
00101                 # error, so you can apply the profile twice without problems.
00102                 if idx_id in zcatalog.indexes():
00103                     zcatalog.delIndex(idx_id)
00104                 continue
00105 
00106             if idx_id not in zcatalog.indexes():
00107                 extra = _extra()
00108                 for sub in child.childNodes:
00109                     if sub.nodeName == 'extra':
00110                         name = str(sub.getAttribute('name'))
00111                         value = str(sub.getAttribute('value'))
00112                         setattr(extra, name, value)
00113                 extra = extra.__dict__ and extra or None
00114 
00115                 meta_type = str(child.getAttribute('meta_type'))
00116                 zcatalog.addIndex(idx_id, meta_type, extra)
00117 
00118             idx = zcatalog._catalog.getIndex(idx_id)
00119             importer = queryMultiAdapter((idx, self.environ), INode)
00120             if importer:
00121                 importer.node = child
00122 
00123     def _extractColumns(self):
00124         fragment = self._doc.createDocumentFragment()
00125         schema = self.context.schema()[:]
00126         schema.sort()
00127         for col in schema:
00128             child = self._doc.createElement('column')
00129             child.setAttribute('value', col)
00130             fragment.appendChild(child)
00131         return fragment
00132 
00133     def _purgeColumns(self):
00134         for col in self.context.schema()[:]:
00135             self.context.delColumn(col)
00136 
00137     def _initColumns(self, node):
00138         for child in node.childNodes:
00139             if child.nodeName != 'column':
00140                 continue
00141             col = str(child.getAttribute('value'))
00142             if child.hasAttribute('remove'):
00143                 # Remove the column if it is there
00144                 if col in self.context.schema()[:]:
00145                     self.context.delColumn(col)
00146                 continue
00147             if col not in self.context.schema()[:]:
00148                 self.context.addColumn(col)
00149