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