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