Project

General

Profile

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

    
3
import java.io.IOException;
4
import java.io.StringReader;
5
import java.io.StringWriter;
6
import java.lang.reflect.Type;
7
import java.util.Collection;
8
import java.util.List;
9
import java.util.stream.Collectors;
10
import javax.servlet.http.HttpServletRequest;
11
import javax.servlet.http.HttpServletResponse;
12

    
13
import com.google.common.collect.ArrayListMultimap;
14
import com.google.common.collect.Lists;
15
import com.google.common.collect.Multimap;
16
import com.google.gson.Gson;
17
import com.google.gson.reflect.TypeToken;
18
import eu.dnetlib.oai.PublisherField;
19
import eu.dnetlib.oai.conf.OAIConfigurationExistReader;
20
import eu.dnetlib.oai.conf.OAIConfigurationWriter;
21
import eu.dnetlib.oai.info.SetInfo;
22
import eu.dnetlib.rmi.enabling.ISLookUpException;
23
import eu.dnetlib.rmi.provision.MDFInfo;
24
import org.apache.commons.io.IOUtils;
25
import org.apache.commons.logging.Log;
26
import org.apache.commons.logging.LogFactory;
27
import org.springframework.beans.factory.annotation.Autowired;
28
import org.springframework.stereotype.Controller;
29
import org.springframework.web.bind.annotation.RequestMapping;
30
import org.springframework.web.bind.annotation.RequestMethod;
31
import org.springframework.web.bind.annotation.RequestParam;
32
import org.springframework.web.bind.annotation.ResponseBody;
33

    
34
@Controller
35
public class OAIInternalController {
36

    
37
	private static final Log log = LogFactory.getLog(OAIInternalController.class); // NOPMD by marko on 11/24/08 5:02 PM
38

    
39
	@Autowired
40
	private OAIConfigurationExistReader configuration;
41
	@Autowired
42
	private OAIConfigurationWriter configurationWriter;
43

    
44
	@RequestMapping(value = "/ui/getExportMDFs.do")
45
	public void getExportMDFs(final HttpServletResponse response) throws Exception {
46
		List<MDFInfo> exportMDFs = configuration.getMetadataFormatInfo();
47
		String json = new Gson().toJson(exportMDFs);
48
		IOUtils.copy(new StringReader(json), response.getOutputStream());
49
	}
50

    
51
	@RequestMapping(value = "/ui/getSourceMDFs.do")
52
	public
53
	@ResponseBody
54
	List<MDFInfo> getSourceMDFs(final HttpServletResponse response) throws Exception {
55

    
56
		return configuration.getMetadataFormatInfo().stream()
57
				.map(mdf -> {
58
					MDFInfo srcOnly = new MDFInfo();
59
					srcOnly.setSourceFormatName(mdf.getSourceFormatName());
60
					srcOnly.setSourceFormatLayout(mdf.getSourceFormatLayout());
61
					srcOnly.setSourceFormatInterpretation(mdf.getSourceFormatInterpretation());
62
					return srcOnly;
63
				})
64
				.distinct()
65
				.collect(Collectors.toList());
66

    
67
	}
68

    
69
	@RequestMapping(value = "/ui/getSetSpecs.do")
70
	public
71
	@ResponseBody
72
	List<String> getSetSpecs(final HttpServletResponse response) throws Exception {
73
		return configuration.getSetSpecs();
74
	}
75

    
76
	@RequestMapping(value = "/ui/getMetadataFormat.do")
77
	public
78
	@ResponseBody
79
	MDFInfo getMetadataFormat(final HttpServletResponse response, @RequestParam(value = "mdPrefix", required = true) final String mdPrefix)
80
			throws Exception {
81
		return this.configuration.getMetadataFormatInfo(mdPrefix);
82

    
83
	}
84

    
85
	@RequestMapping(value = "/ui/saveMetadataFormat.do", method = RequestMethod.POST)
86
	public void saveMetadataFormat(final HttpServletResponse response,
87
			final HttpServletRequest request,
88
			@RequestParam(value = "mdPrefix", required = true) final String mdPrefix) throws Exception {
89

    
90
		MDFInfo update = new Gson().fromJson(request.getReader(), MDFInfo.class);
91
		if (update.getBaseQuery() == null) {
92
			update.setBaseQuery("");
93
		}
94
		if (update.getTransformationRuleID() == null) {
95
			update.setTransformationRuleID("");
96
		}
97
		log.debug("Update to object" + update);
98
		boolean result = false;
99
		if (this.configuration.getMetadataFormatInfo(mdPrefix) != null) {
100
			log.debug("UPDATING mdPrefix = " + mdPrefix);
101
			result = this.configurationWriter.updateMetadataFormat(mdPrefix, update);
102
		} else {
103
			// ADD NEW mdf
104
			log.debug("CREATING new mdPrefix = " + mdPrefix);
105
			result = this.configurationWriter.addMetadataFormat(update);
106
		}
107
		response.getWriter().print(result);
108
	}
109

    
110
	@RequestMapping(value = "/ui/deleteMetadataFormat.do")
111
	public void deleteMetadataFormat(final HttpServletResponse response, @RequestParam(value = "mdPrefix", required = true) final String mdPrefix)
112
			throws Exception {
113
		boolean result = this.configurationWriter.deleteMetadataFormat(mdPrefix);
114
		response.getWriter().print(result);
115
	}
116

    
117
	@RequestMapping(value = "/ui/showSetDetails.do")
118
	public void showSetDetails(final HttpServletResponse response, @RequestParam(value = "setSpec", required = true) final String setSpec) throws Exception {
119
		SetInfo set = this.configuration.getSetInfo(setSpec);
120
		String json = new Gson().toJson(set);
121
		IOUtils.copy(new StringReader(json), response.getOutputStream());
122
	}
123

    
124
	@RequestMapping(value = "/ui/saveOAISet.do", method = RequestMethod.POST)
125
	public void saveOAISet(final HttpServletResponse response,
126
			final HttpServletRequest request,
127
			@RequestParam(value = "setSpec", required = true) final String setSpec) throws Exception {
128

    
129
		log.debug("setSpec = " + setSpec);
130
		SetInfo update = new Gson().fromJson(request.getReader(), SetInfo.class);
131
		log.debug("Update to object" + update);
132
		boolean result = false;
133
		if (this.configuration.getSetInfo(setSpec) != null) {
134
			log.debug("UPDATING set = " + setSpec);
135
			result = this.configurationWriter.updateOAISet(setSpec, update);
136
		} else {
137
			// ADD NEW mdf
138
			log.debug("CREATING new OAIset = " + setSpec);
139
			result = this.configurationWriter.addOAISet(update);
140
		}
141
		response.getWriter().print(result);
142
	}
143

    
144
	@RequestMapping(value = "/ui/deleteOAISet.do")
145
	public void deleteOAISet(final HttpServletResponse response, @RequestParam(value = "setSpec", required = true) final String setSpec) throws Exception {
146
		boolean result = this.configurationWriter.deleteOAISet(setSpec);
147
		response.getWriter().print(result);
148
	}
149

    
150
	@RequestMapping(value = "/ui/showIndices.do")
151
	public void showIndices(final HttpServletResponse response,
152
			@RequestParam(value = "format", required = true) final String format,
153
			@RequestParam(value = "layout", required = true) final String layout,
154
			@RequestParam(value = "interpretation", required = true) final String interpretation) throws Exception {
155

    
156
		List<PublisherField> fields = this.configuration.getFields(format, interpretation, layout);
157

    
158
		// Need to iterate over the values to build an object otherwise angularjs does not allow me to edit the content!
159
		// See http://stackoverflow.com/questions/13714884/difficulty-with-ng-model-ng-repeat-and-inputs
160
		List<OAIIndex> indices = Lists.newArrayList();
161
		for (PublisherField field : fields) {
162
			Collection<String> paths = field.getSources().get(format + "-" + layout + "-" + interpretation);
163
			List<IndexPath> pathList = Lists.newArrayList();
164
			for (String p : paths) {
165
				pathList.add(new IndexPath(p));
166
			}
167
			OAIIndex idx = new OAIIndex();
168
			idx.setRepeatable(field.isRepeatable());
169
			idx.setName(field.getFieldName());
170
			idx.setPaths(pathList);
171
			indices.add(idx);
172
		}
173
		String json = new Gson().toJson(indices);
174
		log.debug("The map of indices: " + json);
175
		IOUtils.copy(new StringReader(json), response.getOutputStream());
176
	}
177

    
178
	@RequestMapping(value = "/ui/saveIndices.do", method = RequestMethod.POST)
179
	public void saveIndices(final HttpServletResponse response,
180
			final HttpServletRequest request,
181
			@RequestParam(value = "format", required = true) final String format,
182
			@RequestParam(value = "layout", required = true) final String layout,
183
			@RequestParam(value = "interpretation", required = true) final String interpretation) throws Exception {
184

    
185
		StringWriter sw = new StringWriter();
186
		IOUtils.copy(request.getInputStream(), sw);
187
		Type collectionType = new TypeToken<List<OAIIndex>>() {
188
		}.getType();
189
		List<OAIIndex> indexes = new Gson().fromJson(sw.toString(), collectionType);
190
		final String mdformatKey = format + "-" + layout + "-" + interpretation;
191
		List<PublisherField> updatedIndices = indexes.stream().map(oaiIndex -> {
192
			PublisherField f = new PublisherField();
193
			f.setFieldName(oaiIndex.getName());
194
			f.setRepeatable(oaiIndex.isRepeatable());
195
			Multimap<String, String> paths = ArrayListMultimap.create();
196
			for (IndexPath path : oaiIndex.getPaths()) {
197
				paths.put(mdformatKey, path.getPath());
198
			}
199
			f.setSources(paths);
200
			return f;
201
		}).collect(Collectors.toList());
202

    
203
		log.debug("Updating indices" + indexes);
204
		boolean result = this.configurationWriter.updateIndices(format, layout, interpretation, updatedIndices);
205
		response.getWriter().print(result);
206
	}
207

    
208
	@RequestMapping(value = "/ui/saveNewIndex.do")
209
	public void saveNewIndex(final HttpServletResponse response,
210
			@RequestParam(value = "format", required = true) final String format,
211
			@RequestParam(value = "layout", required = true) final String layout,
212
			@RequestParam(value = "interpretation", required = true) final String interpretation,
213
			@RequestParam(value = "indexName", required = true) final String indexName,
214
			@RequestParam(value = "repeatable", required = true) final boolean repeatable,
215
			@RequestParam(value = "paths", required = true) final String paths) throws Exception {
216

    
217
		String[] thePaths = paths.split(",");
218
		boolean result = this.configurationWriter.addNewIndex(format, layout, interpretation, indexName, repeatable, thePaths);
219
		response.getWriter().print(result);
220
	}
221

    
222
	@RequestMapping(value = "/ui/saveNewPathIndex.do")
223
	public void saveNewPathIndex(final HttpServletResponse response,
224
			@RequestParam(value = "format", required = true) final String format,
225
			@RequestParam(value = "layout", required = true) final String layout,
226
			@RequestParam(value = "interpretation", required = true) final String interpretation,
227
			@RequestParam(value = "indexName", required = true) final String indexName,
228
			@RequestParam(value = "newPath", required = true) final String newPath) throws Exception {
229

    
230
		List<PublisherField> fields = this.configurationWriter.getConfiguration().getFields(format, interpretation, layout).stream().map(field -> {
231
			if (field.getFieldName().equals(indexName)) {
232
				field.getSources().get(format + "-" + layout + "-" + interpretation).add(newPath);
233
			}
234
			return field;
235
		}).collect(Collectors.toList());
236
		boolean result = this.configurationWriter.updateIndices(format, layout, interpretation, fields);
237
		response.getWriter().print(result);
238
	}
239

    
240
	@RequestMapping(value = "/ui/getTransformationRules.do")
241
	public
242
	@ResponseBody
243
	List<TDSRule> getTransformationRules(final HttpServletResponse response) throws IOException, ISLookUpException {
244

    
245
		final String query = "for $x in collection('/db/DRIVER/TransformationRuleDSResources/TransformationRuleDSResourceType')"
246
				+ " return concat('id:-:',$x//RESOURCE_IDENTIFIER/@value/string(), ':-:title:-:', $x//CONFIGURATION//TITLE)";
247
		return this.configuration.getLookupClient().search(query, TDSRule.class, ":-:");
248
	}
249

    
250
}
(4-4/7)