001package de.deepamehta.webservice;
002
003import de.deepamehta.core.Association;
004import de.deepamehta.core.AssociationType;
005import de.deepamehta.core.DeepaMehtaObject;
006import de.deepamehta.core.RelatedAssociation;
007import de.deepamehta.core.RelatedTopic;
008import de.deepamehta.core.Topic;
009import de.deepamehta.core.TopicType;
010import de.deepamehta.core.model.AssociationModel;
011import de.deepamehta.core.model.AssociationTypeModel;
012import de.deepamehta.core.model.SimpleValue;
013import de.deepamehta.core.model.TopicModel;
014import de.deepamehta.core.model.TopicTypeModel;
015import de.deepamehta.core.osgi.PluginActivator;
016import de.deepamehta.core.service.Directives;
017import de.deepamehta.core.service.DirectivesResponse;
018import de.deepamehta.core.service.PluginInfo;
019import de.deepamehta.core.service.Transactional;
020
021import javax.ws.rs.GET;
022import javax.ws.rs.POST;
023import javax.ws.rs.PUT;
024import javax.ws.rs.DELETE;
025import javax.ws.rs.Consumes;
026import javax.ws.rs.DefaultValue;
027import javax.ws.rs.HeaderParam;
028import javax.ws.rs.Path;
029import javax.ws.rs.PathParam;
030import javax.ws.rs.Produces;
031import javax.ws.rs.QueryParam;
032
033import java.util.List;
034import java.util.logging.Logger;
035
036
037
038@Path("/core")
039@Consumes("application/json")
040@Produces("application/json")
041public class WebservicePlugin extends PluginActivator {
042
043    // ---------------------------------------------------------------------------------------------- Instance Variables
044
045    private Logger logger = Logger.getLogger(getClass().getName());
046
047    // -------------------------------------------------------------------------------------------------- Public Methods
048
049
050
051    // === Topics ===
052
053    // Note: the "include_childs" query paramter is handled by the core's JerseyResponseFilter
054    @GET
055    @Path("/topic/{id}")
056    public Topic getTopic(@PathParam("id") long topicId) {
057        return dm4.getTopic(topicId);
058    }
059
060    // Note: the "include_childs" query paramter is handled by the core's JerseyResponseFilter
061    @GET
062    @Path("/topic/by_uri/{uri}")
063    public Topic getTopicByUri(@PathParam("uri") String uri) {
064        return dm4.getTopicByUri(uri);
065    }
066
067    // Note: the "include_childs" query paramter is handled by the core's JerseyResponseFilter
068    @GET
069    @Path("/topic/by_value/{key}/{value}")
070    public Topic getTopicByValue(@PathParam("key") String key, @PathParam("value") SimpleValue value) {
071        return dm4.getTopicByValue(key, value);
072    }
073
074    // Note: the "include_childs" query paramter is handled by the core's JerseyResponseFilter
075    @GET
076    @Path("/topic/multi/by_value/{key}/{value}")
077    public List<Topic> getTopicsByValue(@PathParam("key") String key, @PathParam("value") SimpleValue value) {
078        return dm4.getTopicsByValue(key, value);
079    }
080
081    // Note: the "include_childs" query paramter is handled by the core's JerseyResponseFilter
082    @GET
083    @Path("/topic/by_type/{topic_type_uri}")
084    public List<Topic> getTopicsByType(@PathParam("topic_type_uri") String topicTypeUri) {
085        return dm4.getTopicsByType(topicTypeUri);
086    }
087
088    // Note: the "include_childs" query paramter is handled by the core's JerseyResponseFilter
089    @GET
090    @Path("/topic")
091    public List<Topic> searchTopics(@QueryParam("search") String searchTerm, @QueryParam("field") String fieldUri) {
092        return dm4.searchTopics(searchTerm, fieldUri);
093    }
094
095    @POST
096    @Path("/topic")
097    @Transactional
098    public DirectivesResponse createTopic(TopicModel model) {
099        return new DirectivesResponse(dm4.createTopic(model));
100    }
101
102    @PUT
103    @Path("/topic/{id}")
104    @Transactional
105    public DirectivesResponse updateTopic(@PathParam("id") long topicId, TopicModel model) {
106        if (model.getId() != -1 && topicId != model.getId()) {
107            throw new RuntimeException("ID mismatch in update request");
108        }
109        model.setId(topicId);
110        dm4.updateTopic(model);
111        return new DirectivesResponse();
112    }
113
114    @DELETE
115    @Path("/topic/{id}")
116    @Transactional
117    public DirectivesResponse deleteTopic(@PathParam("id") long topicId) {
118        dm4.deleteTopic(topicId);
119        return new DirectivesResponse();
120    }
121
122
123
124    // === Associations ===
125
126    // Note: the "include_childs" query paramter is handled by the core's JerseyResponseFilter
127    @GET
128    @Path("/association/{id}")
129    public Association getAssociation(@PathParam("id") long assocId) {
130        return dm4.getAssociation(assocId);
131    }
132
133    // Note: the "include_childs" query paramter is handled by the core's JerseyResponseFilter
134    @GET
135    @Path("/assoc/by_value/{key}/{value}")
136    public Association getAssociationByValue(@PathParam("key") String key, @PathParam("value") SimpleValue value) {
137        return dm4.getAssociationByValue(key, value);
138    }
139
140    // Note: the "include_childs" query paramter is handled by the core's JerseyResponseFilter
141    @GET
142    @Path("/assoc/multi/by_value/{key}/{value}")
143    public List<Association> getAssociationsByValue(@PathParam("key") String key,
144                                                    @PathParam("value") SimpleValue value) {
145        return dm4.getAssociationsByValue(key, value);
146    }
147
148    // Note: the "include_childs" query paramter is handled by the core's JerseyResponseFilter
149    @GET
150    @Path("/association/{assoc_type_uri}/{topic1_id}/{topic2_id}/{role_type1_uri}/{role_type2_uri}")
151    public Association getAssociation(@PathParam("assoc_type_uri") String assocTypeUri,
152                   @PathParam("topic1_id") long topic1Id, @PathParam("topic2_id") long topic2Id,
153                   @PathParam("role_type1_uri") String roleTypeUri1, @PathParam("role_type2_uri") String roleTypeUri2) {
154        return dm4.getAssociation(assocTypeUri, topic1Id, topic2Id, roleTypeUri1, roleTypeUri2);
155    }
156
157    // ---
158
159    // Note: the "include_childs" query paramter is handled by the core's JerseyResponseFilter
160    @GET
161    @Path("/association/multiple/{topic1_id}/{topic2_id}")
162    public List<Association> getAssociations(@PathParam("topic1_id") long topic1Id,
163                                             @PathParam("topic2_id") long topic2Id) {
164        return dm4.getAssociations(topic1Id, topic2Id);
165    }
166
167    // Note: the "include_childs" query paramter is handled by the core's JerseyResponseFilter
168    @GET
169    @Path("/association/multiple/{topic1_id}/{topic2_id}/{assoc_type_uri}")
170    public List<Association> getAssociations(@PathParam("topic1_id") long topic1Id,
171                                             @PathParam("topic2_id") long topic2Id,
172                                             @PathParam("assoc_type_uri") String assocTypeUri) {
173        return dm4.getAssociations(topic1Id, topic2Id, assocTypeUri);
174    }
175
176    // ---
177
178    @POST
179    @Path("/association")
180    @Transactional
181    public DirectivesResponse createAssociation(AssociationModel model) {
182        return new DirectivesResponse(dm4.createAssociation(model));
183    }
184
185    @PUT
186    @Path("/association/{id}")
187    @Transactional
188    public DirectivesResponse updateAssociation(@PathParam("id") long assocId, AssociationModel model) {
189        if (model.getId() != -1 && assocId != model.getId()) {
190            throw new RuntimeException("ID mismatch in update request");
191        }
192        model.setId(assocId);
193        dm4.updateAssociation(model);
194        return new DirectivesResponse();
195    }
196
197    @DELETE
198    @Path("/association/{id}")
199    @Transactional
200    public DirectivesResponse deleteAssociation(@PathParam("id") long assocId) {
201        dm4.deleteAssociation(assocId);
202        return new DirectivesResponse();
203    }
204
205
206
207    // === Topic Types ===
208
209    @GET
210    @Path("/topictype")
211    public List<String> getTopicTypeUris() {
212        return dm4.getTopicTypeUris();
213    }
214
215    @GET
216    @Path("/topictype/{uri}")
217    public TopicType getTopicType(@PathParam("uri") String uri) {
218        return dm4.getTopicType(uri);
219    }
220
221    @GET
222    @Path("/topictype/all")
223    public List<TopicType> getAllTopicTypes() {
224        return dm4.getAllTopicTypes();
225    }
226
227    @POST
228    @Path("/topictype")
229    @Transactional
230    public TopicType createTopicType(TopicTypeModel model) {
231        return dm4.createTopicType(model);
232    }
233
234    @PUT
235    @Path("/topictype")
236    @Transactional
237    public DirectivesResponse updateTopicType(TopicTypeModel model) {
238        dm4.updateTopicType(model);
239        return new DirectivesResponse();
240    }
241
242    @DELETE
243    @Path("/topictype/{uri}")
244    @Transactional
245    public DirectivesResponse deleteTopicType(@PathParam("uri") String uri) {
246        dm4.deleteTopicType(uri);
247        return new DirectivesResponse();
248    }
249
250
251
252    // === Association Types ===
253
254    @GET
255    @Path("/assoctype")
256    public List<String> getAssociationTypeUris() {
257        return dm4.getAssociationTypeUris();
258    }
259
260    @GET
261    @Path("/assoctype/{uri}")
262    public AssociationType getAssociationType(@PathParam("uri") String uri) {
263        return dm4.getAssociationType(uri);
264    }
265
266    @GET
267    @Path("/assoctype/all")
268    public List<AssociationType> getAssociationAllTypes() {
269        return dm4.getAllAssociationTypes();
270    }
271
272    @POST
273    @Path("/assoctype")
274    @Transactional
275    public AssociationType createAssociationType(AssociationTypeModel model) {
276        return dm4.createAssociationType(model);
277    }
278
279    @PUT
280    @Path("/assoctype")
281    @Transactional
282    public DirectivesResponse updateAssociationType(AssociationTypeModel model) {
283        dm4.updateAssociationType(model);
284        return new DirectivesResponse();
285    }
286
287    @DELETE
288    @Path("/assoctype/{uri}")
289    @Transactional
290    public DirectivesResponse deleteAssociationType(@PathParam("uri") String uri) {
291        dm4.deleteAssociationType(uri);
292        return new DirectivesResponse();
293    }
294
295
296
297    // === Role Types ===
298
299    @POST
300    @Path("/roletype")
301    @Transactional
302    public Topic createRoleType(TopicModel model) {
303        return dm4.createRoleType(model);
304    }
305
306
307
308    // === Plugins ===
309
310    @GET
311    @Path("/plugin")
312    public List<PluginInfo> getPluginInfo() {
313        return dm4.getPluginInfo();
314    }
315
316
317
318    // **********************
319    // *** Topic REST API ***
320    // **********************
321
322
323
324    // Note: the "include_childs" query paramter is handled by the core's JerseyResponseFilter
325    @GET
326    @Path("/topic/{id}/related_topics")
327    public List<RelatedTopic> getTopicRelatedTopics(@PathParam("id")                     long topicId,
328                                                    @QueryParam("assoc_type_uri")        String assocTypeUri,
329                                                    @QueryParam("my_role_type_uri")      String myRoleTypeUri,
330                                                    @QueryParam("others_role_type_uri")  String othersRoleTypeUri,
331                                                    @QueryParam("others_topic_type_uri") String othersTopicTypeUri) {
332        Topic topic = dm4.getTopic(topicId);
333        return getRelatedTopics(topic, "topic", assocTypeUri, myRoleTypeUri, othersRoleTypeUri, othersTopicTypeUri);
334    }
335
336    // Note: the "include_childs" query paramter is handled by the core's JerseyResponseFilter
337    @GET
338    @Path("/topic/{id}/related_assocs")
339    public List<RelatedAssociation> getTopicRelatedAssociations(@PathParam("id")            long topicId,
340                                                       @QueryParam("assoc_type_uri")        String assocTypeUri,
341                                                       @QueryParam("my_role_type_uri")      String myRoleTypeUri,
342                                                       @QueryParam("others_role_type_uri")  String othersRoleTypeUri,
343                                                       @QueryParam("others_assoc_type_uri") String othersAssocTypeUri) {
344        Topic topic = dm4.getTopic(topicId);
345        return getRelatedAssociations(topic, "topic", assocTypeUri, myRoleTypeUri, othersRoleTypeUri,
346            othersAssocTypeUri);
347    }
348
349
350
351    // ****************************
352    // *** Association REST API ***
353    // ****************************
354
355
356
357    // Note: the "include_childs" query paramter is handled by the core's JerseyResponseFilter
358    @GET
359    @Path("/association/{id}/related_topics")
360    public List<RelatedTopic> getAssociationRelatedTopics(@PathParam("id")                  long assocId,
361                                                       @QueryParam("assoc_type_uri")        String assocTypeUri,
362                                                       @QueryParam("my_role_type_uri")      String myRoleTypeUri,
363                                                       @QueryParam("others_role_type_uri")  String othersRoleTypeUri,
364                                                       @QueryParam("others_topic_type_uri") String othersTopicTypeUri) {
365        Association assoc = dm4.getAssociation(assocId);
366        return getRelatedTopics(assoc, "association", assocTypeUri, myRoleTypeUri, othersRoleTypeUri,
367            othersTopicTypeUri);
368    }
369
370    // Note: the "include_childs" query paramter is handled by the core's JerseyResponseFilter
371    @GET
372    @Path("/association/{id}/related_assocs")
373    public List<RelatedAssociation> getAssociationRelatedAssociations(@PathParam("id")      long assocId,
374                                                       @QueryParam("assoc_type_uri")        String assocTypeUri,
375                                                       @QueryParam("my_role_type_uri")      String myRoleTypeUri,
376                                                       @QueryParam("others_role_type_uri")  String othersRoleTypeUri,
377                                                       @QueryParam("others_assoc_type_uri") String othersAssocTypeUri) {
378        Association assoc = dm4.getAssociation(assocId);
379        return getRelatedAssociations(assoc, "association", assocTypeUri, myRoleTypeUri, othersRoleTypeUri,
380            othersAssocTypeUri);
381    }
382
383
384
385    // ------------------------------------------------------------------------------------------------- Private Methods
386
387    private List<RelatedTopic> getRelatedTopics(DeepaMehtaObject object, String objectInfo, String assocTypeUri,
388                                            String myRoleTypeUri, String othersRoleTypeUri, String othersTopicTypeUri) {
389        String operation = "Fetching related topics of " + objectInfo + " " + object.getId();
390        String paramInfo = "(assocTypeUri=\"" + assocTypeUri + "\", myRoleTypeUri=\"" + myRoleTypeUri +
391            "\", othersRoleTypeUri=\"" + othersRoleTypeUri + "\", othersTopicTypeUri=\"" + othersTopicTypeUri + "\")";
392        try {
393            logger.info(operation + " " + paramInfo);
394            return object.getRelatedTopics(assocTypeUri, myRoleTypeUri, othersRoleTypeUri, othersTopicTypeUri);
395        } catch (Exception e) {
396            throw new RuntimeException(operation + " failed " + paramInfo, e);
397        }
398    }
399
400    private List<RelatedAssociation> getRelatedAssociations(DeepaMehtaObject object, String objectInfo,
401                       String assocTypeUri, String myRoleTypeUri, String othersRoleTypeUri, String othersAssocTypeUri) {
402        String operation = "Fetching related associations of " + objectInfo + " " + object.getId();
403        String paramInfo = "(assocTypeUri=\"" + assocTypeUri + "\", myRoleTypeUri=\"" + myRoleTypeUri +
404            "\", othersRoleTypeUri=\"" + othersRoleTypeUri + "\", othersAssocTypeUri=\"" + othersAssocTypeUri + "\")";
405        try {
406            logger.info(operation + " " + paramInfo);
407            return object.getRelatedAssociations(assocTypeUri, myRoleTypeUri, othersRoleTypeUri, othersAssocTypeUri);
408        } catch (Exception e) {
409            throw new RuntimeException(operation + " failed " + paramInfo, e);
410        }
411    }
412}