from sqlalchemy.orm import Session

from messageprocessor import MessageProcessorCallback


class SessionProcessorCallback(MessageProcessorCallback):
    def __init__(self, session):
        # type: (Session) -> None
        self.session = session

    def process_begun(self, event_name, request_id, data):
        pass

    def input_parsed(self, event_name, request_id, input_obj):
        pass

    def business_called(self, event_name, request_id, input_obj, output_obj):
        self.session.commit()

    def process_finished(self, event_name, request_id, input_obj, output_obj):
        pass

    def parse_exception(self, event_name, request_id, data, exception):
        self.session.rollback()

    def unhandled_exception(self, event_name, request_id, input_obj, exception):
        self.session.rollback()
