Project

General

Profile

1
package eu.dnetlib.dli.resolver.model;
2

    
3
import com.google.common.collect.Lists;
4
import com.google.gson.Gson;
5
import com.google.gson.GsonBuilder;
6
import eu.dnetlib.dli.DLIUtils;
7
import eu.dnetlib.pid.resolver.model.*;
8
import org.apache.commons.lang3.StringEscapeUtils;
9
import org.apache.commons.lang3.StringUtils;
10
import org.apache.commons.logging.Log;
11
import org.apache.commons.logging.LogFactory;
12

    
13
import java.util.ArrayList;
14
import java.util.List;
15
import java.util.stream.Collectors;
16

    
17
/**
18
 * The Class DLIObject.
19
 */
20
public class DLIResolvedObject extends AbstractResolvedObject {
21
	private static final Log log = LogFactory.getLog(DLIResolvedObject.class);
22

    
23
	/**
24
	 * The titles.
25
	 */
26
	private List<String> titles = new ArrayList<>();
27

    
28
	/**
29
	 * The authors.
30
	 */
31
	private List<String> authors = new ArrayList<>();
32

    
33
	/**
34
	 * The type.
35
	 */
36
	private ObjectType type;
37

    
38
	/**
39
	 * Description of the resolved object
40
	 */
41
	private String description;
42

    
43

    
44
	private List<DLIObjectProvenance> datasourceProvenanceDLI;
45

    
46

    
47
	/**
48
	 * The completion status.
49
	 */
50
	private String completionStatus;
51

    
52
	/**
53
	 * The date.
54
	 */
55
	private String date;
56

    
57
	/**
58
	 * A list of subjects of type scheme term
59
	 */
60
	private List<SubjectType> subjects;
61

    
62
	/**
63
	 * The related objects.
64
	 */
65
	private List<ObjectRelation> relations;
66

    
67

    
68
    /**
69
     * The Date where the record was resolved
70
     */
71
    private String resolvedDate;
72

    
73
	/**
74
	 * Instantiates a new DLI object.
75
	 */
76

    
77
	private static final DLIResolvedObjectDeserializer deserializer = new DLIResolvedObjectDeserializer();
78

    
79
	public static DLIResolvedObject fromJson(final String json) {
80
		try {
81
			return new GsonBuilder().registerTypeAdapter(DLIResolvedObject.class,deserializer ).create().fromJson(json, DLIResolvedObject.class);
82
		} catch (Throwable e) {
83
			log.error("ERROR on parsing "+json);
84
			return null;
85
		}
86
	}
87

    
88
	public DLIResolvedObject() {
89

    
90
	}
91

    
92
	/**
93
	 * Instantiates a new DLI object.
94
	 *
95
	 * @param pid                  the pid
96
	 * @param pidType              the pid type
97
	 * @param titles               the titles
98
	 * @param authors              the authors
99
	 * @param type                 the type
100
	 * @param datasourceProvenance the datasource provenance
101
	 * @param relations            the relations
102
	 * @param status               the status
103
	 */
104
	public DLIResolvedObject(final String pid, final String pidType, final List<String> titles, final List<String> authors, final ObjectType type,
105
							 final List<ObjectProvenance> datasourceProvenance, final List<ObjectRelation> relations, final String status) {
106
		super();
107
		if (pid != null) {
108
			this.pid = pid.replace("http://dx.doi.org/", "").replace("http://doi.org/", "").toLowerCase();
109
		}
110
		this.pidType = pidType;
111
		this.titles = titles;
112
		this.authors = authors;
113
		this.type = type;
114
		this.setDatasourceProvenance(datasourceProvenance);
115
		this.relations = relations;
116
		this.setCompletionStatus(status);
117
	}
118

    
119
	/**
120
	 * Escaped array.
121
	 *
122
	 * @param inputArray the input array
123
	 * @return the string[]
124
	 */
125
	public static List<String> escapedArray(final List<String> inputArray) {
126
		if (inputArray == null || inputArray.size() == 0) return null;
127
		return inputArray.stream().map(input -> input.replaceAll("&", "&amp;")
128
				.replaceAll("\"", "&quot;")
129
				.replaceAll("'", "&apos;")
130
				.replaceAll("<", "&lt;")
131
				.replaceAll(">", "&gt;"))
132
				.collect(Collectors.toList());
133
	}
134

    
135
	/**
136
	 * Gets the doi.
137
	 *
138
	 * @return the doi
139
	 */
140
	public String getPid() {
141
		if (pid != null)
142
			return pid.toLowerCase();
143
		return pid;
144
	}
145

    
146
	/**
147
	 * Sets the doi.
148
	 *
149
	 * @param pid the new pid
150
	 */
151
	@Override
152
	public DLIResolvedObject setPid(final String pid) {
153
		if (pid != null) {
154
			this.pid = pid.replace("http://dx.doi.org/", "").replace("http://doi.org/", "");
155
		}
156
		return this;
157
	}
158

    
159
	/**
160
	 * Check not null fields.
161
	 *
162
	 * @return the string
163
	 */
164
	private String checkNotNullFields() {
165

    
166
		final List<String> notNUllFields = Lists.newArrayList();
167
		if (!StringUtils.isEmpty(pid)) {
168
			notNUllFields.add("pid");
169
		}
170
		if (!StringUtils.isEmpty(pidType)) {
171
			notNUllFields.add("pidType");
172
		}
173
		if (titles == null || titles.size() == 0) {
174
			notNUllFields.add("titles");
175
		}
176
		if (authors == null || authors.size() == 0) {
177
			notNUllFields.add("authors");
178
		}
179
		if (!StringUtils.isEmpty(date)) {
180
			notNUllFields.add("date");
181
		}
182
		return new Gson().toJson(notNUllFields);
183
	}
184

    
185
	/**
186
	 * Fix contribution.
187
	 *
188
	 * @param provenance the provenance
189
	 */
190
	public void fixContribution(final DLIObjectProvenance provenance) {
191
		provenance.setDatasourceContribution(checkNotNullFields());
192
	}
193

    
194
	public String getResolvedPIDUrl() {
195
		if (pidType == null || StringUtils.isBlank(pidType) || pid == null || StringUtils.isBlank(pid))
196
			return "#";
197

    
198
		if (DLIUtils.resolvedTypes.containsKey(pidType.toLowerCase()))
199
			return String.format(DLIUtils.resolvedTypes.get(pidType.toLowerCase()), getEscapedXMLPid());
200

    
201
		return "#";
202
	}
203

    
204
	/**
205
	 * Gets the escaped xml pid.
206
	 *
207
	 * @return the escaped xml pid
208
	 */
209
	public String getEscapedXMLPid() {
210
		return StringEscapeUtils.escapeXml11(pid);
211
	}
212

    
213
	/**
214
	 * Gets the titles.
215
	 *
216
	 * @return the titles
217
	 */
218
	public List<String> getTitles() {
219
		return titles;
220
	}
221

    
222
	/**
223
	 * Sets the titles.
224
	 *
225
	 * @param titles the new titles
226
	 */
227
	public void setTitles(final List<String> titles) {
228
		this.titles = titles;
229
	}
230

    
231
	/**
232
	 * Gets the escaped xml titles.
233
	 *
234
	 * @return the escaped xml titles
235
	 */
236
	public List<String> getEscapedXMLTitles() {
237

    
238
		return escapedArray(titles);
239
	}
240

    
241
	/**
242
	 * Gets the authors.
243
	 *
244
	 * @return the authors
245
	 */
246
	public List<String> getAuthors() {
247
		return authors;
248
	}
249

    
250
	/**
251
	 * Sets the authors.
252
	 *
253
	 * @param authors the new authors
254
	 */
255
	public void setAuthors(final List<String> authors) {
256
		this.authors = authors;
257
	}
258

    
259
	/**
260
	 * Gets the escaped xml authors.
261
	 *
262
	 * @return the escaped xml authors
263
	 */
264
	public List<String> getEscapedXMLAuthors() {
265
		return escapedArray(authors);
266
	}
267

    
268
	/**
269
	 * get All the subjects
270
	 *
271
	 * @return a list of Subjects
272
	 */
273
	public List<SubjectType> getSubjects() {
274
		return subjects;
275
	}
276

    
277
	/**
278
	 * Set all the subjects
279
	 *
280
	 * @param subjects
281
	 */
282
	@Override
283
	public ResolvedObject setSubjects(final List<SubjectType> subjects) {
284
		this.subjects = subjects;
285
		return this;
286
	}
287

    
288
	/**
289
	 * Gets the type.
290
	 *
291
	 * @return the type
292
	 */
293
	public ObjectType getType() {
294
		return type == null ? ObjectType.unknown : type;
295
	}
296

    
297
	/**
298
	 * Sets the type.
299
	 *
300
	 * @param type the new type
301
	 */
302
	public DLIResolvedObject setType(final ObjectType type) {
303
		this.type = type;
304
		return this;
305
	}
306

    
307
	/**
308
	 * Gets the escaped xml type.
309
	 *
310
	 * @return the escaped xml type
311
	 */
312
	public String getEscapedXMLType() {
313
		if (type != null)
314
			return type.toString();
315
		else return null;
316
	}
317

    
318

    
319
	/**
320
	 * Gets the relations.
321
	 *
322
	 * @return the relations
323
	 */
324
	public List<ObjectRelation> getRelations() {
325
		return relations;
326
	}
327

    
328
	/**
329
	 * Sets the relations.
330
	 *
331
	 * @param relations the new relations
332
	 */
333
	public void setRelations(final List<ObjectRelation> relations) {
334
		this.relations = relations;
335
	}
336

    
337
	/**
338
	 * Gets the date.
339
	 *
340
	 * @return the date
341
	 */
342
	public String getDate() {
343
		return date;
344
	}
345

    
346
	/**
347
	 * Sets the date.
348
	 *
349
	 * @param date the date to set
350
	 */
351
	public void setDate(final String date) {
352
		this.date = date;
353
	}
354

    
355
	/**
356
	 * Gets the pid type.
357
	 *
358
	 * @return the pidType
359
	 */
360
	@Override
361
	public String getPidType() {
362
		return pidType;
363
	}
364

    
365
	/**
366
	 * Sets the pid type.
367
	 *
368
	 * @param pidType the pidType to set
369
	 */
370
	@Override
371
	public DLIResolvedObject setPidType(final String pidType) {
372
		this.pidType = pidType;
373
		return this;
374
	}
375

    
376
	/**
377
	 * Gets the completion status.
378
	 *
379
	 * @return the completion status
380
	 */
381
	public String getCompletionStatus() {
382
		return this.completionStatus;
383
	}
384

    
385
	/**
386
	 * Sets the completion status.
387
	 *
388
	 * @param completionStatus the new completion status
389
	 */
390
	public void setCompletionStatus(final String completionStatus) {
391
		this.completionStatus = completionStatus;
392
	}
393

    
394

    
395
	/**
396
	 * {@inheritDoc}
397
	 *
398
	 * @see Object#toString()
399
	 */
400
	@Override
401
    public String toString() {
402
		return new GsonBuilder().setPrettyPrinting().create().toJson(this);
403
	}
404

    
405
    public String getResolvedDate() {
406
        return resolvedDate;
407
    }
408

    
409
    public void setResolvedDate(String resolvedDate) {
410
        this.resolvedDate = resolvedDate;
411
    }
412

    
413
	public String getDescription() {
414
		return description;
415
	}
416

    
417
	public void setDescription(final String description) {
418
		this.description = description;
419
	}
420

    
421
	public String getEscapedDescription() {
422
		return StringEscapeUtils.escapeXml11(description);
423
	}
424

    
425
//	public List<DLIObjectProvenance> getDatasourceProvenanceDLI() {
426
//		return datasourceProvenance.stream().map(it-> (DLIObjectProvenance) it).collect(Collectors.toList());
427
//	}
428

    
429
	public List<DLIObjectProvenance> getDliDatasourceProvenance() {
430
		return datasourceProvenance.stream().map(it-> (DLIObjectProvenance) it).collect(Collectors.toList());
431
	}
432

    
433

    
434
}
(4-4/7)