Skip to content

Commit dbc4f40

Browse files
authored
Merge pull request #523 from hubmapconsortium/karlburke/collectionDatasetUpdates
Karlburke/collection dataset updates
2 parents e4a15b6 + aaa7b55 commit dbc4f40

File tree

2 files changed

+6
-122
lines changed

2 files changed

+6
-122
lines changed

src/app.py

Lines changed: 6 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,6 +1402,12 @@ def update_entity(id):
14021402
# Handle linkages update via `after_update_trigger` methods
14031403
if has_dataset_uuids_to_link or has_dataset_uuids_to_unlink:
14041404
after_update(normalized_entity_type, user_token, merged_updated_dict)
1405+
elif normalized_entity_type == 'Collection':
1406+
# Generate 'before_update_trigger' data and update the entity details in Neo4j
1407+
merged_updated_dict = update_entity_details(request, normalized_entity_type, user_token, json_data_dict, entity_dict)
1408+
1409+
# Handle linkages update via `after_update_trigger` methods
1410+
after_update(normalized_entity_type, user_token, merged_updated_dict)
14051411
else:
14061412
# Generate 'before_update_trigger' data and update the entity details in Neo4j
14071413
merged_updated_dict = update_entity_details(request, normalized_entity_type, user_token, json_data_dict, entity_dict)
@@ -1950,78 +1956,6 @@ def get_next_revisions(id):
19501956

19511957
return jsonify(final_result)
19521958

1953-
1954-
1955-
"""
1956-
Link the given list of datasets to the target collection
1957-
1958-
JSON request body example:
1959-
{
1960-
"dataset_uuids": [
1961-
"fb6757b606ac35be7fa85062fde9c2e1",
1962-
"81a9fa68b2b4ea3e5f7cb17554149473",
1963-
"3ac0768d61c6c84f0ec59d766e123e05",
1964-
"0576b972e074074b4c51a61c3d17a6e3"
1965-
]
1966-
}
1967-
1968-
Parameters
1969-
----------
1970-
collection_uuid : str
1971-
The UUID of target collection
1972-
1973-
Returns
1974-
-------
1975-
json
1976-
JSON string containing a success message with 200 status code
1977-
"""
1978-
@app.route('/collections/<collection_uuid>/add-datasets', methods = ['PUT'])
1979-
def add_datasets_to_collection(collection_uuid):
1980-
if READ_ONLY_MODE:
1981-
forbidden_error("Access not granted when entity-api in READ-ONLY mode")
1982-
1983-
# Get user token from Authorization header
1984-
user_token = get_user_token(request)
1985-
1986-
# Query target entity against uuid-api and neo4j and return as a dict if exists
1987-
entity_dict = query_target_entity(collection_uuid, user_token)
1988-
if entity_dict['entity_type'] != 'Collection':
1989-
bad_request_error(f"The UUID provided in URL is not a Collection: {collection_uuid}")
1990-
1991-
# Always expect a json body
1992-
require_json(request)
1993-
1994-
# Parse incoming json string into json data(python list object)
1995-
json_data_dict = request.get_json()
1996-
1997-
if 'dataset_uuids' not in json_data_dict:
1998-
bad_request_error("Missing 'dataset_uuids' key in the request JSON.")
1999-
2000-
if not json_data_dict['dataset_uuids']:
2001-
bad_request_error("JSON field 'dataset_uuids' can not be empty list.")
2002-
2003-
# Now we have a list of uuids
2004-
dataset_uuids_list = json_data_dict['dataset_uuids']
2005-
2006-
# Make sure all the given uuids are datasets (or publications 2/17/23 ~Derek Furst)
2007-
for dataset_uuid in dataset_uuids_list:
2008-
entity_dict = query_target_entity(dataset_uuid, user_token)
2009-
if not schema_manager.entity_type_instanceof(entity_dict['entity_type'], 'Dataset'):
2010-
bad_request_error(f"The UUID provided in JSON is not a Dataset or Publication: {dataset_uuid}")
2011-
2012-
try:
2013-
app_neo4j_queries.add_datasets_to_collection(neo4j_driver_instance, collection_uuid, dataset_uuids_list)
2014-
except TransactionError:
2015-
msg = "Failed to create the linkage between the given datasets and the target collection"
2016-
# Log the full stack trace, prepend a line with our message
2017-
logger.exception(msg)
2018-
# Terminate and let the users know
2019-
internal_server_error(msg)
2020-
2021-
# Send response with success message
2022-
return jsonify(message = "Successfully added all the specified datasets to the target collection")
2023-
2024-
20251959
"""
20261960
Redirect a request from a doi service for a dataset or collection
20271961

src/app_neo4j_queries.py

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -379,56 +379,6 @@ def get_next_revisions(neo4j_driver, uuid, property_key = None):
379379

380380
return results
381381

382-
383-
"""
384-
Link the datasets to the target collection
385-
386-
Parameters
387-
----------
388-
neo4j_driver : neo4j.Driver object
389-
The neo4j database connection pool
390-
collection_uuid : str
391-
The uuid of target collection
392-
dataset_uuids_list : list
393-
A list of dataset uuids to be linked to collection
394-
"""
395-
def add_datasets_to_collection(neo4j_driver, collection_uuid, dataset_uuids_list):
396-
# Join the list of uuids and wrap each string in single quote
397-
joined_str = ', '.join("'{0}'".format(dataset_uuid) for dataset_uuid in dataset_uuids_list)
398-
# Format a string to be used in Cypher query.
399-
# E.g., ['fb6757b606ac35be7fa85062fde9c2e1', 'ku0gd44535be7fa85062fde98gt5']
400-
dataset_uuids_list_str = '[' + joined_str + ']'
401-
402-
try:
403-
with neo4j_driver.session() as session:
404-
tx = session.begin_transaction()
405-
406-
logger.info("Create relationships between the target Collection and the given Datasets")
407-
408-
query = (f"MATCH (c:Collection), (d:Dataset) "
409-
f"WHERE c.uuid = '{collection_uuid}' AND d.uuid IN {dataset_uuids_list_str} "
410-
# Use MERGE instead of CREATE to avoid creating the relationship multiple times
411-
# MERGE creates the relationship only if there is no existing relationship
412-
f"MERGE (c)<-[r:IN_COLLECTION]-(d)")
413-
414-
logger.info("======add_datasets_to_collection() query======")
415-
logger.info(query)
416-
417-
tx.run(query)
418-
tx.commit()
419-
except TransactionError as te:
420-
msg = f"TransactionError from calling add_datasets_to_collection(): {te.value}"
421-
# Log the full stack trace, prepend a line with our message
422-
logger.exception(msg)
423-
424-
if tx.closed() == False:
425-
logger.info("Failed to commit add_datasets_to_collection() transaction, rollback")
426-
427-
tx.rollback()
428-
429-
raise TransactionError(msg)
430-
431-
432382
"""
433383
Retrive the full tree above the given entity
434384

0 commit comments

Comments
 (0)