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 = './defaults/context.json';
fs = require('fs-extra');
/* 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",
// }
async function datapub2catalog(id, datapub, org) {
const context = await fs.readJson(CONTEXT);
const catalog = {
'@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 = [
organisation,
about(datapub),
spatial(datapub),
temporal(datapub),
licence(datapub),
citation(datapub),
files,
related(datapub)
];
catalog['@graph'] = graph.filter((e) => e);
return catalog;
}
function link_id(item) {
return { '@id': item['@id'] };
}
function dataset(id, datapub, organisation, files) {
return {
'@id': id,
'type': 'Dataset',
'name': datapub['title'],
'description': datapub['description'],
'Publisher': link_id(organisation),
'hasPart': files.map(link_id),
}
}
// 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) {
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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'];
if( id ) {
return {
'@id': id,
'identifier': id,
'name': p['text_full_name'],
'familyName': p['family_name'],
'givenName': p['given_name'],
'email': p['email'],
'affiliation': link_id(organisation)
}
} else {
// warn for unidentifiable creators
return undefined;
}
})
return creators.filter((x) => x)
}
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
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
};