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 eu.dnetlib.utils.MetadataReference;
25
import org.apache.commons.io.IOUtils;
26
import org.apache.commons.logging.Log;
27
import org.apache.commons.logging.LogFactory;
28
import org.springframework.beans.factory.annotation.Autowired;
29
import org.springframework.stereotype.Controller;
30
import org.springframework.web.bind.annotation.RequestMapping;
31
import org.springframework.web.bind.annotation.RequestMethod;
32
import org.springframework.web.bind.annotation.RequestParam;
33
import org.springframework.web.bind.annotation.ResponseBody;
34

    
35
@Controller
36
public class OAIInternalController {
37

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

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

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

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

    
57
		return configuration.getMetadataFormatInfo().stream()
58
				.map(mdf -> mdf.getSourceMetadataReference())
59
				.distinct()
60
				.collect(Collectors.toList());
61

    
62
	}
63

    
64
	@RequestMapping(value = "/ui/getSetSpecs.do")
65
	public
66
	@ResponseBody
67
	List<String> getSetSpecs(final HttpServletResponse response) throws Exception {
68
		return configuration.getSetSpecs();
69
	}
70

    
71
	@RequestMapping(value = "/ui/getMetadataFormat.do")
72
	public
73
	@ResponseBody
74
	MDFInfo getMetadataFormat(final HttpServletResponse response, @RequestParam(value = "mdPrefix", required = true) final String mdPrefix)
75
			throws Exception {
76
		return this.configuration.getMetadataFormatInfo(mdPrefix);
77

    
78
	}
79

    
80
	@RequestMapping(value = "/ui/saveMetadataFormat.do", method = RequestMethod.POST)
81
	public void saveMetadataFormat(final HttpServletResponse response,
82
			final HttpServletRequest request,
83
			@RequestParam(value = "mdPrefix", required = true) final String mdPrefix) throws Exception {
84

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

    
105
	@RequestMapping(value = "/ui/deleteMetadataFormat.do")
106
	public void deleteMetadataFormat(final HttpServletResponse response, @RequestParam(value = "mdPrefix", required = true) final String mdPrefix)
107
			throws Exception {
108
		boolean result = this.configurationWriter.deleteMetadataFormat(mdPrefix);
109
		response.getWriter().print(result);
110
	}
111

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

    
119
	@RequestMapping(value = "/ui/saveOAISet.do", method = RequestMethod.POST)
120
	public void saveOAISet(final HttpServletResponse response,
121
			final HttpServletRequest request,
122
			@RequestParam(value = "setSpec", required = true) final String setSpec) throws Exception {
123

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

    
139
	@RequestMapping(value = "/ui/deleteOAISet.do")
140
	public void deleteOAISet(final HttpServletResponse response, @RequestParam(value = "setSpec", required = true) final String setSpec) throws Exception {
141
		boolean result = this.configurationWriter.deleteOAISet(setSpec);
142
		response.getWriter().print(result);
143
	}
144

    
145
	@RequestMapping(value = "/ui/showIndices.do")
146
	public void showIndices(final HttpServletResponse response,
147
			@RequestParam(value = "format", required = true) final String format,
148
			@RequestParam(value = "layout", required = true) final String layout,
149
			@RequestParam(value = "interpretation", required = true) final String interpretation) throws Exception {
150

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

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

    
173
	@RequestMapping(value = "/ui/saveIndices.do", method = RequestMethod.POST)
174
	public void saveIndices(final HttpServletResponse response,
175
			final HttpServletRequest request,
176
			@RequestParam(value = "format", required = true) final String format,
177
			@RequestParam(value = "layout", required = true) final String layout,
178
			@RequestParam(value = "interpretation", required = true) final String interpretation) throws Exception {
179

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

    
198
		log.debug("Updating indices" + indexes);
199
		boolean result = this.configurationWriter.updateIndices(format, layout, interpretation, updatedIndices);
200
		response.getWriter().print(result);
201
	}
202

    
203
	@RequestMapping(value = "/ui/saveNewIndex.do")
204
	public void saveNewIndex(final HttpServletResponse response,
205
			@RequestParam(value = "format", required = true) final String format,
206
			@RequestParam(value = "layout", required = true) final String layout,
207
			@RequestParam(value = "interpretation", required = true) final String interpretation,
208
			@RequestParam(value = "indexName", required = true) final String indexName,
209
			@RequestParam(value = "repeatable", required = true) final boolean repeatable,
210
			@RequestParam(value = "paths", required = true) final String paths) throws Exception {
211

    
212
		String[] thePaths = paths.split(",");
213
		boolean result = this.configurationWriter.addNewIndex(format, layout, interpretation, indexName, repeatable, thePaths);
214
		response.getWriter().print(result);
215
	}
216

    
217
	@RequestMapping(value = "/ui/saveNewPathIndex.do")
218
	public void saveNewPathIndex(final HttpServletResponse response,
219
			@RequestParam(value = "format", required = true) final String format,
220
			@RequestParam(value = "layout", required = true) final String layout,
221
			@RequestParam(value = "interpretation", required = true) final String interpretation,
222
			@RequestParam(value = "indexName", required = true) final String indexName,
223
			@RequestParam(value = "newPath", required = true) final String newPath) throws Exception {
224

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

    
235
	@RequestMapping(value = "/ui/getTransformationRules.do")
236
	public
237
	@ResponseBody
238
	List<TDSRule> getTransformationRules(final HttpServletResponse response) throws IOException, ISLookUpException {
239

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

    
245
}
(4-4/7)