diff --git a/dctap-dancer b/dctap-dancer index 45dbd73..d9f5e13 160000 --- a/dctap-dancer +++ b/dctap-dancer @@ -1 +1 @@ -Subproject commit 45dbd73c280a6577ef1bf5daca7c104606f421c1 +Subproject commit d9f5e1393f585a5f3d1c83526c49952b872bec2b diff --git a/ldpjs b/ldpjs new file mode 160000 index 0000000..d7aee34 --- /dev/null +++ b/ldpjs @@ -0,0 +1 @@ +Subproject commit d7aee34f2b6acab26ccd714eed2c1e6925f6a642 diff --git a/util-service/routes/marc.js b/util-service/routes/marc.js index 89b7a62..33e3b04 100644 --- a/util-service/routes/marc.js +++ b/util-service/routes/marc.js @@ -1,8 +1,9 @@ /** - * MARC Preview Routes + * MARC Routes * * Handles MARC preview generation: * - POST /marcpreview/:type - Generate MARC preview from RDF + * - POST /marcformat - Generate MARC of 1 format from another format */ const express = require('express'); @@ -88,6 +89,40 @@ function marcRecordHtmlify(data) { return formattedMarcRecord.join('\r\n'); } +/** + * + * @param {*} marcxml MARC record as XML that will be changed + * @param {string} sourceType Type of the incoming MARC, type record is {'leader': <....>, 'fields': [...]} + * @param {string} targetType Type of the outgoing MARC + * @returns + */ +function marcChangeFormat(marc, sourceType, targetType){ + let sourceTypeList = [ 'iso2709', 'marcxml', 'mij', 'record'] + let targetTypeList = [ 'iso2709', 'marcxml', 'mij', 'text', 'json', 'html'] + + if (!sourceTypeList.includes(sourceType)){ + return [false, 'source', sourceTypeList] + } + if (!targetTypeList.includes(targetType)){ + return [false, 'target', targetTypeList] + } + + let formatted + if (targetType != 'html'){ + const record = Marc.parse(marc, sourceType); + formatted = Marc.format(record, targetType) + } else { + if (sourceType == 'record'){ + formatted = marcRecordHtmlify(marc) + } else { + formatted = Marc.parse(marc, 'marcxml') + formatted = marcRecordHtmlify(formatted) + } + } + + return [true, true, formatted] +} + /** * Create MARC routes * @returns {Router} Express router @@ -148,6 +183,27 @@ function createMarcRoutes() { res.json(results); }); + /** + * POST /marcformat - Generate MARC of 1 format from another format + */ + router.post('/marcformat', async (req, res) => { + let marc = req.body.mrc; + const sourceType = req.body.sourceType; + const targetType = req.body.targetType; + + if (sourceType != 'record'){ + marc = marc.replaceAll('marcxml:', '') + } + let recordFormatted = marcChangeFormat(marc, sourceType, targetType) + if (!recordFormatted[0]){ + let message = 'Failed to match ' + recordFormatted[1] + ' format. Available formats: ' + recordFormatted[2].join(", ") + + return res.status(500).json({ msg: 'Error: ' + message }); + } else { + return res.status(200).json({ result: recordFormatted[2] }); + } + }); + return router; }