Revision 48705
Added by Claudio Atzori over 6 years ago
modules/dnet-datasource-rest-api/tags/dnet-datasource-rest-api-1.0.0/src/main/java/eu/dnetlib/datasource/publisher/model/DatasourceParams.java | ||
---|---|---|
1 |
package eu.dnetlib.datasource.publisher.model; |
|
2 |
|
|
3 |
public enum DatasourceParams { |
|
4 |
baseUrl, iis_processing_workflow, metadata_identifier_path |
|
5 |
} |
modules/dnet-datasource-rest-api/tags/dnet-datasource-rest-api-1.0.0/src/main/java/eu/dnetlib/datasource/publisher/model/CollectionMode.java | ||
---|---|---|
1 |
package eu.dnetlib.datasource.publisher.model; |
|
2 |
|
|
3 |
/** |
|
4 |
* Created by claudio on 12/09/16. |
|
5 |
*/ |
|
6 |
public enum CollectionMode { |
|
7 |
REFRESH, INCREMENTAL |
|
8 |
} |
modules/dnet-datasource-rest-api/tags/dnet-datasource-rest-api-1.0.0/src/main/java/eu/dnetlib/datasource/publisher/model/AggregationInfo.java | ||
---|---|---|
1 |
package eu.dnetlib.datasource.publisher.model; |
|
2 |
|
|
3 |
/** |
|
4 |
* Created by claudio on 12/09/16. |
|
5 |
*/ |
|
6 |
public class AggregationInfo { |
|
7 |
|
|
8 |
private int numberOfRecords; |
|
9 |
|
|
10 |
private String date; |
|
11 |
|
|
12 |
private AggregationStage aggregationStage; |
|
13 |
|
|
14 |
private CollectionMode collectionMode; |
|
15 |
|
|
16 |
public AggregationInfo() { |
|
17 |
} |
|
18 |
|
|
19 |
public int getNumberOfRecords() { |
|
20 |
return numberOfRecords; |
|
21 |
} |
|
22 |
|
|
23 |
public void setNumberOfRecords(final int numberOfRecords) { |
|
24 |
this.numberOfRecords = numberOfRecords; |
|
25 |
} |
|
26 |
|
|
27 |
public String getDate() { |
|
28 |
return date; |
|
29 |
} |
|
30 |
|
|
31 |
public void setDate(final String date) { |
|
32 |
this.date = date; |
|
33 |
} |
|
34 |
|
|
35 |
public AggregationStage getAggregationStage() { |
|
36 |
return aggregationStage; |
|
37 |
} |
|
38 |
|
|
39 |
public void setAggregationStage(final AggregationStage aggregationStage) { |
|
40 |
this.aggregationStage = aggregationStage; |
|
41 |
} |
|
42 |
|
|
43 |
public CollectionMode getCollectionMode() { |
|
44 |
return collectionMode; |
|
45 |
} |
|
46 |
|
|
47 |
public void setCollectionMode(final CollectionMode collectionMode) { |
|
48 |
this.collectionMode = collectionMode; |
|
49 |
} |
|
50 |
} |
modules/dnet-datasource-rest-api/tags/dnet-datasource-rest-api-1.0.0/src/main/java/eu/dnetlib/datasource/publisher/model/DatasourceFunctions.java | ||
---|---|---|
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 |
} |
modules/dnet-datasource-rest-api/tags/dnet-datasource-rest-api-1.0.0/src/main/java/eu/dnetlib/datasource/publisher/model/IndexRecordsInfo.java | ||
---|---|---|
1 |
package eu.dnetlib.datasource.publisher.model; |
|
2 |
|
|
3 |
/** |
|
4 |
* Created by claudio on 21/10/2016. |
|
5 |
*/ |
|
6 |
public class IndexRecordsInfo { |
|
7 |
|
|
8 |
private long count; |
|
9 |
|
|
10 |
private String date; |
|
11 |
|
|
12 |
public IndexRecordsInfo(final long count, final String date) { |
|
13 |
this.count = count; |
|
14 |
this.date = date; |
|
15 |
} |
|
16 |
|
|
17 |
public long getCount() { |
|
18 |
return count; |
|
19 |
} |
|
20 |
|
|
21 |
public void setCount(final long count) { |
|
22 |
this.count = count; |
|
23 |
} |
|
24 |
|
|
25 |
public String getDate() { |
|
26 |
return date; |
|
27 |
} |
|
28 |
|
|
29 |
public void setDate(final String date) { |
|
30 |
this.date = date; |
|
31 |
} |
|
32 |
} |
modules/dnet-datasource-rest-api/tags/dnet-datasource-rest-api-1.0.0/src/main/java/eu/dnetlib/datasource/publisher/model/DatasourceInfo.java | ||
---|---|---|
1 |
package eu.dnetlib.datasource.publisher.model; |
|
2 |
|
|
3 |
import java.util.List; |
|
4 |
|
|
5 |
/** |
|
6 |
* Created by claudio on 12/09/16. |
|
7 |
*/ |
|
8 |
public class DatasourceInfo { |
|
9 |
|
|
10 |
private Header responseHeader; |
|
11 |
|
|
12 |
private long indexRecords; |
|
13 |
|
|
14 |
private Datasource datasource; |
|
15 |
|
|
16 |
private String lastIndexingDate; |
|
17 |
|
|
18 |
private List<AggregationInfo> aggregationHistory; |
|
19 |
|
|
20 |
private AggregationInfo lastCollection; |
|
21 |
|
|
22 |
private AggregationInfo lastTransformation; |
|
23 |
|
|
24 |
public DatasourceInfo() { |
|
25 |
responseHeader = new Header(); |
|
26 |
} |
|
27 |
|
|
28 |
public DatasourceInfo setResponseHeader(final Header responseHeader) { |
|
29 |
this.responseHeader = responseHeader; |
|
30 |
return this; |
|
31 |
} |
|
32 |
|
|
33 |
public DatasourceInfo setIndexRecords(final long indexRecords) { |
|
34 |
this.indexRecords = indexRecords; |
|
35 |
return this; |
|
36 |
} |
|
37 |
|
|
38 |
public DatasourceInfo setLastIndexingDate(final String lastIndexingDate) { |
|
39 |
this.lastIndexingDate = lastIndexingDate; |
|
40 |
return this; |
|
41 |
} |
|
42 |
|
|
43 |
public DatasourceInfo setAggregationHistory(final List<AggregationInfo> aggregationHistory) { |
|
44 |
this.aggregationHistory = aggregationHistory; |
|
45 |
return this; |
|
46 |
} |
|
47 |
|
|
48 |
public DatasourceInfo setLastCollection(final AggregationInfo lastCollection) { |
|
49 |
this.lastCollection = lastCollection; |
|
50 |
return this; |
|
51 |
} |
|
52 |
|
|
53 |
public DatasourceInfo setLastTransformation(final AggregationInfo lastTransformation) { |
|
54 |
this.lastTransformation = lastTransformation; |
|
55 |
return this; |
|
56 |
} |
|
57 |
|
|
58 |
public Header getResponseHeader() { |
|
59 |
return responseHeader; |
|
60 |
} |
|
61 |
|
|
62 |
public long getIndexRecords() { |
|
63 |
return indexRecords; |
|
64 |
} |
|
65 |
|
|
66 |
public String getLastIndexingDate() { |
|
67 |
return lastIndexingDate; |
|
68 |
} |
|
69 |
|
|
70 |
public List<AggregationInfo> getAggregationHistory() { |
|
71 |
return aggregationHistory; |
|
72 |
} |
|
73 |
|
|
74 |
public AggregationInfo getLastCollection() { |
|
75 |
return lastCollection; |
|
76 |
} |
|
77 |
|
|
78 |
public AggregationInfo getLastTransformation() { |
|
79 |
return lastTransformation; |
|
80 |
} |
|
81 |
|
|
82 |
public Datasource getDatasource() { |
|
83 |
return datasource; |
|
84 |
} |
|
85 |
|
|
86 |
public void setDatasource(final Datasource datasource) { |
|
87 |
this.datasource = datasource; |
|
88 |
} |
|
89 |
} |
modules/dnet-datasource-rest-api/tags/dnet-datasource-rest-api-1.0.0/src/main/java/eu/dnetlib/datasource/publisher/model/Header.java | ||
---|---|---|
1 |
package eu.dnetlib.datasource.publisher.model; |
|
2 |
|
|
3 |
/** |
|
4 |
* Created by claudio on 24/11/2016. |
|
5 |
*/ |
|
6 |
public class Header { |
|
7 |
|
|
8 |
private long queryTime; |
|
9 |
|
|
10 |
private String datasourceId; |
|
11 |
|
|
12 |
private String error = ""; |
|
13 |
|
|
14 |
public Header() { |
|
15 |
} |
|
16 |
|
|
17 |
public Header setQueryTime(final long queryTime) { |
|
18 |
this.queryTime = queryTime; |
|
19 |
return this; |
|
20 |
} |
|
21 |
|
|
22 |
public Header setDatasourceId(final String datasourceId) { |
|
23 |
this.datasourceId = datasourceId; |
|
24 |
return this; |
|
25 |
} |
|
26 |
|
|
27 |
public Header setError(final String error) { |
|
28 |
this.error = error; |
|
29 |
return this; |
|
30 |
} |
|
31 |
|
|
32 |
public long getQueryTime() { |
|
33 |
return queryTime; |
|
34 |
} |
|
35 |
|
|
36 |
public String getDatasourceId() { |
|
37 |
return datasourceId; |
|
38 |
} |
|
39 |
|
|
40 |
public String getError() { |
|
41 |
return error; |
|
42 |
} |
|
43 |
} |
modules/dnet-datasource-rest-api/tags/dnet-datasource-rest-api-1.0.0/src/main/java/eu/dnetlib/datasource/publisher/model/Api.java | ||
---|---|---|
1 |
package eu.dnetlib.datasource.publisher.model; |
|
2 |
|
|
3 |
import java.util.HashMap; |
|
4 |
import java.util.Map; |
|
5 |
|
|
6 |
/** |
|
7 |
* Created by claudio on 24/11/2016. |
|
8 |
*/ |
|
9 |
public class Api { |
|
10 |
|
|
11 |
private String id; |
|
12 |
private String typology; |
|
13 |
private String compliance; |
|
14 |
private String contentDescription; |
|
15 |
private String accessProtocol; |
|
16 |
private String baseUrl; |
|
17 |
private boolean active = false; |
|
18 |
private boolean removable = false; |
|
19 |
private Map<String, String> accessParams = new HashMap<String, String>(); |
|
20 |
private Map<String, String> extraFields = new HashMap<String, String>(); |
|
21 |
|
|
22 |
public Api() {} |
|
23 |
|
|
24 |
public String getId() { |
|
25 |
return id; |
|
26 |
} |
|
27 |
|
|
28 |
public void setId(final String id) { |
|
29 |
this.id = id; |
|
30 |
} |
|
31 |
|
|
32 |
public String getTypology() { |
|
33 |
return typology; |
|
34 |
} |
|
35 |
|
|
36 |
public void setTypology(final String typology) { |
|
37 |
this.typology = typology; |
|
38 |
} |
|
39 |
|
|
40 |
public String getCompliance() { |
|
41 |
return compliance; |
|
42 |
} |
|
43 |
|
|
44 |
public void setCompliance(final String compliance) { |
|
45 |
this.compliance = compliance; |
|
46 |
} |
|
47 |
|
|
48 |
public String getContentDescription() { |
|
49 |
return contentDescription; |
|
50 |
} |
|
51 |
|
|
52 |
public void setContentDescription(final String contentDescription) { |
|
53 |
this.contentDescription = contentDescription; |
|
54 |
} |
|
55 |
|
|
56 |
public String getAccessProtocol() { |
|
57 |
return accessProtocol; |
|
58 |
} |
|
59 |
|
|
60 |
public void setAccessProtocol(final String accessProtocol) { |
|
61 |
this.accessProtocol = accessProtocol; |
|
62 |
} |
|
63 |
|
|
64 |
public String getBaseUrl() { |
|
65 |
return baseUrl; |
|
66 |
} |
|
67 |
|
|
68 |
public void setBaseUrl(final String baseUrl) { |
|
69 |
this.baseUrl = baseUrl; |
|
70 |
} |
|
71 |
|
|
72 |
public Map<String, String> getAccessParams() { |
|
73 |
return accessParams; |
|
74 |
} |
|
75 |
|
|
76 |
public void setAccessParams(final Map<String, String> accessParams) { |
|
77 |
this.accessParams = accessParams; |
|
78 |
} |
|
79 |
|
|
80 |
public Map<String, String> getExtraFields() { |
|
81 |
return extraFields; |
|
82 |
} |
|
83 |
|
|
84 |
public void setExtraFields(final Map<String, String> extraFields) { |
|
85 |
this.extraFields = extraFields; |
|
86 |
} |
|
87 |
|
|
88 |
public boolean getActive() { |
|
89 |
return active; |
|
90 |
} |
|
91 |
|
|
92 |
public void setActive(final boolean active) { |
|
93 |
this.active = active; |
|
94 |
} |
|
95 |
|
|
96 |
public boolean getRemovable() { |
|
97 |
return removable; |
|
98 |
} |
|
99 |
|
|
100 |
public void setRemovable(final boolean removable) { |
|
101 |
this.removable = removable; |
|
102 |
} |
|
103 |
|
|
104 |
} |
modules/dnet-datasource-rest-api/tags/dnet-datasource-rest-api-1.0.0/src/main/java/eu/dnetlib/datasource/publisher/model/Datasource.java | ||
---|---|---|
1 |
package eu.dnetlib.datasource.publisher.model; |
|
2 |
|
|
3 |
import java.util.Date; |
|
4 |
import java.util.List; |
|
5 |
|
|
6 |
import com.google.common.collect.Lists; |
|
7 |
|
|
8 |
/** |
|
9 |
* Created by claudio on 24/11/2016. |
|
10 |
*/ |
|
11 |
public class Datasource { |
|
12 |
|
|
13 |
private String id; |
|
14 |
private String officialName; |
|
15 |
private String englishName; |
|
16 |
private String websiteUrl; |
|
17 |
private String logoUrl; |
|
18 |
private String contactEmail; |
|
19 |
private String countryCode; |
|
20 |
private String countryName; |
|
21 |
private String organization; |
|
22 |
private Double latitude = 0.0; |
|
23 |
private Double longitude = 0.0; |
|
24 |
private Double timezone = 0.0; |
|
25 |
private String namespacePrefix; |
|
26 |
private String collectedFrom; |
|
27 |
private Date dateOfValidation; |
|
28 |
private String registeredBy; |
|
29 |
private String datasourceClass; |
|
30 |
|
|
31 |
@Deprecated |
|
32 |
private String provenanceActionClass; |
|
33 |
|
|
34 |
private Date dateOfCollection; |
|
35 |
private String typology; |
|
36 |
private String activationId; |
|
37 |
|
|
38 |
private String description; |
|
39 |
|
|
40 |
@Deprecated |
|
41 |
private String missionStatementUrl; |
|
42 |
private String aggregator; |
|
43 |
private String issn; |
|
44 |
private String eissn; |
|
45 |
private String lissn; |
|
46 |
private List<Api> api = Lists.newArrayList(); |
|
47 |
|
|
48 |
public String getId() { |
|
49 |
return id; |
|
50 |
} |
|
51 |
|
|
52 |
public void setId(final String id) { |
|
53 |
this.id = id; |
|
54 |
} |
|
55 |
|
|
56 |
public String getOfficialName() { |
|
57 |
return officialName; |
|
58 |
} |
|
59 |
|
|
60 |
public void setOfficialName(final String officialName) { |
|
61 |
this.officialName = officialName; |
|
62 |
} |
|
63 |
|
|
64 |
public String getEnglishName() { |
|
65 |
return englishName; |
|
66 |
} |
|
67 |
|
|
68 |
public void setEnglishName(final String englishName) { |
|
69 |
this.englishName = englishName; |
|
70 |
} |
|
71 |
|
|
72 |
public String getWebsiteUrl() { |
|
73 |
return websiteUrl; |
|
74 |
} |
|
75 |
|
|
76 |
public void setWebsiteUrl(final String websiteUrl) { |
|
77 |
this.websiteUrl = websiteUrl; |
|
78 |
} |
|
79 |
|
|
80 |
public String getLogoUrl() { |
|
81 |
return logoUrl; |
|
82 |
} |
|
83 |
|
|
84 |
public void setLogoUrl(final String logoUrl) { |
|
85 |
this.logoUrl = logoUrl; |
|
86 |
} |
|
87 |
|
|
88 |
public String getContactEmail() { |
|
89 |
return contactEmail; |
|
90 |
} |
|
91 |
|
|
92 |
public void setContactEmail(final String contactEmail) { |
|
93 |
this.contactEmail = contactEmail; |
|
94 |
} |
|
95 |
|
|
96 |
public String getCountryCode() { |
|
97 |
return countryCode; |
|
98 |
} |
|
99 |
|
|
100 |
public void setCountryCode(final String countryCode) { |
|
101 |
this.countryCode = countryCode; |
|
102 |
} |
|
103 |
|
|
104 |
public String getCountryName() { |
|
105 |
return countryName; |
|
106 |
} |
|
107 |
|
|
108 |
public void setCountryName(final String countryName) { |
|
109 |
this.countryName = countryName; |
|
110 |
} |
|
111 |
|
|
112 |
public String getOrganization() { |
|
113 |
return organization; |
|
114 |
} |
|
115 |
|
|
116 |
public void setOrganization(final String organization) { |
|
117 |
this.organization = organization; |
|
118 |
} |
|
119 |
|
|
120 |
public Double getLatitude() { |
|
121 |
return latitude; |
|
122 |
} |
|
123 |
|
|
124 |
public void setLatitude(final Double latitude) { |
|
125 |
this.latitude = latitude; |
|
126 |
} |
|
127 |
|
|
128 |
public Double getLongitude() { |
|
129 |
return longitude; |
|
130 |
} |
|
131 |
|
|
132 |
public void setLongitude(final Double longitude) { |
|
133 |
this.longitude = longitude; |
|
134 |
} |
|
135 |
|
|
136 |
public Double getTimezone() { |
|
137 |
return timezone; |
|
138 |
} |
|
139 |
|
|
140 |
public void setTimezone(final Double timezone) { |
|
141 |
this.timezone = timezone; |
|
142 |
} |
|
143 |
|
|
144 |
public String getNamespacePrefix() { |
|
145 |
return namespacePrefix; |
|
146 |
} |
|
147 |
|
|
148 |
public void setNamespacePrefix(final String namespacePrefix) { |
|
149 |
this.namespacePrefix = namespacePrefix; |
|
150 |
} |
|
151 |
|
|
152 |
public String getCollectedFrom() { |
|
153 |
return collectedFrom; |
|
154 |
} |
|
155 |
|
|
156 |
public void setCollectedFrom(final String collectedFrom) { |
|
157 |
this.collectedFrom = collectedFrom; |
|
158 |
} |
|
159 |
|
|
160 |
public Date getDateOfValidation() { |
|
161 |
return dateOfValidation; |
|
162 |
} |
|
163 |
|
|
164 |
public void setDateOfValidation(final Date dateOfValidation) { |
|
165 |
this.dateOfValidation = dateOfValidation; |
|
166 |
} |
|
167 |
|
|
168 |
public String getDatasourceClass() { |
|
169 |
return datasourceClass; |
|
170 |
} |
|
171 |
|
|
172 |
public void setDatasourceClass(final String datasourceClass) { |
|
173 |
this.datasourceClass = datasourceClass; |
|
174 |
} |
|
175 |
|
|
176 |
public String getProvenanceActionClass() { |
|
177 |
return provenanceActionClass; |
|
178 |
} |
|
179 |
|
|
180 |
public void setProvenanceActionClass(final String provenanceActionClass) { |
|
181 |
this.provenanceActionClass = provenanceActionClass; |
|
182 |
} |
|
183 |
|
|
184 |
public Date getDateOfCollection() { |
|
185 |
return dateOfCollection; |
|
186 |
} |
|
187 |
|
|
188 |
public void setDateOfCollection(final Date dateOfCollection) { |
|
189 |
this.dateOfCollection = dateOfCollection; |
|
190 |
} |
|
191 |
|
|
192 |
public String getTypology() { |
|
193 |
return typology; |
|
194 |
} |
|
195 |
|
|
196 |
public void setTypology(final String typology) { |
|
197 |
this.typology = typology; |
|
198 |
} |
|
199 |
|
|
200 |
public String getActivationId() { |
|
201 |
return activationId; |
|
202 |
} |
|
203 |
|
|
204 |
public void setActivationId(final String activationId) { |
|
205 |
this.activationId = activationId; |
|
206 |
} |
|
207 |
|
|
208 |
public String getDescription() { |
|
209 |
return description; |
|
210 |
} |
|
211 |
|
|
212 |
public void setDescription(final String description) { |
|
213 |
this.description = description; |
|
214 |
} |
|
215 |
|
|
216 |
public String getMissionStatementUrl() { |
|
217 |
return missionStatementUrl; |
|
218 |
} |
|
219 |
|
|
220 |
public void setMissionStatementUrl(final String missionStatementUrl) { |
|
221 |
this.missionStatementUrl = missionStatementUrl; |
|
222 |
} |
|
223 |
|
|
224 |
public String getAggregator() { |
|
225 |
return aggregator; |
|
226 |
} |
|
227 |
|
|
228 |
public void setAggregator(final String aggregator) { |
|
229 |
this.aggregator = aggregator; |
|
230 |
} |
|
231 |
|
|
232 |
public String getIssn() { |
|
233 |
return issn; |
|
234 |
} |
|
235 |
|
|
236 |
public void setIssn(final String issn) { |
|
237 |
this.issn = issn; |
|
238 |
} |
|
239 |
|
|
240 |
public String getEissn() { |
|
241 |
return eissn; |
|
242 |
} |
|
243 |
|
|
244 |
public void setEissn(final String eissn) { |
|
245 |
this.eissn = eissn; |
|
246 |
} |
|
247 |
|
|
248 |
public String getLissn() { |
|
249 |
return lissn; |
|
250 |
} |
|
251 |
|
|
252 |
public void setLissn(final String lissn) { |
|
253 |
this.lissn = lissn; |
|
254 |
} |
|
255 |
|
|
256 |
public List<Api> getApi() { |
|
257 |
return api; |
|
258 |
} |
|
259 |
|
|
260 |
public void setApi(final List<Api> api) { |
|
261 |
this.api = api; |
|
262 |
} |
|
263 |
|
|
264 |
public String getRegisteredBy() { |
|
265 |
return registeredBy; |
|
266 |
} |
|
267 |
|
|
268 |
public void setRegisteredBy(String registeredBy) { |
|
269 |
this.registeredBy = registeredBy; |
|
270 |
} |
|
271 |
|
|
272 |
} |
modules/dnet-datasource-rest-api/tags/dnet-datasource-rest-api-1.0.0/src/main/java/eu/dnetlib/datasource/publisher/model/IndexDsInfo.java | ||
---|---|---|
1 |
package eu.dnetlib.datasource.publisher.model; |
|
2 |
|
|
3 |
/** |
|
4 |
* Created by claudio on 20/10/2016. |
|
5 |
*/ |
|
6 |
public class IndexDsInfo { |
|
7 |
|
|
8 |
private final String indexBaseUrl; |
|
9 |
private final String indexDsId; |
|
10 |
private final String format; |
|
11 |
private final String coll; |
|
12 |
|
|
13 |
public IndexDsInfo(final String indexBaseUrl, final String indexDsId, final String format, final String coll) { |
|
14 |
this.indexBaseUrl = indexBaseUrl; |
|
15 |
this.indexDsId = indexDsId; |
|
16 |
this.format = format; |
|
17 |
this.coll = coll; |
|
18 |
} |
|
19 |
|
|
20 |
public String getIndexBaseUrl() { |
|
21 |
return indexBaseUrl; |
|
22 |
} |
|
23 |
|
|
24 |
public String getIndexDsId() { |
|
25 |
return indexDsId; |
|
26 |
} |
|
27 |
|
|
28 |
public String getFormat() { |
|
29 |
return format; |
|
30 |
} |
|
31 |
|
|
32 |
public String getColl() { |
|
33 |
return coll; |
|
34 |
} |
|
35 |
|
|
36 |
} |
modules/dnet-datasource-rest-api/tags/dnet-datasource-rest-api-1.0.0/src/main/java/eu/dnetlib/datasource/publisher/model/AggregationStage.java | ||
---|---|---|
1 |
package eu.dnetlib.datasource.publisher.model; |
|
2 |
|
|
3 |
/** |
|
4 |
* Created by claudio on 12/09/16. |
|
5 |
*/ |
|
6 |
public enum AggregationStage { |
|
7 |
COLLECT, TRANSFORM; |
|
8 |
|
|
9 |
public static AggregationStage parse(final String s) { |
|
10 |
switch (s) { |
|
11 |
case "collect": |
|
12 |
case "collection": |
|
13 |
case "COLLECT": |
|
14 |
case "COLLECTION": |
|
15 |
return AggregationStage.COLLECT; |
|
16 |
case "transform": |
|
17 |
case "transformation": |
|
18 |
case "TRANSFORM": |
|
19 |
case "TRANSFORMATION": |
|
20 |
return AggregationStage.TRANSFORM; |
|
21 |
} |
|
22 |
throw new IllegalArgumentException("invalid AggregationStage: " + s); |
|
23 |
} |
|
24 |
} |
modules/dnet-datasource-rest-api/tags/dnet-datasource-rest-api-1.0.0/src/main/java/eu/dnetlib/datasource/publisher/clients/DatasourceIndexClient.java | ||
---|---|---|
1 |
package eu.dnetlib.datasource.publisher.clients; |
|
2 |
|
|
3 |
import java.util.Date; |
|
4 |
import java.util.List; |
|
5 |
import java.util.Map; |
|
6 |
import java.util.concurrent.ConcurrentHashMap; |
|
7 |
|
|
8 |
import com.google.common.collect.Iterables; |
|
9 |
import eu.dnetlib.data.index.CloudIndexClient; |
|
10 |
import eu.dnetlib.data.index.CloudIndexClientException; |
|
11 |
|
|
12 |
import eu.dnetlib.data.index.CloudIndexClientFactory; |
|
13 |
import eu.dnetlib.datasource.publisher.DatasourceException; |
|
14 |
import eu.dnetlib.datasource.publisher.model.DatasourceFunctions; |
|
15 |
import eu.dnetlib.datasource.publisher.model.IndexDsInfo; |
|
16 |
import eu.dnetlib.datasource.publisher.model.IndexRecordsInfo; |
|
17 |
import eu.dnetlib.miscutils.datetime.DateUtils; |
|
18 |
import eu.dnetlib.miscutils.functional.hash.Hashing; |
|
19 |
import org.apache.commons.lang.StringUtils; |
|
20 |
import org.apache.commons.lang.time.DateFormatUtils; |
|
21 |
import org.apache.commons.logging.Log; |
|
22 |
import org.apache.commons.logging.LogFactory; |
|
23 |
import org.apache.solr.client.solrj.response.QueryResponse; |
|
24 |
import org.apache.solr.common.SolrDocument; |
|
25 |
|
|
26 |
/** |
|
27 |
* Created by claudio on 20/10/2016. |
|
28 |
*/ |
|
29 |
public class DatasourceIndexClient { |
|
30 |
|
|
31 |
private static final Log log = LogFactory.getLog(DatasourceIndexClient.class); |
|
32 |
|
|
33 |
public static final String SEPARATOR = "::"; |
|
34 |
public static final String DSVERSION = "__dsversion"; |
|
35 |
|
|
36 |
private static Map<String, CloudIndexClient> indexClientMap = new ConcurrentHashMap<>(); |
|
37 |
|
|
38 |
public IndexRecordsInfo getIndexInfo(final String dsId, final IndexDsInfo info) throws DatasourceException { |
|
39 |
try { |
|
40 |
final String collectedFrom = StringUtils.substringBefore(dsId, SEPARATOR) + SEPARATOR + Hashing.md5(StringUtils.substringAfter(dsId, SEPARATOR)); |
|
41 |
final CloudIndexClient indexClient = getIndexClient(info); |
|
42 |
final String query = String.format("oaftype:result AND deletedbyinference:false AND collectedfromdatasourceid:\"%s\"", collectedFrom); |
|
43 |
log.debug(String.format("query on %s: %s", info.getFormat(), query)); |
|
44 |
|
|
45 |
final QueryResponse rsp = indexClient.query(query, 1); |
|
46 |
|
|
47 |
long count = rsp.getResults().getNumFound(); |
|
48 |
final SolrDocument doc = Iterables.getFirst(rsp.getResults(), new SolrDocument()); |
|
49 |
final String date = getDate(doc); |
|
50 |
|
|
51 |
return new IndexRecordsInfo(count, date); |
|
52 |
} catch (final Throwable e) { |
|
53 |
throw new DatasourceException("Error querying publications from: " + dsId, e); |
|
54 |
} |
|
55 |
} |
|
56 |
|
|
57 |
private String getDate(final SolrDocument doc) { |
|
58 |
final List<Date> dsversion = (List<Date>) doc.get(DSVERSION); |
|
59 |
final Date date = Iterables.getLast(dsversion); |
|
60 |
|
|
61 |
return DateFormatUtils.format(date, DatasourceFunctions.DATE_FORMAT); |
|
62 |
} |
|
63 |
|
|
64 |
public String getLastIndexingDate(final IndexDsInfo info) throws DatasourceException { |
|
65 |
try { |
|
66 |
final QueryResponse rsp = getIndexClient(info).query("oaftype:datasource", 1); |
|
67 |
final SolrDocument doc = Iterables.getFirst(rsp.getResults(), null); |
|
68 |
final String dsversion = doc.get("__dsversion").toString(); |
|
69 |
return StringUtils.substringBefore(dsversion, "T"); |
|
70 |
} catch (CloudIndexClientException e) { |
|
71 |
throw new DatasourceException("Error querying index DS profile: " + info, e); |
|
72 |
} |
|
73 |
} |
|
74 |
|
|
75 |
private synchronized CloudIndexClient getIndexClient(final IndexDsInfo info) throws CloudIndexClientException { |
|
76 |
if (!indexClientMap.containsKey(info.getColl())) { |
|
77 |
indexClientMap.put(info.getColl(), CloudIndexClientFactory.newIndexClient(info.getIndexBaseUrl(), info.getColl(), false)); |
|
78 |
} |
|
79 |
return indexClientMap.get(info.getColl()); |
|
80 |
} |
|
81 |
|
|
82 |
} |
modules/dnet-datasource-rest-api/tags/dnet-datasource-rest-api-1.0.0/src/main/java/eu/dnetlib/datasource/publisher/clients/JdbcDatasourceDao.java | ||
---|---|---|
1 |
package eu.dnetlib.datasource.publisher.clients; |
|
2 |
|
|
3 |
import java.io.IOException; |
|
4 |
import java.sql.Connection; |
|
5 |
import java.sql.PreparedStatement; |
|
6 |
import java.sql.ResultSet; |
|
7 |
import java.sql.SQLException; |
|
8 |
|
|
9 |
import eu.dnetlib.datasource.publisher.DatasourceException; |
|
10 |
import eu.dnetlib.datasource.publisher.model.Datasource; |
|
11 |
import eu.dnetlib.datasource.publisher.model.DatasourceFunctions; |
|
12 |
import org.antlr.stringtemplate.StringTemplate; |
|
13 |
import org.apache.commons.dbcp2.BasicDataSource; |
|
14 |
import org.apache.commons.io.IOUtils; |
|
15 |
import org.apache.commons.logging.Log; |
|
16 |
import org.apache.commons.logging.LogFactory; |
|
17 |
import org.springframework.beans.factory.annotation.Autowired; |
|
18 |
import org.springframework.beans.factory.annotation.Value; |
|
19 |
import org.springframework.core.io.ClassPathResource; |
|
20 |
import org.springframework.core.io.Resource; |
|
21 |
|
|
22 |
/** |
|
23 |
* Created by claudio on 25/11/2016. |
|
24 |
*/ |
|
25 |
public class JdbcDatasourceDao { |
|
26 |
|
|
27 |
private static final Log log = LogFactory.getLog(JdbcDatasourceDao.class); |
|
28 |
|
|
29 |
@Value("${datasource.publisher.getsingle.sql}") |
|
30 |
private ClassPathResource datasourceDetailsSql; |
|
31 |
|
|
32 |
@Autowired |
|
33 |
private BasicDataSource dsDataSource; |
|
34 |
|
|
35 |
public Datasource getDatasource(final String id) throws DatasourceException { |
|
36 |
try { |
|
37 |
final String sql = obtainQuery(datasourceDetailsSql, id); |
|
38 |
|
|
39 |
try (final Connection con = getConn(); final PreparedStatement stm = getStm(sql, con); final ResultSet rs = stm.executeQuery()) { |
|
40 |
if (rs.next()) { |
|
41 |
return DatasourceFunctions.toDatasource(rs); |
|
42 |
} else { |
|
43 |
throw new DatasourceException(String.format("unable to find datasource '%s'", id)); |
|
44 |
} |
|
45 |
} |
|
46 |
} catch(SQLException | IOException e) { |
|
47 |
throw new DatasourceException(e); |
|
48 |
} |
|
49 |
} |
|
50 |
|
|
51 |
private Connection getConn() throws SQLException { |
|
52 |
final Connection conn = dsDataSource.getConnection(); |
|
53 |
conn.setAutoCommit(false); |
|
54 |
return conn; |
|
55 |
} |
|
56 |
|
|
57 |
private PreparedStatement getStm(final String sql, final Connection con) throws SQLException { |
|
58 |
final PreparedStatement stm = con.prepareStatement(sql); |
|
59 |
stm.setFetchSize(10); |
|
60 |
return stm; |
|
61 |
} |
|
62 |
|
|
63 |
protected String obtainQuery(final Resource template, final String id) throws IllegalArgumentException, IOException { |
|
64 |
final StringTemplate st = new StringTemplate(IOUtils.toString(template.getInputStream())); |
|
65 |
st.setAttribute("id", id); |
|
66 |
final String sql = st.toString(); |
|
67 |
log.debug("Generated query: " + sql); |
|
68 |
return sql; |
|
69 |
} |
|
70 |
|
|
71 |
} |
modules/dnet-datasource-rest-api/tags/dnet-datasource-rest-api-1.0.0/src/main/java/eu/dnetlib/datasource/publisher/clients/MongoOptionsFactory.java | ||
---|---|---|
1 |
package eu.dnetlib.datasource.publisher.clients; |
|
2 |
|
|
3 |
import com.mongodb.MongoClientOptions; |
|
4 |
import org.springframework.beans.BeansException; |
|
5 |
import org.springframework.beans.factory.FactoryBean; |
|
6 |
|
|
7 |
/** |
|
8 |
* Created by claudio on 20/10/2016. |
|
9 |
*/ |
|
10 |
public class MongoOptionsFactory implements FactoryBean<MongoClientOptions> { |
|
11 |
private int connectionsPerHost; |
|
12 |
|
|
13 |
@Override |
|
14 |
public MongoClientOptions getObject() throws BeansException { |
|
15 |
return MongoClientOptions.builder().connectionsPerHost(connectionsPerHost).build(); |
|
16 |
} |
|
17 |
|
|
18 |
@Override |
|
19 |
public Class<MongoClientOptions> getObjectType() { |
|
20 |
return MongoClientOptions.class; |
|
21 |
} |
|
22 |
|
|
23 |
@Override |
|
24 |
public boolean isSingleton() { |
|
25 |
return false; |
|
26 |
} |
|
27 |
|
|
28 |
public int getConnectionsPerHost() { |
|
29 |
return connectionsPerHost; |
|
30 |
} |
|
31 |
|
|
32 |
public void setConnectionsPerHost(int connectionsPerHost) { |
|
33 |
this.connectionsPerHost = connectionsPerHost; |
|
34 |
} |
|
35 |
|
|
36 |
} |
modules/dnet-datasource-rest-api/tags/dnet-datasource-rest-api-1.0.0/src/main/java/eu/dnetlib/datasource/publisher/clients/ISLookupClient.java | ||
---|---|---|
1 |
package eu.dnetlib.datasource.publisher.clients; |
|
2 |
|
|
3 |
import eu.dnetlib.datasource.publisher.DatasourceException; |
|
4 |
import eu.dnetlib.datasource.publisher.model.IndexDsInfo; |
|
5 |
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException; |
|
6 |
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService; |
|
7 |
import eu.dnetlib.enabling.locators.UniqueServiceLocator; |
|
8 |
import org.apache.commons.io.IOUtils; |
|
9 |
import org.apache.commons.logging.Log; |
|
10 |
import org.apache.commons.logging.LogFactory; |
|
11 |
import org.springframework.beans.factory.annotation.Autowired; |
|
12 |
import org.springframework.beans.factory.annotation.Value; |
|
13 |
import org.springframework.cache.annotation.Cacheable; |
|
14 |
import org.springframework.core.io.ClassPathResource; |
|
15 |
|
|
16 |
/** |
|
17 |
* Created by claudio on 20/10/2016. |
|
18 |
*/ |
|
19 |
public class ISLookupClient { |
|
20 |
|
|
21 |
private static final Log log = LogFactory.getLog(ISLookupClient.class); |
|
22 |
|
|
23 |
@Autowired |
|
24 |
private UniqueServiceLocator serviceLocator; |
|
25 |
|
|
26 |
@Value(value = "${datasource.publisher.findSolrIndexUrl.xquery}") |
|
27 |
private ClassPathResource findSolrIndexUrl; |
|
28 |
|
|
29 |
@Value(value = "${datasource.publisher.findIndexDsInfo.xquery}") |
|
30 |
private ClassPathResource findIndexDsInfo; |
|
31 |
|
|
32 |
@Cacheable("datasources-is-cache") |
|
33 |
public IndexDsInfo calculateCurrentIndexDsInfo() throws DatasourceException { |
|
34 |
log.warn("calculateCurrentIndexDsInfo(): not using cache"); |
|
35 |
try { |
|
36 |
final String queryUrl = IOUtils.toString(findSolrIndexUrl.getInputStream()); |
|
37 |
final String queryDs = IOUtils.toString(findIndexDsInfo.getInputStream()); |
|
38 |
|
|
39 |
final ISLookUpService lookUpService = serviceLocator.getService(ISLookUpService.class); |
|
40 |
final String indexBaseUrl = lookUpService.getResourceProfileByQuery(queryUrl); |
|
41 |
final String[] arr = lookUpService.getResourceProfileByQuery(queryDs).split("@@@"); |
|
42 |
|
|
43 |
return new IndexDsInfo(indexBaseUrl, arr[0].trim(), arr[1].trim(), arr[2].trim()); |
|
44 |
} catch (Exception e) { |
|
45 |
throw new DatasourceException(e); |
|
46 |
} |
|
47 |
} |
|
48 |
|
|
49 |
public String getLastIndexingDate(final IndexDsInfo info) throws DatasourceException { |
|
50 |
final String xquery = String.format("for $x in collection('')/RESOURCE_PROFILE[./HEADER/RESOURCE_TYPE/@value = 'IndexDSResourceType' and " |
|
51 |
+ "./BODY/CONFIGURATION/METADATA_FORMAT = '%s']\n" |
|
52 |
+ "return $x/BODY/STATUS/INDEX_LAST_UPDATE/text()", info.getFormat()); |
|
53 |
try { |
|
54 |
final ISLookUpService lookUpService = serviceLocator.getService(ISLookUpService.class); |
|
55 |
return lookUpService.getResourceProfileByQuery(xquery); |
|
56 |
} catch (ISLookUpException e) { |
|
57 |
throw new DatasourceException("Error querying index DS profile: " + info.getFormat(), e); |
|
58 |
} |
|
59 |
} |
|
60 |
|
|
61 |
} |
modules/dnet-datasource-rest-api/tags/dnet-datasource-rest-api-1.0.0/src/main/java/eu/dnetlib/datasource/publisher/clients/DatasourceInfoRetriever.java | ||
---|---|---|
1 |
package eu.dnetlib.datasource.publisher.clients; |
|
2 |
|
|
3 |
import java.util.List; |
|
4 |
import java.util.Queue; |
|
5 |
import java.util.concurrent.*; |
|
6 |
|
|
7 |
import com.google.common.base.Function; |
|
8 |
import com.google.common.base.Joiner; |
|
9 |
import com.google.common.base.Predicate; |
|
10 |
import com.google.common.collect.Iterables; |
|
11 |
import com.google.common.util.concurrent.*; |
|
12 |
import eu.dnetlib.datasource.publisher.DatasourceException; |
|
13 |
import eu.dnetlib.datasource.publisher.model.*; |
|
14 |
import org.apache.commons.lang.exception.ExceptionUtils; |
|
15 |
import org.apache.commons.logging.Log; |
|
16 |
import org.apache.commons.logging.LogFactory; |
|
17 |
import org.apache.cxf.common.i18n.Exception; |
|
18 |
import org.springframework.beans.factory.annotation.Autowired; |
|
19 |
|
|
20 |
/** |
|
21 |
* Created by claudio on 20/10/2016. |
|
22 |
*/ |
|
23 |
public class DatasourceInfoRetriever { |
|
24 |
|
|
25 |
private static final Log log = LogFactory.getLog(DatasourceInfoRetriever.class); |
|
26 |
|
|
27 |
@Autowired |
|
28 |
private MongoLoggerClient mongoLoggerClient; |
|
29 |
|
|
30 |
@Autowired |
|
31 |
private DatasourceIndexClient datasourceIndexClient; |
|
32 |
|
|
33 |
@Autowired |
|
34 |
private ISLookupClient lookupClient; |
|
35 |
|
|
36 |
@Autowired |
|
37 |
private JdbcDatasourceDao jdbcDatasourceDao; |
|
38 |
|
|
39 |
private ListeningExecutorService service; |
|
40 |
|
|
41 |
private final static int WORKERS = 100; |
|
42 |
|
|
43 |
public DatasourceInfoRetriever() { |
|
44 |
service = MoreExecutors.listeningDecorator( |
|
45 |
new ScheduledThreadPoolExecutor(WORKERS, |
|
46 |
new ThreadFactoryBuilder().setNameFormat("datasource-info-retriever-%d").build())); |
|
47 |
} |
|
48 |
|
|
49 |
public DatasourceInfo getInfo(final String dsId) throws DatasourceException { |
|
50 |
|
|
51 |
final CountDownLatch outerLatch = new CountDownLatch(3); |
|
52 |
final Queue<Throwable> errors = new ConcurrentLinkedQueue<>(); |
|
53 |
final DatasourceInfo datasourceInfo = new DatasourceInfo(); |
|
54 |
|
|
55 |
Futures.addCallback(getAggregationHistory(dsId), new FutureCallback<List<AggregationInfo>>() { |
|
56 |
public void onSuccess(List<AggregationInfo> info) { |
|
57 |
setAggregationHistory(datasourceInfo, info); |
|
58 |
outerLatch.countDown(); |
|
59 |
} |
|
60 |
public void onFailure(Throwable e) { |
|
61 |
errors.offer(e); |
|
62 |
outerLatch.countDown(); |
|
63 |
} |
|
64 |
}); |
|
65 |
|
|
66 |
Futures.addCallback(getDatasourceDetails(dsId), new FutureCallback<Datasource>() { |
|
67 |
@Override |
|
68 |
public void onSuccess(final Datasource datasource) { |
|
69 |
datasourceInfo.setDatasource(datasource); |
|
70 |
outerLatch.countDown(); |
|
71 |
} |
|
72 |
|
|
73 |
@Override |
|
74 |
public void onFailure(final Throwable e) { |
|
75 |
errors.offer(e); |
|
76 |
outerLatch.countDown(); |
|
77 |
} |
|
78 |
}); |
|
79 |
|
|
80 |
Futures.addCallback(calculateCurrentIndexDsInfo(), new FutureCallback<IndexDsInfo>() { |
|
81 |
|
|
82 |
public void onSuccess(final IndexDsInfo info) { |
|
83 |
|
|
84 |
final CountDownLatch innerLatch = new CountDownLatch(1); |
|
85 |
|
|
86 |
Futures.addCallback(getIndexInfo(dsId, info), new FutureCallback<IndexRecordsInfo>() { |
|
87 |
public void onSuccess(IndexRecordsInfo info) { |
|
88 |
datasourceInfo.setIndexRecords(info.getCount()).setLastIndexingDate(info.getDate()); |
|
89 |
innerLatch.countDown(); |
|
90 |
} |
|
91 |
public void onFailure(Throwable e) { |
|
92 |
errors.offer(e); |
|
93 |
innerLatch.countDown(); |
|
94 |
} |
|
95 |
}); |
|
96 |
waitLatch(innerLatch, errors, 10); |
|
97 |
|
|
98 |
outerLatch.countDown(); |
|
99 |
} |
|
100 |
|
|
101 |
public void onFailure(final Throwable e) { |
|
102 |
errors.offer(e); |
|
103 |
outerLatch.countDown(); |
|
104 |
} |
|
105 |
}); |
|
106 |
|
|
107 |
waitLatch(outerLatch, errors, 10); |
|
108 |
|
|
109 |
if (!errors.isEmpty()) { |
|
110 |
datasourceInfo.getResponseHeader().setError(Joiner.on("\n").join(Iterables.transform(errors, new Function<Throwable, String>() { |
|
111 |
@Override |
|
112 |
public String apply(final Throwable e) { |
|
113 |
return e.getMessage(); |
|
114 |
} |
|
115 |
}))); |
|
116 |
log.error(Joiner.on("\n").join(Iterables.transform(errors, new Function<Throwable, String>() { |
|
117 |
@Override |
|
118 |
public String apply(final Throwable e) { |
|
119 |
return ExceptionUtils.getFullStackTrace(e); |
|
120 |
} |
|
121 |
}))); |
|
122 |
} |
|
123 |
|
|
124 |
return datasourceInfo; |
|
125 |
} |
|
126 |
|
|
127 |
private ListenableFuture<Datasource> getDatasourceDetails(final String dsId) { |
|
128 |
return service.submit(new Callable<Datasource>() { |
|
129 |
@Override |
|
130 |
public Datasource call() throws DatasourceException { |
|
131 |
return jdbcDatasourceDao.getDatasource(dsId); |
|
132 |
} |
|
133 |
}); |
|
134 |
} |
|
135 |
|
|
136 |
private ListenableFuture<IndexDsInfo> calculateCurrentIndexDsInfo() { |
|
137 |
return service.submit(new Callable<IndexDsInfo>() { |
|
138 |
public IndexDsInfo call() throws DatasourceException { |
|
139 |
return lookupClient.calculateCurrentIndexDsInfo(); |
|
140 |
} |
|
141 |
}); |
|
142 |
} |
|
143 |
|
|
144 |
private ListenableFuture<IndexRecordsInfo> getIndexInfo(final String dsId, final IndexDsInfo info) { |
|
145 |
return service.submit(new Callable<IndexRecordsInfo>() { |
|
146 |
public IndexRecordsInfo call() throws DatasourceException { |
|
147 |
return datasourceIndexClient.getIndexInfo(dsId, info); |
|
148 |
} |
|
149 |
}); |
|
150 |
} |
|
151 |
|
|
152 |
private ListenableFuture<List<AggregationInfo>> getAggregationHistory(final String dsId) { |
|
153 |
return service.submit(new Callable<List<AggregationInfo>>() { |
|
154 |
public List<AggregationInfo> call() throws DatasourceException { |
|
155 |
return mongoLoggerClient.getAggregationHistory(dsId); |
|
156 |
} |
|
157 |
}); |
|
158 |
} |
|
159 |
|
|
160 |
private void setAggregationHistory(DatasourceInfo datasourceInfo, final List<AggregationInfo> info) { |
|
161 |
datasourceInfo.setAggregationHistory(info); |
|
162 |
if (!info.isEmpty()) { |
|
163 |
datasourceInfo.setLastCollection(Iterables.find(info, new Predicate<AggregationInfo>() { |
|
164 |
@Override |
|
165 |
public boolean apply(final AggregationInfo a) { |
|
166 |
return AggregationStage.COLLECT.equals(a.getAggregationStage()); |
|
167 |
} |
|
168 |
})).setLastTransformation(Iterables.find(info, new Predicate<AggregationInfo>() { |
|
169 |
@Override |
|
170 |
public boolean apply(final AggregationInfo a) { |
|
171 |
return AggregationStage.TRANSFORM.equals(a.getAggregationStage()); |
|
172 |
} |
|
173 |
})); |
|
174 |
} |
|
175 |
} |
|
176 |
|
|
177 |
private void waitLatch(final CountDownLatch latch, final Queue<Throwable> errors, final int waitSeconds) { |
|
178 |
try { |
|
179 |
if (!latch.await(waitSeconds, TimeUnit.SECONDS)) { |
|
180 |
errors.offer(new TimeoutException("Waiting for requests to complete has timed out.")); |
|
181 |
} |
|
182 |
} catch (final InterruptedException e) { |
|
183 |
errors.offer(e); |
|
184 |
} |
|
185 |
} |
|
186 |
|
|
187 |
} |
modules/dnet-datasource-rest-api/tags/dnet-datasource-rest-api-1.0.0/src/main/java/eu/dnetlib/datasource/publisher/clients/MongoLoggerClient.java | ||
---|---|---|
1 |
package eu.dnetlib.datasource.publisher.clients; |
|
2 |
|
|
3 |
import java.util.List; |
|
4 |
|
|
5 |
import com.google.common.base.Function; |
|
6 |
import com.google.common.collect.Iterables; |
|
7 |
import com.google.common.collect.Lists; |
|
8 |
import com.google.common.primitives.Ints; |
|
9 |
import com.mongodb.BasicDBObject; |
|
10 |
import com.mongodb.MongoClient; |
|
11 |
import com.mongodb.client.FindIterable; |
|
12 |
import com.mongodb.client.MongoCollection; |
|
13 |
import com.mongodb.client.model.Filters; |
|
14 |
import com.mongodb.client.model.Projections; |
|
15 |
import eu.dnetlib.datasource.publisher.DatasourceException; |
|
16 |
import eu.dnetlib.datasource.publisher.model.AggregationInfo; |
|
17 |
import eu.dnetlib.datasource.publisher.model.AggregationStage; |
|
18 |
import eu.dnetlib.datasource.publisher.model.CollectionMode; |
|
19 |
import eu.dnetlib.datasource.publisher.model.DatasourceFunctions; |
|
20 |
import eu.dnetlib.miscutils.datetime.DateUtils; |
|
21 |
import org.apache.commons.lang.StringUtils; |
|
22 |
import org.apache.commons.lang.time.DateFormatUtils; |
|
23 |
import org.apache.commons.logging.Log; |
|
24 |
import org.apache.commons.logging.LogFactory; |
|
25 |
import org.bson.Document; |
|
26 |
import org.bson.conversions.Bson; |
|
27 |
import org.springframework.beans.factory.annotation.Autowired; |
|
28 |
import org.springframework.beans.factory.annotation.Value; |
|
29 |
import org.springframework.cache.annotation.CacheEvict; |
|
30 |
import org.springframework.cache.annotation.Cacheable; |
|
31 |
|
|
32 |
/** |
|
33 |
* Created by claudio on 20/10/2016. |
|
34 |
*/ |
|
35 |
public class MongoLoggerClient { |
|
36 |
|
|
37 |
private static final Log log = LogFactory.getLog(MongoLoggerClient.class); |
|
38 |
|
|
39 |
@Autowired |
|
40 |
private MongoClient datasourcePublisherMongoClient; |
|
41 |
@Value("${datasource.publisher.mongodb.collection}") |
|
42 |
private String collectionName; |
|
43 |
@Value("${datasource.publisher.mongodb.db}") |
|
44 |
private String dbName; |
|
45 |
@Value("${datasource.publisher.mongodb.query.limit}") |
|
46 |
private int limit; |
|
47 |
|
|
48 |
private static Bson fields = getFields(); |
|
49 |
|
|
50 |
private static MongoCollection<Document> collection = null; |
|
51 |
|
|
52 |
private Bson getQuery(final String dsId) { |
|
53 |
return Filters.and(dbo("parentDatasourceId", dsId), dbo("system:profileFamily", "aggregator"), dbo("system:isCompletedSuccessfully", "true")); |
|
54 |
} |
|
55 |
|
|
56 |
private synchronized MongoCollection<Document> getCollection() { |
|
57 |
if (collection == null) { |
|
58 |
collection = datasourcePublisherMongoClient.getDatabase(dbName).getCollection(collectionName); |
|
59 |
} |
|
60 |
return collection; |
|
61 |
} |
|
62 |
|
|
63 |
@Cacheable("datasources-mongo-cache") |
|
64 |
public List<AggregationInfo> getAggregationHistory(final String dsId) throws DatasourceException { |
|
65 |
|
|
66 |
log.warn("getAggregationHistory(): not using cache"); |
|
67 |
|
|
68 |
final Bson query = getQuery(dsId); |
|
69 |
|
|
70 |
final FindIterable<Document> iterable = getCollection().find(query).projection(fields).limit(limit).sort(dbo("system:startHumanDate", -1)); |
|
71 |
final Iterable<AggregationInfo> transform = Iterables.transform(iterable, new Function<Document, AggregationInfo>() { |
|
72 |
|
|
73 |
@Override |
|
74 |
public AggregationInfo apply(final Document d) { |
|
75 |
final AggregationInfo info = new AggregationInfo(); |
|
76 |
info.setAggregationStage(AggregationStage.parse(d.getString("system:wfName"))); |
|
77 |
if (AggregationStage.COLLECT.equals(info.getAggregationStage())) { |
|
78 |
info.setCollectionMode(CollectionMode.valueOf(d.getString("system:node:SELECT_MODE:selection"))); |
|
79 |
} |
|
80 |
|
|
81 |
info.setNumberOfRecords(getNumberOfRecords(d)); |
|
82 |
info.setDate(getDate(d)); |
|
83 |
return info; |
|
84 |
} |
|
85 |
|
|
86 |
private String getDate(final Document d) { |
|
87 |
return DateFormatUtils.format(new DateUtils().parse(d.getString("system:startHumanDate")), DatasourceFunctions.DATE_FORMAT); |
|
88 |
} |
|
89 |
|
|
90 |
private Integer getNumberOfRecords(final Document d) { |
|
91 |
final String sinkSize = d.getString("mainlog:sinkSize"); |
|
92 |
final String total = d.getString("mainlog:total"); |
|
93 |
|
|
94 |
if(StringUtils.isNotBlank(sinkSize)) { |
|
95 |
return Ints.tryParse(sinkSize); |
|
96 |
} else if(StringUtils.isNotBlank(total)) { |
|
97 |
return Ints.tryParse(total); |
|
98 |
} else { |
|
99 |
return -1; |
|
100 |
} |
|
101 |
} |
|
102 |
}); |
|
103 |
|
|
104 |
return Lists.newArrayList(transform); |
|
105 |
} |
|
106 |
|
|
107 |
private static Bson getFields() { |
|
108 |
return Projections.fields(dbo("system:wfName", 1), |
|
109 |
dbo("system:node:SELECT_MODE:selection", 1), |
|
110 |
dbo("mainlog:sinkSize", 1), |
|
111 |
dbo("mainlog:writeOps", 1), |
|
112 |
dbo("mainlog:total", 1), |
|
113 |
dbo("system:startHumanDate", 1), |
|
114 |
dbo("system:profileName", 1)); |
|
115 |
} |
|
116 |
|
|
117 |
private static BasicDBObject dbo(final String key, final Object value) { |
|
118 |
return new BasicDBObject(key, value); |
|
119 |
} |
|
120 |
|
|
121 |
|
|
122 |
} |
modules/dnet-datasource-rest-api/tags/dnet-datasource-rest-api-1.0.0/src/main/java/eu/dnetlib/datasource/publisher/DatasourceException.java | ||
---|---|---|
1 |
package eu.dnetlib.datasource.publisher; |
|
2 |
|
|
3 |
/** |
|
4 |
* Created by claudio on 25/11/2016. |
|
5 |
*/ |
|
6 |
public class DatasourceException extends Exception { |
|
7 |
|
|
8 |
public DatasourceException(final String message) { |
|
9 |
super(message); |
|
10 |
} |
|
11 |
|
|
12 |
public DatasourceException(final Throwable e) { |
|
13 |
super(e); |
|
14 |
} |
|
15 |
|
|
16 |
public DatasourceException(final String message, final Throwable e) { |
|
17 |
super(message, e); |
|
18 |
} |
|
19 |
} |
modules/dnet-datasource-rest-api/tags/dnet-datasource-rest-api-1.0.0/src/main/java/eu/dnetlib/datasource/publisher/DatasourceExceptionController.java | ||
---|---|---|
1 |
package eu.dnetlib.datasource.publisher; |
|
2 |
|
|
3 |
import org.springframework.http.HttpStatus; |
|
4 |
import org.springframework.web.bind.annotation.ControllerAdvice; |
|
5 |
import org.springframework.web.bind.annotation.ExceptionHandler; |
|
6 |
import org.springframework.web.bind.annotation.ResponseStatus; |
|
7 |
import org.springframework.web.servlet.ModelAndView; |
|
8 |
|
|
9 |
@ControllerAdvice |
|
10 |
public class DatasourceExceptionController { |
|
11 |
|
|
12 |
@ResponseStatus(HttpStatus.NOT_FOUND) |
|
13 |
@ExceptionHandler(DatasourceException.class) |
|
14 |
public ModelAndView exception(final DatasourceException e) { |
|
15 |
|
|
16 |
ModelAndView mav = new ModelAndView("error"); |
|
17 |
mav.addObject("message", e.getMessage()); |
|
18 |
return mav; |
|
19 |
} |
|
20 |
} |
Also available in: Unified diff
implementation moved in dnet-openaire-exporter