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