+ for step in self.explicit_steps:
+ if step.parent_step_id is not None:
+ try:
+ parent_step = ProcessStep.by_id(db_conn,
+ step.parent_step_id)
+ if parent_step.owner_id != self.id_:
+ step.parent_step_id = None
+ except NotFoundException:
+ step.parent_step_id = None
+ step.save(db_conn)
+
+
+class ProcessStep:
+ """Sub-unit of Processes."""
+
+ def __init__(self, id_: int | None, owner_id: int, step_process_id: int,
+ parent_step_id: int | None) -> None:
+ self.id_ = id_
+ self.owner_id = owner_id
+ self.step_process_id = step_process_id
+ self.parent_step_id = parent_step_id
+
+ @classmethod
+ def from_table_row(cls, db_conn: DatabaseConnection,
+ row: Row) -> ProcessStep:
+ """Make ProcessStep from database row, store in DB cache."""
+ step = cls(row[0], row[1], row[2], row[3])
+ assert step.id_ is not None
+ db_conn.cached_process_steps[step.id_] = step
+ return step
+
+ @classmethod
+ def by_id(cls, db_conn: DatabaseConnection, id_: int) -> ProcessStep:
+ """Retrieve ProcessStep by id_, or throw NotFoundException."""
+ if id_ in db_conn.cached_process_steps.keys():
+ step = db_conn.cached_process_steps[id_]
+ assert isinstance(step, ProcessStep)
+ return step
+ for row in db_conn.exec('SELECT * FROM process_steps '
+ 'WHERE step_id = ?', (id_,)):
+ return cls.from_table_row(db_conn, row)
+ raise NotFoundException(f'found no ProcessStep of ID {id_}')
+
+ def save(self, db_conn: DatabaseConnection) -> None:
+ """Save to database and cache."""
+ cursor = db_conn.exec('REPLACE INTO process_steps VALUES (?, ?, ?, ?)',
+ (self.id_, self.owner_id, self.step_process_id,
+ self.parent_step_id))
+ self.id_ = cursor.lastrowid
+ assert self.id_ is not None
+ db_conn.cached_process_steps[self.id_] = self