## author: Harald Friessnegger <harald (at) webmeisterei dot com>
## date: 2006-05-09
## install this as an external method and change the names for
## source and destination folders.
##
## ATTENTION: i changed variable names from customer project specific names to source and dest
## which might have broken the code. please report errors to the above email address.

from Products.Archetypes.debug import log
import transaction

def cutAndPasteObjects(context):

    portal = context.portal_url.getPortalObject()
    source = portal.restrictedTraverse('sourcefolder')
    dest = portal.restrictedTraversse('destination')
    catalog = portal.portal_catalog


    sourcePath = '/'.join(source.getPhysicalPath())
    destPath = '/'.join(dest.getPhysicalPath())

    sourceSubObjs = catalog(path={'query': sourcePath, 'depth': 1})
    destSubObjs = catalog(path={'query': destPath, 'depth': 1})


    #print "objects in source according catalog: %d" % len(sourceSubObjs)

    #print "objects in destination according catalog: %d" % len(destSubObjs)

    #print "objectIds in source: %s" % len(source.objectIds())

    #print "objectIds in destination: %s" % len(dest.objectIds())

    REQUEST = context.REQUEST

    idList = source.objectIds()
    nrObjects = len(idList)
    count = 0
    for id in idList:
        count = count + 1
        newLocation = destPath + "/" + id
        log("migration btree: copy obj %d of %d to %s" % (count, nrObjects, newLocation))
        cutMsg = source.manage_cutObjects(id, REQUEST)
        pasteMsg = dest.manage_pasteObjects(REQUEST['__cp'])
        transaction.commit()


    return "success!"
