1
|
package eu.dnetlib.wds.utils;
|
2
|
|
3
|
import eu.dnetlib.data.transform.Ontologies;
|
4
|
import eu.dnetlib.data.transform.OntologyLoader;
|
5
|
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
|
6
|
import eu.dnetlib.miscutils.collections.Pair;
|
7
|
import eu.dnetlib.miscutils.functional.xml.DnetXsltFunctions;
|
8
|
import eu.dnetlib.rmi.enabling.ISLookUpException;
|
9
|
import eu.dnetlib.rmi.enabling.ISLookUpService;
|
10
|
import org.apache.commons.lang3.StringEscapeUtils;
|
11
|
import org.apache.commons.lang3.StringUtils;
|
12
|
import org.springframework.beans.factory.annotation.Autowired;
|
13
|
|
14
|
import javax.annotation.PostConstruct;
|
15
|
import java.util.HashMap;
|
16
|
import java.util.List;
|
17
|
import java.util.Map;
|
18
|
import java.util.stream.Collectors;
|
19
|
|
20
|
public class WDSUtils {
|
21
|
|
22
|
public static Ontologies ontologies;
|
23
|
|
24
|
|
25
|
public final static Map<String, Pair<String, String>> hostedByMap = new HashMap<>();
|
26
|
public final static Map<String, Pair<String, String>> collectedFromMap = new HashMap<>();
|
27
|
public static String HOSTED_BY_MAP_QUERY = "for $x in collection('/db/DRIVER/RepositoryServiceResources/RepositoryServiceResourceType') " +
|
28
|
"where $x//FIELD/key/text()= 'archive_center' return " +
|
29
|
"concat($x//DATASOURCE_ORIGINAL_ID, '<-->', $x//FIELD[./key/text()= 'archive_center' ]/value/string(), '<-->', $x//OFFICIAL_NAME)";
|
30
|
|
31
|
|
32
|
public static String COLLECTEDFROM_DATASOURCE_MAP_QUERY = "for $x in collection('/db/DRIVER/RepositoryServiceResources/RepositoryServiceResourceType') " +
|
33
|
"where $x//FIELD/key/text()!= 'archive_center' return concat($x//DATASOURCE_ORIGINAL_ID, '<-->', " +
|
34
|
"$x//FIELD[./key/text()= 'NamespacePrefix' ]/value/string(), '<-->', $x//OFFICIAL_NAME)";
|
35
|
|
36
|
private static WDSUtils instance;
|
37
|
|
38
|
|
39
|
@Autowired
|
40
|
private UniqueServiceLocator serviceLocator;
|
41
|
|
42
|
public static String getInverse(final String relation) throws Exception {
|
43
|
if (ontologies == null) {
|
44
|
ontologies = OntologyLoader.loadOntologiesFromIS();
|
45
|
}
|
46
|
final String normalizedRelation = normalizeRelation(relation);
|
47
|
|
48
|
|
49
|
try {
|
50
|
return ontologies.getTerms(normalizedRelation).stream().findFirst().get().getInverseCode();
|
51
|
} catch (Throwable e) {
|
52
|
System.out.println("Relation not found = " + normalizedRelation);
|
53
|
return "related";
|
54
|
}
|
55
|
}
|
56
|
|
57
|
|
58
|
public static String normalizeRelation(final String relation) {
|
59
|
if (relation == null || StringUtils.isEmpty(relation)) {
|
60
|
return null;
|
61
|
}
|
62
|
return Character.toLowerCase(relation.charAt(0)) + relation.substring(1);
|
63
|
|
64
|
}
|
65
|
|
66
|
|
67
|
public static void generateDSMapFromQuery(final Map<String, Pair<String, String>> aMap, final String aQUERY) throws Exception {
|
68
|
final ISLookUpService lookUpService = instance.getServiceLocator().getService(ISLookUpService.class);
|
69
|
final List<String> hostedByList = lookUpService.quickSearchProfile(aQUERY);
|
70
|
aMap.clear();
|
71
|
aMap.putAll(
|
72
|
hostedByList.stream()
|
73
|
.map(s -> s.split("<-->"))
|
74
|
.map(s -> new Pair<>(StringEscapeUtils.escapeXml11(s[1]), new Pair<>(s[0], s[2])))
|
75
|
.collect(Collectors.toMap(
|
76
|
Pair::getKey,
|
77
|
Pair::getValue)));
|
78
|
}
|
79
|
|
80
|
public static String getNameFromDataSourcePrefix(final String datasourcePrefix) throws Exception {
|
81
|
if (collectedFromMap.keySet().size() == 0) {
|
82
|
generateDSMapFromQuery(collectedFromMap, COLLECTEDFROM_DATASOURCE_MAP_QUERY);
|
83
|
}
|
84
|
if (!collectedFromMap.containsKey(datasourcePrefix))
|
85
|
return "";
|
86
|
return collectedFromMap.get(datasourcePrefix).getValue();
|
87
|
}
|
88
|
|
89
|
|
90
|
public static String getIdFromDataSourcePrefix(final String datasourcePrefix) throws Exception {
|
91
|
if (collectedFromMap.keySet().size() == 0) {
|
92
|
generateDSMapFromQuery(collectedFromMap, COLLECTEDFROM_DATASOURCE_MAP_QUERY);
|
93
|
}
|
94
|
if (!collectedFromMap.containsKey(datasourcePrefix))
|
95
|
return "";
|
96
|
return collectedFromMap.get(datasourcePrefix).getKey();
|
97
|
}
|
98
|
|
99
|
|
100
|
public static String getDatasourceName(final String dataCenter) throws Exception {
|
101
|
if (hostedByMap.keySet().isEmpty()) {
|
102
|
generateDSMapFromQuery(hostedByMap, HOSTED_BY_MAP_QUERY);
|
103
|
}
|
104
|
|
105
|
final Pair<String, String> result = hostedByMap.get(dataCenter);
|
106
|
return result == null ? null : result.getValue();
|
107
|
}
|
108
|
|
109
|
|
110
|
public static String getDatasourceId(final String dataCenter) throws Exception {
|
111
|
if (hostedByMap.keySet().isEmpty()) {
|
112
|
generateDSMapFromQuery(hostedByMap, HOSTED_BY_MAP_QUERY);
|
113
|
}
|
114
|
|
115
|
final Pair<String, String> result = hostedByMap.get(dataCenter);
|
116
|
return result == null ? null : result.getKey();
|
117
|
}
|
118
|
|
119
|
|
120
|
public static String generateWDSNsPrefix(final String input, final String prefix, final int maxLength) {
|
121
|
if (StringUtils.isNotEmpty(input) && StringUtils.isNotEmpty(prefix)) {
|
122
|
String cleanedString = input.replace("/", "_");
|
123
|
int remainingLength = maxLength - prefix.length();
|
124
|
if (cleanedString.length() > remainingLength)
|
125
|
cleanedString = cleanedString.substring(0, remainingLength);
|
126
|
else
|
127
|
cleanedString = StringUtils.rightPad(cleanedString, remainingLength, "_");
|
128
|
return prefix + cleanedString;
|
129
|
}
|
130
|
return null;
|
131
|
}
|
132
|
|
133
|
|
134
|
/**
|
135
|
* This method is used only for test Scope
|
136
|
*
|
137
|
* @param mockInstance
|
138
|
*/
|
139
|
public static void setInstance(final WDSUtils mockInstance) {
|
140
|
instance = mockInstance;
|
141
|
}
|
142
|
|
143
|
@PostConstruct
|
144
|
public void registerInstance() throws Exception {
|
145
|
instance = this;
|
146
|
}
|
147
|
|
148
|
|
149
|
public UniqueServiceLocator getServiceLocator() {
|
150
|
return serviceLocator;
|
151
|
}
|
152
|
|
153
|
public void setServiceLocator(UniqueServiceLocator serviceLocator) {
|
154
|
this.serviceLocator = serviceLocator;
|
155
|
}
|
156
|
|
157
|
public static String generateIdentifier(final String pid, final String pidtype) {
|
158
|
if (StringUtils.isBlank(pid) || StringUtils.isBlank(pidtype))
|
159
|
throw new RuntimeException("Error pid or pidtype cannot be null");
|
160
|
return DnetXsltFunctions.md5(String.format("%s::%s", pid.toLowerCase().trim(), pidtype.toLowerCase().trim()));
|
161
|
}
|
162
|
}
|