1
|
package eu.dnetlib.datasource.publisher.model;
|
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.datasource.publisher.DatasourceException;
|
13
|
import eu.dnetlib.miscutils.datetime.DateUtils;
|
14
|
import org.apache.commons.lang.StringEscapeUtils;
|
15
|
import org.apache.commons.lang.StringUtils;
|
16
|
import org.apache.commons.lang.time.DateFormatUtils;
|
17
|
|
18
|
import org.apache.commons.logging.Log;
|
19
|
import org.apache.commons.logging.LogFactory;
|
20
|
|
21
|
public class DatasourceFunctions {
|
22
|
|
23
|
private static final Log log = LogFactory.getLog(DatasourceFunctions.class); // NOPMD by marko on 11/24/08 5:02 PM
|
24
|
|
25
|
private static final String DATE_FIELDS_SUFFIX = "_date";
|
26
|
|
27
|
public static final String DATE_FORMAT = "yyyy-MM-dd";
|
28
|
|
29
|
public static Datasource toDatasource(final ResultSet rs) throws DatasourceException, SQLException {
|
30
|
|
31
|
final Datasource ds = new Datasource();
|
32
|
|
33
|
ds.setId(rs.getString("id"));
|
34
|
ds.setOfficialName(rs.getString("officialname"));
|
35
|
ds.setEnglishName(rs.getString("englishname"));
|
36
|
ds.setWebsiteUrl(rs.getString("websiteurl"));
|
37
|
ds.setLogoUrl(rs.getString("logourl"));
|
38
|
ds.setCountryCode(rs.getString("countrycode"));
|
39
|
ds.setCountryName(rs.getString("countryname"));
|
40
|
ds.setOrganization(rs.getString("organization"));
|
41
|
ds.setContactEmail(rs.getString("contactemail"));
|
42
|
ds.setLatitude(rs.getDouble("latitude"));
|
43
|
ds.setLongitude(rs.getDouble("longitude"));
|
44
|
ds.setTimezone(rs.getDouble("timezone"));
|
45
|
ds.setNamespacePrefix(rs.getString("namespaceprefix"));
|
46
|
ds.setCollectedFrom(rs.getString("collectedfrom"));
|
47
|
ds.setDateOfValidation(rs.getDate("dateofvalidation"));
|
48
|
ds.setRegisteredBy(rs.getString("registeredby"));
|
49
|
ds.setDatasourceClass(rs.getString("datasourceclass"));
|
50
|
ds.setProvenanceActionClass(rs.getString("provenanceactionclass"));
|
51
|
ds.setDateOfCollection(rs.getDate("dateofcollection"));
|
52
|
ds.setTypology(rs.getString("typology"));
|
53
|
ds.setActivationId(rs.getString("activationid"));
|
54
|
ds.setDescription(rs.getString("description"));
|
55
|
ds.setMissionStatementUrl(rs.getString("missionstatementurl"));
|
56
|
ds.setAggregator(rs.getString("aggregator"));
|
57
|
ds.setIssn(rs.getString("issn"));
|
58
|
ds.setEissn(rs.getString("eissn"));
|
59
|
ds.setLissn(rs.getString("lissn"));
|
60
|
ds.setApi(parseInfoPackage(rs));
|
61
|
|
62
|
return ds;
|
63
|
}
|
64
|
|
65
|
private static Api generateIfaceDesc(final String infopackage) {
|
66
|
if (infopackage == null || infopackage.trim().isEmpty()) { return null; }
|
67
|
|
68
|
final Api iface = new Api();
|
69
|
iface.setId(getValueBetween(infopackage, null, "<==1==>"));
|
70
|
iface.setTypology(getValueBetween(infopackage, "<==1==>", "<==2==>"));
|
71
|
iface.setCompliance(getValueBetween(infopackage, "<==2==>", "<==3==>"));
|
72
|
iface.setContentDescription(getValueBetween(infopackage, "<==3==>", "<==4==>"));
|
73
|
iface.setAccessProtocol(getValueBetween(infopackage, "<==4==>", "<==5==>"));
|
74
|
iface.setActive(Boolean.parseBoolean(getValueBetween(infopackage, "<==5==>", "<==6==>")));
|
75
|
iface.setRemovable(Boolean.parseBoolean(getValueBetween(infopackage, "<==6==>", "<==7==>")));
|
76
|
|
77
|
final Map<String, String> accessParams = Maps.newHashMap();
|
78
|
final Map<String, String> extraParams = Maps.newHashMap();
|
79
|
|
80
|
for (String param : parseParams(getValueBetween(infopackage, "<==7==>", null))) {
|
81
|
|
82
|
final String[] arr = param.split("###");
|
83
|
|
84
|
if (arr.length == 3) {
|
85
|
final boolean accessParam = Boolean.parseBoolean(arr[0].trim());
|
86
|
final String paramName = arr[1].trim();
|
87
|
final String paramValue = arr.length > 2 ? arr[2].trim() : "";
|
88
|
|
89
|
if (accessParam) {
|
90
|
if (paramName.equals(DatasourceParams.baseUrl.toString())) {
|
91
|
iface.setBaseUrl(paramValue);
|
92
|
} else {
|
93
|
accessParams.put(paramName, paramValue);
|
94
|
}
|
95
|
} else {
|
96
|
if(paramName.endsWith(DATE_FIELDS_SUFFIX)) {
|
97
|
extraParams.put(paramName, DateFormatUtils.format(new DateUtils().parse(paramValue), DATE_FORMAT));
|
98
|
} else {
|
99
|
extraParams.put(paramName, paramValue);
|
100
|
}
|
101
|
}
|
102
|
} else {
|
103
|
log.debug("Invalid Datasource Parameter");
|
104
|
}
|
105
|
}
|
106
|
|
107
|
iface.setAccessParams(accessParams);
|
108
|
iface.setExtraFields(extraParams);
|
109
|
|
110
|
return iface;
|
111
|
}
|
112
|
|
113
|
@SuppressWarnings("unchecked")
|
114
|
private static List<Api> parseInfoPackage(final ResultSet rs) throws DatasourceException, SQLException {
|
115
|
final List<Api> res = Lists.newArrayList();
|
116
|
final Array array = rs.getArray("accessinfopackage");
|
117
|
if (array != null) {
|
118
|
final String[] sa = (String[]) array.getArray();
|
119
|
for(final String s : sa) {
|
120
|
final Api api = generateIfaceDesc(s);
|
121
|
if (api != null) {
|
122
|
res.add(api);
|
123
|
}
|
124
|
}
|
125
|
}
|
126
|
return res;
|
127
|
}
|
128
|
|
129
|
private static String getValueBetween(final String s, final String pre, final String post) {
|
130
|
if (pre == null && post == null) {
|
131
|
return StringEscapeUtils.escapeXml(s);
|
132
|
} else if (pre == null) {
|
133
|
return StringEscapeUtils.escapeXml(StringUtils.substringBefore(s, post));
|
134
|
} else if (post == null) {
|
135
|
return StringEscapeUtils.escapeXml(StringUtils.substringAfter(s, pre));
|
136
|
} else {
|
137
|
return StringEscapeUtils.escapeXml(StringUtils.substringBetween(s, pre, post));
|
138
|
}
|
139
|
}
|
140
|
|
141
|
private static Iterable<String> parseParams(final String s) {
|
142
|
return Splitter.on("@@@").omitEmptyStrings().trimResults().split(s);
|
143
|
}
|
144
|
|
145
|
}
|