示例#1
0
    def get_relation(self, relation_id):
        """
        Get a relation

        :param relation_id:
        :return: The relation
        :rtype: DbRelation
        """

        session = self._create_session()
        result_set = session.run(
            cypher_commands.NEO4J_GET_RELATION_RETURN_NODE,
            {"relation_id": int(relation_id)})

        relation = None
        for result in result_set:
            if not isinstance(result["relation"], Relationship):
                raise DatabaseException(
                    " should return only relationship {}, command {}".format(
                        relation_id,
                        cypher_commands.NEO4J_GET_RECORD_RETURN_NODE))

            relation = result["relation"]

        if relation is None:
            raise NotFoundException(
                "We cant find the relation with the id: {}, database command {}"
                .format(relation_id,
                        cypher_commands.NEO4J_GET_RECORD_RETURN_NODE))

        return self._split_attributes_metadata_from_node(relation)
示例#2
0
    def get_bundle(self, bundle_id):
        bundle_id = str(bundle_id)
        session = self._create_session()
        records = list()
        result_set = session.run(NEO4J_GET_BUNDLE_RETURN_NODES_RELATIONS,
                                 {"bundle_id": bundle_id})
        for result in result_set:
            record = result["re"]

            if record is None:
                raise DatabaseException("Record response should not be None")
            relation_record = self._split_attributes_metadata_from_node(record)
            records.append(relation_record)

        # Get bundle node and set identifier if there is a bundle node.
        bundle_node_result = session.run(NEO4j_GET_BUNDLE_RETURN_BUNDLE_NODE,
                                         {"bundle_id": bundle_id})

        raw_record = None
        for bundle in bundle_node_result:
            raw_record = self._split_attributes_metadata_from_node(bundle["b"])

        if raw_record is None and len(records) == 0:
            raise NotFoundException(
                "bundle with the id {} was not found ".format(bundle_id))

        bundle = namedtuple('Bundle', 'records, bundle_record')

        return bundle(records, raw_record)
示例#3
0
    def get_record(self, record_id):
        """
        Try to find the record in the database

        :param record_id:
        :return: DbRecord
        :rtype: DbRecord
        """

        session = self._create_session()
        result_set = session.run(cypher_commands.NEO4J_GET_RECORD_RETURN_NODE,
                                 {"record_id": int(record_id)})

        node = None
        for result in result_set:
            if node is not None:
                raise DatabaseException(
                    "get_record should return only one node for the id {}, command {}"
                    .format(record_id,
                            cypher_commands.NEO4J_GET_RECORD_RETURN_NODE))
            node = result["node"]

        if node is None:
            raise NotFoundException(
                "We cant find the node with the id: {}, database command {}".
                format(record_id,
                       cypher_commands.NEO4J_GET_RECORD_RETURN_NODE))

        return self._split_attributes_metadata_from_node(node)
示例#4
0
    def get_records_by_filter(self, attributes_dict=None, metadata_dict=None):
        """
        Return the records by a certain filter

        :param attributes_dict: Filter dict
        :type attributes_dict: dict
        :param metadata_dict: Filter dict for metadata
        :type metadata_dict: dict
        :return: list of all nodes and relations that fit the conditions
        :rtype: list(DbRecord and DbRelation)
        """

        if attributes_dict is None:
            attributes_dict = dict()
        if metadata_dict is None:
            metadata_dict = dict()

        (encoded_params,
         cypher_str) = self._get_cypher_filter_params(attributes_dict,
                                                      metadata_dict)

        session = self._create_session()
        records = list()
        result_set = session.run(
            cypher_commands.NEO4J_GET_RECORDS_BY_PROPERTY_DICT.format(
                filter_dict=cypher_str), encoded_params)
        for result in result_set:
            record = result["re"]

            if record is None:
                raise DatabaseException("Record response should not be None")
            relation_record = self._split_attributes_metadata_from_node(record)
            records.append(relation_record)
        return records
示例#5
0
    def save_document(self):
        session = self._create_session()
        result = session.run(NEO4J_CREATE_DOCUMENT_NODE_RETURN_ID)
        record_id = None
        for record in result:
            record_id = record["ID"]

        result_delete = session.run(NEO4J_DELETE__NODE_BY_ID,
                                    {"node_id": record_id})

        if record_id is None:
            raise DatabaseException("Could not get a valid ID result back")

        return str(record_id + 1)
示例#6
0
    def get_records_tail(self,
                         attributes_dict=None,
                         metadata_dict=None,
                         depth=None):
        """
        Return all connected nodes form the origin.


        :param attributes_dict: Filter dict
        :type attributes_dict: dict
        :param metadata_dict: Filter dict for metadata
        :type metadata_dict: dict
        :param depth: Max steps
        :return: list of all nodes and relations that fit the conditions
        :rtype: list(DbRecord and DbRelation)
        """

        if attributes_dict is None:
            attributes_dict = dict()
        if metadata_dict is None:
            metadata_dict = dict()

        (encoded_params,
         cypher_str) = self._get_cypher_filter_params(attributes_dict,
                                                      metadata_dict)

        depth_str = ""
        if depth is not None:
            depth_str = "1..{max}".format(max=depth)

        session = self._create_session()
        result_set = session.run(
            cypher_commands.NEO4J_GET_RECORDS_TAIL_BY_FILTER.format(
                filter_dict=cypher_str, depth=depth_str), encoded_params)
        records = list()
        for result in result_set:
            record = result["re"]

            if record is None:
                raise DatabaseException("Record response should not be None")
            relation_record = self._split_attributes_metadata_from_node(record)
            records.append(relation_record)

        return records
示例#7
0
    def get_bundle_records(self, bundle_identifier):
        """
        Return all records and relations for the bundle


        :param bundle_identifier:
        :return:
        """

        session = self._create_session()
        result_set = session.run(cypher_commands.NEO4J_GET_BUNDLE_RECORDS, {
            'meta:{}'.format(METADATA_KEY_IDENTIFIER):
            str(bundle_identifier)
        })
        records = list()
        for result in result_set:
            record = result["re"]

            if record is None:
                raise DatabaseException("Record response should not be None")
            relation_record = self._split_attributes_metadata_from_node(record)
            records.append(relation_record)

        return records