1
|
package eu.dnetlib.functionality.modular.ui.repositories;
|
2
|
|
3
|
import java.io.StringReader;
|
4
|
import java.util.ArrayList;
|
5
|
import java.util.Collections;
|
6
|
import java.util.List;
|
7
|
import java.util.Map;
|
8
|
import java.util.Set;
|
9
|
|
10
|
import org.antlr.stringtemplate.StringTemplate;
|
11
|
import org.apache.commons.io.IOUtils;
|
12
|
import org.apache.commons.lang3.StringUtils;
|
13
|
import org.apache.commons.logging.Log;
|
14
|
import org.apache.commons.logging.LogFactory;
|
15
|
import org.dom4j.Document;
|
16
|
import org.dom4j.Node;
|
17
|
import org.dom4j.io.SAXReader;
|
18
|
import org.springframework.beans.factory.annotation.Autowired;
|
19
|
|
20
|
import com.google.common.collect.Lists;
|
21
|
import com.google.common.collect.Maps;
|
22
|
import com.google.common.collect.Sets;
|
23
|
|
24
|
import eu.dnetlib.data.collector.rmi.CollectorService;
|
25
|
import eu.dnetlib.data.collector.rmi.ProtocolDescriptor;
|
26
|
import eu.dnetlib.data.collector.rmi.ProtocolParameter;
|
27
|
import eu.dnetlib.enabling.datasources.common.Api;
|
28
|
import eu.dnetlib.enabling.datasources.common.ApiParam;
|
29
|
import eu.dnetlib.enabling.datasources.common.Datasource;
|
30
|
import eu.dnetlib.enabling.datasources.common.LocalDatasourceManager;
|
31
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException;
|
32
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
|
33
|
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
|
34
|
import eu.dnetlib.functionality.modular.ui.repositories.objects.RepoInterfaceEntry;
|
35
|
import eu.dnetlib.functionality.modular.ui.repositories.objects.RepoMetaWfEntry;
|
36
|
import eu.dnetlib.functionality.modular.ui.repositories.objects.SimpleParamEntry;
|
37
|
import eu.dnetlib.functionality.modular.ui.repositories.objects.VocabularyEntry;
|
38
|
import eu.dnetlib.miscutils.datetime.DateUtils;
|
39
|
import eu.dnetlib.msro.workflows.util.WorkflowsConstants.WorkflowStatus;
|
40
|
|
41
|
public class RepoUIUtils {
|
42
|
|
43
|
@Autowired
|
44
|
private UniqueServiceLocator serviceLocator;
|
45
|
@Autowired
|
46
|
private LocalDatasourceManager<Datasource<?, ?>, Api<?>> dsManager;
|
47
|
|
48
|
private static final Log log = LogFactory.getLog(RepoUIUtils.class);
|
49
|
|
50
|
private final Map<String, List<ProtocolParameter>> parametersMap = Maps.newHashMap();
|
51
|
|
52
|
public RepoInterfaceEntry getApi(final String dsId, final String apiId) throws Exception {
|
53
|
final RepoInterfaceEntry ifc = new RepoInterfaceEntry();
|
54
|
|
55
|
final Datasource<?, ?> ds = dsManager.getDs(dsId);
|
56
|
|
57
|
final Api<?> api = dsManager.getApis(dsId)
|
58
|
.stream()
|
59
|
.filter(a -> a.getId().equals(apiId))
|
60
|
.findFirst()
|
61
|
.orElseThrow(() -> new Exception("Api not found"));
|
62
|
|
63
|
final List<?> metaWfs = new ArrayList<>(); // TODO
|
64
|
|
65
|
ifc.setId(api.getId());
|
66
|
ifc.setLabel(String.format("%s (%s)", StringUtils.defaultIfBlank(api.getTypology(), "-"), StringUtils.defaultIfBlank(api.getCompatibility(), "-")));
|
67
|
ifc.setRemovable(api.getRemovable() && metaWfs.isEmpty());
|
68
|
ifc.setRepoId(ds.getId());
|
69
|
ifc.setRepoName(ds.getOfficialname());
|
70
|
ifc.setRepoCountry(ds.getOrganizations()
|
71
|
.stream()
|
72
|
.map(o -> o.getCountry())
|
73
|
.filter(o -> o != null)
|
74
|
.findFirst()
|
75
|
.orElse("UNKNOWN"));
|
76
|
ifc.setRepoPrefix(ds.getNamespaceprefix());
|
77
|
ifc.setRepoType(ds.getTypology());
|
78
|
ifc.setEmail(ds.getContactemail());
|
79
|
ifc.setActive(api.getActive());
|
80
|
ifc.setProtocol(api.getProtocol());
|
81
|
|
82
|
final Set<String> toVerifyParams = getParameterNamesForProtocol(ifc.getProtocol());
|
83
|
ifc.getAccessParams().add(new SimpleParamEntry("baseUrl", api.getBaseurl()));
|
84
|
|
85
|
for (final ApiParam p : api.getApiParams()) {
|
86
|
if (toVerifyParams.contains(p.getParam())) {
|
87
|
ifc.getAccessParams().add(new SimpleParamEntry(p.getParam(), p.getValue()));
|
88
|
toVerifyParams.remove(p.getParam());
|
89
|
} else {
|
90
|
log.warn("Invalid param [" + p.getParam() + "] for protocol " + p.getValue() + " in ds " + dsId);
|
91
|
}
|
92
|
}
|
93
|
|
94
|
for (final String pname : toVerifyParams) {
|
95
|
ifc.getAccessParams().add(new SimpleParamEntry(pname, ""));
|
96
|
log.info("Adding missing param [" + pname + "] for protocol " + api.getProtocol() + " in ds " + dsId);
|
97
|
}
|
98
|
|
99
|
ifc.setMetadataIdentifierPath(api.getMetadataIdentifierPath());
|
100
|
ifc.setCompliance(api.getCompatibility());
|
101
|
ifc.setComplianceOverrided(api.isCompatibilityOverrided());
|
102
|
|
103
|
if (api.getLastAggregationDate() != null) {
|
104
|
ifc.setAggrDate(DateUtils.calculate_ISO8601(api.getLastAggregationDate().getTime()));
|
105
|
ifc.setAggrTotal(api.getLastAggregationTotal());
|
106
|
ifc.setAggrMdId(api.getLastAggregationMdid());
|
107
|
}
|
108
|
if (api.getLastCollectionDate() != null) {
|
109
|
ifc.setCollDate(DateUtils.calculate_ISO8601(api.getLastCollectionDate().getTime()));
|
110
|
ifc.setCollTotal(api.getLastCollectionTotal());
|
111
|
ifc.setCollMdId(api.getLastCollectionMdid());
|
112
|
}
|
113
|
if (api.getLastDownloadDate() != null) {
|
114
|
ifc.setDownloadDate(DateUtils.calculate_ISO8601(api.getLastDownloadDate().getTime()));
|
115
|
ifc.setDownloadTotal(api.getLastDownloadTotal());
|
116
|
ifc.setDownloadObjId(api.getLastDownloadObjid());
|
117
|
}
|
118
|
|
119
|
final StringTemplate st = new StringTemplate(IOUtils.toString(getClass()
|
120
|
.getResourceAsStream("/eu/dnetlib/functionality/modular/ui/repositories/templates/getMetaWfs.xquery.st")));
|
121
|
st.setAttribute("dsId", dsId);
|
122
|
st.setAttribute("apiId", apiId);
|
123
|
|
124
|
final String s = serviceLocator.getService(ISLookUpService.class).getResourceProfileByQuery(st.toString());
|
125
|
|
126
|
final Document doc = new SAXReader().read(new StringReader(s));
|
127
|
|
128
|
for (final Object o : doc.selectNodes("//metaWF")) {
|
129
|
final Node n = (Node) o;
|
130
|
|
131
|
final String id = n.valueOf("./id");
|
132
|
final String name = n.valueOf("./name");
|
133
|
final String status = n.valueOf("./status");
|
134
|
final String repoByeWfId = n.valueOf("./destroyWorkflow");
|
135
|
|
136
|
int progress = 0;
|
137
|
try {
|
138
|
switch (WorkflowStatus.valueOf(status)) {
|
139
|
case MISSING:
|
140
|
progress = 0;
|
141
|
break;
|
142
|
case ASSIGNED:
|
143
|
progress = 25;
|
144
|
break;
|
145
|
case WAIT_SYS_SETTINGS:
|
146
|
progress = 50;
|
147
|
break;
|
148
|
case WAIT_USER_SETTINGS:
|
149
|
progress = 75;
|
150
|
break;
|
151
|
case EXECUTABLE:
|
152
|
progress = 100;
|
153
|
break;
|
154
|
}
|
155
|
} catch (final Exception e) {
|
156
|
progress = 0;
|
157
|
}
|
158
|
ifc.getMetaWFs().add(new RepoMetaWfEntry(id, name, status, repoByeWfId, progress));
|
159
|
}
|
160
|
|
161
|
return ifc;
|
162
|
}
|
163
|
|
164
|
public List<VocabularyEntry> fetchVocabularyTerms(final String voc) throws ISLookUpException {
|
165
|
final String xquery = "for $x in collection('/db/DRIVER/VocabularyDSResources/VocabularyDSResourceType')[.//VOCABULARY_NAME/@code = '"
|
166
|
+ voc.trim() + "']//TERM return concat($x/@code, ' @@@ ', $x/@english_name)";
|
167
|
|
168
|
final List<VocabularyEntry> list = Lists.newArrayList();
|
169
|
for (final String s : serviceLocator.getService(ISLookUpService.class).quickSearchProfile(xquery)) {
|
170
|
final String[] arr = s.split("@@@");
|
171
|
list.add(new VocabularyEntry(arr[0].trim(), arr[1].trim()));
|
172
|
}
|
173
|
Collections.sort(list);
|
174
|
|
175
|
return list;
|
176
|
}
|
177
|
|
178
|
private Set<String> getParameterNamesForProtocol(final String protocol) {
|
179
|
if (parametersMap.isEmpty()) {
|
180
|
for (final ProtocolDescriptor d : serviceLocator.getService(CollectorService.class).listProtocols()) {
|
181
|
parametersMap.put(d.getName().toLowerCase(), d.getParams());
|
182
|
}
|
183
|
}
|
184
|
final Set<String> res = Sets.newHashSet();
|
185
|
if (parametersMap.containsKey(protocol.toLowerCase())) {
|
186
|
for (final ProtocolParameter p : parametersMap.get(protocol.toLowerCase())) {
|
187
|
res.add(p.getName());
|
188
|
}
|
189
|
}
|
190
|
|
191
|
return res;
|
192
|
}
|
193
|
|
194
|
}
|