Project

General

Profile

1
import { Injectable } from "@angular/core";
2
import { JsonldDocument, Identifier, Person, License, Citation } from "../model/jsonld-document";
3
import * as _ from "lodash";
4

    
5
@Injectable()
6
export class OpenAireJsonldConverterService {
7
	constructor() { }
8

    
9
	convertPublication(result: any, URL): JsonldDocument {
10
		const doc = new JsonldDocument();
11

    
12
		doc.title = this.getTitle(result);
13
		doc.description = this.getDescription(result);
14
		doc.identifier = this.getIdentifier(result);
15
		doc.url = URL;
16
		doc.sameAs = this.getSameAs(result);
17
		doc.creator = this.getCreator(result);
18
		doc.dateCreated = this.getDateCreated(result);
19
		doc.citation = this.getCitation(result);
20
		doc.license = this.getLicense(result);
21
		doc.keyword = this.getKeyword(result);
22

    
23
		return doc;
24
	}
25

    
26
	private getTitle(result: any): String[] {
27
		const item = _.get(result, "result.metadata.oaf:entity.oaf:result.title.content", null);
28
		if (!item) return null;
29
		return [item as String];
30
	}
31

    
32
	private getDescription(result: any): String[] {
33
		const item = _.get(result, "result.metadata.oaf:entity.oaf:result.description", null);
34
		if (!item) return null;
35
		return [item as String];
36
	}
37

    
38
	private getDateCreated(result: any): String[] {
39
		const item = _.get(result, "result.metadata.oaf:entity.oaf:result.dateofacceptance", null);
40
		if (!item) return null;
41
		return [item as String];
42
	}
43

    
44
	private getLicense(result: any): License[] {
45
		const item = _.get(result, "result.metadata.oaf:entity.oaf:result.bestaccessright", null);
46
		if (!item) return null;
47
		if (!_.has(item, "classid")) return null;
48
		if (!_.has(item, "classname")) return null;
49
		if (!_.has(item, "schemeid")) return null;
50

    
51
		return [{
52
			title: [_.get(item, "classname")],
53
			identifier: [{
54
				id: _.get(item, "classid"),
55
				schema: _.get(item, "schemeid")
56
			}]
57
		}];
58
	}
59

    
60
	private getIdentifier(result: any): Identifier[] {
61
		const item = _.get(result, "result.metadata.oaf:entity.oaf:result.pid", null);
62
		if (!item) return null;
63
		const array = new Array<Identifier>();
64
		if (Array.isArray(item)) {
65
			const itemArray = item as Array<any>;
66
			for (var i = 0; i < itemArray.length; i += 1) {
67
				const val = this.getSingleIdentifier(itemArray[i]);
68
				if (val) array.push(val);
69
			}
70
		}
71
		else {
72
			const val = this.getSingleIdentifier(item);
73
			if (val) array.push(val);
74
		}
75
		if (array.length == 0) return null;
76
		return array;
77
	}
78

    
79
	private getSingleIdentifier(item: any): Identifier {
80
		if (!_.has(item, "classname")) return null;
81
		if (!_.has(item, "content")) return null;
82
		return {
83
			schema: _.get(item, "classname"),
84
			id: _.get(item, "content")
85
		};
86
	}
87

    
88
	private getSameAs(result: any): String[] {
89
		const instances = _.get(result, "result.metadata.oaf:entity.oaf:result.children.instance", null);
90
		if (!instances) return null;
91
		if (!Array.isArray(instances)) return null;
92

    
93
		const array = new Array<String>();
94

    
95
		const instanceArray = instances as Array<any>;
96
		for (var i = 0; i < instanceArray.length; i += 1) {
97
			const webresources = _.get(instanceArray[i], "webresource", null);
98
			if (!webresources) continue;
99
			if (Array.isArray(webresources)) {
100
				const webresourceArray = webresources as Array<any>;
101
				for (var q = 0; q < webresourceArray.length; q += 1) {
102
					const url = _.get(webresourceArray[q], "url", null);
103
					if (!url) continue;
104
					array.push(url as String);
105
				}
106
			}
107
			else {
108
				const url = _.get(webresources, "url", null);
109
				if (!url) continue;
110
				array.push(url as String);
111
			}
112
		}
113
		if (array.length == 0) return null;
114
		return array;
115
	}
116

    
117
	private getKeyword(result: any): String[] {
118
		const subjects = _.get(result, "result.metadata.oaf:entity.oaf:result.subject", null);
119
		if (!subjects) return null;
120
		if (!Array.isArray(subjects)) return null;
121

    
122
		const array = new Array<String>();
123

    
124
		const subjectArray = subjects as Array<any>;
125
		for (var i = 0; i < subjectArray.length; i += 1) {
126
			const classid = _.get(subjectArray[i], "classid", null);
127
			if (classid !== "keyword") continue;
128

    
129
			const sub = _.get(subjectArray[i], "content", null);
130
			if (!sub) return null;
131

    
132
			array.push(sub as String);
133
		}
134
		if (array.length == 0) return null;
135
		return array;
136
	}
137

    
138
	private getCreator(result: any): Person[] {
139
		const item = _.get(result, "result.metadata.oaf:entity.oaf:result.creator", null);
140
		if (!item) return null;
141
		const array = new Array<Person>();
142
		if (Array.isArray(item)) {
143
			const itemArray = item as Array<any>;
144
			for (var i = 0; i < itemArray.length; i += 1) {
145
				const val = this.getSinglePerson(itemArray[i]);
146
				if (val) array.push(val);
147
			}
148
		}
149
		else {
150
			const val = this.getSinglePerson(item);
151
			if (val) array.push(val);
152
		}
153
		if (array.length == 0) return null;
154
		return array;
155
	}
156

    
157
	private getSinglePerson(item: any): Person {
158
		if (!_.has(item, "surname") && !_.has(item, "name") && !_.has(item, "content")) return null;
159
		return {
160
			familyName: _.get(item, "surname", null),
161
			givenName: _.get(item, "name", null),
162
			name: _.get(item, "content", null)
163
		};
164
	}
165

    
166
	private getCitation(result: any): Citation[] {
167
		const item = _.get(result, "result.metadata.oaf:entity.extraInfo.citations.citation", null);
168
		if (!item) return null;
169
		const array = new Array<Citation>();
170
		if (Array.isArray(item)) {
171
			const itemArray = item as Array<any>;
172
			for (var i = 0; i < itemArray.length; i += 1) {
173
				const val = this.getSingleCitation(itemArray[i]);
174
				if (val) array.push(val);
175
			}
176
		}
177
		else {
178
			const val = this.getSingleCitation(item);
179
			if (val) array.push(val);
180
		}
181
		if (array.length == 0) return null;
182
		return array;
183
	}
184

    
185
	private getSingleCitation(item: any): Citation {
186
		if (!_.has(item, "rawText")) return null;
187
		if (!_.has(item, "id")) return null;
188

    
189
		const array = new Array<Identifier>();
190

    
191
		const ids = _.get(item, "id", null);
192
		if (Array.isArray(ids)) {
193
			const idsArray = ids as Array<any>;
194

    
195
			for (var i = 0; i < idsArray.length; i += 1) {
196
				const type = _.get(idsArray[i], "type", null);
197
				const value = _.get(idsArray[i], "value", null);
198
				if (!type || !value) continue;
199
				array.push({
200
					id: value,
201
					schema: type
202
				});
203
			}
204
		}
205
		else {
206
			const type = _.get(ids, "type", null);
207
			const value = _.get(ids, "value", null);
208
			if (type && value) {
209
				array.push({
210
					id: value,
211
					schema: type
212
				});
213
			}
214
		}
215

    
216
		if (array.length == 0) return null;
217

    
218
		return {
219
			title: [_.get(item, "rawText")],
220
			identifier: array
221
		};
222
	}
223

    
224

    
225
}
(2-2/2)