## Controller Python Script "bibliography_entry_addOwner"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=with_disabled=False
##title=
##

from Products.CMFCore.permissions import ModifyPortalContent

bib_tool = context.portal_bibliography
plone_utils = context.plone_utils
mtool = context.portal_membership

if context.portal_type in bib_tool.getReferenceTypes() and mtool.checkPermission(ModifyPortalContent, context):

    bibfolder = context.getBibFolder()
    	
    # id cooking
    if (bibfolder.getCookIdsOnBibRefCreation() and plone_utils.isIDAutoGenerated(context.getId())) or bibfolder.getCookIdsAfterBibRefEdit():
	new_id = bib_tool.cookReferenceId(ref=context, idcooker_id=bibfolder.getReferenceIdCookingMethod(), with_disabled=with_disabled)
	if new_id != 'nobody1000':
	    while bibfolder.hasObject(new_id) and new_id != context.getId():
		new_id = bibfolder.nextId(new_id)
	    bib_tool.transaction_savepoint(optimistic=True)
	    
	    # as of plone-2.1.3 this will cause an uncatalog warning in the logs if member reference support
	    # is enabled in the bib tool. Should be mended within Archetypes' reference engine...
            context.setId(value=new_id)	    

