#from Products.CMFCore.utils import getToolByName
#from zope.app import zapi
#from Products.GenericSetup.interfaces import INode
#import Products

def _importNode(self, node):
    """Import the object from the DOM node.
    """
    na = node.attributes.items()
    something_changed = False
    new_indexes = []
    new_columns = []
    meta_type = na[0][1]
    name = na[1][1]
    if self.environ.shouldPurge():
        self._purgeProperties()
        self._purgeObjects()
        self._purgeIndexes()
        self._purgeColumns()
    columns = []
    # for col in self._extractColumns()._get_childNodes():
    #     columns.append(col.attributes.items()[0][1])
    for col in self.context.schema():
        columns.append(col)
    inds = {}
    # for ind in self._extractIndexes()._get_childNodes():
    #     inds[ind.attributes.items()[1][1]] = ind.attributes.items()[0][1]
    for ind in self.context.getIndexObjects():
        inds[ind.getId()] = ind.meta_type
    if meta_type in ('CMF Catalog',"WAeUP Table"):
        self._logger.info("Working on %s"  % na[1][1])
        for ch in node._get_childNodes():
            if ch.nodeName in ('index',):
                cha = ch.attributes.items()
                iname = cha[1][1]
                itype = cha[0][1]
                if iname in inds.keys() and inds[iname] == itype:
                    continue
                new_indexes.append(iname)
                something_changed = True
            elif ch.nodeName in ('column',):
                cha = ch.attributes.items()
                iname = cha[0][1]
                if iname in columns:
                    continue
                new_columns.append(iname)
                something_changed = True
            else:
                continue
    if something_changed :
        #import pdb;pdb.set_trace()
        self._logger.info("Catalog tool %s cleared."  % na[1][1])
        self._logger.info("%s modified." % na[1][1])
        self._initProperties(node)
        if new_indexes and len(self.context()) == 0: #disable creation of new indexes if records in the table
            self._initObjects(node)
            self._initIndexes(node)
        self._initColumns(node)
        #self._refreshCatalog()

    self._logger.info("Catalog tool %s imported."  % na[1][1])
    
from Products.CPSCore.exportimport.catalog import CatalogToolXMLAdapter
CatalogToolXMLAdapter._importNode = _importNode
