001 package de.deepamehta.plugins.webservice; 002 003 import de.deepamehta.core.Association; 004 import de.deepamehta.core.AssociationType; 005 import de.deepamehta.core.DeepaMehtaObject; 006 import de.deepamehta.core.RelatedTopic; 007 import de.deepamehta.core.Topic; 008 import de.deepamehta.core.TopicType; 009 import de.deepamehta.core.model.AssociationModel; 010 import de.deepamehta.core.model.AssociationTypeModel; 011 import de.deepamehta.core.model.SimpleValue; 012 import de.deepamehta.core.model.TopicModel; 013 import de.deepamehta.core.model.TopicTypeModel; 014 import de.deepamehta.core.osgi.PluginActivator; 015 import de.deepamehta.core.service.ClientState; 016 import de.deepamehta.core.service.Directives; 017 import de.deepamehta.core.service.PluginInfo; 018 import de.deepamehta.core.service.ResultList; 019 020 import javax.ws.rs.GET; 021 import javax.ws.rs.POST; 022 import javax.ws.rs.PUT; 023 import javax.ws.rs.DELETE; 024 import javax.ws.rs.Consumes; 025 import javax.ws.rs.DefaultValue; 026 import javax.ws.rs.HeaderParam; 027 import javax.ws.rs.Path; 028 import javax.ws.rs.PathParam; 029 import javax.ws.rs.Produces; 030 import javax.ws.rs.QueryParam; 031 032 import java.util.List; 033 import java.util.logging.Logger; 034 035 036 037 @Path("/core") 038 @Consumes("application/json") 039 @Produces("application/json") 040 public class WebservicePlugin extends PluginActivator { 041 042 // ---------------------------------------------------------------------------------------------- Instance Variables 043 044 private Logger logger = Logger.getLogger(getClass().getName()); 045 046 // -------------------------------------------------------------------------------------------------- Public Methods 047 048 049 050 // === Topics === 051 052 @GET 053 @Path("/topic/{id}") 054 public Topic getTopic(@PathParam("id") long topicId, 055 @QueryParam("fetch_composite") @DefaultValue("true") boolean fetchComposite) { 056 return dms.getTopic(topicId, fetchComposite); 057 } 058 059 @GET 060 @Path("/topic/by_value/{key}/{value}") 061 public Topic getTopic(@PathParam("key") String key, @PathParam("value") SimpleValue value, 062 @QueryParam("fetch_composite") @DefaultValue("true") boolean fetchComposite) { 063 return dms.getTopic(key, value, fetchComposite); 064 } 065 066 @GET 067 @Path("/topic/multi/by_value/{key}/{value}") 068 public List<Topic> getTopics(@PathParam("key") String key, @PathParam("value") SimpleValue value, 069 @QueryParam("fetch_composite") @DefaultValue("false") boolean fetchComposite) { 070 return dms.getTopics(key, value, fetchComposite); 071 } 072 073 @GET 074 @Path("/topic/by_type/{type_uri}") 075 public ResultList<RelatedTopic> getTopics(@PathParam("type_uri") String typeUri, 076 @QueryParam("fetch_composite") @DefaultValue("false") boolean fetchComposite, 077 @QueryParam("max_result_size") int maxResultSize) { 078 return dms.getTopics(typeUri, fetchComposite, maxResultSize); 079 } 080 081 @GET 082 @Path("/topic") 083 public List<Topic> searchTopics(@QueryParam("search") String searchTerm, @QueryParam("field") String fieldUri) { 084 return dms.searchTopics(searchTerm, fieldUri); 085 } 086 087 @POST 088 @Path("/topic") 089 public Topic createTopic(TopicModel model, @HeaderParam("Cookie") ClientState clientState) { 090 return dms.createTopic(model, clientState); 091 } 092 093 @PUT 094 @Path("/topic/{id}") 095 public Directives updateTopic(@PathParam("id") long topicId, TopicModel model, 096 @HeaderParam("Cookie") ClientState clientState) { 097 if (model.getId() != -1 && topicId != model.getId()) { 098 throw new RuntimeException("ID mismatch in update request"); 099 } 100 model.setId(topicId); 101 return dms.updateTopic(model, clientState); 102 } 103 104 @DELETE 105 @Path("/topic/{id}") 106 public Directives deleteTopic(@PathParam("id") long topicId) { 107 return dms.deleteTopic(topicId); 108 } 109 110 111 112 // === Associations === 113 114 @GET 115 @Path("/association/{id}") 116 public Association getAssociation(@PathParam("id") long assocId, 117 @QueryParam("fetch_composite") @DefaultValue("true") boolean fetchComposite) { 118 return dms.getAssociation(assocId, fetchComposite); 119 } 120 121 @GET 122 @Path("/association/{assoc_type_uri}/{topic1_id}/{topic2_id}/{role_type1_uri}/{role_type2_uri}") 123 public Association getAssociation(@PathParam("assoc_type_uri") String assocTypeUri, 124 @PathParam("topic1_id") long topic1Id, @PathParam("topic2_id") long topic2Id, 125 @PathParam("role_type1_uri") String roleTypeUri1, @PathParam("role_type2_uri") String roleTypeUri2, 126 @QueryParam("fetch_composite") @DefaultValue("true") boolean fetchComposite) { 127 return dms.getAssociation(assocTypeUri, topic1Id, topic2Id, roleTypeUri1, roleTypeUri2, fetchComposite); 128 } 129 130 // --- 131 132 @GET 133 @Path("/association/multiple/{topic1_id}/{topic2_id}") 134 public List<Association> getAssociations(@PathParam("topic1_id") long topic1Id, 135 @PathParam("topic2_id") long topic2Id) { 136 return dms.getAssociations(topic1Id, topic2Id); 137 } 138 139 @GET 140 @Path("/association/multiple/{topic1_id}/{topic2_id}/{assoc_type_uri}") 141 public List<Association> getAssociations(@PathParam("topic1_id") long topic1Id, 142 @PathParam("topic2_id") long topic2Id, 143 @PathParam("assoc_type_uri") String assocTypeUri) { 144 return dms.getAssociations(topic1Id, topic2Id, assocTypeUri); 145 } 146 147 // --- 148 149 @POST 150 @Path("/association") 151 public Association createAssociation(AssociationModel model, @HeaderParam("Cookie") ClientState clientState) { 152 return dms.createAssociation(model, clientState); 153 } 154 155 @PUT 156 @Path("/association/{id}") 157 public Directives updateAssociation(@PathParam("id") long assocId, AssociationModel model, 158 @HeaderParam("Cookie") ClientState clientState) { 159 if (model.getId() != -1 && assocId != model.getId()) { 160 throw new RuntimeException("ID mismatch in update request"); 161 } 162 model.setId(assocId); 163 return dms.updateAssociation(model, clientState); 164 } 165 166 @DELETE 167 @Path("/association/{id}") 168 public Directives deleteAssociation(@PathParam("id") long assocId) { 169 return dms.deleteAssociation(assocId); 170 } 171 172 173 174 // === Topic Types === 175 176 @GET 177 @Path("/topictype") 178 public List<String> getTopicTypeUris() { 179 return dms.getTopicTypeUris(); 180 } 181 182 @GET 183 @Path("/topictype/{uri}") 184 public TopicType getTopicType(@PathParam("uri") String uri) { 185 return dms.getTopicType(uri); 186 } 187 188 @GET 189 @Path("/topictype/all") 190 public List<TopicType> getAllTopicTypes() { 191 return dms.getAllTopicTypes(); 192 } 193 194 @POST 195 @Path("/topictype") 196 public TopicType createTopicType(TopicTypeModel topicTypeModel, @HeaderParam("Cookie") ClientState clientState) { 197 return dms.createTopicType(topicTypeModel, clientState); 198 } 199 200 @PUT 201 @Path("/topictype") 202 public Directives updateTopicType(TopicTypeModel model, @HeaderParam("Cookie") ClientState clientState) { 203 return dms.updateTopicType(model, clientState); 204 } 205 206 @DELETE 207 @Path("/topictype/{uri}") 208 public Directives deleteTopicType(@PathParam("uri") String uri) { 209 return dms.deleteTopicType(uri); 210 } 211 212 213 214 // === Association Types === 215 216 @GET 217 @Path("/assoctype") 218 public List<String> getAssociationTypeUris() { 219 return dms.getAssociationTypeUris(); 220 } 221 222 @GET 223 @Path("/assoctype/{uri}") 224 public AssociationType getAssociationType(@PathParam("uri") String uri) { 225 return dms.getAssociationType(uri); 226 } 227 228 @GET 229 @Path("/assoctype/all") 230 public List<AssociationType> getAssociationAllTypes() { 231 return dms.getAllAssociationTypes(); 232 } 233 234 @POST 235 @Path("/assoctype") 236 public AssociationType createAssociationType(AssociationTypeModel assocTypeModel, 237 @HeaderParam("Cookie") ClientState clientState) { 238 return dms.createAssociationType(assocTypeModel, clientState); 239 } 240 241 @PUT 242 @Path("/assoctype") 243 public Directives updateAssociationType(AssociationTypeModel model, 244 @HeaderParam("Cookie") ClientState clientState) { 245 return dms.updateAssociationType(model, clientState); 246 } 247 248 @DELETE 249 @Path("/assoctype/{uri}") 250 public Directives deleteAssociationType(@PathParam("uri") String uri) { 251 return dms.deleteAssociationType(uri); 252 } 253 254 255 256 // === Plugins === 257 258 @GET 259 @Path("/plugin") 260 public List<PluginInfo> getPluginInfo() { 261 return dms.getPluginInfo(); 262 } 263 264 265 266 // ********************** 267 // *** Topic REST API *** 268 // ********************** 269 270 271 272 @GET 273 @Path("/topic/{id}/related_topics") 274 public ResultList<RelatedTopic> getTopicRelatedTopics(@PathParam("id") long topicId, 275 @QueryParam("assoc_type_uri") String assocTypeUri, 276 @QueryParam("my_role_type_uri") String myRoleTypeUri, 277 @QueryParam("others_role_type_uri") String othersRoleTypeUri, 278 @QueryParam("others_topic_type_uri") String othersTopicTypeUri, 279 @QueryParam("max_result_size") int maxResultSize) { 280 Topic topic = dms.getTopic(topicId, false); 281 return getRelatedTopics(topic, "topic", assocTypeUri, myRoleTypeUri, othersRoleTypeUri, 282 othersTopicTypeUri, maxResultSize); 283 } 284 285 286 287 // **************************** 288 // *** Association REST API *** 289 // **************************** 290 291 292 293 @GET 294 @Path("/association/{id}/related_topics") 295 public ResultList<RelatedTopic> getAssociationRelatedTopics(@PathParam("id") long assocId, 296 @QueryParam("assoc_type_uri") String assocTypeUri, 297 @QueryParam("my_role_type_uri") String myRoleTypeUri, 298 @QueryParam("others_role_type_uri") String othersRoleTypeUri, 299 @QueryParam("others_topic_type_uri") String othersTopicTypeUri, 300 @QueryParam("max_result_size") int maxResultSize) { 301 Association assoc = dms.getAssociation(assocId, false); 302 return getRelatedTopics(assoc, "association", assocTypeUri, myRoleTypeUri, othersRoleTypeUri, 303 othersTopicTypeUri, maxResultSize); 304 } 305 306 307 308 // ------------------------------------------------------------------------------------------------- Private Methods 309 310 private ResultList<RelatedTopic> getRelatedTopics(DeepaMehtaObject object, String objectInfo, String assocTypeUri, 311 String myRoleTypeUri, String othersRoleTypeUri, 312 String othersTopicTypeUri, int maxResultSize) { 313 String operation = "Fetching related topics of " + objectInfo + " " + object.getId(); 314 String paramInfo = "(assocTypeUri=\"" + assocTypeUri + "\", myRoleTypeUri=\"" + myRoleTypeUri + 315 "\", othersRoleTypeUri=\"" + othersRoleTypeUri + "\", othersTopicTypeUri=\"" + othersTopicTypeUri + 316 "\", maxResultSize=" + maxResultSize + ")"; 317 try { 318 logger.info(operation + " " + paramInfo); 319 return object.getRelatedTopics(assocTypeUri, myRoleTypeUri, othersRoleTypeUri, othersTopicTypeUri, 320 false, false, maxResultSize); 321 } catch (Exception e) { 322 throw new RuntimeException(operation + " failed " + paramInfo, e); 323 } 324 } 325 }