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

    
11
import java.util.ArrayList;
12
import java.util.List;
13
import java.util.stream.Collectors;
14

    
15
/**
16
 * The Class DLIObject.
17
 */
18
public class DLIResolvedObject extends AbstractResolvedObject {
19

    
20
	/**
21
	 * The titles.
22
	 */
23
	private List<String> titles = new ArrayList<>();
24

    
25
	/**
26
	 * The authors.
27
	 */
28
	private List<String> authors = new ArrayList<>();
29

    
30
	/**
31
	 * The type.
32
	 */
33
	private ObjectType type;
34

    
35
	/**
36
	 * Description of the resolved object
37
	 */
38
	private String description;
39

    
40

    
41
	/**
42
	 * The completion status.
43
	 */
44
	private String completionStatus;
45

    
46
	/**
47
	 * The date.
48
	 */
49
	private String date;
50

    
51
	/**
52
	 * A list of subjects of type scheme term
53
	 */
54
	private List<SubjectType> subjects;
55

    
56
	/**
57
	 * The related objects.
58
	 */
59
	private List<ObjectRelation> relations;
60

    
61

    
62
    /**
63
     * The Date where the record was resolved
64
     */
65
    private String resolvedDate;
66

    
67
	/**
68
	 * Instantiates a new DLI object.
69
	 */
70
	public DLIResolvedObject() {
71

    
72
	}
73

    
74
	/**
75
	 * Instantiates a new DLI object.
76
	 *
77
	 * @param pid                  the pid
78
	 * @param pidType              the pid type
79
	 * @param titles               the titles
80
	 * @param authors              the authors
81
	 * @param type                 the type
82
	 * @param datasourceProvenance the datasource provenance
83
	 * @param relations            the relations
84
	 * @param status               the status
85
	 */
86
	public DLIResolvedObject(final String pid, final String pidType, final List<String> titles, final List<String> authors, final ObjectType type,
87
							 final List<ObjectProvenance> datasourceProvenance, final List<ObjectRelation> relations, final String status) {
88
		super();
89
		if (pid != null) {
90
			this.pid = pid.replace("http://dx.doi.org/", "").replace("http://doi.org/", "").toLowerCase();
91
		}
92
		this.pidType = pidType;
93
		this.titles = titles;
94
		this.authors = authors;
95
		this.type = type;
96
		this.setDatasourceProvenance(datasourceProvenance);
97
		this.relations = relations;
98
		this.setCompletionStatus(status);
99
	}
100

    
101
	/**
102
	 * Escaped array.
103
	 *
104
	 * @param inputArray the input array
105
	 * @return the string[]
106
	 */
107
	public static List<String> escapedArray(final List<String> inputArray) {
108
		if (inputArray == null || inputArray.size() == 0) return null;
109
		return inputArray.stream().map(input -> input.replaceAll("&", "&amp;")
110
				.replaceAll("\"", "&quot;")
111
				.replaceAll("'", "&apos;")
112
				.replaceAll("<", "&lt;")
113
				.replaceAll(">", "&gt;"))
114
				.collect(Collectors.toList());
115
	}
116

    
117
	/**
118
	 * Gets the doi.
119
	 *
120
	 * @return the doi
121
	 */
122
	public String getPid() {
123
		if (pid != null)
124
			return pid.toLowerCase();
125
		return pid;
126
	}
127

    
128
	/**
129
	 * Sets the doi.
130
	 *
131
	 * @param pid the new pid
132
	 */
133
	@Override
134
	public DLIResolvedObject setPid(final String pid) {
135
		if (pid != null) {
136
			this.pid = pid.replace("http://dx.doi.org/", "").replace("http://doi.org/", "");
137
		}
138
		return this;
139
	}
140

    
141
	/**
142
	 * Check not null fields.
143
	 *
144
	 * @return the string
145
	 */
146
	private String checkNotNullFields() {
147

    
148
		final List<String> notNUllFields = Lists.newArrayList();
149
		if (!StringUtils.isEmpty(pid)) {
150
			notNUllFields.add("pid");
151
		}
152
		if (!StringUtils.isEmpty(pidType)) {
153
			notNUllFields.add("pidType");
154
		}
155
		if (titles == null || titles.size() == 0) {
156
			notNUllFields.add("titles");
157
		}
158
		if (authors == null || authors.size() == 0) {
159
			notNUllFields.add("authors");
160
		}
161
		if (!StringUtils.isEmpty(date)) {
162
			notNUllFields.add("date");
163
		}
164
		return new Gson().toJson(notNUllFields);
165
	}
166

    
167
	/**
168
	 * Fix contribution.
169
	 *
170
	 * @param provenance the provenance
171
	 */
172
	public void fixContribution(final DLIObjectProvenance provenance) {
173
		provenance.setDatasourceContribution(checkNotNullFields());
174
	}
175

    
176
	public String getResolvedPIDUrl() {
177
		if (pidType == null || StringUtils.isBlank(pidType) || pid == null || StringUtils.isBlank(pid))
178
			return "#";
179

    
180
		if (DLIUtils.resolvedTypes.containsKey(pidType.toLowerCase()))
181
			return String.format(DLIUtils.resolvedTypes.get(pidType.toLowerCase()), getEscapedXMLPid());
182

    
183
		return "#";
184
	}
185

    
186
	/**
187
	 * Gets the escaped xml pid.
188
	 *
189
	 * @return the escaped xml pid
190
	 */
191
	public String getEscapedXMLPid() {
192
		return StringEscapeUtils.escapeXml11(pid);
193
	}
194

    
195
	/**
196
	 * Gets the titles.
197
	 *
198
	 * @return the titles
199
	 */
200
	public List<String> getTitles() {
201
		return titles;
202
	}
203

    
204
	/**
205
	 * Sets the titles.
206
	 *
207
	 * @param titles the new titles
208
	 */
209
	public void setTitles(final List<String> titles) {
210
		this.titles = titles;
211
	}
212

    
213
	/**
214
	 * Gets the escaped xml titles.
215
	 *
216
	 * @return the escaped xml titles
217
	 */
218
	public List<String> getEscapedXMLTitles() {
219

    
220
		return escapedArray(titles);
221
	}
222

    
223
	/**
224
	 * Gets the authors.
225
	 *
226
	 * @return the authors
227
	 */
228
	public List<String> getAuthors() {
229
		return authors;
230
	}
231

    
232
	/**
233
	 * Sets the authors.
234
	 *
235
	 * @param authors the new authors
236
	 */
237
	public void setAuthors(final List<String> authors) {
238
		this.authors = authors;
239
	}
240

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

    
250
	/**
251
	 * get All the subjects
252
	 *
253
	 * @return a list of Subjects
254
	 */
255
	public List<SubjectType> getSubjects() {
256
		return subjects;
257
	}
258

    
259
	/**
260
	 * Set all the subjects
261
	 *
262
	 * @param subjects
263
	 */
264
	@Override
265
	public ResolvedObject setSubjects(final List<SubjectType> subjects) {
266
		this.subjects = subjects;
267
		return this;
268
	}
269

    
270
	/**
271
	 * Gets the type.
272
	 *
273
	 * @return the type
274
	 */
275
	public ObjectType getType() {
276
		return type == null ? ObjectType.unknown : type;
277
	}
278

    
279
	/**
280
	 * Sets the type.
281
	 *
282
	 * @param type the new type
283
	 */
284
	public DLIResolvedObject setType(final ObjectType type) {
285
		this.type = type;
286
		return this;
287
	}
288

    
289
	/**
290
	 * Gets the escaped xml type.
291
	 *
292
	 * @return the escaped xml type
293
	 */
294
	public String getEscapedXMLType() {
295
		if (type != null)
296
			return type.toString();
297
		else return null;
298
	}
299

    
300

    
301
	/**
302
	 * Gets the relations.
303
	 *
304
	 * @return the relations
305
	 */
306
	public List<ObjectRelation> getRelations() {
307
		return relations;
308
	}
309

    
310
	/**
311
	 * Sets the relations.
312
	 *
313
	 * @param relations the new relations
314
	 */
315
	public void setRelations(final List<ObjectRelation> relations) {
316
		this.relations = relations;
317
	}
318

    
319
	/**
320
	 * Gets the date.
321
	 *
322
	 * @return the date
323
	 */
324
	public String getDate() {
325
		return date;
326
	}
327

    
328
	/**
329
	 * Sets the date.
330
	 *
331
	 * @param date the date to set
332
	 */
333
	public void setDate(final String date) {
334
		this.date = date;
335
	}
336

    
337
	/**
338
	 * Gets the pid type.
339
	 *
340
	 * @return the pidType
341
	 */
342
	@Override
343
	public String getPidType() {
344
		return pidType;
345
	}
346

    
347
	/**
348
	 * Sets the pid type.
349
	 *
350
	 * @param pidType the pidType to set
351
	 */
352
	@Override
353
	public DLIResolvedObject setPidType(final String pidType) {
354
		this.pidType = pidType;
355
		return this;
356
	}
357

    
358
	/**
359
	 * Gets the completion status.
360
	 *
361
	 * @return the completion status
362
	 */
363
	public String getCompletionStatus() {
364
		return this.completionStatus;
365
	}
366

    
367
	/**
368
	 * Sets the completion status.
369
	 *
370
	 * @param completionStatus the new completion status
371
	 */
372
	public void setCompletionStatus(final String completionStatus) {
373
		this.completionStatus = completionStatus;
374
	}
375

    
376

    
377
	/**
378
	 * {@inheritDoc}
379
	 *
380
	 * @see Object#toString()
381
	 */
382
	@Override
383
    public String toString() {
384
		return new GsonBuilder().setPrettyPrinting().create().toJson(this);
385
	}
386

    
387
    public String getResolvedDate() {
388
        return resolvedDate;
389
    }
390

    
391
    public void setResolvedDate(String resolvedDate) {
392
        this.resolvedDate = resolvedDate;
393
    }
394

    
395
	public String getDescription() {
396
		return description;
397
	}
398

    
399
	public void setDescription(final String description) {
400
		this.description = description;
401
	}
402

    
403
	public String getEscapedDescription() {
404
		return StringEscapeUtils.escapeXml11(description);
405
	}
406
}
(4-4/6)