from ppms.migration import PacketCategory, MaxVersionDependency, VersionType, ChangeDTPPacket

OLD_MACRO='''def processInput(di, oldvalue):
    rec = di.get_dtp_record()
    if rec.sql_rule.get_value() != "":
        return di.get_value()
    
    if rec.__getattr__("class").get_value() == "Python":
        if rec.__getattr__("rule_type").get_value() == 250:
            default_value="""def executeProcessAction(mts_rec=None,object_id=None):
        
        return True

ppms.get_session_dict()["planta_functions"]=[executeProcessAction]
    """
        else:
            default_value="""def computeProcessRule(result,di_to_check,object_id):
    
    return result
    
di.computeProcessRule = computeProcessRule
        """
        
        rec.sql_rule.set_value(default_value)
    else:
        rec.sql_rule.set_value("")   
        
    return di.get_value()
    
processInput.deps=("DI041230","DI059084")'''

NEW_MACRO='''def processInput(di, oldvalue):
    rec = di.get_dtp_record()
    if rec.sql_rule.get_value() != "":
        return di.get_value()
    
    if rec.__getattr__("class").get_value() == "Python":
        if rec.__getattr__("rule_type").get_value() == 250:
            default_value="""def executeProcessAction(mts_rec=None,object_id=None):
        
        return True

ppms.get_session_dict()["planta_functions"]=[executeProcessAction]
    """
        else:
            default_value="""def computeProcessRule(result,di_to_check,object_id,dtpRec):
    
    return result
    
di.computeProcessRule = computeProcessRule
        """
        
        rec.sql_rule.set_value(default_value)
    else:
        rec.sql_rule.set_value("")   
        
    return di.get_value()
    
processInput.deps=("DI041230","DI059084")'''

class ChangeMacroOfDI057860(ChangeDTPPacket):
    '''Fixes a wrong process rules macro template'''
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 14161
    
    def get_dependencies(self):
        return (MaxVersionDependency(version='39.4', version_type=VersionType.CUSTOMIZING),)
    
    def fix(self, runner):
        record = self.get_record(412, ['057860'], ['valuerange'])
        if not self.change_value(record, 'valuerange', OLD_MACRO, NEW_MACRO):
            return self.fail()
        return self.success()