Project

General

Profile

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

    
3
import java.util.List;
4
import java.util.Set;
5
import javax.annotation.Resource;
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 com.googlecode.sarasvati.Arc;
12
import com.googlecode.sarasvati.NodeToken;
13
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException;
14
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
15
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
16
import eu.dnetlib.msro.workflows.nodes.SimpleJobNode;
17
import org.springframework.beans.factory.annotation.Required;
18

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

    
25
	/** The metadata format. */
26
	private String metadataFormat;
27

    
28
	/** The interpretation. */
29
	private String interpretation;
30

    
31
	/** The provider id. */
32
	private String providerId;
33

    
34
	/** The service locator. */
35
	@Resource
36
	private UniqueServiceLocator serviceLocator;
37

    
38
	/*
39
	 * (non-Javadoc)
40
	 *
41
	 * @see eu.dnetlib.msro.workflows.nodes.SimpleJobNode#execute(com.googlecode.sarasvati.NodeToken)
42
	 */
43
	@Override
44
	protected String execute(final NodeToken token) throws Exception {
45

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

    
49
		final ISLookUpService isLookUpService = serviceLocator.getService(ISLookUpService.class);
50
		List<String> result = isLookUpService.quickSearchProfile(String.format(workflowQuery, providerId));
51
		if (result.size() == 0) { throw new RuntimeException("there is no mdStore Associated to the provider " + token.getEnv().getAttribute(getProviderId())); }
52
		Set<String> workflowIds = Sets.newHashSet(result);
53

    
54
		Set<String> metadataIds = getMdStores(isLookUpService, workflowIds);
55
		Gson g = new GsonBuilder().disableHtmlEscaping().create();
56
		token.getEnv().setAttribute("mdId", g.toJson(metadataIds));
57

    
58
		token.getEnv().setAttribute("mdFormat", getMetadataFormat());
59
		return Arc.DEFAULT_ARC;
60
	}
61

    
62
	private Set<String> getMdStores(final ISLookUpService isLookUpService, final Set<String> workflowsId) {
63
		try {
64

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

    
67
			Set<String> mdStores = Sets.newHashSet();
68

    
69
			if (workflowsId == null) { return null; }
70

    
71
			for (String workflowId : workflowsId) {
72
				List<String> result = isLookUpService.quickSearchProfile(String.format(query, workflowId));
73
				Set<String> metadataIds = Sets.newHashSet(result);
74
				mdStores.addAll(getRightMetadataId(isLookUpService, Lists.newArrayList(metadataIds)));
75
			}
76
			return mdStores;
77

    
78
		} catch (ISLookUpException e) {
79

    
80
			return null;
81
		}
82
	}
83

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

    
95
		for (String id : ids) {
96

    
97
			List<String> results = isLookUpService.quickSearchProfile(String.format(query, id));
98
			if (results.size() > 0) {
99
				String[] values = results.get(0).split("::<<>>::");
100
				if (metadataFormat.equals(values[0].trim()) && interpretation.equals(values[1].trim())) {
101
					result.add(id);
102
				}
103
			}
104
		}
105
		return result;
106

    
107
	}
108

    
109
	/**
110
	 * Gets the interpretation.
111
	 *
112
	 * @return the interpretation
113
	 */
114
	public String getInterpretation() {
115
		return interpretation;
116
	}
117

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

    
129
	/**
130
	 * Gets the metadata format.
131
	 *
132
	 * @return the metadataFormat
133
	 */
134
	public String getMetadataFormat() {
135
		return metadataFormat;
136
	}
137

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

    
149
	/**
150
	 * Gets the provider id.
151
	 *
152
	 * @return the providerId
153
	 */
154
	public String getProviderId() {
155
		return providerId;
156
	}
157

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

    
168
}
(3-3/5)