1
|
package eu.dnetlib.data.transformation.inspector;
|
2
|
|
3
|
import java.util.List;
|
4
|
import java.util.stream.Collectors;
|
5
|
|
6
|
import javax.annotation.Resource;
|
7
|
|
8
|
import org.springframework.stereotype.Controller;
|
9
|
import org.springframework.ui.Model;
|
10
|
import org.springframework.web.bind.annotation.RequestMapping;
|
11
|
import org.springframework.web.bind.annotation.RequestParam;
|
12
|
|
13
|
import eu.dnetlib.data.transformation.service.DataTransformerFactory;
|
14
|
import eu.dnetlib.data.transformation.service.SimpleDataTransformer;
|
15
|
import eu.dnetlib.enabling.inspector.AbstractInspectorController;
|
16
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException;
|
17
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
|
18
|
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
|
19
|
|
20
|
@Controller
|
21
|
public class DataTransformationController extends AbstractInspectorController {
|
22
|
|
23
|
@Resource
|
24
|
private DataTransformerFactory dataTransformerFactory;
|
25
|
|
26
|
@Resource
|
27
|
private UniqueServiceLocator serviceLocator;
|
28
|
|
29
|
public static class SelectOption implements Comparable<SelectOption> {
|
30
|
|
31
|
private final String id;
|
32
|
private final String name;
|
33
|
private final boolean selected;
|
34
|
|
35
|
public SelectOption(final String id, final String name, final boolean selected) {
|
36
|
this.id = id;
|
37
|
this.name = name;
|
38
|
this.selected = selected;
|
39
|
}
|
40
|
|
41
|
public String getId() {
|
42
|
return id;
|
43
|
}
|
44
|
|
45
|
public String getName() {
|
46
|
return name;
|
47
|
}
|
48
|
|
49
|
public boolean isSelected() {
|
50
|
return selected;
|
51
|
}
|
52
|
|
53
|
@Override
|
54
|
public int compareTo(final SelectOption o) {
|
55
|
return getName().toLowerCase().compareTo(o.getName().toLowerCase());
|
56
|
}
|
57
|
}
|
58
|
|
59
|
@RequestMapping(value = "/inspector/transform.do")
|
60
|
public void transform(final Model model,
|
61
|
@RequestParam(value = "rule", required = false) final String ruleId,
|
62
|
@RequestParam(value = "record", required = false) final String record) throws Exception {
|
63
|
|
64
|
model.addAttribute("rules", obtainRuleProfiles(ruleId));
|
65
|
|
66
|
if (ruleId != null && record != null) {
|
67
|
final SimpleDataTransformer transformer = dataTransformerFactory.createTransformer(ruleId);
|
68
|
model.addAttribute("input", record);
|
69
|
model.addAttribute("output", transformer.evaluate(record));
|
70
|
}
|
71
|
}
|
72
|
|
73
|
private List<SelectOption> obtainRuleProfiles(final String currentId) throws ISLookUpException {
|
74
|
final String xquery = "for $x " +
|
75
|
"in collection('/db/DRIVER/TransformationRuleDSResources/TransformationRuleDSResourceType') " +
|
76
|
"return concat($x//RESOURCE_IDENTIFIER/@value, ' @@@ ', $x//TITLE)";
|
77
|
|
78
|
return serviceLocator.getService(ISLookUpService.class)
|
79
|
.quickSearchProfile(xquery)
|
80
|
.stream()
|
81
|
.map(s -> {
|
82
|
final String[] arr = s.split("@@@");
|
83
|
final String id = arr[0].trim();
|
84
|
final String name = arr[1].trim();
|
85
|
return new SelectOption(id, name, id.equals(currentId));
|
86
|
})
|
87
|
.sorted()
|
88
|
.collect(Collectors.toList());
|
89
|
|
90
|
}
|
91
|
}
|