def fix_ar_sample_workflow(brain_or_object):
    """Re-set the state of an AR, Sample and SamplePartition to match the
    least-early state of all contained valid/current analyses. Ignores
    retracted/rejected/cancelled analyses.
    """

    def log_change_state(ar_id, obj_id, src, dst):
        msg = "While fixing {ar_id}: " \
              "state changed for {obj_id}: " \
              "{src} -> {dst}".format(**locals())

    ar = get_object(brain_or_object)
    if not IAnalysisRequest.providedBy(ar):
        return

    wf = api.get_tool('portal_workflow')
    arwf = wf['bika_ar_workflow']
    anwf = wf['bika_analysis_workflow']
    swf = wf['bika_sample_workflow']
    ignored = ['retracted', 'rejected']

    tmp = filter(lambda x: x[0] not in ignored, arwf.states.items())
    arstates = OrderedDict(tmp)
    tmp = filter(lambda x: x[0] not in ignored, swf.states.items())
    samplestates = OrderedDict(tmp)
    tmp = filter(lambda x: x[0] in arstates, anwf.states.items())
    anstates = OrderedDict(tmp)

    # find least-early analysis state
    # !!! Assumes states in definitions are roughly ordered earliest to latest
    ar_dest_state = arstates.items()[0][0]
    for anstate in anstates:
        if ar.getAnalyses(review_state=anstate):
            ar_dest_state = anstate

    # Force state of AR
    ar_state = get_review_status(ar)
    if ar_state != ar_dest_state:
        changeWorkflowState(ar, arwf.id, ar_dest_state)
        log_change_state(ar.id, ar.id, ar_state, ar_dest_state)

    # Force state of Sample
    sample = ar.getSample()
    sample_state = get_review_status(sample)
    if ar_dest_state in samplestates:
        changeWorkflowState(sample, swf.id, ar_dest_state)
        log_change_state(ar.id, sample.id, sample_state, ar_dest_state)

        # Force states of Partitions
        for part in sample.objectValues():
            part_state = get_review_status(part)
            if get_review_status(part) != ar_dest_state:
                changeWorkflowState(sample, swf.id, ar_dest_state)
                log_change_state(ar.id, part.id, part_state, ar_dest_state)
def fix_ar_sample_workflow ( brain_or_object ) : def log_change_state ( ar_id , obj_id , src , dst ) : msg = "While fixing {ar_id}: " "state changed for {obj_id}: " "{src} -> {dst}" . format ( ** locals ( ) ) ar = get_object ( brain_or_object ) if not IAnalysisRequest . providedBy ( ar ) : return wf = api . get_tool ( 'portal_workflow' ) arwf = wf [ 'bika_ar_workflow' ] anwf = wf [ 'bika_analysis_workflow' ] swf = wf [ 'bika_sample_workflow' ] ignored = [ 'retracted' , 'rejected' ] tmp = filter ( lambda x : x [ 0 ] not in ignored , arwf . states . items ( ) ) arstates = OrderedDict ( tmp ) tmp = filter ( lambda x : x [ 0 ] not in ignored , swf . states . items ( ) ) samplestates = OrderedDict ( tmp ) tmp = filter ( lambda x : x [ 0 ] in arstates , anwf . states . items ( ) ) anstates = OrderedDict ( tmp ) ar_dest_state = arstates . items ( ) [ 0 ] [ 0 ] for anstate in anstates : if ar . getAnalyses ( review_state = anstate ) : ar_dest_state = anstate ar_state = get_review_status ( ar ) if ar_state != ar_dest_state : changeWorkflowState ( ar , arwf . id , ar_dest_state ) log_change_state ( ar . id , ar . id , ar_state , ar_dest_state ) sample = ar . getSample ( ) sample_state = get_review_status ( sample ) if ar_dest_state in samplestates : changeWorkflowState ( sample , swf . id , ar_dest_state ) log_change_state ( ar . id , sample . id , sample_state , ar_dest_state ) for part in sample . objectValues ( ) : part_state = get_review_status ( part ) if get_review_status ( part ) != ar_dest_state : changeWorkflowState ( sample , swf . id , ar_dest_state ) log_change_state ( ar . id , part . id , part_state , ar_dest_state )
Re-set the state of an AR, Sample and SamplePartition to match the
    least-early state of all contained valid/current analyses. Ignores
    retracted/rejected/cancelled analyses.