Project

General

Profile

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

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

    
9
import javax.annotation.Resource;
10
import javax.servlet.http.HttpServletRequest;
11
import javax.servlet.http.HttpServletResponse;
12

    
13
import org.apache.commons.lang3.BooleanUtils;
14
import org.dom4j.Document;
15
import org.dom4j.Node;
16
import org.dom4j.io.SAXReader;
17
import org.springframework.beans.factory.annotation.Autowired;
18
import org.springframework.beans.factory.annotation.Required;
19
import org.springframework.ui.ModelMap;
20

    
21
import com.google.common.base.Splitter;
22
import com.google.common.collect.Lists;
23
import com.google.common.collect.Sets;
24
import com.google.gson.Gson;
25

    
26
import eu.dnetlib.enabling.datasources.common.Api;
27
import eu.dnetlib.enabling.datasources.common.Datasource;
28
import eu.dnetlib.enabling.datasources.common.LocalDatasourceManager;
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.ModuleEntryPoint;
33
import eu.dnetlib.miscutils.collections.Pair;
34

    
35
public class RepoApisEntryPointController extends ModuleEntryPoint {
36

    
37
	@Resource
38
	private UniqueServiceLocator serviceLocator;
39

    
40
	@Resource
41
	private RepoUIUtils repoUIUtils;
42

    
43
	private String compatibilityLevelsVocabulary;
44

    
45
	private String validatorAddress;
46
	private String validatorServiceAddress;
47

    
48
	@Autowired
49
	private LocalDatasourceManager<Datasource<?, ?, ?>, Api<?>> dsManager;
50

    
51
	public class RepoHIWorkflow implements Comparable<RepoHIWorkflow> {
52

    
53
		private final String id;
54
		private final String name;
55
		private final Set<String> ifaceTypes;
56
		private final Set<String> compliances;
57
		private final String consentTermsOfUseLabel;
58
		private final String fullTextDownloadLabel;
59

    
60
		private List<Pair<String, String>> fields;
61

    
62
		public RepoHIWorkflow(final String id, final String name, final Set<String> ifaceTypes, final Set<String> compliances,
63
			final String consentTermsOfUseLabel,
64
			final String fullTextDownloadLabel,
65
			final List<Pair<String, String>> fields) {
66
			this.id = id;
67
			this.name = name;
68
			this.ifaceTypes = ifaceTypes;
69
			this.compliances = compliances;
70
			this.consentTermsOfUseLabel = consentTermsOfUseLabel;
71
			this.fullTextDownloadLabel = fullTextDownloadLabel;
72
			this.fields = fields;
73
		}
74

    
75
		public String getId() {
76
			return id;
77
		}
78

    
79
		public String getName() {
80
			return name;
81
		}
82

    
83
		public Set<String> getIfaceTypes() {
84
			return ifaceTypes;
85
		}
86

    
87
		public Set<String> getCompliances() {
88
			return compliances;
89
		}
90

    
91
		@Override
92
		public int compareTo(final RepoHIWorkflow o) {
93
			return getName().compareTo(o.getName());
94
		}
95

    
96
		/**
97
		 * @return the fields
98
		 */
99
		public List<Pair<String, String>> getFields() {
100
			return fields;
101
		}
102

    
103
		/**
104
		 * @param fields
105
		 *            the fields to set
106
		 */
107
		public void setFields(final List<Pair<String, String>> fields) {
108
			this.fields = fields;
109
		}
110

    
111
		public String getConsentTermsOfUseLabel() {
112
			return consentTermsOfUseLabel;
113
		}
114

    
115
		public String getFullTextDownloadLabel() {
116
			return fullTextDownloadLabel;
117
		}
118

    
119
	}
120

    
121
	private RepoHIWorkflow parseQuery(final String input) {
122
		final SAXReader reader = new SAXReader();
123
		try {
124
			final Document doc = reader.read(new StringReader(input));
125

    
126
			final String id = doc.valueOf("//id");
127
			final String name = doc.valueOf("//name");
128
			final String type = doc.valueOf("//types");
129
			final String compliance = doc.valueOf("//compliances");
130
			final Set<String> ifcTypes = Sets.newHashSet(Splitter.on(",").omitEmptyStrings().trimResults().split(type));
131
			final Set<String> compliances = Sets.newHashSet(Splitter.on(",").omitEmptyStrings().trimResults().split(compliance));
132
			final String consentTermsOfUse = BooleanUtils.toString(BooleanUtils.toBooleanObject(doc.valueOf("//consentTermsOfUse")), "YES", "NO", "UNKNOWN");
133
			final String fullTextDownload = BooleanUtils.toString(BooleanUtils.toBooleanObject(doc.valueOf("//fullTextDownload")), "YES", "NO", "UNKNOWN");
134

    
135
			final List<Pair<String, String>> fields = new ArrayList<>();
136
			for (final Object o : doc.selectNodes(".//FIELD")) {
137
				final Node currentNode = (Node) o;
138
				final String key = currentNode.valueOf("@name");
139
				final String value = currentNode.getText();
140
				fields.add(new Pair<>(key, value));
141
			}
142
			return new RepoHIWorkflow(id, name, ifcTypes, compliances, consentTermsOfUse, fullTextDownload, fields);
143
		} catch (final Exception e) {
144
			return null;
145
		}
146
	}
147

    
148
	private List<RepoHIWorkflow> listRepoHIWorkflows() throws ISLookUpException {
149

    
150
		final String xquery =
151
			"for $x in collection('/db/DRIVER/WorkflowDSResources/WorkflowDSResourceType') "
152
				+ "where $x//WORKFLOW_TYPE='REPO_HI' "
153
				+ "return <result> "
154
				+ "<id>{$x//RESOURCE_IDENTIFIER/@value/string()}</id>"
155
				+ "<name>{$x//WORKFLOW_NAME/text()}</name> "
156
				+ "<types>{$x//PARAM[@name='expectedDatasourceTypes']/text()}</types>"
157
				+ "<compliances>{$x//PARAM[@name='expectedCompatibilityLevels']/text()}</compliances> "
158
				+ "<consentTermsOfUse>{$x//PARAM[@name='expectedConsentTermsOfUse']/text()}</consentTermsOfUse> "
159
				+ "<fullTextDownload>{$x//PARAM[@name='expectedFullTextDownload']/text()}</fullTextDownload> "
160
				+ "{$x//FIELD}"
161
				+ "</result>";
162
		final List<RepoHIWorkflow> list = Lists.newArrayList();
163
		for (final String s : serviceLocator.getService(ISLookUpService.class).quickSearchProfile(xquery)) {
164
			final RepoHIWorkflow repoHiInfo = parseQuery(s);
165
			if (repoHiInfo != null) {
166
				list.add(repoHiInfo);
167
			}
168
		}
169
		Collections.sort(list);
170
		return list;
171
	}
172

    
173
	@Override
174
	protected void initialize(final ModelMap map, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
175
		final Gson gson = new Gson();
176
		map.addAttribute("availableRepohiWfs", gson.toJson(listRepoHIWorkflows()));
177
		map.addAttribute("compatibilityLevels", gson.toJson(repoUIUtils.fetchVocabularyTerms(getCompatibilityLevelsVocabulary())));
178
		map.addAttribute("browseFields", gson.toJson(dsManager.listBrowsableFields()));
179
		map.addAttribute("validatorAddress", getValidatorAddress());
180
		map.addAttribute("validatorServiceAddress", getValidatorServiceAddress());
181
	}
182

    
183
	public String getCompatibilityLevelsVocabulary() {
184
		return compatibilityLevelsVocabulary;
185
	}
186

    
187
	@Required
188
	public void setCompatibilityLevelsVocabulary(final String compatibilityLevelsVocabulary) {
189
		this.compatibilityLevelsVocabulary = compatibilityLevelsVocabulary;
190
	}
191

    
192
	public String getValidatorAddress() {
193
		return validatorAddress;
194
	}
195

    
196
	@Required
197
	public void setValidatorAddress(final String validatorAddress) {
198
		this.validatorAddress = validatorAddress;
199
	}
200

    
201
	public String getValidatorServiceAddress() {
202
		return validatorServiceAddress;
203
	}
204

    
205
	@Required
206
	public void setValidatorServiceAddress(final String validatorServiceAddress) {
207
		this.validatorServiceAddress = validatorServiceAddress;
208
	}
209

    
210
}
(3-3/6)