Project

General

Profile

1
package eu.dnetlib.data.transform.xml;
2

    
3
import java.lang.reflect.Type;
4
import java.util.List;
5

    
6
import com.google.gson.Gson;
7
import com.google.gson.reflect.TypeToken;
8
import org.apache.commons.lang.StringUtils;
9

    
10

    
11
/**
12
 * * <xsl:stylesheet ... xmlns:openTrials="eu.dnetlib.miscutils.functional.xml.OpenTrialsFunctions"> ... </xsl:stylesheet>
13
 * Created by alessia on 13/05/16.
14
 */
15
public class OpenTrialsXsltFunctions {
16

    
17
	/**
18
	 * Parses a json to get the first url item.
19
	 *
20
	 * @param jsonProvList A json string in the following format: [{"url" : "theUrl", "sourceID" : "theSourceId", "sourceName" : "theSourceName"}]
21
	 * @return the url value in the first object in the list
22
	 */
23
	@Deprecated
24
	public static String getMainIdentifierURL(String jsonProvList) {
25
		List<JsonProv> provs = getProvs(jsonProvList);
26
		for (JsonProv prov : provs) {
27
			if (StringUtils.isNotBlank(prov.getUrl())) return prov.getUrl();
28
		}
29
		return "";
30
	}
31

    
32
	/**
33
	 * Parses a Json to get the url item associated to the primary record if any
34
	 * @param jsonRecordsList A json string in the format [{"source_id","theSourceId","source_url","theUrlOfTheTrialForThisSource", "is_primary","typeOfTheTrial")}]
35
	 * @return the url value of the primary record
36
	 */
37
	public static String getPrimaryRecordUrl(String jsonRecordsList){
38
		List<JsonRecord> records = getRecords(jsonRecordsList);
39
		for (JsonRecord record : records) {
40
			if(record.isIs_primary() && StringUtils.isNotBlank(record.getSource_url()))
41
				return record.getSource_url();
42
		}
43
		if (!records.isEmpty() && StringUtils.isNotBlank(records.get(0).getSource_url())){
44
			return records.get(0).getSource_url();
45
		}
46
		return "";
47
	}
48

    
49
	/**
50
	 * Parses a Json to get the id of the source associated to the primary record if any
51
	 * @param jsonRecordsList A json string in the format [{"source_id","theSourceId","source_url","theUrlOfTheTrialForThisSource", "is_primary","typeOfTheTrial")}]
52
	 * @return the id of the source of the primary record
53
	 */
54
	public static String getPrimaryRecordIdentifier(String jsonRecordsList){
55
		List<JsonRecord> records = getRecords(jsonRecordsList);
56
		for (JsonRecord record : records) {
57
			if(record.isIs_primary() && StringUtils.isNotBlank(record.getSource_id()))
58
				return record.getSource_id();
59
		}
60
		if (!records.isEmpty() && StringUtils.isNotBlank(records.get(0).getSource_url())){
61
			return records.get(0).getSource_id();
62
		}
63
		return "";
64
	}
65

    
66
	/**
67
	 * Parses a Json to get the list of the not primary url associated to the trial if any
68
	 * @param jsonRecordsList A json string in the format [{"source_id","theSourceId","source_url","theUrlOfTheTrialForThisSource", "is_primary","typeOfTheTrial")}]
69
	 * @return a string containing the not primary source urls divided by "@@"
70
	 */
71
	public static String getNotPrimaryRecordUrl(String jsonRecordsList){
72
		boolean found = false;
73
		String ret = "";
74
		List<JsonRecord> records = getRecords(jsonRecordsList);
75
		for (JsonRecord record : records) {
76
			if(record.isIs_primary())
77
				found = true;
78
			else
79
			if(StringUtils.isNotBlank(record.getSource_url()))
80
				ret += record.getSource_url() + "@@";
81
		}
82
		if (!found && ret.length() > 0){
83
			ret = ret.substring(ret.indexOf("@@")+2);
84
		}
85
		if(ret.endsWith("@@"))
86
			ret = ret.substring(0,ret.length()-2);
87

    
88
		return ret;
89
	}
90

    
91
	/**
92
	 * Parses a Json to get the names of the principal investigators of the trial if any
93
	 * @param jsonPeopleList A json string in the format [{"person_name", "thePersonName", "person_id","thePersonId", "person_role",thePersonRole)}]
94
	 * @return a string containing the names of the principal investigators divided by @@
95
	 */
96
	public static String getPrincipalInvestigators(String jsonPeopleList){
97
		List<JsonPeople> people = getPeople(jsonPeopleList);
98
		String ret ="";
99
		for (JsonPeople person : people) {
100
			if(StringUtils.isNotBlank(person.getPerson_role()) && (person.getPerson_role().equals("principal_investigator"))){
101
				ret += conformToGuidelines(person.getPerson_name()) + "@@";
102
			}
103

    
104
		}
105
		if(ret.endsWith("@@")){
106
			ret=ret.substring(0,ret.length()-2);
107
		}
108
		return ret;
109
	}
110

    
111
	private static String conformToGuidelines(String person_name) {
112

    
113
		if (person_name.indexOf(",") > -1)
114
			person_name = person_name.substring(0,person_name.indexOf(","));
115
		String[] name = person_name.split(" ");
116
		person_name = name[name.length-1] + ",";
117
		for(int i=0;i<name.length-1; i++)
118
			person_name += " " + name[i];
119

    
120
		return person_name;
121
	}
122

    
123
	/**
124
	 * Parses a Json to get the names of the organizations supporting the trial if any
125
	 * @param jsonOrganizationList A json string in the format [{"organization_name", "theOrganizationName", "organization_id","theOrganizationId", "organization_role",theOrganizationRole)}]
126
	 * @return a string containing the names and roles of the organizations
127
	 */
128
	public static String getTrialOrganizations(String jsonOrganizationList){
129
		List<JsonOrganization> organizations = getOrganizations(jsonOrganizationList);
130
		String ret ="";
131
		for (JsonOrganization o : organizations) {
132
			if (StringUtils.isNotBlank(o.getOrganization_id())) {
133
				ret += o.getOrganization_name() + "@";
134
				if (o.getOrganization_role().equalsIgnoreCase("primary_sponsor"))
135
					ret += "sponsor@@";
136
				else
137
					ret += o.getOrganization_role() + "@@";
138
			}
139
		}
140
		if(ret.endsWith("@@")){
141
			ret=ret.substring(0,ret.length()-2);
142
		}
143
		return ret;
144
	}
145

    
146
	/**
147
	 * Parses a Json to get the names of the locations where the trial take place if any
148
	 * @param jsonLocationsList A json string in the format [{"location_name","theLocationName"}]
149
	 * @return a string containing the locations associated to the trial divided by "@@"
150
	 */
151
	public static String getTrialLocations(String jsonLocationsList){
152
		List<JsonLocation> locations = getLocations(jsonLocationsList);
153
		String ret ="";
154
		for (JsonLocation l : locations) {
155
			if (StringUtils.isNotBlank(l.getLocation_name()))
156
				ret += l.getLocation_name() + "@@";
157

    
158
		}
159
		if(ret.endsWith("@@")){
160
			ret=ret.substring(0,ret.length()-2);
161
		}
162
		return ret;
163
	}
164

    
165
	@Deprecated
166
	public static List<JsonProv> getProvs(String jsonProvList) {
167
		Gson gson = new Gson();
168
		Type type = new TypeToken<List<JsonProv>>() {
169
		}.getType();
170
		return gson.fromJson(jsonProvList, type);
171
	}
172

    
173
	public static List<JsonRecord> getRecords(String jsonRecordsList) {
174
		Gson gson = new Gson();
175
		Type type = new TypeToken<List<JsonRecord>>() {
176
		}.getType();
177
		return gson.fromJson(jsonRecordsList, type);
178
	}
179

    
180

    
181
	public static List<JsonPeople> getPeople(String jsonPeopleList) {
182
		Gson gson = new Gson();
183
		Type type = new TypeToken<List<JsonPeople>>() {
184
		}.getType();
185
		return gson.fromJson(jsonPeopleList, type);
186
	}
187

    
188
	public static List<JsonOrganization> getOrganizations(String jsonOrganizationsList) {
189
		Gson gson = new Gson();
190
		Type type = new TypeToken<List<JsonOrganization>>() {
191
		}.getType();
192
		return gson.fromJson(jsonOrganizationsList, type);
193
	}
194

    
195
	public static List<JsonLocation> getLocations(String jsonLocationsList) {
196
		Gson gson = new Gson();
197
		Type type = new TypeToken<List<JsonLocation>>() {
198
		}.getType();
199
		return gson.fromJson(jsonLocationsList, type);
200
	}
201

    
202
	@Deprecated
203
	static class JsonProv {
204

    
205
		String url, sourceId, sourceName;
206

    
207
		public String getUrl() {
208
			return url;
209
		}
210

    
211
		public void setUrl(final String url) {
212
			this.url = url;
213
		}
214

    
215
		public String getSourceId() {
216
			return sourceId;
217
		}
218

    
219
		public void setSourceId(final String sourceId) {
220
			this.sourceId = sourceId;
221
		}
222

    
223
		public String getSourceName() {
224
			return sourceName;
225
		}
226

    
227
		public void setSourceName(final String sourceName) {
228
			this.sourceName = sourceName;
229
		}
230
	}
231

    
232
	static class JsonRecord{
233
		String source_id, source_url;
234
		boolean is_primary;
235

    
236
		public String getSource_id() {
237
			return source_id;
238
		}
239

    
240
		public String getSource_url() {
241
			return source_url;
242
		}
243

    
244
		public void setSource_id(String source_id) {
245
			this.source_id = source_id;
246
		}
247

    
248
		public void setSource_url(String source_url) {
249
			this.source_url = source_url;
250
		}
251

    
252
		public boolean isIs_primary() {
253
			return is_primary;
254
		}
255

    
256
		public void setIs_primary(final boolean is_primary) {
257
			this.is_primary = is_primary;
258
		}
259
	}
260

    
261
	static class JsonPeople{
262
		String person_name, person_id, person_role;
263

    
264
		public String getPerson_name() {
265
			return person_name;
266
		}
267

    
268
		public void setPerson_name(String person_name) {
269
			this.person_name = person_name;
270
		}
271

    
272
		public String getPerson_id() {
273
			return person_id;
274
		}
275

    
276
		public void setPerson_id(String person_id) {
277
			this.person_id = person_id;
278
		}
279

    
280
		public String getPerson_role() {
281
			return person_role;
282
		}
283

    
284
		public void setPerson_role(String person_role) {
285
			this.person_role = person_role;
286
		}
287
	}
288

    
289
	static class JsonOrganization{
290
		String organization_name, organization_id, organization_role;
291

    
292
		public String getOrganization_name() {
293
			return organization_name;
294
		}
295

    
296
		public void setOrganization_name(String organization_name) {
297
			this.organization_name = organization_name;
298
		}
299

    
300
		public String getOrganization_id() {
301
			return organization_id;
302
		}
303

    
304
		public void setOrganization_id(String organization_id) {
305
			this.organization_id = organization_id;
306
		}
307

    
308
		public String getOrganization_role() {
309
			return organization_role;
310
		}
311

    
312
		public void setOrganization_role(String organization_role) {
313
			this.organization_role = organization_role;
314
		}
315
	}
316

    
317
	static class JsonLocation{
318
		String location_name;
319

    
320
		public String getLocation_name() {
321
			return location_name;
322
		}
323

    
324
		public void setLocation_name(String location_name) {
325
			this.location_name = location_name;
326
		}
327
	}
328
}
(9-9/10)