Skip to content
Snippets Groups Projects
catalog.js 3.53 KiB
Newer Older
/*
This is part of datacrate, a node.js library for working with 
DataCrates.  Copyright (C) 2018  University of Technology Sydney

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

const CONTEXT = { };


/* Convert metadata from a redbox data publication and
   its data record to a JSON-LD datacrate catalogue */


// TODO; add contentSize, encodingFormat and fileFormat
// from siegfried to the dataLocations array
// org is the organisation as an array with keys @id
// and name:
// { 
//   "@id": "https://www.uts.edu.au/"
//   "name": "University of Technology Sydney",
// }

function datapub2catalog(datapub, org) {
	const catalog = {
		'@context': CONTEXT,
		'@graph': []
	};

	const organisation = { 
		'@id': org['@id'],
		'@type': 'Organization',
		'identifier': org['@id'],
		'name': org['name']
	};

	// assuming that all creators are affiliated to this one organisation

	const people = creators(datapub, organisation);
	const files = attachments(datapub);

	const graph = [
		dataset(datapub, files, organisation),
		organisation, 
		funding(datapub),
		about(datapub),
		spatial(datapub),
		temporal(datapub),
		creators,
		licence(datapub),
		citation(datapub),
		files,
		related(datapub)
	];

	catalog['@graph'] = graph.filter((e) => e);
	return catalog;
}


function dataset(datapub, files) {
		
}



// files(datapub)

// crosswalk dataLocations to an array of File items.
// The dataLocations are expected to have contentSize, 
// encodingFormat and fileFormat already set

function attachments(datapub) {
	if( !datapub['dataLocations'] ) {
		return [];
	}
	const files = datapub['dataLocations'].map((dl) => {
		if( dl['type'] == 'attachment' ) {
			return {
				'@id': dl['name'],
				'name': dl['name'],
				'@type': 'File',
				'contentSize': dl['contentSize'],
				'encodingFormat': dl['encodingFormat'],
				'fileFormat': dl['fileFormat']
			}
		} else {
			// todo: URLs and physical locations
			return undefined;
		}
	});
	return files.filter((x) => x);
}

// creators(datapub)
//
// returns the array of Person items from the creators
// field of the data publication

// TODO: affiliations

function creators(datapub, organisation) {
	if( !datapub['creators'] ) {
		throw Error("A DataCrate has to have at least one creator");
	}	
	const creators = datapub['creators'].map((p) => {
		const id = p['orcid'] || p['email'] || p['text_full_name'];
		return {
			'@id': id,
			'identifier': id,
			'name': p['text_full_name'],
			'familyName': p['family_name'],
			'givenName': p['given_name'],
			'email': p['email'],
			'affiliation': { '@id': organisation['@id'] }
function funding(datapub) {
	return undefined;
}

function about(datapub) {
	return undefined;
}

function spatial(datapub) {
	return undefined;
}

function temporal(datapub) {
	return undefined;
}

function licence(datapub) {
	return undefined;
}

function citation(datapub) {
	return undefined;
}

function related(datapub) {
	return undefined;
}


module.exports = {
	'datapub2catalog': datapub2catalog
};