-
Notifications
You must be signed in to change notification settings - Fork 1
/
EMLCreator.js
executable file
·223 lines (193 loc) · 6.6 KB
/
EMLCreator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
const EML_KEYS = [
'title',
'creators',
// 'organization-code',
// 'dataset-name',
// 'dataset-id',
// 'keywords',
'abstract',
// 'additional-info',
// 'taxonomic-coverage',
// 'auto-taxonomic-coverage',
// 'taxonimic-field',
// 'geographic-coverage',
// 'auto-geographic-coverage',
// 'purpose',
// 'rights',
// 'lang',
// 'enable-manual-eml',
// 'manual-eml',
// Metadata Provider:
'given-name',
'surname',
'email',
'organization-name',
];
/**
* Create the EML/XML file in specified folder
*
* @param {Folder} folder to create the EML file
* @return {File} EML file
*/
function createEML_(folder) {
var props = PropertiesService.getDocumentProperties();
var eml = JSON.parse(props.getProperty('eml'));
var xml = '';
if (eml['manual-eml'] == '') {
var root = XmlService.createElement("eml")
// Package ID will be filled with Zenodo Upload Key
.setAttribute("packageId", "")
.setAttribute("system", "https://zenodo.org")
.setAttribute("scope", "system")
.setNamespace(XmlService.getNamespace("eml", "https://eml.ecoinformatics.org/eml-2.2.0"))
.setNamespace(XmlService.getNamespace("dc", "http://purl.org/dc/terms/"))
.setNamespace(XmlService.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance"));
var dataset = XmlService.createElement("dataset");
var title = XmlService.createElement("title")
.setText(eml['title']);
dataset.addContent(title);
for(var i = 0; i < eml['creators'].length; i++) {
// Creator
var creator = eml['creators'][i];
var creatorElem = XmlService.createElement("creator")
.addContent(
XmlService.createElement("individualName")
.addContent(XmlService.createElement("givenName").setText(creator['givenName']))
.addContent(XmlService.createElement("surName").setText(creator['surname'])))
.addContent(XmlService.createElement("organizationName").setText(creator['org']));
if ( creator['email'] != '') {
creatorElem.addContent(
XmlService.createElement("electronicMailAddress").setText(creator['email']));
}
dataset.addContent(creatorElem);
}
//Contact
var contact = eml['creators'][0];
var contactElem = XmlService.createElement("contact")
.addContent(
XmlService.createElement("individualName")
.addContent(XmlService.createElement("givenName").setText(contact['givenName']))
.addContent(XmlService.createElement("surName").setText(contact['surname'])))
.addContent(XmlService.createElement("organizationName").setText(contact['org']));
if (contact['email'] != '') {
contactElem.addContent(
XmlService.createElement("electronicMailAddress").setText(contact['email']));
}
dataset.addContent(contactElem);
// Medatada Provider
dataset.addContent(XmlService.createElement("metadataProvider")
.addContent(XmlService.createElement("individualName")
.addContent(XmlService.createElement("givenName").setText(eml['given-name']))
.addContent(XmlService.createElement("surName").setText(eml['surname'])))
.addContent(XmlService.createElement("organizationName").setText(eml['organization-name'])));
// Abstract
dataset.addContent(XmlService.createElement("abstract")
.addContent(XmlService.createElement("para").setText(eml['abstract'])));
// Keywords
if (eml['keywords'] != '') {
var keywords = eml['keywords'].split(';');
var keywordSet = XmlService.createElement("keywordSet");
for (var i = 0; i < keywords.length; i++) {
keywordSet.addContent(XmlService.createElement("keyword").setText(keywords[i]));
}
dataset.addContent(keywordSet);
}
// Additional Information
if (eml['additional-info'] != '') {
dataset.addContent(XmlService.createElement("additionalInfo")
.addContent(XmlService.createElement("para").setText(eml['additional-info'])));
}
// Intellectual rights
if (eml['rights'] != '') {
dataset.addContent(XmlService.createElement("intellectualRights")
.addContent(XmlService.createElement("para").setText(eml['rights'])));
}
// Language
if (eml['lang'] != '') {
dataset.addContent(XmlService.createElement("language").setText(eml['lang']));
}
root.addContent(dataset);
var document = XmlService.createDocument(root);
xml = XmlService.getPrettyFormat().format(document);
} else {
xml = eml['manual-eml'];
}
// Check if file already exists
var iter = folder.getFilesByName('eml.xml');
if (iter.hasNext()) {
var file = iter.next();
file.setContent(xml);
return file;
}
//Output to file
return folder.createFile("eml.xml", xml);
}
/**
* Saves EML data from editor to Document properties
*
* @param {array} EML form data
*
*/
function saveEML(data){
var prop = PropertiesService.getDocumentProperties();
// Check required fields
if ( data['enable-manual-eml'] &&
(data['manual-eml'] === undefined || data['manual-eml'] == '')) {
return {
error: true,
message: "EML can't be empty! Please, provide an EML metadata or disable the manually enter of EML content"
};
} else {
for(var i = 0; i < EML_KEYS.length; i++) {
var key = EML_KEYS[i];
if ( data[key] === undefined || data[key] == '') {
return {
error: true,
message: 'Field ' + key + 'is obligatory',
field: key
};
} else if( Array.isArray(data[key]) && data[key].length == 0) {
return {
error: true,
message: 'Field ' + key + 'is obligatory',
field: key
};
} else if (key === 'creators') {
for (var i = 0; i < data[key].length; i++) {
var creator = data[key][i];
if( creator['givenName'] == '' ||creator['surname'] == ''
|| creator['org'] == '') {
return {
error: true,
message: 'A creator must have a given name, surname and organization'
};
}
}
}
}
}
//Store eml data into document properties
prop.setProperty('eml', JSON.stringify(data));
return true;
}
/**
* Returns the EML data stored in Document properties
* @return {array}
*/
function getEMLData(){
var props = PropertiesService.getDocumentProperties();
var data = props.getProperty('eml');
return data ? JSON.parse(data) : {};
}
/**
* Show EML Editor Dialog
*
*/
function openEMLEditor(){
var html = HtmlService.createTemplateFromFile('EMLDialog')
.evaluate()
.setWidth(600)
.setHeight(800);
SpreadsheetApp.getUi()
.showModalDialog(html, 'Ecological Metadata Language');
}