diff --git a/src/pds_doi_service/core/actions/update.py b/src/pds_doi_service/core/actions/update.py index f0630a12..2237e368 100644 --- a/src/pds_doi_service/core/actions/update.py +++ b/src/pds_doi_service/core/actions/update.py @@ -16,6 +16,7 @@ from pds_doi_service.core.entities.doi import Doi from pds_doi_service.core.entities.doi import DoiStatus from pds_doi_service.core.entities.exceptions import collect_exception_classes_and_messages +from pds_doi_service.core.entities.exceptions import UnknownIdentifierException from pds_doi_service.core.entities.exceptions import CriticalDOIException from pds_doi_service.core.entities.exceptions import DuplicatedTitleDOIException from pds_doi_service.core.entities.exceptions import InputFormatException @@ -220,36 +221,68 @@ def _update_dois(self, dois): updated_dois = [] for updated_doi in dois: - if not updated_doi.doi : - if self._force: - updated_doi.doi = self._record_service - else: - raise WarningDOIException( - f"Record provided for identifier {updated_doi.pds_identifier} does not have a DOI assigned in the label.\n" - "Use the Reserve action to acquire a DOI and add it in the Citation_Information/doi tag \n" - "if the version of PDS4 information model you are using allows it." + if updated_doi.doi: + existing_doi = self._get_doi_record_from_doi_identifier(updated_doi.doi) + elif self._force: + try: + # try to get DOI from the local database + existing_doi = self._get_doi_record_from_pds_identifier(updated_doi.pds_identifier) + except UnknownIdentifierException as e: + raise CriticalDOIException( + f"The identifier {updated_doi.pds_identifier} does not have DOI yet" + "use the reserve step to get one" ) + else: + raise WarningDOIException( + f"Record provided for identifier {updated_doi.pds_identifier} does not have a DOI assigned.\n" + "Add it in the Citation_Information/doi tag in the label\n" + " if the version of PDS4 information model you are using allows it.\n" + "Otherwise ignore this warning" + ) + + updated_doi = self._meld_dois(existing_doi, updated_doi) + + updated_dois.append(updated_doi) - # Get the record from the transaction database for the current DOI value - transaction_record = self._list_action.transaction_for_doi(updated_doi.doi) + return updated_dois - # Get the last output label associated with the transaction. - # This represents the latest version of the metadata for the DOI. - output_label = self._list_action.output_label_for_transaction(transaction_record) + def _get_doi_record_from_pds_identifier(self, pds_identifier): - # Output labels can contain multiple entries, so get only the one for - # the current DOI value - existing_doi_label, _ = self._web_parser.get_record_for_doi(output_label, updated_doi.doi) + # Get the record from the transaction database for the current DOI value + transaction_record = self._list_action.transaction_for_identifier(pds_identifier) - # Parse the existing Doi object, and meld it with the new one - existing_dois, _ = self._web_parser.parse_dois_from_label(existing_doi_label) + # Get the last output label associated with the transaction. + # This represents the latest version of the metadata for the DOI. + output_label = self._list_action.output_label_for_transaction(transaction_record) - updated_doi = self._meld_dois(existing_dois[0], updated_doi) + # Output labels can contain multiple entries, so get only the one for + # the current DOI value + existing_doi_label, _ = self._web_parser.get_record_for_identifier(output_label, pds_identifier) - updated_dois.append(updated_doi) + # Parse the existing Doi object, and meld it with the new one + existing_dois, _ = self._web_parser.parse_dois_from_label(existing_doi_label) + + return existing_dois[0] + + def _get_doi_record_from_doi_identifier(self, doi_identifier): + + # Get the record from the transaction database for the current DOI value + transaction_record = self._list_action.transaction_for_doi(doi_identifier) + + # Get the last output label associated with the transaction. + # This represents the latest version of the metadata for the DOI. + output_label = self._list_action.output_label_for_transaction(transaction_record) + + # Output labels can contain multiple entries, so get only the one for + # the current DOI value + existing_doi_label, _ = self._web_parser.get_record_for_doi(output_label, doi_identifier) + + # Parse the existing Doi object, and meld it with the new one + existing_dois, _ = self._web_parser.parse_dois_from_label(existing_doi_label) + + return existing_dois[0] - return updated_dois def _validate_dois(self, dois): """ diff --git a/tests/end_to_end/bundle_pds4.xml b/tests/end_to_end/bundle_pds4.xml index 580dbd25..6d719e2a 100644 --- a/tests/end_to_end/bundle_pds4.xml +++ b/tests/end_to_end/bundle_pds4.xml @@ -2,9 +2,15 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://pds.nasa.gov/pds4/pds/v1"> +<<<<<<< Updated upstream {{random_lid}} 1.0 InSight Cameras Bundle +======= +urn:nasa:pds:insight_cameras +54.0 +InSight Cameras XXXX Example Bundle +>>>>>>> Stashed changes 1.11.1.0 Product_Bundle @@ -20,6 +26,10 @@ InSight Cameras Experiment Data Record (EDR) and Reduced Data Record (RDR) Data Products +<<<<<<< Updated upstream +======= +10.13143/43g3-kk20 +>>>>>>> Stashed changes