"""StatementPeriodAdjustmentFile Model. It is a READ ONLY model for accessing data. The real business logic lives in the ows-royalties repository. """ from abacus_common_logic.connectors.database import db from sqlalchemy import select, text from sqlalchemy.sql import literal_column, table class StatementPeriodAdjustmentFile: """StatementPeriodAdjustmentFile Model.""" @classmethod def get_statement_period_adjustment_file_by_id( cls, statement_period_adjustment_file_id: int, ): """Get a statement_period_adjustment_file by statement_period_adjustment_file_id. Args: statement_period_adjustment_file_id (int): id of the related statement_period_adjustment_file Returns: a statement_period_adjustment_file record """ statement_period_adjustment_file_query = ( select([literal_column('spaf.*')]) .where( text( 'spaf.statement_period_adjustment_file_id = :statement_period_adjustment_file_id' ) ) .select_from(table('statement_period_adjustment_file').alias('spaf')) ) return db.session.execute( statement_period_adjustment_file_query, { 'statement_period_adjustment_file_id': statement_period_adjustment_file_id }, ).fetchone() @classmethod def is_apply_complete(cls, statement_period_adjustment_file_id): """Return True if the batch's apply_file action is complete (locked).""" row = db.session.execute( text( 'SELECT 1 FROM abacus_state ' "WHERE action_name = 'apply_file' " "AND parent_table_name = 'statement_period_adjustment_file' " 'AND parent_table_id = :file_id ' "AND action_status = 'complete' " 'LIMIT 1' ), {'file_id': statement_period_adjustment_file_id}, ).fetchone() return row is not None