def buildAndStorePreview(obj, event):
    u"""
    We get a buildAndStorePreview request.
    Let's check wether we have to do it or not, depending on options
    and test wether file has been modified or not.
    If this is ok, call the adapter's method buildAndStorePreview.
    """
    logger = logging.getLogger('buildAndStorePreview')
    #print "BUILD AND STORE PREVIEW %s on %s" % (obj.getPhysicalPath(),event)
    form = obj.REQUEST.form
    if form.get('file_delete', 'changed') == "nochange":
        msg = ("File field of %s has not changed"
              ": no preview computation" % "/".join(obj.getPhysicalPath()))
        logger.log(logging.INFO, msg)
        # file is modified ; dirty preview
        previewable.fileModified()
        previewable.reindexFilePreview()
        return
    
    previewable = IPreviewable(obj)
    isPreviewable = getattr(obj, 'isPreviewable', "always")
    if isPreviewable == "always":
        previewable.buildAndStorePreview()
    else:
        # file is modified ; dirty preview
        previewable.fileModified()
        previewable.reindexFilePreview()
        msg = ("File's preview option is %s"
              ": no new preview for %s " % (isPreviewable,
                                            "/".join(obj.getPhysicalPath())))
class PreviewProvider( BrowserView ):

    implements( IPreviewProvider )
  
    def __init__(self, context, request):
        self.context = context
        self.request = request
        self.object = IPreviewable(context)
       
    def hasPreview(self):
        """
        """
        return self.object.hasPreview()
  
    def getPreview(self):
        """
        """
        return self.object.getPreview()
    
    def updatePreview(self):
        """
        """
        self.object.buildAndStorePreview()
    

    ## XXX This should NOT be here !!
    ## but I wanted the feature badly...
    def updateAllPreviews(self):
        """
        """
        pc = self.context.portal_catalog
        brains = pc(portal_type='File')
        status=""
        for brain in brains:
            status+="<div>"+brain.getPath()
            try:
                obj=brain.getObject()
                IPreviewable(obj).buildAndStorePreview()
                obj.reindexObject()
            except Exception, e:
                status+=" %s %s</div>" % (str(e.__class__.__name__), str(e))
            else:
                status+=" OK</div>\n"
        return status
class PreviewProvider( BrowserView ):

    implements( IPreviewProvider )
  
    def __init__(self, context, request):
        self.context = context
        self.request = request
        self.object = IPreviewable(context)
       
    def hasPreview(self):
        u"""
        """
        return self.object.hasPreview()
  
    def getPreview(self):
        u"""
        """
        return self.object.getPreview() 
    
    def updatePreview(self):
        u"""
        """
        self.object.buildAndStorePreview()

    def updatePreviewOnDemand(self):
        u"""
        """
        self.object.buildAndStorePreview()
        self.request.response.redirect(self.context.absolute_url()+'/view')


    ## XXX This should NOT be here !!
    ## but I wanted the feature badly...
    def updateAllPreviews(self, updateNewOnly = False):
        u"""
        update all objects' preview ; may be usefull if you change a transform
        """
        logger = logging.getLogger('UpdateAllPreviewsLog')
        pc = self.context.portal_catalog
        #brains = pc(portal_type='File')
        brains = pc(object_provides="Products.ARFilePreview.interfaces.IPreviewAware",sort_on='modified',sort_order='reverse')
        status=""
        for brain in brains:
            if updateNewOnly and ( brain.lastFileChange < brain.lastPreviewUpdate ):
                continue
            status+="<div>"+brain.getPath()
            logger.log(logging.INFO, brain.getPath())
            try:
                obj=brain.getObject()
                IPreviewable(obj).updatePreview()
                obj.reindexObject()
                
            except Exception, e:
                msg = "%s %s %s" % (brain.getPath(), str(e.__class__.__name__), str(e))
                status+= "%s </div>" % msg
                logger.log(logging.ERROR, msg)
            else:
                msg = "%s OK" % (brain.getPath(),)
                status+= "%s </div>" % msg
                logger.log(logging.INFO, msg)
            try:
                transaction.commit()
            except Exception, e:
                msg = "Commit error on object %s : %s %s ; trying abort" % (brain.getPath(), str(e.__class__.__name__), str(e))
                logger.log(logging.ERROR, msg)
                try:
                    transaction.abort()
                except:
                    msg = "Abort error on object %s : %s %s " % (brain.getPath(), str(e.__class__.__name__), str(e))
                    logger.log(logging.ERROR, msg)
                    pass