Project

General

Profile

1
package eu.dnetlib.msro.workflows.nodes.objectStore;
2

    
3
import java.util.HashSet;
4
import java.util.List;
5
import java.util.Set;
6

    
7
import com.google.common.collect.Lists;
8
import com.google.common.collect.Sets;
9
import com.google.gson.Gson;
10
import com.google.gson.GsonBuilder;
11
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
12
import eu.dnetlib.msro.workflows.graph.Arc;
13
import eu.dnetlib.msro.workflows.nodes.SimpleJobNode;
14
import eu.dnetlib.msro.workflows.procs.Env;
15
import eu.dnetlib.rmi.enabling.ISLookUpException;
16
import eu.dnetlib.rmi.enabling.ISLookUpService;
17
import org.springframework.beans.factory.annotation.Autowired;
18
import org.springframework.beans.factory.annotation.Required;
19

    
20
/**
21
 * The Class RetrieveMdStoreId is a job node used to retrieve the correct MDStore from which extract the url of the file to download.
22
 * metadata format and interpretation are injected as properties
23
 */
24
public class RetrieveMdStoreId extends SimpleJobNode {
25

    
26
	/**
27
	 * The metadata format.
28
	 */
29
	private String metadataFormat;
30

    
31
	/**
32
	 * The interpretation.
33
	 */
34
	private String interpretation;
35

    
36
	/**
37
	 * The provider id.
38
	 */
39
	private String providerId;
40

    
41
	/**
42
	 * The service locator.
43
	 */
44
	@Autowired
45
	private UniqueServiceLocator serviceLocator;
46

    
47
	@Override
48
	protected String execute(final Env env) throws Exception {
49

    
50
		final String workflowQuery =
51
				"for $x in collection('/db/DRIVER/MetaWorkflowDSResources/MetaWorkflowDSResourceType') where($x//DATAPROVIDER/@id='%s') return  distinct-values($x//WORKFLOW/@id/string())";
52

    
53
		final List<String> result = this.serviceLocator.getService(ISLookUpService.class).quickSearchProfile(String.format(workflowQuery, this.providerId));
54
		if (result.size() == 0) { throw new RuntimeException("there is no mdStore Associated to the provider " + env.getAttribute(getProviderId())); }
55
		final Set<String> workflowIds = Sets.newHashSet(result);
56

    
57
		final Set<String> metadataIds = getMdStores(workflowIds);
58
		final Gson g = new GsonBuilder().disableHtmlEscaping().create();
59
		env.setAttribute("mdId", g.toJson(metadataIds));
60

    
61
		env.setAttribute("mdFormat", getMetadataFormat());
62
		return Arc.DEFAULT_ARC;
63
	}
64

    
65
	private Set<String> getMdStores(final Set<String> workflowsId) {
66
		try {
67

    
68
			final String query = "//RESOURCE_PROFILE[.//RESOURCE_IDENTIFIER/@value='%s']//PARAM[./@category/string()='MDSTORE_ID']/text()";
69

    
70
			final Set<String> mdStores = new HashSet<>();
71

    
72
			if (workflowsId == null) { return null; }
73

    
74
			for (final String workflowId : workflowsId) {
75
				final List<String> result = this.serviceLocator.getService(ISLookUpService.class).quickSearchProfile(String.format(query, workflowId));
76
				final Set<String> metadataIds = Sets.newHashSet(result);
77
				mdStores.addAll(getRightMetadataId(Lists.newArrayList(metadataIds)));
78
			}
79
			return mdStores;
80

    
81
		} catch (final ISLookUpException e) {
82

    
83
			return null;
84
		}
85
	}
86

    
87
	/**
88
	 * Gets the right metadata id whith the format metadataFormat and interpretation interpretation
89
	 *
90
	 * @return the right metadata id
91
	 * @throws ISLookUpException
92
	 */
93
	private Set<String> getRightMetadataId(final Iterable<String> ids) throws ISLookUpException {
94
		final String query =
95
				"let $x:=//RESOURCE_PROFILE[.//RESOURCE_IDENTIFIER/@value='%s'] return concat($x//METADATA_FORMAT/text(), '::<<>>::', $x//METADATA_FORMAT_INTERPRETATION/text())";
96
		final Set<String> result = Sets.newHashSet();
97

    
98
		for (final String id : ids) {
99

    
100
			final List<String> results = this.serviceLocator.getService(ISLookUpService.class).quickSearchProfile(String.format(query, id));
101
			if (results.size() > 0) {
102
				final String[] values = results.get(0).split("::<<>>::");
103
				if (this.metadataFormat.equals(values[0]) && this.interpretation.equals(values[1])) {
104
					result.add(id);
105
				}
106
			}
107
		}
108
		return result;
109

    
110
	}
111

    
112
	/**
113
	 * Gets the interpretation.
114
	 *
115
	 * @return the interpretation
116
	 */
117
	public String getInterpretation() {
118
		return this.interpretation;
119
	}
120

    
121
	/**
122
	 * Sets the interpretation.
123
	 *
124
	 * @param interpretation
125
	 *            the interpretation to set
126
	 */
127
	@Required
128
	public void setInterpretation(final String interpretation) {
129
		this.interpretation = interpretation;
130
	}
131

    
132
	/**
133
	 * Gets the metadata format.
134
	 *
135
	 * @return the metadataFormat
136
	 */
137
	public String getMetadataFormat() {
138
		return this.metadataFormat;
139
	}
140

    
141
	/**
142
	 * Sets the metadata format.
143
	 *
144
	 * @param metadataFormat
145
	 *            the metadataFormat to set
146
	 */
147
	@Required
148
	public void setMetadataFormat(final String metadataFormat) {
149
		this.metadataFormat = metadataFormat;
150
	}
151

    
152
	/**
153
	 * Gets the provider id.
154
	 *
155
	 * @return the providerId
156
	 */
157
	public String getProviderId() {
158
		return this.providerId;
159
	}
160

    
161
	/**
162
	 * Sets the provider id.
163
	 *
164
	 * @param providerId
165
	 *            the providerId to set
166
	 */
167
	public void setProviderId(final String providerId) {
168
		this.providerId = providerId;
169
	}
170

    
171
}
(4-4/6)