Revision 46876
Added by Claudio Atzori almost 7 years ago
DatasourceFunctions.java | ||
---|---|---|
1 | 1 |
package eu.dnetlib.datasource.publisher.clients.utils; |
2 | 2 |
|
3 |
import java.sql.Array; |
|
4 |
import java.sql.ResultSet; |
|
5 |
import java.sql.SQLException; |
|
6 |
import java.util.List; |
|
7 |
import java.util.Map; |
|
8 |
|
|
9 |
import com.google.common.base.Splitter; |
|
10 |
import com.google.common.collect.Lists; |
|
11 |
import com.google.common.collect.Maps; |
|
12 |
import eu.dnetlib.miscutils.datetime.DateUtils; |
|
13 |
import eu.dnetlib.datasource.publisher.model.Api; |
|
14 |
import eu.dnetlib.datasource.publisher.model.Datasource; |
|
15 |
import org.apache.commons.lang.StringEscapeUtils; |
|
16 |
import org.apache.commons.lang.StringUtils; |
|
17 |
import org.apache.commons.lang.time.DateFormatUtils; |
|
18 |
|
|
19 | 3 |
import org.apache.commons.logging.Log; |
20 | 4 |
import org.apache.commons.logging.LogFactory; |
21 | 5 |
|
... | ... | |
29 | 13 |
|
30 | 14 |
public static final String DATE_FORMAT = "yyyy-MM-dd"; |
31 | 15 |
|
32 |
public static Datasource toDatasource(final ResultSet rs) throws SQLException { |
|
33 |
|
|
34 |
final Datasource ds = new Datasource(); |
|
35 |
|
|
36 |
ds.id(rs.getString("id")) |
|
37 |
.officialname(rs.getString("officialname")) |
|
38 |
.englishname(rs.getString("englishname")) |
|
39 |
.websiteurl(rs.getString("websiteurl")) |
|
40 |
.logourl(rs.getString("logourl")) |
|
41 |
.countrycode(rs.getString("countrycode")) |
|
42 |
.countryname(rs.getString("countryname")) |
|
43 |
.organization(rs.getString("organization")) |
|
44 |
.contactemail(rs.getString("contactemail")) |
|
45 |
.latitude(rs.getDouble("latitude")) |
|
46 |
.longitude(rs.getDouble("longitude")) |
|
47 |
.timezone(rs.getString("timezone")) |
|
48 |
.collectedfrom(rs.getString("collectedfrom")) |
|
49 |
.dateofvalidation(rs.getString("dateofvalidation")) |
|
50 |
.registeredby(rs.getString("registeredby")) |
|
51 |
.datasourceclass(rs.getString("datasourceclass")) |
|
52 |
.typology(rs.getString("typology")) |
|
53 |
.activationid(rs.getString("activationid")) |
|
54 |
.description(rs.getString("description")) |
|
55 |
.issn(rs.getString("issn")) |
|
56 |
.eissn(rs.getString("eissn")) |
|
57 |
.lissn(rs.getString("lissn")) |
|
58 |
.api(parseInfoPackage(rs)); |
|
59 |
|
|
60 |
return ds; |
|
61 |
} |
|
62 |
|
|
63 |
private static Api generateIfaceDesc(final String infopackage) { |
|
64 |
if (infopackage == null || infopackage.trim().isEmpty()) { return null; } |
|
65 |
|
|
66 |
final Api iface = new Api() |
|
67 |
.id(getValueBetween(infopackage, null, "<==1==>")) |
|
68 |
.typology(getValueBetween(infopackage, "<==1==>", "<==2==>")) |
|
69 |
.compliance(getValueBetween(infopackage, "<==2==>", "<==3==>")) |
|
70 |
.contentdescription(getValueBetween(infopackage, "<==3==>", "<==4==>")) |
|
71 |
.accessprotocol(getValueBetween(infopackage, "<==4==>", "<==5==>")) |
|
72 |
.active(Boolean.parseBoolean(getValueBetween(infopackage, "<==5==>", "<==6==>"))) |
|
73 |
.removable(Boolean.parseBoolean(getValueBetween(infopackage, "<==6==>", "<==7==>"))); |
|
74 |
|
|
75 |
final Map<String, String> accessParams = Maps.newHashMap(); |
|
76 |
final Map<String, String> extraParams = Maps.newHashMap(); |
|
77 |
|
|
78 |
for (String param : parseParams(getValueBetween(infopackage, "<==7==>", null))) { |
|
79 |
|
|
80 |
final String[] arr = param.split("###"); |
|
81 |
|
|
82 |
if (arr.length == 3) { |
|
83 |
final boolean accessParam = Boolean.parseBoolean(arr[0].trim()); |
|
84 |
final String paramName = arr[1].trim(); |
|
85 |
final String paramValue = arr.length > 2 ? arr[2].trim() : ""; |
|
86 |
|
|
87 |
if (accessParam) { |
|
88 |
if (paramName.equals(DatasourceParams.baseUrl.toString())) { |
|
89 |
iface.setBaseurl(paramValue); |
|
90 |
} else { |
|
91 |
accessParams.put(paramName, paramValue); |
|
92 |
} |
|
93 |
} else { |
|
94 |
if(paramName.endsWith(DATE_FIELDS_SUFFIX)) { |
|
95 |
extraParams.put(paramName, DateFormatUtils.format(new DateUtils().parse(paramValue), DATE_FORMAT)); |
|
96 |
} else if (!paramName.endsWith(MDID_SUFFIX) & !paramName.equals(DatasourceParams.metadata_identifier_path.name())) { |
|
97 |
extraParams.put(paramName, paramValue); |
|
98 |
} |
|
99 |
} |
|
100 |
} else { |
|
101 |
log.debug("Invalid Datasource Parameter"); |
|
102 |
} |
|
103 |
} |
|
104 |
|
|
105 |
return iface.accessParams(accessParams).extraFields(extraParams); |
|
106 |
} |
|
107 |
|
|
108 |
@SuppressWarnings("unchecked") |
|
109 |
private static List<Api> parseInfoPackage(final ResultSet rs) throws SQLException { |
|
110 |
final List<Api> res = Lists.newArrayList(); |
|
111 |
final Array array = rs.getArray("accessinfopackage"); |
|
112 |
if (array != null) { |
|
113 |
final String[] sa = (String[]) array.getArray(); |
|
114 |
for(final String s : sa) { |
|
115 |
final Api api = generateIfaceDesc(s); |
|
116 |
if (api != null) { |
|
117 |
res.add(api); |
|
118 |
} |
|
119 |
} |
|
120 |
} |
|
121 |
return res; |
|
122 |
} |
|
123 |
|
|
124 |
private static String getValueBetween(final String s, final String pre, final String post) { |
|
125 |
if (pre == null && post == null) { |
|
126 |
return StringEscapeUtils.escapeXml(s); |
|
127 |
} else if (pre == null) { |
|
128 |
return StringEscapeUtils.escapeXml(StringUtils.substringBefore(s, post)); |
|
129 |
} else if (post == null) { |
|
130 |
return StringEscapeUtils.escapeXml(StringUtils.substringAfter(s, pre)); |
|
131 |
} else { |
|
132 |
return StringEscapeUtils.escapeXml(StringUtils.substringBetween(s, pre, post)); |
|
133 |
} |
|
134 |
} |
|
135 |
|
|
136 |
private static Iterable<String> parseParams(final String s) { |
|
137 |
return Splitter.on("@@@").omitEmptyStrings().trimResults().split(s); |
|
138 |
} |
|
139 |
|
|
140 | 16 |
} |
Also available in: Unified diff
cleanup, added method to search among apis