Revision 55254
Added by Michele Artini about 5 years ago
modules/dnet-data-transformation-service/trunk/src/main/java/eu/dnetlib/data/transformation/inspector/DataTransformationController.java | ||
---|---|---|
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 |
} |
modules/dnet-data-transformation-service/trunk/src/main/java/eu/dnetlib/data/transformation/ui/TransformerUtils.java | ||
---|---|---|
1 |
package eu.dnetlib.data.transformation.ui; |
|
2 |
|
|
3 |
import java.util.List; |
|
4 |
import java.util.stream.Collectors; |
|
5 |
|
|
6 |
import javax.annotation.Resource; |
|
7 |
|
|
8 |
import org.springframework.stereotype.Component; |
|
9 |
|
|
10 |
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException; |
|
11 |
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService; |
|
12 |
import eu.dnetlib.enabling.locators.UniqueServiceLocator; |
|
13 |
|
|
14 |
@Component |
|
15 |
public class TransformerUtils { |
|
16 |
|
|
17 |
@Resource |
|
18 |
private UniqueServiceLocator serviceLocator; |
|
19 |
|
|
20 |
public List<SelectOption> obtainRuleProfiles(final String currentId) throws ISLookUpException { |
|
21 |
final String xquery = "for $x " + |
|
22 |
"in collection('/db/DRIVER/TransformationRuleDSResources/TransformationRuleDSResourceType') " + |
|
23 |
"return concat($x//RESOURCE_IDENTIFIER/@value, ' @@@ ', $x//TITLE)"; |
|
24 |
|
|
25 |
return serviceLocator.getService(ISLookUpService.class) |
|
26 |
.quickSearchProfile(xquery) |
|
27 |
.stream() |
|
28 |
.map(s -> { |
|
29 |
final String[] arr = s.split("@@@"); |
|
30 |
final String id = arr[0].trim(); |
|
31 |
final String name = arr[1].trim(); |
|
32 |
return new SelectOption(id, name, id.equals(currentId)); |
|
33 |
}) |
|
34 |
.sorted() |
|
35 |
.collect(Collectors.toList()); |
|
36 |
|
|
37 |
} |
|
38 |
} |
modules/dnet-data-transformation-service/trunk/src/main/java/eu/dnetlib/data/transformation/ui/DataTransformationController.java | ||
---|---|---|
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 |
|
|
57 |
System.out.println(record); |
|
58 |
|
|
59 |
IOUtils.write(dataTransformerFactory.createTransformer(ruleId).evaluate(record), res.getOutputStream()); |
|
60 |
} |
|
61 |
|
|
62 |
@ExceptionHandler(Exception.class) |
|
63 |
@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR) |
|
64 |
public @ResponseBody ErrorMessage handleException(final HttpServletRequest req, final Exception e) { |
|
65 |
return new ErrorMessage(e.getMessage(), ExceptionUtils.getStackTrace(e)); |
|
66 |
} |
|
67 |
} |
modules/dnet-data-transformation-service/trunk/src/main/java/eu/dnetlib/data/transformation/ui/SelectOption.java | ||
---|---|---|
1 |
package eu.dnetlib.data.transformation.ui; |
|
2 |
|
|
3 |
public class SelectOption implements Comparable<SelectOption> { |
|
4 |
|
|
5 |
private final String id; |
|
6 |
private final String name; |
|
7 |
private final boolean selected; |
|
8 |
|
|
9 |
public SelectOption(final String id, final String name, final boolean selected) { |
|
10 |
this.id = id; |
|
11 |
this.name = name; |
|
12 |
this.selected = selected; |
|
13 |
} |
|
14 |
|
|
15 |
public String getId() { |
|
16 |
return id; |
|
17 |
} |
|
18 |
|
|
19 |
public String getName() { |
|
20 |
return name; |
|
21 |
} |
|
22 |
|
|
23 |
public boolean isSelected() { |
|
24 |
return selected; |
|
25 |
} |
|
26 |
|
|
27 |
@Override |
|
28 |
public int compareTo(final SelectOption o) { |
|
29 |
return getName().toLowerCase().compareTo(o.getName().toLowerCase()); |
|
30 |
} |
|
31 |
} |
modules/dnet-data-transformation-service/trunk/src/main/java/eu/dnetlib/data/transformation/ui/TransformEntryPointController.java | ||
---|---|---|
1 |
package eu.dnetlib.data.transformation.ui; |
|
2 |
|
|
3 |
import javax.annotation.Resource; |
|
4 |
import javax.servlet.http.HttpServletRequest; |
|
5 |
import javax.servlet.http.HttpServletResponse; |
|
6 |
|
|
7 |
import org.springframework.ui.ModelMap; |
|
8 |
|
|
9 |
import eu.dnetlib.functionality.modular.ui.ModuleEntryPoint; |
|
10 |
|
|
11 |
public class TransformEntryPointController extends ModuleEntryPoint { |
|
12 |
|
|
13 |
@Resource |
|
14 |
private TransformerUtils transformerUtils; |
|
15 |
|
|
16 |
@Override |
|
17 |
protected void initialize(final ModelMap map, final HttpServletRequest request, final HttpServletResponse response) throws Exception { |
|
18 |
map.addAttribute("rules", transformerUtils.obtainRuleProfiles(null)); |
|
19 |
} |
|
20 |
|
|
21 |
} |
modules/dnet-data-transformation-service/trunk/src/main/resources/eu/dnetlib/webContext-dnet-data-transformation-inspector.xml | ||
---|---|---|
16 | 16 |
</property> |
17 | 17 |
</bean> |
18 | 18 |
|
19 |
<bean name="/ui/transform.do" |
|
20 |
class="eu.dnetlib.data.transformation.ui.TransformEntryPointController" |
|
21 |
p:menu="Transformation Tester" p:title="Transformation Tester" |
|
22 |
p:description="Transformation Tester" p:group="Tools" |
|
23 |
p:order="6"> |
|
24 |
<property name="permissionLevels"> |
|
25 |
<set> |
|
26 |
<value>DS_ADMIN</value> |
|
27 |
</set> |
|
28 |
</property> |
|
29 |
</bean> |
|
30 |
|
|
19 | 31 |
</beans> |
modules/dnet-data-transformation-service/trunk/src/main/resources/eu/dnetlib/functionality/modular/ui/views/ui/transform.st | ||
---|---|---|
1 |
$common/master( |
|
2 |
|
|
3 |
header={ |
|
4 |
<script type="text/javascript" src="../resources/js/angular.min.js"></script> |
|
5 |
<script type="text/javascript" src="../resources/js/transform/transform.js"></script> |
|
6 |
}, |
|
7 |
onLoad={}, |
|
8 |
body={ |
|
9 |
<div class="row" ng-app="transformUI" ng-controller="transformCtrl"> |
|
10 |
<div class="col-xs-12 col-md-10 col-lg-8"> |
|
11 |
<form> |
|
12 |
<div class="form-group"> |
|
13 |
<label>Transformation rules:</label> |
|
14 |
<select name="rule" class="form-control" ng-model="rule"> |
|
15 |
$rules:{<option $if(it.selected)$selected$endif$ value="$it.id$">$it.name$</option>}$ |
|
16 |
</select> |
|
17 |
</div> |
|
18 |
<div class="form-group"> |
|
19 |
<label>Input Record:</label> |
|
20 |
<textarea cols="100" rows="10" class="form-control" ng-model="inputRecord"></textarea> |
|
21 |
</div> |
|
22 |
|
|
23 |
<button class="btn btn-sm btn-primary" ng-click="transform(rule, inputRecord)" ng-disabled="!rule || !inputRecord">transform</button> |
|
24 |
<button class="btn btn-sm btn-default pull-right" ng-click="showProfile(rule)" ng-disabled="!rule">show transformation rule</button> |
|
25 |
</form> |
|
26 |
|
|
27 |
<br /> |
|
28 |
|
|
29 |
<p ng-if="outputRecord"> |
|
30 |
<label>Output Record:</label> |
|
31 |
<pre ng-if="outputRecord">{{outputRecord}}</pre> |
|
32 |
</p> |
|
33 |
|
|
34 |
<p ng-if="error.message"> |
|
35 |
<label>Error: <i>{{error.message}}</i></label> |
|
36 |
<pre ng-if="error.stacktrace">{{error.stacktrace}}</pre> |
|
37 |
</p> |
|
38 |
</div> |
|
39 |
</div> |
|
40 |
} |
|
41 |
)$ |
modules/dnet-data-transformation-service/trunk/src/main/resources/eu/dnetlib/web/resources/js/transform/transform.js | ||
---|---|---|
1 |
var module = angular.module('transformUI', []); |
|
2 |
|
|
3 |
module.controller('transformCtrl', function ($scope, $http) { |
|
4 |
$scope.outputRecord = ''; |
|
5 |
$scope.error = {}; |
|
6 |
|
|
7 |
$scope.transform = function(rule, s) { |
|
8 |
$scope.outputRecord = ''; |
|
9 |
$scope.error = {}; |
|
10 |
$http.defaults.headers.post["Content-Type"] = "application/json; charset=UTF-8"; |
|
11 |
$http.post('transform/transform.do?rule=' + encodeURIComponent(rule), s).success(function(res) { |
|
12 |
if(res) { |
|
13 |
$scope.outputRecord = res; |
|
14 |
} else { |
|
15 |
$scope.error({ |
|
16 |
'message' : 'empty response', |
|
17 |
}); |
|
18 |
} |
|
19 |
}).error(function(err) { |
|
20 |
$scope.error = err; |
|
21 |
}); |
|
22 |
|
|
23 |
} |
|
24 |
|
|
25 |
$scope.showProfile = function(rule) { |
|
26 |
location.href = './isManager.do#/profile/' + encodeURIComponent(rule); |
|
27 |
} |
|
28 |
|
|
29 |
}); |
modules/dnet-data-transformation-service/trunk/pom.xml | ||
---|---|---|
40 | 40 |
<version>[2.0.0,3.0.0)</version> |
41 | 41 |
</dependency> |
42 | 42 |
<dependency> |
43 |
<groupId>eu.dnetlib</groupId> |
|
44 |
<artifactId>dnet-modular-ui</artifactId> |
|
45 |
<version>[3.0.0,4.0.0)</version> |
|
46 |
</dependency> |
|
47 |
<dependency> |
|
48 |
<groupId>javax.servlet</groupId> |
|
49 |
<artifactId>javax.servlet-api</artifactId> |
|
50 |
<version>${javax.servlet.version}</version> |
|
51 |
<scope>provided</scope> |
|
52 |
</dependency> |
|
53 |
<dependency> |
|
43 | 54 |
<groupId>junit</groupId> |
44 | 55 |
<artifactId>junit</artifactId> |
45 | 56 |
<version>${junit.version}</version> |
Also available in: Unified diff
implemented modular ui