ERP5DocumentConduit.py 7.46 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
# -*- coding: utf-8 -*-
##############################################################################
#
# Copyright (c) 2009 Nexedi SA and Contributors. All Rights Reserved.
#          Danièle Vanbaelinghem <daniele@gmail.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
##############################################################################

from Products.ERP5SyncML.Conduit.ERP5Conduit import ERP5Conduit
from AccessControl import ClassSecurityInfo
from Products.ERP5Type import Permissions
from Products.ERP5SyncML.SyncCode import SyncCode
34 35
from ERP5Diff import ERP5Diff
import re
36 37
from lxml import etree
parser = etree.XMLParser(remove_blank_text=True)
38

39 40
# Declarative security
security = ClassSecurityInfo()
41 42 43 44 45 46 47 48

class ERP5DocumentConduit(ERP5Conduit):
  """
  ERP5DocumentConduit provides two methods who permit to have the GID
  The Gid is composed by the title : "Reference-Version-Language"
  this class is made for unit test
  """

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
  security.declareProtected(Permissions.ModifyPortalContent, 'applyXupdate')
  def applyXupdate(self, object=None, xupdate=None, conduit=None, force=0,
                   simulate=0, reset=0, **kw):
    """
    Parse the xupdate and then it will call the conduit
    """
    conflict_list = []
    if isinstance(xupdate, (str, unicode)):
      xupdate = etree.XML(xupdate, parser=parser)
    xupdate = self.manageDataModification(xml_xupdate=xupdate,\
                   previous_xml=kw['previous_xml'], object=object,
                   simulate=simulate, reset=reset)
    for subnode in xupdate:
      sub_xupdate = self.getSubObjectXupdate(subnode)
      if subnode.xpath('name()') in self.XUPDATE_INSERT_OR_ADD:
        conflict_list += conduit.addNode(xml=sub_xupdate, object=object,
                                         force=force, simulate=simulate,
                                         reset=reset, **kw)['conflict_list']
      elif subnode.xpath('name()') in self.XUPDATE_DEL:
        conflict_list += conduit.deleteNode(xml=sub_xupdate, object=object,
                                            force=force, simulate=simulate,
Nicolas Delaby's avatar
Nicolas Delaby committed
70
                                            reset=reset, **kw)
71 72 73
      elif subnode.xpath('name()') in self.XUPDATE_UPDATE:
        conflict_list += conduit.updateNode(xml=sub_xupdate, object=object,
                                            force=force, simulate=simulate,
Nicolas Delaby's avatar
Nicolas Delaby committed
74
                                            reset=reset, **kw)
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

    return conflict_list

  security.declareProtected(Permissions.ModifyPortalContent, 'manageDataModification')
  def manageDataModification(self, xml_xupdate, previous_xml, object,
      simulate=None, reset=None):
    data_change = {}
    if previous_xml is not None:
      previous_xml = etree.XML(previous_xml)
    else:
      previous_xml = etree.XML(object.asXML())
    from copy import deepcopy
    xml_previous = deepcopy(previous_xml)
    #retrieve new data
    for subnode in xml_xupdate:
      sub_xupdate = self.getSubObjectXupdate(subnode)
      attribute = sub_xupdate.attrib.get('select', None)
      if 'block_data' in attribute:
        #retrieve path for the element and use on previous_xml
        prop_list = attribute.split('/')
        prop_id = prop_list[1]
        path_prop_id = '//' + prop_id
        if data_change.has_key(prop_id):
          xml = data_change[prop_id]
        else:
          xml = xml_previous.xpath(path_prop_id)[0]
101
        request = prop_list[-1]
102 103 104
        if getattr(ERP5Diff, '__version__', 0.0) <= 0.2:
          #Old ERP5Diff, xpath position start from 0, so add +1 to be compliant
          request = re.sub('(\d+)', lambda match:str(int(match.group(0))+1), request)
105
        if subnode.xpath('name()') in self.XUPDATE_DEL:
106 107
          node_to_remove_list = xml.xpath(request)
          if node_to_remove_list:
Nicolas Delaby's avatar
Nicolas Delaby committed
108
            xml.remove(node_to_remove_list[0]) 
109
            data_change[prop_id] = xml
110 111 112
          xml_xupdate.remove(subnode)
        elif subnode.xpath('name()') in self.XUPDATE_UPDATE:
          #retrieve element in previous_xml
113 114
          element = xml.xpath(request)[0]
          element.text = subnode.text
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
          data_change[prop_id] = xml
          xml_xupdate.remove(subnode)
      elif subnode.xpath('name()') in self.XUPDATE_INSERT_OR_ADD:
        if self.getSubObjectDepth(subnode[0]) == 0:
          #check element have not sub object
          attribute = subnode.attrib.get('select', None)
          if 'block_data' in attribute:
            prop_id = attribute.split('/')[2]
            if prop_id in self.data_type_tag_list:
              path_prop_id = '//' + prop_id
              if data_change.has_key(prop_id):
                xml = data_change[prop_id]
              else:
                xml = xml_previous.xpath(path_prop_id)[0]
              for element in self.getXupdateElementList(subnode):
                name_element = element.attrib.get('name', None)
                if name_element:
                  for sub_element in element:
                    if sub_element.xpath('name()') in 'xupdate:attribute':
                      name_attribute = sub_element.attrib.get('name')
                      value_attribute = sub_element.text
                  block = etree.SubElement(xml, name_element)
                  block.set(name_attribute, value_attribute)
                  #change structure in xupdate because is bad formed
                  value = etree.tostring(element).split('</')[1].split('>')[1]
                  block.text = value
              data_change[prop_id] = xml
              xml_xupdate.remove(subnode)

    #apply modification
145
    if len(data_change):
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
      args = {}
      for key in data_change.keys():
        node = data_change[key]
        node.text = None
        data = self.convertXmlValue(node)
        args[key] = data
        args = self.getFormatedArgs(args=args)
        #XXX manage conflict
        if args != {} and (not simulate or reset):
          self.editDocument(object=object, **args)
          # It is sometimes required to do something after an edit
          if getattr(object, 'manage_afterEdit', None) is not None:
            object.manage_afterEdit()

    return xml_xupdate

162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
  # Declarative security
  security = ClassSecurityInfo()
  def getGidFromObject(self, object):
    """
    return the Gid generate with the reference, object, language of the object
    """
    return "%s-%s-%s" %\
      (object.getReference(), object.getVersion(), object.getLanguage())

#  def getGidFromXML(self, xml):
#    """
#    return the Gid composed of FirstName and LastName generate with a peace of
#    xml
#    """
#    #to be defined