1
|
package eu.dnetlib.data.transformation.ui;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
|
5
|
import javax.annotation.Resource;
|
6
|
import javax.servlet.http.HttpServletRequest;
|
7
|
import javax.servlet.http.HttpServletResponse;
|
8
|
|
9
|
import org.apache.commons.io.IOUtils;
|
10
|
import org.apache.commons.lang3.exception.ExceptionUtils;
|
11
|
import org.springframework.http.HttpStatus;
|
12
|
import org.springframework.stereotype.Controller;
|
13
|
import org.springframework.ui.Model;
|
14
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
15
|
import org.springframework.web.bind.annotation.RequestBody;
|
16
|
import org.springframework.web.bind.annotation.RequestMapping;
|
17
|
import org.springframework.web.bind.annotation.RequestParam;
|
18
|
import org.springframework.web.bind.annotation.ResponseBody;
|
19
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
20
|
|
21
|
import eu.dnetlib.data.transformation.service.DataTransformerFactory;
|
22
|
import eu.dnetlib.data.transformation.service.SimpleDataTransformer;
|
23
|
import eu.dnetlib.enabling.inspector.AbstractInspectorController;
|
24
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpDocumentNotFoundException;
|
25
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException;
|
26
|
import eu.dnetlib.functionality.modular.ui.error.ErrorMessage;
|
27
|
|
28
|
@Controller
|
29
|
public class DataTransformationController extends AbstractInspectorController {
|
30
|
|
31
|
@Resource
|
32
|
private DataTransformerFactory dataTransformerFactory;
|
33
|
|
34
|
@Resource
|
35
|
private TransformerUtils transformerUtils;
|
36
|
|
37
|
@RequestMapping(value = "/inspector/transform.do")
|
38
|
public void transform(final Model model,
|
39
|
@RequestParam(value = "rule", required = false) final String ruleId,
|
40
|
@RequestParam(value = "record", required = false) final String record) throws Exception {
|
41
|
|
42
|
model.addAttribute("rules", transformerUtils.obtainRuleProfiles(ruleId));
|
43
|
|
44
|
if (ruleId != null && record != null) {
|
45
|
final SimpleDataTransformer transformer = dataTransformerFactory.createTransformer(ruleId);
|
46
|
model.addAttribute("input", record);
|
47
|
model.addAttribute("output", transformer.evaluate(record));
|
48
|
}
|
49
|
}
|
50
|
|
51
|
@RequestMapping(value = "/ui/transform/transform.do")
|
52
|
public void transform(final HttpServletResponse res,
|
53
|
@RequestParam(value = "rule", required = true) final String ruleId,
|
54
|
@RequestBody final String record) throws ISLookUpDocumentNotFoundException, ISLookUpException, IOException {
|
55
|
res.setContentType("text/xml");
|
56
|
IOUtils.write(dataTransformerFactory.createTransformer(ruleId).evaluate(record), res.getOutputStream());
|
57
|
}
|
58
|
|
59
|
@ExceptionHandler(Exception.class)
|
60
|
@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
|
61
|
public @ResponseBody ErrorMessage handleException(final HttpServletRequest req, final Exception e) {
|
62
|
return new ErrorMessage(e.getMessage(), ExceptionUtils.getStackTrace(e));
|
63
|
}
|
64
|
}
|