Project

General

Profile

1
package eu.dnetlib.functionality.modular.ui.repositories;
2

    
3
import java.io.StringReader;
4
import java.util.Collections;
5
import java.util.List;
6
import java.util.Map;
7
import java.util.Set;
8

    
9
import javax.annotation.Resource;
10

    
11
import org.antlr.stringtemplate.StringTemplate;
12
import org.apache.commons.io.IOUtils;
13
import org.apache.commons.lang.StringEscapeUtils;
14
import org.apache.commons.lang.math.NumberUtils;
15
import org.apache.commons.logging.Log;
16
import org.apache.commons.logging.LogFactory;
17
import org.dom4j.Document;
18
import org.dom4j.Node;
19
import org.dom4j.io.SAXReader;
20
import org.springframework.core.io.ClassPathResource;
21

    
22
import com.google.common.collect.Lists;
23
import com.google.common.collect.Maps;
24
import com.google.common.collect.Sets;
25

    
26
import eu.dnetlib.data.collector.rmi.CollectorService;
27
import eu.dnetlib.data.collector.rmi.ProtocolDescriptor;
28
import eu.dnetlib.data.collector.rmi.ProtocolParameter;
29
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException;
30
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
31
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
32
import eu.dnetlib.functionality.modular.ui.repositories.objects.RepoInterfaceEntry;
33
import eu.dnetlib.functionality.modular.ui.repositories.objects.RepoMetaWfEntry;
34
import eu.dnetlib.functionality.modular.ui.repositories.objects.SimpleParamEntry;
35
import eu.dnetlib.functionality.modular.ui.repositories.objects.VocabularyEntry;
36
import eu.dnetlib.msro.workflows.util.WorkflowsConstants.WorkflowStatus;
37

    
38
public class RepoUIUtils {
39

    
40
	@Resource
41
	private UniqueServiceLocator serviceLocator;
42

    
43
	private final ClassPathResource getRepoApiQueryTmpl =
44
			new ClassPathResource("/eu/dnetlib/functionality/modular/ui/repositories/templates/getRepoApi.xquery.st");
45

    
46
	private static final Log log = LogFactory.getLog(RepoUIUtils.class);
47

    
48
	private final Map<String, List<ProtocolParameter>> parametersMap = Maps.newHashMap();
49

    
50
	public RepoInterfaceEntry getApi(final String repoId, final String ifaceId) throws Exception {
51
		final RepoInterfaceEntry ifc = new RepoInterfaceEntry();
52

    
53
		final StringTemplate st = new StringTemplate(IOUtils.toString(getRepoApiQueryTmpl.getInputStream()));
54
		st.setAttribute("dsId", repoId);
55
		st.setAttribute("ifaceId", ifaceId);
56

    
57
		final String query = st.toString();
58

    
59
		final SAXReader reader = new SAXReader();
60

    
61
		final String s = serviceLocator.getService(ISLookUpService.class).getResourceProfileByQuery(query);
62

    
63
		final Document doc = reader.read(new StringReader(s));
64

    
65
		ifc.setId(doc.valueOf("/api/id"));
66
		ifc.setLabel(doc.valueOf("/api/label"));
67
		ifc.setRemovable(doc.valueOf("/api/removable").equalsIgnoreCase("true") && doc.selectNodes("//metaWF").isEmpty());
68
		ifc.setRepoId(doc.valueOf("/api/repo/@id"));
69
		ifc.setRepoName(StringEscapeUtils.unescapeHtml(doc.valueOf("/api/repo")));
70
		ifc.setRepoCountry(doc.valueOf("/api/repo/@country"));
71
		ifc.setRepoPrefix(doc.valueOf("/api/repo/@prefix"));
72
		ifc.setRepoType(doc.valueOf("/api/repo/@type"));
73
		ifc.setEmail(doc.valueOf("/api/repo/@email"));
74
		final String protocol = doc.valueOf("/api/protocol");
75
		ifc.setProtocol(protocol);
76

    
77
		final Set<String> toVerifyParams = getParameterNamesForProtocol(ifc.getProtocol());
78
		for (final Object o : doc.selectNodes("/api/commonParams/param")) {
79
			final Node n = (Node) o;
80
			ifc.getCommonParams().add(new SimpleParamEntry(n.valueOf("@name"), n.getText()));
81
		}
82

    
83
		log.debug("****** " + toVerifyParams);
84
		for (final Object o : doc.selectNodes("/api/accessParams/param")) {
85
			final Node n = (Node) o;
86
			final String pname = n.valueOf("@name");
87
			if (toVerifyParams.contains(pname)) {
88
				ifc.getAccessParams().add(new SimpleParamEntry(pname, n.getText()));
89
				toVerifyParams.remove(pname);
90
			} else {
91
				log.warn("Invalid param [" + pname + "] for protocol " + protocol + " in repo " + repoId);
92
			}
93
		}
94
		for (final String pname : toVerifyParams) {
95
			ifc.getAccessParams().add(new SimpleParamEntry(pname, ""));
96
			log.info("Adding missing param [" + pname + "] for protocol " + protocol + " in repo " + repoId);
97
		}
98

    
99
		for (final Object o : doc.selectNodes("/api/extraFields/field")) {
100
			final Node n = (Node) o;
101
			final String name = n.valueOf("@name");
102
			final String value = n.getText();
103
			if (name.equalsIgnoreCase("overriding_compliance")) {
104
				for (final SimpleParamEntry e : ifc.getCommonParams()) {
105
					if (e.getName().equals("compliance")) {
106
						// The original compliance (assigned by the validator) is stored in otherValue
107
						e.setOtherValue(e.getValue());
108
						e.setValue(value);
109
					}
110
				}
111
			} else if (name.equalsIgnoreCase("last_aggregation_date")) {
112
				ifc.setAggrDate(value);
113
			} else if (name.equalsIgnoreCase("last_aggregation_total")) {
114
				ifc.setAggrTotal(NumberUtils.toInt(value, 0));
115
			} else if (name.equalsIgnoreCase("last_aggregation_mdId")) {
116
				ifc.setAggrMdId(value);
117
			} else if (name.equalsIgnoreCase("last_collection_date")) {
118
				ifc.setCollDate(value);
119
			} else if (name.equalsIgnoreCase("last_collection_total")) {
120
				ifc.setCollTotal(NumberUtils.toInt(value, 0));
121
			} else if (name.equalsIgnoreCase("last_collection_mdId")) {
122
				ifc.setCollMdId(value);
123
			} else if (name.equalsIgnoreCase("last_download_date")) {
124
				ifc.setDownloadDate(value);
125
			} else if (name.equalsIgnoreCase("last_download_total")) {
126
				ifc.setDownloadTotal(NumberUtils.toInt(value, 0));
127
			} else if (name.equalsIgnoreCase("last_download_objId")) {
128
				ifc.setDownloadObjId(value);
129
			} else {
130
				ifc.getOtherParams().add(new SimpleParamEntry(name, value));
131
			}
132
		}
133

    
134
		if (doc.selectNodes("/api/extraFields/field[@name='metadata_identifier_path']").isEmpty()) {
135
			ifc.getOtherParams().add(new SimpleParamEntry("metadata_identifier_path", ""));
136
		}
137

    
138
		for (final Object o : doc.selectNodes("//metaWF")) {
139
			final Node n = (Node) o;
140

    
141
			final String id = n.valueOf("./id");
142
			final String name = n.valueOf("./name");
143
			final String status = n.valueOf("./status");
144
			final String repoByeWfId = n.valueOf("./destroyWorkflow");
145

    
146
			int progress = 0;
147
			try {
148
				switch (WorkflowStatus.valueOf(status)) {
149
				case MISSING:
150
					progress = 0;
151
					break;
152
				case ASSIGNED:
153
					progress = 25;
154
					break;
155
				case WAIT_SYS_SETTINGS:
156
					progress = 50;
157
					break;
158
				case WAIT_USER_SETTINGS:
159
					progress = 75;
160
					break;
161
				case EXECUTABLE:
162
					progress = 100;
163
					break;
164
				}
165
			} catch (final Exception e) {
166
				progress = 0;
167
			}
168
			ifc.getMetaWFs().add(new RepoMetaWfEntry(id, name, status, repoByeWfId, progress));
169
		}
170

    
171
		return ifc;
172
	}
173

    
174
	public List<VocabularyEntry> fetchVocabularyTerms(final String voc) throws ISLookUpException {
175
		final String xquery = "for $x in collection('/db/DRIVER/VocabularyDSResources/VocabularyDSResourceType')[.//VOCABULARY_NAME/@code = '"
176
				+ voc.trim() + "']//TERM return concat($x/@code, ' @@@ ', $x/@english_name)";
177

    
178
		final List<VocabularyEntry> list = Lists.newArrayList();
179
		for (final String s : serviceLocator.getService(ISLookUpService.class).quickSearchProfile(xquery)) {
180
			final String[] arr = s.split("@@@");
181
			list.add(new VocabularyEntry(arr[0].trim(), arr[1].trim()));
182
		}
183
		Collections.sort(list);
184

    
185
		return list;
186
	}
187

    
188
	private Set<String> getParameterNamesForProtocol(final String protocol) {
189
		if (parametersMap.isEmpty()) {
190
			for (final ProtocolDescriptor d : serviceLocator.getService(CollectorService.class).listProtocols()) {
191
				parametersMap.put(d.getName().toLowerCase(), d.getParams());
192
			}
193
		}
194
		final Set<String> res = Sets.newHashSet();
195
		if (parametersMap.containsKey(protocol.toLowerCase())) {
196
			res.add("baseUrl");
197
			for (final ProtocolParameter p : parametersMap.get(protocol.toLowerCase())) {
198
				res.add(p.getName());
199
			}
200
		}
201

    
202
		return res;
203
	}
204

    
205
}
(6-6/7)