Revision 44352
Added by Sandro La Bruzzo over 7 years ago
DLIUtils.java | ||
---|---|---|
1 | 1 |
package eu.dnetlib.dli; |
2 | 2 |
|
3 |
import java.util.HashMap; |
|
4 |
import java.util.List; |
|
5 |
import java.util.Map; |
|
6 |
import javax.annotation.PostConstruct; |
|
7 |
|
|
3 | 8 |
import com.google.common.collect.BiMap; |
4 | 9 |
import com.google.common.collect.HashBiMap; |
10 |
import eu.dnetlib.enabling.locators.UniqueServiceLocator; |
|
5 | 11 |
import eu.dnetlib.miscutils.functional.xml.DnetXsltFunctions; |
12 |
import eu.dnetlib.rmi.enabling.ISLookUpException; |
|
13 |
import eu.dnetlib.rmi.enabling.ISLookUpService; |
|
6 | 14 |
import org.apache.commons.lang3.StringUtils; |
15 |
import org.apache.commons.lang3.tuple.ImmutablePair; |
|
16 |
import org.apache.commons.lang3.tuple.Pair; |
|
17 |
import org.springframework.beans.factory.annotation.Autowired; |
|
7 | 18 |
|
8 | 19 |
public class DLIUtils { |
9 | 20 |
|
21 |
public final static Map<String, Pair<String, String>> datasources = new HashMap<>(); |
|
22 |
private static DLIUtils instance; |
|
10 | 23 |
private static BiMap<String, String> relations; |
24 |
@Autowired |
|
25 |
private UniqueServiceLocator serviceLocator; |
|
11 | 26 |
|
12 | 27 |
private static BiMap<String, String> getRelationMap() { |
13 | 28 |
|
... | ... | |
20 | 35 |
return relations; |
21 | 36 |
} |
22 | 37 |
|
38 |
public static String getNameFromDataSourcePrefix(final String datasourcePrefix) throws ISLookUpException { |
|
39 |
if (datasources.keySet() == null || datasources.keySet().size() == 0) { |
|
40 |
generateDSMap(); |
|
41 |
} |
|
42 |
if (!datasources.containsKey(datasourcePrefix)) |
|
43 |
return ""; |
|
44 |
return datasources.get(datasourcePrefix).getRight(); |
|
45 |
} |
|
46 |
|
|
47 |
public static String getIdFromDataSourcePrefix(final String datasourcePrefix) throws ISLookUpException { |
|
48 |
if (datasources.keySet() == null || datasources.keySet().size() == 0) { |
|
49 |
generateDSMap(); |
|
50 |
} |
|
51 |
if (!datasources.containsKey(datasourcePrefix)) |
|
52 |
return ""; |
|
53 |
return datasources.get(datasourcePrefix).getLeft(); |
|
54 |
} |
|
55 |
|
|
56 |
public static void generateDSMap() throws ISLookUpException { |
|
57 |
if (datasources.keySet() != null && datasources.keySet().size() > 0) |
|
58 |
return; |
|
59 |
|
|
60 |
final String query = "for $x in collection('/db/DRIVER/RepositoryServiceResources/RepositoryServiceResourceType') " |
|
61 |
+ "return concat($x//FIELD[./key='NamespacePrefix']/value/text(),'@--@',$x//FIELD[./key='DataSourceId']/value/text(),'@--@',$x//ENGLISH_NAME )"; |
|
62 |
final List<String> results = instance.serviceLocator.getService(ISLookUpService.class).quickSearchProfile(query); |
|
63 |
datasources.clear(); |
|
64 |
if (results != null) |
|
65 |
results.forEach(it -> { |
|
66 |
final String[] splitted = it.split("@--@"); |
|
67 |
if (splitted != null && splitted.length == 3) { |
|
68 |
datasources.put(splitted[0], new ImmutablePair<>(splitted[1], splitted[2])); |
|
69 |
} |
|
70 |
}); |
|
71 |
} |
|
72 |
|
|
23 | 73 |
public static String fixPID(String input) { |
24 | 74 |
if (input != null) { |
25 | 75 |
return input.replace("http://dx.doi.org/", "").replace("http://doi.org/", ""); |
... | ... | |
44 | 94 |
throw new RuntimeException("Error pid or pidtype cannot be null"); |
45 | 95 |
return DnetXsltFunctions.md5(String.format("%s::%s", pid.trim(), pidtype.toLowerCase().trim())); |
46 | 96 |
} |
97 |
|
|
98 |
@PostConstruct |
|
99 |
public void registerInstance() throws ISLookUpException { |
|
100 |
instance = this; |
|
101 |
//DLIUtils.generateDSMap(); |
|
102 |
} |
|
103 |
|
|
104 |
public UniqueServiceLocator getServiceLocator() { |
|
105 |
return serviceLocator; |
|
106 |
} |
|
107 |
|
|
108 |
public void setServiceLocator(final UniqueServiceLocator serviceLocator) { |
|
109 |
this.serviceLocator = serviceLocator; |
|
110 |
} |
|
47 | 111 |
} |
Also available in: Unified diff
fixed bug