|
1 | | -import { postTransfer } from "../sinopiaApi" |
| 1 | +import { postTransfer, fetchResource } from "../sinopiaApi" |
2 | 2 | import { addError } from "actions/errors" |
| 3 | +import { clearLocalIds, setLocalId } from "actions/transfer" |
| 4 | +import rdf from "rdf-ext" |
3 | 5 |
|
4 | 6 | export const transfer = |
5 | | - (resourceUri, group, target, errorKey) => (dispatch) => { |
6 | | - postTransfer(resourceUri, group, target).catch((err) => { |
| 7 | + (resourceUri, group, target, localId, errorKey) => (dispatch) => |
| 8 | + postTransfer(resourceUri, group, target, localId).catch((err) => { |
7 | 9 | dispatch( |
8 | 10 | addError(errorKey, `Error requesting transfer: ${err.message || err}`) |
9 | 11 | ) |
10 | 12 | }) |
| 13 | + |
| 14 | +export const loadLocalIds = |
| 15 | + (resourceKey, sinopiaLocalAdminMetadataRefs, errorKey) => (dispatch) => { |
| 16 | + dispatch(clearLocalIds(resourceKey)) |
| 17 | + sinopiaLocalAdminMetadataRefs.forEach((resourceUri) => { |
| 18 | + dispatch(fetchLocalId(resourceUri, errorKey)).then( |
| 19 | + ([target, group, localId]) => { |
| 20 | + if (!target) { |
| 21 | + return |
| 22 | + } |
| 23 | + dispatch(setLocalId(resourceKey, target, group, localId)) |
| 24 | + } |
| 25 | + ) |
| 26 | + }) |
11 | 27 | } |
12 | 28 |
|
13 | | -export const noop = () => {} |
| 29 | +const fetchLocalId = (uri, errorKey) => (dispatch) => |
| 30 | + fetchResource(uri) |
| 31 | + .then(([dataset, response]) => { |
| 32 | + if (!dataset) return [false, false, false] |
| 33 | + const identifierNode = identifierNodeFromDataset(uri, dataset) |
| 34 | + if (!identifierNode) return [false, false, false] |
| 35 | + const localId = localIdFromIdentifierNode(identifierNode, dataset) |
| 36 | + const target = targetFromIdentifierNode(identifierNode, dataset) |
| 37 | + return [target, response.group, localId] |
| 38 | + }) |
| 39 | + .catch((err) => { |
| 40 | + dispatch( |
| 41 | + addError(errorKey, `Error retrieving ${uri}: ${err.message || err}`) |
| 42 | + ) |
| 43 | + return [false, false, false] |
| 44 | + }) |
| 45 | + |
| 46 | +const localIdFromIdentifierNode = (identifierNode, dataset) => |
| 47 | + dataset |
| 48 | + .match( |
| 49 | + identifierNode, |
| 50 | + rdf.namedNode("http://www.w3.org/1999/02/22-rdf-syntax-ns#value") |
| 51 | + ) |
| 52 | + .toArray()[0].object.value |
| 53 | + |
| 54 | +const targetFromIdentifierNode = (identifierNode, dataset) => { |
| 55 | + const sourceNode = dataset |
| 56 | + .match( |
| 57 | + identifierNode, |
| 58 | + rdf.namedNode("http://id.loc.gov/ontologies/bibframe/source") |
| 59 | + ) |
| 60 | + .toArray()[0].object |
| 61 | + return dataset |
| 62 | + .match( |
| 63 | + sourceNode, |
| 64 | + rdf.namedNode("http://www.w3.org/2000/01/rdf-schema#label") |
| 65 | + ) |
| 66 | + .toArray()[0].object.value |
| 67 | +} |
| 68 | + |
| 69 | +const identifierNodeFromDataset = (uri, dataset) => |
| 70 | + dataset |
| 71 | + .match( |
| 72 | + rdf.namedNode(uri), |
| 73 | + rdf.namedNode("http://id.loc.gov/ontologies/bibframe/identifier") |
| 74 | + ) |
| 75 | + .toArray()[0].object |
0 commit comments