Project

General

Profile

1
package eu.dnetlib.wds.utils;
2

    
3
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
4
import eu.dnetlib.miscutils.collections.Pair;
5
import eu.dnetlib.rmi.enabling.ISLookUpException;
6
import eu.dnetlib.rmi.enabling.ISLookUpService;
7
import org.apache.commons.lang3.StringEscapeUtils;
8
import org.apache.commons.lang3.StringUtils;
9
import org.springframework.beans.factory.annotation.Autowired;
10

    
11
import javax.annotation.PostConstruct;
12
import java.util.HashMap;
13
import java.util.List;
14
import java.util.Map;
15
import java.util.stream.Collectors;
16

    
17
public class WDSUtils {
18

    
19

    
20
    public final static Map<String, Pair<String, String>> hostedByMap = new HashMap<>();
21
    public final static Map<String, Pair<String, String>> collectedFromMap = new HashMap<>();
22
    public static String HOSTED_BY_MAP_QUERY = "for $x in collection('/db/DRIVER/RepositoryServiceResources/RepositoryServiceResourceType') " +
23
            "where $x//FIELD/key/text()= 'archive_center'  return " +
24
            "concat($x//DATASOURCE_ORIGINAL_ID, '<-->', $x//FIELD[./key/text()= 'archive_center' ]/value/string(), '<-->', $x//OFFICIAL_NAME)";
25

    
26

    
27
    public static String COLLECTEDFROM_DATASOURCE_MAP_QUERY = "for $x in collection('/db/DRIVER/RepositoryServiceResources/RepositoryServiceResourceType')  " +
28
            "where $x//FIELD/key/text()!= 'archive_center'  return  concat($x//DATASOURCE_ORIGINAL_ID, '<-->', " +
29
            "$x//FIELD[./key/text()= 'NamespacePrefix' ]/value/string(), '<-->', $x//OFFICIAL_NAME)";
30

    
31
    private static WDSUtils instance;
32
    @Autowired
33
    private UniqueServiceLocator serviceLocator;
34

    
35

    
36
    public static void generateDSMapFromQuery(final Map<String, Pair<String, String>> aMap, final String aQUERY) throws Exception {
37
        final ISLookUpService lookUpService = instance.getServiceLocator().getService(ISLookUpService.class);
38
        final List<String> hostedByList = lookUpService.quickSearchProfile(aQUERY);
39
        aMap.clear();
40
        aMap.putAll(
41
                hostedByList.stream()
42
                        .map(s -> s.split("<-->"))
43
                        .map(s -> new Pair<>(StringEscapeUtils.escapeXml11(s[1]), new Pair<>(s[0], s[2])))
44
                        .collect(Collectors.toMap(
45
                                Pair::getKey,
46
                                Pair::getValue)));
47
    }
48

    
49
    public static String getNameFromDataSourcePrefix(final String datasourcePrefix) throws Exception {
50
        if (collectedFromMap.keySet().size() == 0) {
51
            generateDSMapFromQuery(collectedFromMap, COLLECTEDFROM_DATASOURCE_MAP_QUERY);
52
        }
53
        if (!collectedFromMap.containsKey(datasourcePrefix))
54
            return "";
55
        return collectedFromMap.get(datasourcePrefix).getValue();
56
    }
57

    
58

    
59
    public static String getIdFromDataSourcePrefix(final String datasourcePrefix) throws Exception {
60
        if (collectedFromMap.keySet().size() == 0) {
61
            generateDSMapFromQuery(collectedFromMap, COLLECTEDFROM_DATASOURCE_MAP_QUERY);
62
        }
63
        if (!collectedFromMap.containsKey(datasourcePrefix))
64
            return "";
65
        return collectedFromMap.get(datasourcePrefix).getKey();
66
    }
67

    
68

    
69
    public static String getDatasourceName(final String dataCenter) throws Exception {
70
        if (hostedByMap.keySet().isEmpty()) {
71
            generateDSMapFromQuery(hostedByMap, HOSTED_BY_MAP_QUERY);
72
        }
73

    
74
        final Pair<String, String> result = hostedByMap.get(dataCenter);
75
        return result == null ? null : result.getValue();
76
    }
77

    
78

    
79
    public static String getDatasourceId(final String dataCenter) throws Exception {
80
        if (hostedByMap.keySet().isEmpty()) {
81
            generateDSMapFromQuery(hostedByMap, HOSTED_BY_MAP_QUERY);
82
        }
83

    
84
        final Pair<String, String> result = hostedByMap.get(dataCenter);
85
        return result == null ? null : result.getKey();
86
    }
87

    
88

    
89
    public static String generateWDSNsPrefix(final String input, final String prefix, final int maxLength) {
90
        if (StringUtils.isNotEmpty(input) && StringUtils.isNotEmpty(prefix)) {
91
            String cleanedString = input.replace("/", "_");
92
            int remainingLength = maxLength - prefix.length();
93
            if (cleanedString.length() > remainingLength)
94
                cleanedString = cleanedString.substring(0, remainingLength);
95
            else
96
                cleanedString = StringUtils.rightPad(cleanedString, remainingLength, "_");
97
            return prefix + cleanedString;
98
        }
99
        return null;
100
    }
101

    
102

    
103
    /**
104
     * This method is used only for test Scope
105
     *
106
     * @param mockInstance
107
     */
108
    public static void setInstance(final WDSUtils mockInstance) {
109
        instance = mockInstance;
110
    }
111

    
112
    @PostConstruct
113
    public void registerInstance() throws Exception {
114
        instance = this;
115
    }
116

    
117

    
118
    public UniqueServiceLocator getServiceLocator() {
119
        return serviceLocator;
120
    }
121

    
122
    public void setServiceLocator(UniqueServiceLocator serviceLocator) {
123
        this.serviceLocator = serviceLocator;
124
    }
125
}
    (1-1/1)