Project

General

Profile

1
package eu.dnetlib.msro.openaireplus.workflows.nodes.dhp;
2
import java.net.URI;
3
import java.util.HashMap;
4
import java.util.Map;
5

    
6
import com.google.gson.Gson;
7
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
8
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
9
import eu.dnetlib.msro.rmi.MSROException;
10
import org.apache.commons.lang3.StringUtils;
11
import org.apache.commons.logging.Log;
12
import org.apache.commons.logging.LogFactory;
13
import org.springframework.beans.factory.annotation.Required;
14
import org.springframework.web.client.RestTemplate;
15
import org.springframework.web.util.UriComponentsBuilder;
16

    
17
import com.googlecode.sarasvati.Arc;
18
import com.googlecode.sarasvati.NodeToken;
19

    
20
import eu.dnetlib.data.mdstore.manager.common.model.MDStoreWithInfo;
21
import eu.dnetlib.msro.workflows.nodes.SimpleJobNode;
22
import eu.dnetlib.msro.workflows.util.WorkflowsConstants;
23

    
24
import javax.annotation.Resource;
25

    
26
public class CreateMDStoreHadoopJobNode extends SimpleJobNode {
27

    
28
    private static final Log log = LogFactory.getLog(CreateMDStoreHadoopJobNode.class);
29

    
30
    /* Workflow params */
31
    private String format;
32
    private String layout;
33
    private String interpretation;
34
    private String outputPrefix = "mdstore";
35

    
36
    /* Spring managed params */
37
    private String mdStoreManagerUrl;
38

    
39
    @Resource
40
    private UniqueServiceLocator serviceLocator;
41

    
42
    @Override
43
    protected String execute(final NodeToken token) throws Exception {
44
        final String url = getMdStoreManagerUrl() + "/new/{format}/{layout}/{interpretation}";
45

    
46
        final Map<String, Object> params = new HashMap<>();
47
        params.put("format", getFormat());
48
        params.put("layout", getLayout());
49
        params.put("interpretation", getInterpretation());
50

    
51
        final String repositoryId = token.getFullEnv().getAttribute(WorkflowsConstants.DATAPROVIDER_ID);
52
        final String xquery = String.format(
53
                "for $x in collection('/db/DRIVER/RepositoryServiceResources/RepositoryServiceResourceType') \n" +
54
                "where .//RESOURCE_IDENTIFIER[./@value='%s']\n" +
55
                "return $x//EXTRA_FIELDS/FIELD[./key/text() = 'OpenAireDataSourceId']/value/text()",
56
                repositoryId);
57

    
58
        log.info("resolving openaAireDatasourceId: " + xquery);
59

    
60
        final String openAireDataSourceId = serviceLocator.getService(ISLookUpService.class).getResourceProfileByQuery(xquery);
61

    
62
        if (StringUtils.isBlank(openAireDataSourceId)) {
63
            throw new MSROException("unable to find openAireDataSourceId in repository profile " + repositoryId);
64
        }
65

    
66
        log.info(String.format("got OpenAIRE datasource id '%s' for repository profile '%s'", openAireDataSourceId, repositoryId));
67

    
68
        final URI uri = UriComponentsBuilder.fromUriString(url)
69
                .queryParam("dsName", token.getFullEnv().getAttribute(WorkflowsConstants.DATAPROVIDER_NAME))
70
                .queryParam("dsId", openAireDataSourceId)
71
                .queryParam("apiId", token.getFullEnv().getAttribute(WorkflowsConstants.DATAPROVIDER_INTERFACE))
72
                .buildAndExpand(params)
73
                .toUri();
74

    
75
        final String requestUri = uri.toASCIIString();
76
        log.info("create mdstore request: " + requestUri);
77

    
78
        final RestTemplate restTemplate = new RestTemplate();
79
        final MDStoreWithInfo result = restTemplate.getForObject(requestUri, MDStoreWithInfo.class);
80

    
81
        log.info("created mdstore: " + new Gson().toJson(result));
82

    
83
        token.getEnv().setAttribute(getOutputPrefix() + "format", format);
84
        token.getEnv().setAttribute(getOutputPrefix() + "layout", layout);
85
        token.getEnv().setAttribute(getOutputPrefix() + "interpretation", interpretation);
86
        token.getEnv().setAttribute(getOutputPrefix() + "id", result.getId());
87

    
88
        return Arc.DEFAULT_ARC;
89
    }
90

    
91
    public String getFormat() {
92
        return format;
93
    }
94

    
95
    public void setFormat(final String format) {
96
        this.format = format;
97
    }
98

    
99
    public String getLayout() {
100
        return layout;
101
    }
102

    
103
    public void setLayout(final String layout) {
104
        this.layout = layout;
105
    }
106

    
107
    public String getInterpretation() {
108
        return interpretation;
109
    }
110

    
111
    public void setInterpretation(final String interpretation) {
112
        this.interpretation = interpretation;
113
    }
114

    
115
    public String getOutputPrefix() {
116
        return outputPrefix;
117
    }
118

    
119
    public void setOutputPrefix(final String outputPrefix) {
120
        this.outputPrefix = outputPrefix;
121
    }
122

    
123
    public String getMdStoreManagerUrl() {
124
        return mdStoreManagerUrl;
125
    }
126

    
127
    @Required
128
    public void setMdStoreManagerUrl(final String mdStoreManagerUrl) {
129
        this.mdStoreManagerUrl = mdStoreManagerUrl;
130
    }
131

    
132
}
(3-3/11)