From b60fb4f690c96857eeed01fec7a8f29ec792145f Mon Sep 17 00:00:00 2001 From: Keiran Price Date: Tue, 11 Jun 2019 14:11:51 +0100 Subject: [PATCH] Add common package --- common/__init__.py | 0 common/constants.py | 2 + common/database_helpers.py | 176 ++++++++ common/exceptions.py | 10 + common/helpers.py | 86 ++++ common/models/__init__.py | 0 common/models/db_models.py | 826 +++++++++++++++++++++++++++++++++++++ 7 files changed, 1100 insertions(+) create mode 100644 common/__init__.py create mode 100644 common/constants.py create mode 100644 common/database_helpers.py create mode 100644 common/exceptions.py create mode 100644 common/helpers.py create mode 100644 common/models/__init__.py create mode 100644 common/models/db_models.py diff --git a/common/__init__.py b/common/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/common/constants.py b/common/constants.py new file mode 100644 index 00000000..5ac55a36 --- /dev/null +++ b/common/constants.py @@ -0,0 +1,2 @@ +class Constants: + DATABASE_URL = "mysql+pymysql://root:rootpw@localhost:13306/icatdb" diff --git a/common/database_helpers.py b/common/database_helpers.py new file mode 100644 index 00000000..c94f6e90 --- /dev/null +++ b/common/database_helpers.py @@ -0,0 +1,176 @@ +import json +import datetime + +from sqlalchemy import create_engine +from sqlalchemy.orm import sessionmaker + +from common.constants import Constants +from common.exceptions import MissingRecordError, BadFilterError + + +def get_record_by_id(table, id): + """ + Gets a row from the dummy data credential database + :param table: the table class mapping + :param id: the id to find + :return: the row from the table + """ + session = get_db_session() + result = session.query(table).filter(table.ID == id).first() + if result is not None: + session.close() + return result + session.close() + raise MissingRecordError() + + +def get_db_session(): + """ + Gets a session in the dummy data database, currently used for credentials until Authentication is understood + :return: the dummy data DB session + """ + engine = create_engine("mysql+pymysql://root:root@localhost:3306/icatdummy") + Session = sessionmaker(bind=engine) + session = Session() + return session + + +def get_icat_db_session(): + """ + Gets a session and connects with the ICAT database + :return: the session object + """ + engine = create_engine(Constants.DATABASE_URL) + Session = sessionmaker(bind=engine) + session = Session() + return session + + +def insert_row_into_table(row): + """ + Insert the given row into its table + :param row: The row to be inserted + """ + session = get_icat_db_session() + session.add(row) + session.commit() + session.close() + + +def create_row_from_json(table, json): + """ + Given a json dictionary create a row in the table from it + :param table: the table for the row to be inserted into + :param json: the dictionary containing the values + :return: nothing atm + """ + session = get_icat_db_session() + record = table() + record.update_from_dict(json) + record.CREATE_TIME = datetime.datetime.now() # These should probably change + record.CREATE_ID = "user" + record.MOD_TIME = datetime.datetime.now() + record.MOD_ID = "user" + session.add(record) + session.commit() + session.close() + + +def get_row_by_id(table, id): + """ + Gets the row matching the given ID from the given table, raises MissingRecordError if it can not be found + :param table: the table to be searched + :param id: the id of the record to find + :return: the record retrieved + """ + session = get_icat_db_session() + result = session.query(table).filter(table.ID == id).first() + if result is not None: + session.close() + return result + session.close() + raise MissingRecordError() + + +def delete_row_by_id(table, id): + """ + Deletes the row matching the given ID from the given table, raises MissingRecordError if it can not be found + :param table: the table to be searched + :param id: the id of the record to delete + """ + session = get_icat_db_session() + result = get_row_by_id(table, id) + if result is not None: + session.delete(result) + session.commit() + session.close() + return + session.close() + raise MissingRecordError() + + +def update_row_from_id(table, id, new_values): + """ + Updates a record in a table + :param table: The table the record is in + :param id: The id of the record + :param new_values: A JSON string containing what columns are to be updated + """ + session = get_icat_db_session() + record = session.query(table).filter(table.ID == id).first() + if record is not None: + record.update_from_dict(new_values) + session.commit() + session.close() + return + session.close() + raise MissingRecordError() + + +def get_rows_by_filter(table, filters): + session = get_icat_db_session() + base_query = session.query(table) + for filter in filters: + if list(filter)[0].lower() == "where": + for key in filter: + where_part = filter[key] + for k in where_part: + column = getattr(table, k.upper()) + base_query = base_query.filter(column.in_([where_part[k]])) + + elif list(filter)[0].lower() == "order": + base_query.order() # do something probably not .order + elif list(filter)[0].lower() == "skip": + for key in filter: + skip = filter[key] + base_query = base_query.offset(skip) + elif list(filter)[0].lower() == "include": + base_query.include() # do something probably not .include + elif list(filter)[0].lower() == "limit": + for key in filter: + limit = filter[key] + base_query = base_query.limit(limit) + else: + raise BadFilterError() + session.close() + return list(map(lambda x: x.to_dict(), base_query.all())) + + +def get_filtered_row_count(table, filters): + """ + returns the count of the rows that match a given filter in a given table + :param table: the table to be checked + :param filters: the filters to be applied to the query + :return: int: the count of the rows + """ + return len(get_rows_by_filter(table, filters)) + + +def get_first_filtered_row(table, filters): + """ + returns the first row that matches a given filter, in a given table + :param table: the table to be checked + :param filters: the filter to be applied to the query + :return: the first row matching the filter + """ + return get_rows_by_filter(table, filters)[0] diff --git a/common/exceptions.py b/common/exceptions.py new file mode 100644 index 00000000..78cbef68 --- /dev/null +++ b/common/exceptions.py @@ -0,0 +1,10 @@ +class ApiError(Exception): + pass + + +class MissingRecordError(ApiError): + pass + + +class BadFilterError(ApiError): + pass diff --git a/common/helpers.py b/common/helpers.py new file mode 100644 index 00000000..797bb1d1 --- /dev/null +++ b/common/helpers.py @@ -0,0 +1,86 @@ +import json +from functools import wraps + +from flask import request +from flask_restful import reqparse +from sqlalchemy.exc import IntegrityError + +from common.database_helpers import get_icat_db_session +from common.exceptions import MissingRecordError, BadFilterError +from common.models.db_models import SESSION + + +def requires_session_id(method): + """ + Decorator for endpoint resources that makes sure a valid session_id is provided in requests to that endpoint + :param method: The method for the endpoint + :returns a 403, "Forbidden" if a valid session_id is not provided with the request + """ + + @wraps(method) + def wrapper_requires_session(*args, **kwargs): + session = get_icat_db_session() + query = session.query(SESSION).filter( + SESSION.ID == get_session_id_from_auth_header()).first() + if query is not None: + return method(*args, **kwargs) + else: + return "Forbidden", 403 + + return wrapper_requires_session + + +def queries_records(method): + """ + Decorator for endpoint resources that search for a record in a table + :param method: The method for the endpoint + :return: Will return a 404, "No such record" if a MissingRecordError is caught + """ + + @wraps(method) + def wrapper_gets_records(*args, **kwargs): + try: + return method(*args, **kwargs) + except MissingRecordError: + return "No such record in table", 404 + except BadFilterError: + return "Invalid filter requested", 400 + except ValueError: + return "Bad request", 400 + except TypeError: + return "Bad request", 400 + except IntegrityError as e: + return "Bad request", 400 + + return wrapper_gets_records + + +def get_session_id_from_auth_header(): + """ + Gets the sessionID from the Authorization header of a request + :return: String: SessionID + """ + parser = reqparse.RequestParser() + parser.add_argument("Authorization", location="headers") + args = parser.parse_args() + if args["Authorization"] is not None: + return args["Authorization"] + return "" + + +def is_valid_json(string): + """ + Determines if a string is valid JSON + :param string: The string to be tested + :return: boolean representing if the string is valid JSON + """ + try: + json_object = json.loads(string) + except ValueError: + return False + return True + + +def get_filters_from_query_string(): + filters = request.args.getlist("filter") + return list(map(lambda x: json.loads(x), filters)) diff --git a/common/models/__init__.py b/common/models/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/common/models/db_models.py b/common/models/db_models.py new file mode 100644 index 00000000..2093e859 --- /dev/null +++ b/common/models/db_models.py @@ -0,0 +1,826 @@ +from sqlalchemy import Index, Column, BigInteger, String, DateTime, ForeignKey, Integer, Float, FetchedValue +from sqlalchemy.ext.declarative import declarative_base +from sqlalchemy.orm import relationship + +Base = declarative_base() + + +class EntityHelper(object): + """ + EntityHelper class that contains methods to be shared across all entities + """ + + def to_dict(self): + """ + Turns the columns and values of an entity into a dictionary + :return: dict: dictionay containing the fields and values of an entity + """ + dictionary = {} + for column in self.__table__.columns: + dictionary[column.name] = str(getattr(self, column.name)) + return dictionary + + def update_from_dict(self, dictionary): + """ + Given a dictionary containing field names and variables, updates the entity from the given dictionary + :param dictionary: dict: dictionary containing the new values + """ + for key in dictionary: + setattr(self, key.upper(), dictionary[key]) + + +class APPLICATION(Base, EntityHelper): + __tablename__ = 'APPLICATION' + __table_args__ = ( + Index('UNQ_APPLICATION_0', 'FACILITY_ID', 'NAME', 'VERSION'), + ) + + ID = Column(BigInteger, primary_key=True) + CREATE_ID = Column(String(255), nullable=False) + CREATE_TIME = Column(DateTime, nullable=False) + MOD_ID = Column(String(255), nullable=False) + MOD_TIME = Column(DateTime, nullable=False) + NAME = Column(String(255), nullable=False) + VERSION = Column(String(255), nullable=False) + FACILITY_ID = Column(ForeignKey('FACILITY.ID'), nullable=False) + + FACILITY = relationship('FACILITY', primaryjoin='APPLICATION.FACILITY_ID == FACILITY.ID', backref='applications') + + +class FACILITY(Base, EntityHelper): + __tablename__ = 'FACILITY' + + ID = Column(BigInteger, primary_key=True) + CREATE_ID = Column(String(255), nullable=False) + CREATE_TIME = Column(DateTime, nullable=False) + DAYSUNTILRELEASE = Column(Integer) + DESCRIPTION = Column(String(1023)) + FULLNAME = Column(String(255)) + MOD_ID = Column(String(255), nullable=False) + MOD_TIME = Column(DateTime, nullable=False) + NAME = Column(String(255), nullable=False, unique=True) + URL = Column(String(255)) + + +class DATACOLLECTION(Base, EntityHelper): + __tablename__ = 'DATACOLLECTION' + + ID = Column(BigInteger, primary_key=True) + CREATE_ID = Column(String(255), nullable=False) + CREATE_TIME = Column(DateTime, nullable=False) + DOI = Column(String(255)) + MOD_ID = Column(String(255), nullable=False) + MOD_TIME = Column(DateTime, nullable=False) + + +class DATACOLLECTIONDATAFILE(Base, EntityHelper): + __tablename__ = 'DATACOLLECTIONDATAFILE' + __table_args__ = ( + Index('UNQ_DATACOLLECTIONDATAFILE_0', 'DATACOLLECTION_ID', 'DATAFILE_ID'), + ) + + ID = Column(BigInteger, primary_key=True) + CREATE_ID = Column(String(255), nullable=False) + CREATE_TIME = Column(DateTime, nullable=False) + MOD_ID = Column(String(255), nullable=False) + MOD_TIME = Column(DateTime, nullable=False) + DATACOLLECTION_ID = Column(ForeignKey('DATACOLLECTION.ID'), nullable=False) + DATAFILE_ID = Column(ForeignKey('DATAFILE.ID'), nullable=False, index=True) + + DATACOLLECTION = relationship('DATACOLLECTION', + primaryjoin='DATACOLLECTIONDATAFILE.DATACOLLECTION_ID == DATACOLLECTION.ID', + backref='datacollectiondatafiles') + DATAFILE = relationship('DATAFILE', primaryjoin='DATACOLLECTIONDATAFILE.DATAFILE_ID == DATAFILE.ID', + backref='datacollectiondatafiles') + + +class DATACOLLECTIONDATASET(Base, EntityHelper): + __tablename__ = 'DATACOLLECTIONDATASET' + __table_args__ = ( + Index('UNQ_DATACOLLECTIONDATASET_0', 'DATACOLLECTION_ID', 'DATASET_ID'), + ) + + ID = Column(BigInteger, primary_key=True) + CREATE_ID = Column(String(255), nullable=False) + CREATE_TIME = Column(DateTime, nullable=False) + MOD_ID = Column(String(255), nullable=False) + MOD_TIME = Column(DateTime, nullable=False) + DATACOLLECTION_ID = Column(ForeignKey('DATACOLLECTION.ID'), nullable=False) + DATASET_ID = Column(ForeignKey('DATASET.ID'), nullable=False, index=True) + + DATACOLLECTION = relationship('DATACOLLECTION', + primaryjoin='DATACOLLECTIONDATASET.DATACOLLECTION_ID == DATACOLLECTION.ID', + backref='datacollectiondatasets') + DATASET = relationship('DATASET', primaryjoin='DATACOLLECTIONDATASET.DATASET_ID == DATASET.ID', + backref='datacollectiondatasets') + + +class DATACOLLECTIONPARAMETER(Base, EntityHelper): + __tablename__ = 'DATACOLLECTIONPARAMETER' + __table_args__ = ( + Index('UNQ_DATACOLLECTIONPARAMETER_0', 'DATACOLLECTION_ID', 'PARAMETER_TYPE_ID'), + ) + + ID = Column(BigInteger, primary_key=True) + CREATE_ID = Column(String(255), nullable=False) + CREATE_TIME = Column(DateTime, nullable=False) + DATETIME_VALUE = Column(DateTime) + ERROR = Column(Float(asdecimal=True)) + MOD_ID = Column(String(255), nullable=False) + MOD_TIME = Column(DateTime, nullable=False) + NUMERIC_VALUE = Column(Float(asdecimal=True)) + RANGEBOTTOM = Column(Float(asdecimal=True)) + RANGETOP = Column(Float(asdecimal=True)) + STRING_VALUE = Column(String(4000)) + DATACOLLECTION_ID = Column(ForeignKey('DATACOLLECTION.ID'), nullable=False) + PARAMETER_TYPE_ID = Column(ForeignKey('PARAMETERTYPE.ID'), nullable=False, index=True) + + DATACOLLECTION = relationship('DATACOLLECTION', + primaryjoin='DATACOLLECTIONPARAMETER.DATACOLLECTION_ID == DATACOLLECTION.ID', + backref='datacollectionparameters') + PARAMETERTYPE = relationship('PARAMETERTYPE', + primaryjoin='DATACOLLECTIONPARAMETER.PARAMETER_TYPE_ID == PARAMETERTYPE.ID', + backref='datacollectionparameters') + + +class DATAFILE(Base, EntityHelper): + __tablename__ = 'DATAFILE' + __table_args__ = ( + Index('UNQ_DATAFILE_0', 'DATASET_ID', 'NAME'), + ) + + ID = Column(BigInteger, primary_key=True) + CHECKSUM = Column(String(255)) + CREATE_ID = Column(String(255), nullable=False) + CREATE_TIME = Column(DateTime, nullable=False) + DATAFILECREATETIME = Column(DateTime) + DATAFILEMODTIME = Column(DateTime) + DESCRIPTION = Column(String(255)) + DOI = Column(String(255)) + FILESIZE = Column(BigInteger) + LOCATION = Column(String(255), index=True) + MOD_ID = Column(String(255), nullable=False) + MOD_TIME = Column(DateTime, nullable=False) + NAME = Column(String(255), nullable=False) + DATAFILEFORMAT_ID = Column(ForeignKey('DATAFILEFORMAT.ID'), index=True) + DATASET_ID = Column(ForeignKey('DATASET.ID'), nullable=False) + + DATAFILEFORMAT = relationship('DATAFILEFORMAT', primaryjoin='DATAFILE.DATAFILEFORMAT_ID == DATAFILEFORMAT.ID', + backref='datafiles') + DATASET = relationship('DATASET', primaryjoin='DATAFILE.DATASET_ID == DATASET.ID', backref='datafiles') + + +class DATAFILEFORMAT(Base, EntityHelper): + __tablename__ = 'DATAFILEFORMAT' + __table_args__ = ( + Index('UNQ_DATAFILEFORMAT_0', 'FACILITY_ID', 'NAME', 'VERSION'), + ) + + ID = Column(BigInteger, primary_key=True) + CREATE_ID = Column(String(255), nullable=False) + CREATE_TIME = Column(DateTime, nullable=False) + DESCRIPTION = Column(String(255)) + MOD_ID = Column(String(255), nullable=False) + MOD_TIME = Column(DateTime, nullable=False) + NAME = Column(String(255), nullable=False) + TYPE = Column(String(255)) + VERSION = Column(String(255), nullable=False) + FACILITY_ID = Column(ForeignKey('FACILITY.ID'), nullable=False) + + FACILITY = relationship('FACILITY', primaryjoin='DATAFILEFORMAT.FACILITY_ID == FACILITY.ID', + backref='datafileformats') + + +class DATAFILEPARAMETER(Base, EntityHelper): + __tablename__ = 'DATAFILEPARAMETER' + __table_args__ = ( + Index('UNQ_DATAFILEPARAMETER_0', 'DATAFILE_ID', 'PARAMETER_TYPE_ID'), + ) + + ID = Column(BigInteger, primary_key=True) + CREATE_ID = Column(String(255), nullable=False) + CREATE_TIME = Column(DateTime, nullable=False) + DATETIME_VALUE = Column(DateTime) + ERROR = Column(Float(asdecimal=True)) + MOD_ID = Column(String(255), nullable=False) + MOD_TIME = Column(DateTime, nullable=False) + NUMERIC_VALUE = Column(Float(asdecimal=True)) + RANGEBOTTOM = Column(Float(asdecimal=True)) + RANGETOP = Column(Float(asdecimal=True)) + STRING_VALUE = Column(String(4000)) + DATAFILE_ID = Column(ForeignKey('DATAFILE.ID'), nullable=False) + PARAMETER_TYPE_ID = Column(ForeignKey('PARAMETERTYPE.ID'), nullable=False, index=True) + + DATAFILE = relationship('DATAFILE', primaryjoin='DATAFILEPARAMETER.DATAFILE_ID == DATAFILE.ID', + backref='datafileparameters') + PARAMETERTYPE = relationship('PARAMETERTYPE', primaryjoin='DATAFILEPARAMETER.PARAMETER_TYPE_ID == PARAMETERTYPE.ID', + backref='datafileparameters') + + +class DATASET(Base, EntityHelper): + __tablename__ = 'DATASET' + __table_args__ = ( + Index('UNQ_DATASET_0', 'INVESTIGATION_ID', 'NAME'), + ) + + ID = Column(BigInteger, primary_key=True) + COMPLETE = Column(Integer, nullable=False, server_default=FetchedValue()) + CREATE_ID = Column(String(255), nullable=False) + CREATE_TIME = Column(DateTime, nullable=False) + DESCRIPTION = Column(String(255)) + DOI = Column(String(255)) + END_DATE = Column(DateTime) + LOCATION = Column(String(255)) + MOD_ID = Column(String(255), nullable=False) + MOD_TIME = Column(DateTime, nullable=False) + NAME = Column(String(255), nullable=False) + STARTDATE = Column(DateTime) + INVESTIGATION_ID = Column(ForeignKey('INVESTIGATION.ID'), nullable=False) + SAMPLE_ID = Column(ForeignKey('SAMPLE.ID'), index=True) + TYPE_ID = Column(ForeignKey('DATASETTYPE.ID'), nullable=False, index=True) + + INVESTIGATION = relationship('INVESTIGATION', primaryjoin='DATASET.INVESTIGATION_ID == INVESTIGATION.ID', + backref='datasets') + SAMPLE = relationship('SAMPLE', primaryjoin='DATASET.SAMPLE_ID == SAMPLE.ID', backref='datasets') + DATASETTYPE = relationship('DATASETTYPE', primaryjoin='DATASET.TYPE_ID == DATASETTYPE.ID', backref='datasets') + + +class DATASETPARAMETER(Base, EntityHelper): + __tablename__ = 'DATASETPARAMETER' + __table_args__ = ( + Index('UNQ_DATASETPARAMETER_0', 'DATASET_ID', 'PARAMETER_TYPE_ID'), + ) + + ID = Column(BigInteger, primary_key=True) + CREATE_ID = Column(String(255), nullable=False) + CREATE_TIME = Column(DateTime, nullable=False) + DATETIME_VALUE = Column(DateTime) + ERROR = Column(Float(asdecimal=True)) + MOD_ID = Column(String(255), nullable=False) + MOD_TIME = Column(DateTime, nullable=False) + NUMERIC_VALUE = Column(Float(asdecimal=True)) + RANGEBOTTOM = Column(Float(asdecimal=True)) + RANGETOP = Column(Float(asdecimal=True)) + STRING_VALUE = Column(String(4000)) + DATASET_ID = Column(ForeignKey('DATASET.ID'), nullable=False) + PARAMETER_TYPE_ID = Column(ForeignKey('PARAMETERTYPE.ID'), nullable=False, index=True) + + DATASET = relationship('DATASET', primaryjoin='DATASETPARAMETER.DATASET_ID == DATASET.ID', + backref='datasetparameters') + PARAMETERTYPE = relationship('PARAMETERTYPE', primaryjoin='DATASETPARAMETER.PARAMETER_TYPE_ID == PARAMETERTYPE.ID', + backref='datasetparameters') + + +class DATASETTYPE(Base, EntityHelper): + __tablename__ = 'DATASETTYPE' + __table_args__ = ( + Index('UNQ_DATASETTYPE_0', 'FACILITY_ID', 'NAME'), + ) + + ID = Column(BigInteger, primary_key=True) + CREATE_ID = Column(String(255), nullable=False) + CREATE_TIME = Column(DateTime, nullable=False) + DESCRIPTION = Column(String(255)) + MOD_ID = Column(String(255), nullable=False) + MOD_TIME = Column(DateTime, nullable=False) + NAME = Column(String(255), nullable=False) + FACILITY_ID = Column(ForeignKey('FACILITY.ID'), nullable=False) + + FACILITY = relationship('FACILITY', primaryjoin='DATASETTYPE.FACILITY_ID == FACILITY.ID', backref='datasettypes') + + +class FACILITYCYCLE(Base, EntityHelper): + __tablename__ = 'FACILITYCYCLE' + __table_args__ = ( + Index('UNQ_FACILITYCYCLE_0', 'FACILITY_ID', 'NAME'), + ) + + ID = Column(BigInteger, primary_key=True) + CREATE_ID = Column(String(255), nullable=False) + CREATE_TIME = Column(DateTime, nullable=False) + DESCRIPTION = Column(String(255)) + ENDDATE = Column(DateTime) + MOD_ID = Column(String(255), nullable=False) + MOD_TIME = Column(DateTime, nullable=False) + NAME = Column(String(255), nullable=False) + STARTDATE = Column(DateTime) + FACILITY_ID = Column(ForeignKey('FACILITY.ID'), nullable=False) + + FACILITY = relationship('FACILITY', primaryjoin='FACILITYCYCLE.FACILITY_ID == FACILITY.ID', + backref='facilitycycles') + + +class GROUPING(Base, EntityHelper): + __tablename__ = 'GROUPING' + + ID = Column(BigInteger, primary_key=True) + CREATE_ID = Column(String(255), nullable=False) + CREATE_TIME = Column(DateTime, nullable=False) + MOD_ID = Column(String(255), nullable=False) + MOD_TIME = Column(DateTime, nullable=False) + NAME = Column(String(255), nullable=False, unique=True) + + +class INSTRUMENT(Base, EntityHelper): + __tablename__ = 'INSTRUMENT' + __table_args__ = ( + Index('UNQ_INSTRUMENT_0', 'FACILITY_ID', 'NAME'), + ) + + ID = Column(BigInteger, primary_key=True) + CREATE_ID = Column(String(255), nullable=False) + CREATE_TIME = Column(DateTime, nullable=False) + DESCRIPTION = Column(String(4000)) + FULLNAME = Column(String(255)) + MOD_ID = Column(String(255), nullable=False) + MOD_TIME = Column(DateTime, nullable=False) + NAME = Column(String(255), nullable=False) + TYPE = Column(String(255)) + URL = Column(String(255)) + FACILITY_ID = Column(ForeignKey('FACILITY.ID'), nullable=False) + + FACILITY = relationship('FACILITY', primaryjoin='INSTRUMENT.FACILITY_ID == FACILITY.ID', backref='instruments') + + +class INSTRUMENTSCIENTIST(Base, EntityHelper): + __tablename__ = 'INSTRUMENTSCIENTIST' + __table_args__ = ( + Index('UNQ_INSTRUMENTSCIENTIST_0', 'USER_ID', 'INSTRUMENT_ID'), + ) + + ID = Column(BigInteger, primary_key=True) + CREATE_ID = Column(String(255), nullable=False) + CREATE_TIME = Column(DateTime, nullable=False) + MOD_ID = Column(String(255), nullable=False) + MOD_TIME = Column(DateTime, nullable=False) + INSTRUMENT_ID = Column(ForeignKey('INSTRUMENT.ID'), nullable=False, index=True) + USER_ID = Column(ForeignKey('USER_.ID'), nullable=False) + + INSTRUMENT = relationship('INSTRUMENT', primaryjoin='INSTRUMENTSCIENTIST.INSTRUMENT_ID == INSTRUMENT.ID', + backref='instrumentscientists') + USER_ = relationship('USER', primaryjoin='INSTRUMENTSCIENTIST.USER_ID == USER.ID', backref='instrumentscientists') + + +class INVESTIGATION(Base, EntityHelper): + __tablename__ = 'INVESTIGATION' + __table_args__ = ( + Index('UNQ_INVESTIGATION_0', 'FACILITY_ID', 'NAME', 'VISIT_ID'), + ) + + ID = Column(BigInteger, primary_key=True) + CREATE_ID = Column(String(255), nullable=False) + CREATE_TIME = Column(DateTime, nullable=False) + DOI = Column(String(255)) + ENDDATE = Column(DateTime) + MOD_ID = Column(String(255), nullable=False) + MOD_TIME = Column(DateTime, nullable=False) + NAME = Column(String(255), nullable=False) + RELEASEDATE = Column(DateTime) + STARTDATE = Column(DateTime) + SUMMARY = Column(String(4000)) + TITLE = Column(String(255), nullable=False) + VISIT_ID = Column(String(255), nullable=False) + FACILITY_ID = Column(ForeignKey('FACILITY.ID'), nullable=False) + TYPE_ID = Column(ForeignKey('INVESTIGATIONTYPE.ID'), nullable=False, index=True) + + FACILITY = relationship('FACILITY', primaryjoin='INVESTIGATION.FACILITY_ID == FACILITY.ID', + backref='investigations') + INVESTIGATIONTYPE = relationship('INVESTIGATIONTYPE', primaryjoin='INVESTIGATION.TYPE_ID == INVESTIGATIONTYPE.ID', + backref='investigations') + + +class INVESTIGATIONGROUP(Base, EntityHelper): + __tablename__ = 'INVESTIGATIONGROUP' + __table_args__ = ( + Index('UNQ_INVESTIGATIONGROUP_0', 'GROUP_ID', 'INVESTIGATION_ID', 'ROLE'), + ) + + ID = Column(BigInteger, primary_key=True) + CREATE_ID = Column(String(255), nullable=False) + CREATE_TIME = Column(DateTime, nullable=False) + MOD_ID = Column(String(255), nullable=False) + MOD_TIME = Column(DateTime, nullable=False) + ROLE = Column(String(255), nullable=False) + GROUP_ID = Column(ForeignKey('GROUPING.ID'), nullable=False) + INVESTIGATION_ID = Column(ForeignKey('INVESTIGATION.ID'), nullable=False, index=True) + + GROUPING = relationship('GROUPING', primaryjoin='INVESTIGATIONGROUP.GROUP_ID == GROUPING.ID', + backref='investigationgroups') + INVESTIGATION = relationship('INVESTIGATION', primaryjoin='INVESTIGATIONGROUP.INVESTIGATION_ID == INVESTIGATION.ID', + backref='investigationgroups') + + +class INVESTIGATIONINSTRUMENT(Base, EntityHelper): + __tablename__ = 'INVESTIGATIONINSTRUMENT' + __table_args__ = ( + Index('UNQ_INVESTIGATIONINSTRUMENT_0', 'INVESTIGATION_ID', 'INSTRUMENT_ID'), + ) + + ID = Column(BigInteger, primary_key=True) + CREATE_ID = Column(String(255), nullable=False) + CREATE_TIME = Column(DateTime, nullable=False) + MOD_ID = Column(String(255), nullable=False) + MOD_TIME = Column(DateTime, nullable=False) + INSTRUMENT_ID = Column(ForeignKey('INSTRUMENT.ID'), nullable=False, index=True) + INVESTIGATION_ID = Column(ForeignKey('INVESTIGATION.ID'), nullable=False) + + INSTRUMENT = relationship('INSTRUMENT', primaryjoin='INVESTIGATIONINSTRUMENT.INSTRUMENT_ID == INSTRUMENT.ID', + backref='investigationinstruments') + INVESTIGATION = relationship('INVESTIGATION', + primaryjoin='INVESTIGATIONINSTRUMENT.INVESTIGATION_ID == INVESTIGATION.ID', + backref='investigationinstruments') + + +class INVESTIGATIONPARAMETER(Base, EntityHelper): + __tablename__ = 'INVESTIGATIONPARAMETER' + __table_args__ = ( + Index('UNQ_INVESTIGATIONPARAMETER_0', 'INVESTIGATION_ID', 'PARAMETER_TYPE_ID'), + ) + + ID = Column(BigInteger, primary_key=True) + CREATE_ID = Column(String(255), nullable=False) + CREATE_TIME = Column(DateTime, nullable=False) + DATETIME_VALUE = Column(DateTime) + ERROR = Column(Float(asdecimal=True)) + MOD_ID = Column(String(255), nullable=False) + MOD_TIME = Column(DateTime, nullable=False) + NUMERIC_VALUE = Column(Float(asdecimal=True)) + RANGEBOTTOM = Column(Float(asdecimal=True)) + RANGETOP = Column(Float(asdecimal=True)) + STRING_VALUE = Column(String(4000)) + INVESTIGATION_ID = Column(ForeignKey('INVESTIGATION.ID'), nullable=False) + PARAMETER_TYPE_ID = Column(ForeignKey('PARAMETERTYPE.ID'), nullable=False, index=True) + + INVESTIGATION = relationship('INVESTIGATION', + primaryjoin='INVESTIGATIONPARAMETER.INVESTIGATION_ID == INVESTIGATION.ID', + backref='investigationparameters') + PARAMETERTYPE = relationship('PARAMETERTYPE', + primaryjoin='INVESTIGATIONPARAMETER.PARAMETER_TYPE_ID == PARAMETERTYPE.ID', + backref='investigationparameters') + + +class INVESTIGATIONTYPE(Base, EntityHelper): + __tablename__ = 'INVESTIGATIONTYPE' + __table_args__ = ( + Index('UNQ_INVESTIGATIONTYPE_0', 'NAME', 'FACILITY_ID'), + ) + + ID = Column(BigInteger, primary_key=True) + CREATE_ID = Column(String(255), nullable=False) + CREATE_TIME = Column(DateTime, nullable=False) + DESCRIPTION = Column(String(255)) + MOD_ID = Column(String(255), nullable=False) + MOD_TIME = Column(DateTime, nullable=False) + NAME = Column(String(255), nullable=False) + FACILITY_ID = Column(ForeignKey('FACILITY.ID'), nullable=False, index=True) + + FACILITY = relationship('FACILITY', primaryjoin='INVESTIGATIONTYPE.FACILITY_ID == FACILITY.ID', + backref='investigationtypes') + + +class INVESTIGATIONUSER(Base, EntityHelper): + __tablename__ = 'INVESTIGATIONUSER' + __table_args__ = ( + Index('UNQ_INVESTIGATIONUSER_0', 'USER_ID', 'INVESTIGATION_ID', 'ROLE'), + ) + + ID = Column(BigInteger, primary_key=True) + CREATE_ID = Column(String(255), nullable=False) + CREATE_TIME = Column(DateTime, nullable=False) + MOD_ID = Column(String(255), nullable=False) + MOD_TIME = Column(DateTime, nullable=False) + ROLE = Column(String(255), nullable=False) + INVESTIGATION_ID = Column(ForeignKey('INVESTIGATION.ID'), nullable=False, index=True) + USER_ID = Column(ForeignKey('USER_.ID'), nullable=False) + + INVESTIGATION = relationship('INVESTIGATION', primaryjoin='INVESTIGATIONUSER.INVESTIGATION_ID == INVESTIGATION.ID', + backref='investigationusers') + USER_ = relationship('USER', primaryjoin='INVESTIGATIONUSER.USER_ID == USER.ID', backref='investigationusers') + + +class JOB(Base, EntityHelper): + __tablename__ = 'JOB' + + ID = Column(BigInteger, primary_key=True) + ARGUMENTS = Column(String(255)) + CREATE_ID = Column(String(255), nullable=False) + CREATE_TIME = Column(DateTime, nullable=False) + MOD_ID = Column(String(255), nullable=False) + MOD_TIME = Column(DateTime, nullable=False) + APPLICATION_ID = Column(ForeignKey('APPLICATION.ID'), nullable=False, index=True) + INPUTDATACOLLECTION_ID = Column(ForeignKey('DATACOLLECTION.ID'), index=True) + OUTPUTDATACOLLECTION_ID = Column(ForeignKey('DATACOLLECTION.ID'), index=True) + + APPLICATION = relationship('APPLICATION', primaryjoin='JOB.APPLICATION_ID == APPLICATION.ID', backref='jobs') + DATACOLLECTION = relationship('DATACOLLECTION', primaryjoin='JOB.INPUTDATACOLLECTION_ID == DATACOLLECTION.ID', + backref='datacollection_jobs') + DATACOLLECTION1 = relationship('DATACOLLECTION', primaryjoin='JOB.OUTPUTDATACOLLECTION_ID == DATACOLLECTION.ID', + backref='datacollection_jobs_0') + + +class KEYWORD(Base, EntityHelper): + __tablename__ = 'KEYWORD' + __table_args__ = ( + Index('UNQ_KEYWORD_0', 'NAME', 'INVESTIGATION_ID'), + ) + + ID = Column(BigInteger, primary_key=True) + CREATE_ID = Column(String(255), nullable=False) + CREATE_TIME = Column(DateTime, nullable=False) + MOD_ID = Column(String(255), nullable=False) + MOD_TIME = Column(DateTime, nullable=False) + NAME = Column(String(255), nullable=False) + INVESTIGATION_ID = Column(ForeignKey('INVESTIGATION.ID'), nullable=False, index=True) + + INVESTIGATION = relationship('INVESTIGATION', primaryjoin='KEYWORD.INVESTIGATION_ID == INVESTIGATION.ID', + backref='keywords') + + +class PARAMETERTYPE(Base, EntityHelper): + __tablename__ = 'PARAMETERTYPE' + __table_args__ = ( + Index('UNQ_PARAMETERTYPE_0', 'FACILITY_ID', 'NAME', 'UNITS'), + ) + + ID = Column(BigInteger, primary_key=True) + APPLICABLETODATACOLLECTION = Column(Integer, server_default=FetchedValue()) + APPLICABLETODATAFILE = Column(Integer, server_default=FetchedValue()) + APPLICABLETODATASET = Column(Integer, server_default=FetchedValue()) + APPLICABLETOINVESTIGATION = Column(Integer, server_default=FetchedValue()) + APPLICABLETOSAMPLE = Column(Integer, server_default=FetchedValue()) + CREATE_ID = Column(String(255), nullable=False) + CREATE_TIME = Column(DateTime, nullable=False) + DESCRIPTION = Column(String(255)) + ENFORCED = Column(Integer, server_default=FetchedValue()) + MAXIMUMNUMERICVALUE = Column(Float(asdecimal=True)) + MINIMUMNUMERICVALUE = Column(Float(asdecimal=True)) + MOD_ID = Column(String(255), nullable=False) + MOD_TIME = Column(DateTime, nullable=False) + NAME = Column(String(255), nullable=False) + UNITS = Column(String(255), nullable=False) + UNITSFULLNAME = Column(String(255)) + VALUETYPE = Column(Integer, nullable=False) + VERIFIED = Column(Integer, server_default=FetchedValue()) + FACILITY_ID = Column(ForeignKey('FACILITY.ID'), nullable=False) + + FACILITY = relationship('FACILITY', primaryjoin='PARAMETERTYPE.FACILITY_ID == FACILITY.ID', + backref='parametertypes') + + +class PERMISSIBLESTRINGVALUE(Base, EntityHelper): + __tablename__ = 'PERMISSIBLESTRINGVALUE' + __table_args__ = ( + Index('UNQ_PERMISSIBLESTRINGVALUE_0', 'VALUE', 'PARAMETERTYPE_ID'), + ) + + ID = Column(BigInteger, primary_key=True) + CREATE_ID = Column(String(255), nullable=False) + CREATE_TIME = Column(DateTime, nullable=False) + MOD_ID = Column(String(255), nullable=False) + MOD_TIME = Column(DateTime, nullable=False) + VALUE = Column(String(255), nullable=False) + PARAMETERTYPE_ID = Column(ForeignKey('PARAMETERTYPE.ID'), nullable=False, index=True) + + PARAMETERTYPE = relationship('PARAMETERTYPE', + primaryjoin='PERMISSIBLESTRINGVALUE.PARAMETERTYPE_ID == PARAMETERTYPE.ID', + backref='permissiblestringvalues') + + +class PUBLICATION(Base, EntityHelper): + __tablename__ = 'PUBLICATION' + + ID = Column(BigInteger, primary_key=True) + CREATE_ID = Column(String(255), nullable=False) + CREATE_TIME = Column(DateTime, nullable=False) + DOI = Column(String(255)) + FULLREFERENCE = Column(String(511), nullable=False) + MOD_ID = Column(String(255), nullable=False) + MOD_TIME = Column(DateTime, nullable=False) + REPOSITORY = Column(String(255)) + REPOSITORYID = Column(String(255)) + URL = Column(String(255)) + INVESTIGATION_ID = Column(ForeignKey('INVESTIGATION.ID'), nullable=False, index=True) + + INVESTIGATION = relationship('INVESTIGATION', primaryjoin='PUBLICATION.INVESTIGATION_ID == INVESTIGATION.ID', + backref='publications') + + +class PUBLICSTEP(Base, EntityHelper): + __tablename__ = 'PUBLICSTEP' + __table_args__ = ( + Index('UNQ_PUBLICSTEP_0', 'ORIGIN', 'FIELD'), + ) + + ID = Column(BigInteger, primary_key=True) + CREATE_ID = Column(String(255), nullable=False) + CREATE_TIME = Column(DateTime, nullable=False) + FIELD = Column(String(32), nullable=False) + MOD_ID = Column(String(255), nullable=False) + MOD_TIME = Column(DateTime, nullable=False) + ORIGIN = Column(String(32), nullable=False) + + +class RELATEDDATAFILE(Base, EntityHelper): + __tablename__ = 'RELATEDDATAFILE' + __table_args__ = ( + Index('UNQ_RELATEDDATAFILE_0', 'SOURCE_DATAFILE_ID', 'DEST_DATAFILE_ID'), + ) + + ID = Column(BigInteger, primary_key=True) + CREATE_ID = Column(String(255), nullable=False) + CREATE_TIME = Column(DateTime, nullable=False) + MOD_ID = Column(String(255), nullable=False) + MOD_TIME = Column(DateTime, nullable=False) + RELATION = Column(String(255), nullable=False) + DEST_DATAFILE_ID = Column(ForeignKey('DATAFILE.ID'), nullable=False, index=True) + SOURCE_DATAFILE_ID = Column(ForeignKey('DATAFILE.ID'), nullable=False) + + DATAFILE = relationship('DATAFILE', primaryjoin='RELATEDDATAFILE.DEST_DATAFILE_ID == DATAFILE.ID', + backref='datafile_relateddatafiles') + DATAFILE1 = relationship('DATAFILE', primaryjoin='RELATEDDATAFILE.SOURCE_DATAFILE_ID == DATAFILE.ID', + backref='datafile_relateddatafiles_0') + + +class RULE(Base, EntityHelper): + __tablename__ = 'RULE_' + + ID = Column(BigInteger, primary_key=True) + ATTRIBUTE = Column(String(255)) + BEAN = Column(String(255)) + C = Column(Integer, server_default=FetchedValue()) + CREATE_ID = Column(String(255), nullable=False) + CREATE_TIME = Column(DateTime, nullable=False) + CRUDFLAGS = Column(String(4), nullable=False) + CRUDJPQL = Column(String(1024)) + D = Column(Integer, server_default=FetchedValue()) + INCLUDEJPQL = Column(String(1024)) + MOD_ID = Column(String(255), nullable=False) + MOD_TIME = Column(DateTime, nullable=False) + R = Column(Integer, server_default=FetchedValue()) + RESTRICTED = Column(Integer, server_default=FetchedValue()) + SEARCHJPQL = Column(String(1024)) + U = Column(Integer, server_default=FetchedValue()) + WHAT = Column(String(1024), nullable=False) + GROUPING_ID = Column(ForeignKey('GROUPING.ID'), index=True) + + GROUPING = relationship('GROUPING', primaryjoin='RULE.GROUPING_ID == GROUPING.ID', backref='rules') + + +class SAMPLE(Base, EntityHelper): + __tablename__ = 'SAMPLE' + __table_args__ = ( + Index('UNQ_SAMPLE_0', 'INVESTIGATION_ID', 'NAME'), + ) + + ID = Column(BigInteger, primary_key=True) + CREATE_ID = Column(String(255), nullable=False) + CREATE_TIME = Column(DateTime, nullable=False) + MOD_ID = Column(String(255), nullable=False) + MOD_TIME = Column(DateTime, nullable=False) + NAME = Column(String(255), nullable=False) + INVESTIGATION_ID = Column(ForeignKey('INVESTIGATION.ID'), nullable=False) + SAMPLETYPE_ID = Column(ForeignKey('SAMPLETYPE.ID'), index=True) + + INVESTIGATION = relationship('INVESTIGATION', primaryjoin='SAMPLE.INVESTIGATION_ID == INVESTIGATION.ID', + backref='samples') + SAMPLETYPE = relationship('SAMPLETYPE', primaryjoin='SAMPLE.SAMPLETYPE_ID == SAMPLETYPE.ID', backref='samples') + + +class SAMPLEPARAMETER(Base, EntityHelper): + __tablename__ = 'SAMPLEPARAMETER' + __table_args__ = ( + Index('UNQ_SAMPLEPARAMETER_0', 'SAMPLE_ID', 'PARAMETER_TYPE_ID'), + ) + + ID = Column(BigInteger, primary_key=True) + CREATE_ID = Column(String(255), nullable=False) + CREATE_TIME = Column(DateTime, nullable=False) + DATETIME_VALUE = Column(DateTime) + ERROR = Column(Float(asdecimal=True)) + MOD_ID = Column(String(255), nullable=False) + MOD_TIME = Column(DateTime, nullable=False) + NUMERIC_VALUE = Column(Float(asdecimal=True)) + RANGEBOTTOM = Column(Float(asdecimal=True)) + RANGETOP = Column(Float(asdecimal=True)) + STRING_VALUE = Column(String(4000)) + SAMPLE_ID = Column(ForeignKey('SAMPLE.ID'), nullable=False) + PARAMETER_TYPE_ID = Column(ForeignKey('PARAMETERTYPE.ID'), nullable=False, index=True) + + PARAMETERTYPE = relationship('PARAMETERTYPE', primaryjoin='SAMPLEPARAMETER.PARAMETER_TYPE_ID == PARAMETERTYPE.ID', + backref='sampleparameters') + SAMPLE = relationship('SAMPLE', primaryjoin='SAMPLEPARAMETER.SAMPLE_ID == SAMPLE.ID', backref='sampleparameters') + + +class SESSION(Base, EntityHelper): + __tablename__ = 'SESSION_' + + ID = Column(String(255), primary_key=True) + EXPIREDATETIME = Column(DateTime) + USERNAME = Column(String(255)) + + +class SHIFT(Base, EntityHelper): + __tablename__ = 'SHIFT' + __table_args__ = ( + Index('UNQ_SHIFT_0', 'INVESTIGATION_ID', 'STARTDATE', 'ENDDATE'), + ) + + ID = Column(BigInteger, primary_key=True) + COMMENT = Column(String(255)) + CREATE_ID = Column(String(255), nullable=False) + CREATE_TIME = Column(DateTime, nullable=False) + ENDDATE = Column(DateTime, nullable=False) + MOD_ID = Column(String(255), nullable=False) + MOD_TIME = Column(DateTime, nullable=False) + STARTDATE = Column(DateTime, nullable=False) + INVESTIGATION_ID = Column(ForeignKey('INVESTIGATION.ID'), nullable=False) + + INVESTIGATION = relationship('INVESTIGATION', primaryjoin='SHIFT.INVESTIGATION_ID == INVESTIGATION.ID', + backref='shifts') + + +class USER(Base, EntityHelper): + __tablename__ = 'USER_' + + ID = Column(BigInteger, primary_key=True) + CREATE_ID = Column(String(255), nullable=False) + CREATE_TIME = Column(DateTime, nullable=False) + EMAIL = Column(String(255)) + FULLNAME = Column(String(255)) + MOD_ID = Column(String(255), nullable=False) + MOD_TIME = Column(DateTime, nullable=False) + NAME = Column(String(255), nullable=False, unique=True) + ORCIDID = Column(String(255)) + + +class USERGROUP(Base, EntityHelper): + __tablename__ = 'USERGROUP' + __table_args__ = ( + Index('UNQ_USERGROUP_0', 'USER_ID', 'GROUP_ID'), + ) + + ID = Column(BigInteger, primary_key=True) + CREATE_ID = Column(String(255), nullable=False) + CREATE_TIME = Column(DateTime, nullable=False) + MOD_ID = Column(String(255), nullable=False) + MOD_TIME = Column(DateTime, nullable=False) + GROUP_ID = Column(ForeignKey('GROUPING.ID'), nullable=False, index=True) + USER_ID = Column(ForeignKey('USER_.ID'), nullable=False) + + GROUPING = relationship('GROUPING', primaryjoin='USERGROUP.GROUP_ID == GROUPING.ID', backref='usergroups') + USER_ = relationship('USER', primaryjoin='USERGROUP.USER_ID == USER.ID', backref='usergroups') + + +class STUDYINVESTIGATION(Base, EntityHelper): + __tablename__ = 'STUDYINVESTIGATION' + __table_args__ = ( + Index('UNQ_STUDYINVESTIGATION_0', 'STUDY_ID', 'INVESTIGATION_ID'), + ) + + ID = Column(BigInteger, primary_key=True) + CREATE_ID = Column(String(255), nullable=False) + CREATE_TIME = Column(DateTime, nullable=False) + MOD_ID = Column(String(255), nullable=False) + MOD_TIME = Column(DateTime, nullable=False) + INVESTIGATION_ID = Column(ForeignKey('INVESTIGATION.ID'), nullable=False, index=True) + STUDY_ID = Column(ForeignKey('STUDY.ID'), nullable=False) + + INVESTIGATION = relationship('INVESTIGATION', primaryjoin='STUDYINVESTIGATION.INVESTIGATION_ID == INVESTIGATION.ID', + backref='studyinvestigations') + STUDY = relationship('STUDY', primaryjoin='STUDYINVESTIGATION.STUDY_ID == STUDY.ID', backref='studyinvestigations') + + +class STUDY(Base, EntityHelper): + __tablename__ = 'STUDY' + + ID = Column(BigInteger, primary_key=True) + CREATE_ID = Column(String(255), nullable=False) + CREATE_TIME = Column(DateTime, nullable=False) + DESCRIPTION = Column(String(4000)) + MOD_ID = Column(String(255), nullable=False) + MOD_TIME = Column(DateTime, nullable=False) + NAME = Column(String(255), nullable=False) + STARTDATE = Column(DateTime) + STATUS = Column(Integer) + USER_ID = Column(ForeignKey('USER_.ID'), index=True) + + USER_ = relationship('USER', primaryjoin='STUDY.USER_ID == USER.ID', backref='studies') + + +class SAMPLETYPE(Base, EntityHelper): + __tablename__ = 'SAMPLETYPE' + __table_args__ = ( + Index('UNQ_SAMPLETYPE_0', 'FACILITY_ID', 'NAME', 'MOLECULARFORMULA'), + ) + + ID = Column(BigInteger, primary_key=True) + CREATE_ID = Column(String(255), nullable=False) + CREATE_TIME = Column(DateTime, nullable=False) + MOD_ID = Column(String(255), nullable=False) + MOD_TIME = Column(DateTime, nullable=False) + MOLECULARFORMULA = Column(String(255), nullable=False) + NAME = Column(String(255), nullable=False) + SAFETYINFORMATION = Column(String(4000)) + FACILITY_ID = Column(ForeignKey('FACILITY.ID'), nullable=False) + + FACILITY = relationship('FACILITY', primaryjoin='SAMPLETYPE.FACILITY_ID == FACILITY.ID', backref='sampletypes')