Revision 37560
Added by Michele Artini over 9 years ago
modules/dnet-information-service/trunk/src/main/java/eu/dnetlib/functionality/modular/ui/is/IsAjaxController.java | ||
---|---|---|
27 | 27 |
import com.google.common.collect.Iterables; |
28 | 28 |
import com.google.common.collect.Lists; |
29 | 29 |
import com.google.common.collect.Maps; |
30 |
import com.google.gson.Gson; |
|
31 | 30 |
|
32 | 31 |
import eu.dnetlib.common.services.locators.DnetServiceLocator; |
33 |
import eu.dnetlib.enabling.datastructures.AbstractJsonResource; |
|
32 |
import eu.dnetlib.enabling.annotations.DnetResourceHelper; |
|
33 |
import eu.dnetlib.enabling.datastructures.BaseResource; |
|
34 | 34 |
import eu.dnetlib.enabling.nodeManager.ContentInitializer; |
35 | 35 |
import eu.dnetlib.enabling.utils.DnetAnnotationUtils; |
36 | 36 |
import eu.dnetlib.functionality.modular.ui.AbstractAjaxController; |
... | ... | |
40 | 40 |
import eu.dnetlib.rmi.objects.is.DnetResourceType; |
41 | 41 |
import eu.dnetlib.rmi.objects.is.DnetService; |
42 | 42 |
import eu.dnetlib.rmi.soap.InformationService; |
43 |
import eu.dnetlib.rmi.soap.exceptions.InformationServiceException; |
|
43 | 44 |
|
44 | 45 |
@Controller |
45 | 46 |
@RequestMapping("/ui/ajax_is") |
... | ... | |
87 | 88 |
|
88 | 89 |
@RequestMapping("registerDs.do") |
89 | 90 |
public @ResponseBody |
90 |
String registerDs(@RequestParam(value = "code", required = true) final String code, |
|
91 |
@RequestParam(value = "name", required = true) final String name, |
|
92 |
@RequestParam(value = "description", required = false) final String description, |
|
93 |
@RequestParam(value = "type", required = true) final String type, |
|
91 |
String registerDs(@RequestParam(value = "type", required = true) final String type, |
|
94 | 92 |
@RequestParam(value = "serviceId", required = false) final String serviceId, |
95 | 93 |
@RequestParam(value = "content", required = true) final String content) throws Exception { |
96 |
final DnetDataStructure ds = new DnetDataStructure(); |
|
97 |
|
|
98 |
ds.setCode(code); |
|
99 |
ds.setName(name); |
|
100 |
ds.setDescription(description); |
|
101 |
ds.setType(type); |
|
102 |
ds.setServiceId(serviceId); |
|
103 |
final Class<? extends AbstractJsonResource> cl = DnetAnnotationUtils.findClass(type); |
|
104 |
if (cl != null) { |
|
105 |
ds.setContentFromObject(new Gson().fromJson(content, cl)); |
|
106 |
} else { |
|
107 |
ds.setContent(content); |
|
94 |
final Class<? extends BaseResource> clazz = DnetAnnotationUtils.findClass(type); |
|
95 |
if (clazz != null) { |
|
96 |
final DnetDataStructure ds = DnetResourceHelper.convertToObject(content, clazz).asDnetDataStructure(); |
|
97 |
ds.setServiceId(serviceId); |
|
98 |
ds.setDate(new Date()); |
|
99 |
return serviceLocator.getService(InformationService.class).registerDs(ds); |
|
108 | 100 |
} |
109 |
return serviceLocator.getService(InformationService.class).registerDs(ds);
|
|
101 |
throw new InformationServiceException("Invalid type: " + type);
|
|
110 | 102 |
} |
111 | 103 |
|
112 | 104 |
@RequestMapping("updateDs.do") |
113 | 105 |
public @ResponseBody |
114 | 106 |
String updateDs( |
115 | 107 |
@RequestParam(value = "id", required = true) final String id, |
116 |
@RequestParam(value = "name", required = true) final String name, |
|
117 |
@RequestParam(value = "description", required = true) final String description, |
|
108 |
@RequestParam(value = "type", required = true) final String type, |
|
118 | 109 |
@RequestParam(value = "content", required = true) final String content) throws Exception { |
119 | 110 |
|
120 |
final InformationService is = serviceLocator.getService(InformationService.class); |
|
121 |
|
|
122 |
final DnetDataStructure ds = is.getDsById(id); |
|
123 |
if (name != null) { |
|
124 |
ds.setName(name); |
|
111 |
final Class<? extends BaseResource> cl = DnetAnnotationUtils.findClass(type); |
|
112 |
if (cl != null) { |
|
113 |
final DnetDataStructure ds = DnetResourceHelper.convertToObject(content, cl).asDnetDataStructure(); |
|
114 |
ds.setId(id); |
|
115 |
ds.setDate(new Date()); |
|
116 |
return serviceLocator.getService(InformationService.class).registerDs(ds); |
|
125 | 117 |
} |
126 |
if (description != null) { |
|
127 |
ds.setDescription(description); |
|
128 |
} |
|
129 |
if (content != null) { |
|
130 |
final Class<? extends AbstractJsonResource> cl = DnetAnnotationUtils.findClass(ds.getType()); |
|
131 |
if (cl != null) { |
|
132 |
ds.setContentFromObject(new Gson().fromJson(content, cl)); |
|
133 |
} else { |
|
134 |
ds.setContent(content); |
|
135 |
} |
|
136 |
ds.setContent(content); |
|
137 |
} |
|
138 |
ds.setDate(new Date()); |
|
139 |
|
|
140 |
return is.registerDs(ds); |
|
141 |
|
|
118 |
throw new InformationServiceException("Invalid type: " + type); |
|
142 | 119 |
} |
143 | 120 |
|
144 | 121 |
@RequestMapping("deleteDs.do") |
modules/dnet-information-service/trunk/src/main/resources/eu/dnetlib/web/resources/html/is_templates/ds.html | ||
---|---|---|
3 | 3 |
<div class="well">Missing resource !!!</div> |
4 | 4 |
</div> |
5 | 5 |
<div class="col-xs-12" ng-show="resource && resource.id"> |
6 |
<form class="form-horizontal" style="margin-top: 30px"> |
|
7 |
<div class="form-group"> |
|
8 |
<label class="col-sm-2 control-label">D-Net ID</label> |
|
9 |
<div class="col-sm-9"> |
|
10 |
<p class="form-control-static">{{resource.id}}</p> |
|
11 |
</div> |
|
12 |
</div> |
|
13 |
<div class="form-group"> |
|
14 |
<label class="col-sm-2 control-label">Code</label> |
|
15 |
<div class="col-sm-9"> |
|
16 |
<p class="form-control-static">{{resource.code}}</p> |
|
17 |
</div> |
|
18 |
</div> |
|
19 |
<div class="form-group"> |
|
20 |
<label class="col-sm-2 control-label">Type</label> |
|
21 |
<div class="col-sm-9"> |
|
22 |
<p class="form-control-static">{{resource.type}}</p> |
|
23 |
</div> |
|
24 |
</div> |
|
25 |
<div class="form-group"> |
|
26 |
<label class="col-sm-2 control-label">Creation date</label> |
|
27 |
<div class="col-sm-9"> |
|
28 |
<p class="form-control-static">{{resource.date | date:'yyyy-MM-dd HH:mm:ss'}}</p> |
|
29 |
</div> |
|
30 |
</div> |
|
31 |
<div class="form-group"> |
|
32 |
<label class="col-sm-2 control-label" for="inputName">Name</label> |
|
33 |
<div class="col-sm-9"> |
|
34 |
<input class="form-control" id="inputName" placeholder="Name" ng-model="resource.name"> |
|
35 |
</div> |
|
36 |
</div> |
|
37 |
<div class="form-group"> |
|
38 |
<label class="col-sm-2 control-label" for="inputDesc">Description</label> |
|
39 |
<div class="col-sm-9"> |
|
40 |
<textarea id="inputDesc" class="form-control" rows="5" placeholder="Description" ng-model="resource.description"></textarea> |
|
41 |
</div> |
|
42 |
</div> |
|
43 |
<div class="form-group"> |
|
44 |
<div class="col-sm-offset-2 col-sm-9"> |
|
45 |
<button type="button" class="btn btn-sm btn-primary" ng-click="saveInfo()">Save</button> |
|
46 |
<button type="button" class="btn btn-sm btn-default" data-toggle="modal" data-target="#contentModal" ng-click="setupEditContent()">Show/edit content</button> |
|
47 |
<button type="button" class="btn btn-sm btn-default" ng-click="getResource(resource.id)">Reset</button> |
|
48 |
<button type="submit" class="btn btn-sm btn-danger pull-right" ng-click="deleteResource()">Delete</button> |
|
49 |
</div> |
|
50 |
</div> |
|
6 |
<div class="well"> |
|
7 |
<table class="table table-striped"> |
|
8 |
<tr> |
|
9 |
<th class="col-sm-2">D-Net ID</th> |
|
10 |
<td class="col-sm-9">{{resource.id}}</td> |
|
11 |
</tr> |
|
12 |
<tr> |
|
13 |
<th class="col-sm-2">Code</th> |
|
14 |
<td class="col-sm-9">{{resource.code}}</td> |
|
15 |
</tr> |
|
16 |
<tr> |
|
17 |
<th class="col-sm-2">Type</th> |
|
18 |
<td class="col-sm-9">{{resource.type}}</td> |
|
19 |
</tr> |
|
20 |
<tr> |
|
21 |
<th class="col-sm-2">Creation date</th> |
|
22 |
<td class="col-sm-9">{{resource.date | date:'yyyy-MM-dd HH:mm:ss'}}</td> |
|
23 |
</tr> |
|
24 |
<tr> |
|
25 |
<th class="col-sm-2">Name</th> |
|
26 |
<td class="col-sm-9">{{resource.name}}</td> |
|
27 |
</tr> |
|
28 |
<tr> |
|
29 |
<th class="col-sm-2">Description</th> |
|
30 |
<td class="col-sm-9">{{resource.description}}</td> |
|
31 |
</tr> |
|
32 |
</table> |
|
33 |
</div> |
|
34 |
<form class="form-inline"> |
|
35 |
<button type="button" class="btn btn-sm btn-default" data-toggle="modal" data-target="#contentModal" ng-click="setupEditContent()">Show/edit content</button> |
|
36 |
<button type="submit" class="btn btn-sm btn-danger" ng-click="deleteResource()">Delete</button> |
|
51 | 37 |
</form> |
52 | 38 |
</div> |
53 | 39 |
</div> |
modules/dnet-information-service/trunk/src/main/resources/eu/dnetlib/web/resources/js/is_controllers.js | ||
---|---|---|
136 | 136 |
alert("Invalid code : " + parsed.code); |
137 | 137 |
return; |
138 | 138 |
} |
139 |
$scope.resource.name = parsed.name; |
|
140 |
$scope.resource.description = parsed.description; |
|
141 | 139 |
$scope.parsedContent = JSON.stringify(parsed); |
142 | 140 |
} |
143 | 141 |
|
144 | 142 |
$scope.saveDs({ |
145 | 143 |
'id' : $scope.resource.id, |
146 |
'name' : $scope.resource.name, |
|
147 |
'description' : $scope.resource.description, |
|
144 |
'type' : $scope.resource.type, |
|
148 | 145 |
'content' : $scope.parsedContent |
149 | 146 |
}); |
150 | 147 |
} catch(err) { |
... | ... | |
152 | 149 |
} |
153 | 150 |
} |
154 | 151 |
|
155 |
$scope.saveInfo = function() { |
|
156 |
if ($scope.format == 'JSON') { |
|
157 |
var parsed = JSON.parse($scope.resource.content); |
|
158 |
if (parsed.code != $scope.resource.code) { |
|
159 |
alert("Invalid code : " + parsed.code); |
|
160 |
return; |
|
161 |
} |
|
162 |
parsed.name = $scope.resource.name; |
|
163 |
parsed.description = $scope.resource.description; |
|
164 |
$scope.parsedContent = JSON.stringify(parsed); |
|
165 |
} |
|
166 |
|
|
167 |
$scope.saveDs({ |
|
168 |
'id' : $scope.resource.id, |
|
169 |
'name' : $scope.resource.name, |
|
170 |
'description' : $scope.resource.description, |
|
171 |
'content' : $scope.parsedContent |
|
172 |
}); |
|
173 |
} |
|
174 |
|
|
175 | 152 |
$scope.saveDs = function(map) { |
176 | 153 |
$scope.showSpinner(); |
177 | 154 |
$http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8"; |
modules/dnet-components/trunk/src/main/java/eu/dnetlib/enabling/utils/DnetAnnotationUtils.java | ||
---|---|---|
12 | 12 |
import eu.dnetlib.enabling.annotations.DnetResource; |
13 | 13 |
import eu.dnetlib.enabling.annotations.IndexedField; |
14 | 14 |
import eu.dnetlib.enabling.annotations.IndexedFieldsContainer; |
15 |
import eu.dnetlib.enabling.datastructures.AbstractJsonResource;
|
|
16 |
import eu.dnetlib.rmi.objects.is.DnetResourceFormat;
|
|
15 |
import eu.dnetlib.enabling.datastructures.AbstractXmlResource;
|
|
16 |
import eu.dnetlib.enabling.datastructures.BaseResource;
|
|
17 | 17 |
|
18 | 18 |
public class DnetAnnotationUtils { |
19 | 19 |
|
20 | 20 |
private static final Log log = LogFactory.getLog(DnetAnnotationUtils.class); |
21 | 21 |
|
22 |
private static Map<String, Class<? extends AbstractJsonResource>> mapJsonClasses = Maps.newHashMap();
|
|
22 |
private static Map<String, Class<? extends BaseResource>> mapClasses = Maps.newHashMap();
|
|
23 | 23 |
|
24 | 24 |
public static class IndexedFields { |
25 | 25 |
|
... | ... | |
63 | 63 |
public static Map<String, String> getIndexedFields(final Object o) { |
64 | 64 |
final Map<String, String> map = Maps.newLinkedHashMap(); |
65 | 65 |
if (o.getClass().isAnnotationPresent(DnetResource.class)) { |
66 |
getIndexedFields(o, map); |
|
66 |
switch (o.getClass().getAnnotation(DnetResource.class).format()) { |
|
67 |
case JSON: |
|
68 |
getIndexedFields(o, map); |
|
69 |
break; |
|
70 |
case XML: |
|
71 |
if (o instanceof AbstractXmlResource) { |
|
72 |
map.putAll(((AbstractXmlResource) o).getProperties()); |
|
73 |
} |
|
74 |
break; |
|
75 |
default: |
|
76 |
break; |
|
77 |
} |
|
67 | 78 |
} |
68 | 79 |
return map; |
69 | 80 |
} |
... | ... | |
129 | 140 |
} |
130 | 141 |
|
131 | 142 |
@SuppressWarnings("unchecked") |
132 |
public static Class<? extends AbstractJsonResource> findClass(final String type) {
|
|
133 |
if (mapJsonClasses.isEmpty()) {
|
|
143 |
public static Class<? extends BaseResource> findClass(final String type) {
|
|
144 |
if (mapClasses.isEmpty()) { |
|
134 | 145 |
final Reflections reflections = new Reflections("eu.dnetlib.enabling.datastructures"); |
135 | 146 |
for (Class<?> cl : reflections.getTypesAnnotatedWith(DnetResource.class)) { |
136 | 147 |
final DnetResource annotation = cl.getAnnotation(DnetResource.class); |
137 |
if (annotation.format() == DnetResourceFormat.JSON && AbstractJsonResource.class.isAssignableFrom(cl)) {
|
|
138 |
mapJsonClasses.put(annotation.type(), (Class<? extends AbstractJsonResource>) cl);
|
|
148 |
if (BaseResource.class.isAssignableFrom(cl)) {
|
|
149 |
mapClasses.put(annotation.type(), (Class<? extends BaseResource>) cl);
|
|
139 | 150 |
log.info("Annotated JSON class: " + cl.getName()); |
140 | 151 |
} |
141 | 152 |
} |
142 | 153 |
} |
143 |
return mapJsonClasses.get(type);
|
|
154 |
return mapClasses.get(type); |
|
144 | 155 |
} |
145 | 156 |
} |
modules/dnet-components/trunk/src/main/java/eu/dnetlib/enabling/annotations/DnetResourceHelper.java | ||
---|---|---|
2 | 2 |
|
3 | 3 |
import com.google.gson.Gson; |
4 | 4 |
|
5 |
import eu.dnetlib.enabling.datastructures.AbstractXmlResource; |
|
5 | 6 |
import eu.dnetlib.rmi.objects.is.DnetResourceType; |
6 | 7 |
import eu.dnetlib.rmi.soap.exceptions.InformationServiceException; |
7 | 8 |
|
... | ... | |
35 | 36 |
case JSON: |
36 | 37 |
return new Gson().toJson(o); |
37 | 38 |
case XML: |
38 |
throw new InformationServiceException("NOT IMPLEMENTED"); |
|
39 |
if (o instanceof AbstractXmlResource) { |
|
40 |
return ((AbstractXmlResource) o).asXML(); |
|
41 |
} else { |
|
42 |
throw new InformationServiceException("Class " + o.getClass().getName() + " not extends AbstractXmlResource"); |
|
43 |
} |
|
39 | 44 |
case TEXT: |
40 | 45 |
return o.toString(); |
41 | 46 |
default: |
Also available in: Unified diff
update of datastructures