Back to index

plone3  3.1.7
policy.py
Go to the documentation of this file.
00001 ##################################################################
00002 #
00003 # (C) Copyright 2006-2007 ObjectRealms, LLC
00004 # All Rights Reserved
00005 #
00006 # This file is part of iterate.
00007 #
00008 # iterate is free software; you can redistribute it and/or modify
00009 # it under the terms of the GNU General Public License as published by
00010 # the Free Software Foundation; either version 2 of the License, or
00011 # (at your option) any later version.
00012 #
00013 # iterate is distributed in the hope that it will be useful,
00014 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 # GNU General Public License for more details.
00017 #
00018 # You should have received a copy of the GNU General Public License
00019 # along with CMFDeployment; if not, write to the Free Software
00020 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021 ##################################################################
00022 """
00023 A Default Checkin Checkout Policy For Content
00024 
00025 $Id: policy.py 1824 2007-02-08 17:59:41Z hazmat $
00026 """
00027 
00028 from zope import component
00029 from zope.event import notify
00030 from zope.interface import implements
00031 from zope.annotation.interfaces import IAnnotations
00032 
00033 from Acquisition import Implicit, aq_base, aq_inner, aq_parent
00034 
00035 import interfaces
00036 import event
00037 import lock
00038 
00039 from relation import WorkingCopyRelation
00040 
00041 class CheckinCheckoutPolicyAdapter( object ):
00042     """
00043     Default Checkin Checkout Policy For Content
00044     
00045     on checkout context is the baseline
00046     
00047     on checkin context is the working copy
00048     """
00049     
00050     implements( interfaces.ICheckinCheckoutPolicy )
00051     component.adapts( interfaces.IIterateAware )
00052 
00053     # used when creating baseline version for first time
00054     default_base_message = "Created Baseline"
00055     
00056     def __init__( self, context ):
00057         self.context = context
00058 
00059     def checkout( self, container ):
00060         # see interface
00061         notify( event.BeforeCheckoutEvent( self.context ) )
00062 
00063         # use the object copier to checkout the content to the container
00064         copier = component.queryAdapter( self.context, interfaces.IObjectCopier )
00065         working_copy, relation = copier.copyTo( container )
00066 
00067         # publish the event for any subscribers
00068         notify( event.CheckoutEvent( self.context, working_copy, relation ) )
00069        
00070         # finally return the working copy 
00071         return working_copy
00072 
00073     def checkin( self, checkin_message ):
00074         # see interface
00075 
00076         # get the baseline for this working copy, raise if not found
00077         baseline = self._getBaseline()
00078 
00079         # get a hold of the relation object
00080         wc_ref = self.context.getReferenceImpl( WorkingCopyRelation.relationship )[ 0]
00081         
00082         # publish the event for subscribers, early because contexts are about to be manipulated
00083         notify( event.CheckinEvent( self.context, baseline, wc_ref, checkin_message ) )
00084 
00085         # merge the object back to the baseline with a copier
00086         copier = component.queryAdapter( self.context, interfaces.IObjectCopier )
00087         new_baseline = copier.merge()
00088 
00089         # don't need to unlock the lock disappears with old baseline deletion
00090         notify( event.AfterCheckinEvent( new_baseline, checkin_message ) )
00091         
00092         return new_baseline
00093 
00094     def cancelCheckout( self ):
00095         # see interface
00096         
00097         # get the baseline
00098         baseline = self._getBaseline()
00099 
00100         # publish an event
00101         notify( event.CancelCheckoutEvent( self.context, baseline ) )
00102         
00103         # delete the working copy        
00104         wc_container =  aq_parent( aq_inner( self.context ) )
00105         wc_container.manage_delObjects( [ self.context.getId() ] )
00106 
00107         return baseline
00108 
00109     #################################
00110     ## Checkin Support Methods
00111 
00112     def _getBaseline( self ):
00113         # follow the working copy's reference back to the baseline
00114         refs = self.context.getRefs( WorkingCopyRelation.relationship )
00115 
00116         if not len(refs) == 1:
00117             raise interfaces.CheckinException( "Baseline count mismatch" )
00118 
00119         if not refs or refs[0] is None:
00120             raise interfaces.CheckinException( "Baseline has disappeared" )
00121 
00122         baseline = refs[0]
00123         return baseline