Skip to content

Allow pre-filling Research Outputs with DOI through DataCite and Crossref APIs - #3635

Open
momo3404 wants to merge 22 commits into
developmentfrom
momo/feat/doi-research-outputs
Open

momo3404 wants to merge 22 commits into
developmentfrom
momo/feat/doi-research-outputs

Conversation

@momo3404

@momo3404 momo3404 commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

The code in this PR was written and reviewed by @momo3404, @aaronskiba, @gjacob24, @alasdair-macleod through two previous PRs on the DMP Assistant sister repository: The first PR and the second one. This PR exists to merge that code into the Roadmap repository.

Changes proposed in this PR:

  • Adds DOI lookup and pre-fill integration to Research Outputs so users can import metadata from DataCite and Crossref into new and edit forms while validating and persisting DOI identifiers consistently across the UI and v2 API.
  • DOI identifier scheme: Create a DOI identifier scheme to allow attaching DOIs to Research Output records.
  • DOI Resolution & Provider Services (app/services/external_apis/): Adds DoiResolutionService, DataciteService, and CrossrefService to normalize DOIs (stripping doi.org/dx.doi.org), resolve metadata with DataCite-to-Crossref failover and 5-minute caching, and sanitize JATS descriptions.
  • Model Validation (app/models/identifier.rb): Adds valid_doi_format validation scoped to doi scheme identifiers using DoiResolutionService.normalize_doi.
  • Controller Prefill (app/controllers/research_outputs_controller.rb): Populates @prefill_doi and resolves metadata on new/edit, assigns or marks cleared DOI identifiers for destruction before saving, and wraps update in a transaction with ActiveRecord::Rollback to preserve repositories and metadata standards on validation failure.
  • Form UI & Client-Side Validation (app/views/research_outputs/_form.html.erb, app/javascript/src/researchOutputs/form.js): Adds the #doi_lookup_panel card with client-side prefix normalization, format validation, and inline status feedback.
  • Specs: Adds and updates service, model, and request specs covering DOI resolution and format validation.

…OutputsController`

- Updates `#new` and `#edit` to populate `@prefill_doi` (falling back to the existing DOI identifier on edit) and resolve metadata via `prefill_from_doi` when `params[:prefill_doi]` is supplied.
 - Adds `assign_doi_identifier` and `set_doi_identifier` to normalize, build/update, or mark cleared DOI identifiers for destruction prior to saving.
  - Wraps `#update` association clearing, DOI assignment, and `@research_output.update` in a `ResearchOutput.transaction` with `ActiveRecord::Rollback` so existing repositories and metadata standards are preserved if validation fails.
  - Re-renders `new` and `edit` in place with `flash.now[:alert]` and preserved `@prefill_doi` on validation errors.
  - Adds `prefill_from_doi` and `doi_error_message` private helpers to map DOI resolution statuses to localized user alerts.
- Implements `.api_base_url` and `.active?` configuration helpers.
  - Adds `.execute_api_get` to query the Crossref `/works/:doi` REST endpoint with polite `User-Agent` headers and 5-second open/read timeouts.
  - Adds `.parse_attributes` to extract title, sanitized abstract description, partial `issued.date-parts` release dates, mapped output type, and normalized DOI.
  - Adds `.map_crossref_type` to map Crossref work types to `ResearchOutput` enum values (`:dataset`, `:software`, `:text`, `:other`).
  - Adds `.log_and_notify_error` to log lookup failures and report them to Rollbar.
- Implements `.api_base_url` and `.active?` configuration helpers.
  - Adds `.execute_api_get` to query the DataCite `/dois/:doi` endpoint with JSON:API `Accept` headers and 5-second open/read timeouts.
  - Adds `.parse_attributes` to extract title, sanitized description (preferring `Abstract` with fallback to the first description), mapped `ResearchOutput` type, `Issued` release date, and normalized DOI.
  - Adds `.log_and_notify_error` to log lookup failures and report them to Rollbar.
…d normalization

- Defines shared `MODERN_DOI_PATTERN` and `OLD_DOI_PATTERN` regex constants alongside `.normalize_doi` to strip `doi.org` and `dx.doi.org` prefixes and validate DOI syntax.
  - Implements `.fetch_metadata` with 5-minute top-level and per-provider `Rails.cache` caching, querying DataCite first and falling back to Crossref (`resolve_from_providers`, `execute_fetch`, `evaluate_statuses`).
  - Adds `.sanitize_and_normalize_description` to convert JATS XML markup (`<jats:p>`, `<jats:italic>`, `<jats:bold>`, `<jats:list>`, `<jats:list-item>`) into standard HTML, strip `<script>`/`<style>` nodes, and sanitize output against `ALLOWED_TAGS` and `ALLOWED_ATTRIBUTES`.
  - Adds `.active?` to check whether DataCite or Crossref integration is enabled.
- Add title sanitization
- Renders a `#doi_lookup_panel` card when `ExternalApis::DoiResolutionService.active?` is enabled.
  - Adds `#doi_lookup_input` (`name="prefill_doi"`) pre-populated with `@prefill_doi` so users can trigger a metadata lookup via `#btn_prefill_doi` or submit/clear the DOI directly with the form.
  - Sets `data-prefill-path` on `#btn_prefill_doi` to route lookups to `new_plan_research_output_path` or `edit_plan_research_output_path` based on record state, alongside a hidden `#doi_lookup_status` element for client-side feedback.
…put form

- Resets `#btn_prefill_doi` disabled state and default label on `pageshow` when restored from the browser back-forward cache.
  - Adds a `#btn_toggle_doi_form` click handler to show/hide `#doi_lookup_panel` and focus `#doi_lookup_input` when opened.
  - Adds a `#btn_prefill_doi` click handler that strips `doi.org` and `dx.doi.org` URL prefixes, validates the DOI against `DOI_MODERN_PATTERN` and `DOI_OLD_PATTERN` constants, displays inline validation errors via `#doi_lookup_status`, and redirects to `data-prefill-path` with `?prefill_doi=<clean_doi>`.
@momo3404
momo3404 force-pushed the momo/feat/doi-research-outputs branch from 54ed724 to 84c46d6 Compare December 1, 2026 20:15
@momo3404
momo3404 force-pushed the momo/feat/doi-research-outputs branch from 643ac4d to 18a7b70 Compare December 1, 2026 20:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants