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/by_type/{type_uri}") 068 public ResultList<RelatedTopic> getTopics(@PathParam("type_uri") String typeUri, 069 @QueryParam("fetch_composite") @DefaultValue("false") boolean fetchComposite, 070 @QueryParam("max_result_size") int maxResultSize) { 071 return dms.getTopics(typeUri, fetchComposite, maxResultSize); 072 } 073 074 @GET 075 @Path("/topic") 076 public List<Topic> searchTopics(@QueryParam("search") String searchTerm, @QueryParam("field") String fieldUri) { 077 return dms.searchTopics(searchTerm, fieldUri); 078 } 079 080 @POST 081 @Path("/topic") 082 public Topic createTopic(TopicModel model, @HeaderParam("Cookie") ClientState clientState) { 083 return dms.createTopic(model, clientState); 084 } 085 086 @PUT 087 @Path("/topic/{id}") 088 public Directives updateTopic(@PathParam("id") long topicId, TopicModel model, 089 @HeaderParam("Cookie") ClientState clientState) { 090 if (model.getId() != -1 && topicId != model.getId()) { 091 throw new RuntimeException("ID mismatch in update request"); 092 } 093 model.setId(topicId); 094 return dms.updateTopic(model, clientState); 095 } 096 097 @DELETE 098 @Path("/topic/{id}") 099 public Directives deleteTopic(@PathParam("id") long topicId) { 100 return dms.deleteTopic(topicId); 101 } 102 103 104 105 // === Associations === 106 107 @GET 108 @Path("/association/{id}") 109 public Association getAssociation(@PathParam("id") long assocId, 110 @QueryParam("fetch_composite") @DefaultValue("true") boolean fetchComposite) { 111 return dms.getAssociation(assocId, fetchComposite); 112 } 113 114 @GET 115 @Path("/association/{assoc_type_uri}/{topic1_id}/{topic2_id}/{role_type1_uri}/{role_type2_uri}") 116 public Association getAssociation(@PathParam("assoc_type_uri") String assocTypeUri, 117 @PathParam("topic1_id") long topic1Id, @PathParam("topic2_id") long topic2Id, 118 @PathParam("role_type1_uri") String roleTypeUri1, @PathParam("role_type2_uri") String roleTypeUri2, 119 @QueryParam("fetch_composite") @DefaultValue("true") boolean fetchComposite) { 120 return dms.getAssociation(assocTypeUri, topic1Id, topic2Id, roleTypeUri1, roleTypeUri2, fetchComposite); 121 } 122 123 // --- 124 125 @GET 126 @Path("/association/multiple/{topic1_id}/{topic2_id}") 127 public List<Association> getAssociations(@PathParam("topic1_id") long topic1Id, 128 @PathParam("topic2_id") long topic2Id) { 129 return dms.getAssociations(topic1Id, topic2Id); 130 } 131 132 @GET 133 @Path("/association/multiple/{topic1_id}/{topic2_id}/{assoc_type_uri}") 134 public List<Association> getAssociations(@PathParam("topic1_id") long topic1Id, 135 @PathParam("topic2_id") long topic2Id, 136 @PathParam("assoc_type_uri") String assocTypeUri) { 137 return dms.getAssociations(topic1Id, topic2Id, assocTypeUri); 138 } 139 140 // --- 141 142 @POST 143 @Path("/association") 144 public Association createAssociation(AssociationModel model, @HeaderParam("Cookie") ClientState clientState) { 145 return dms.createAssociation(model, clientState); 146 } 147 148 @PUT 149 @Path("/association/{id}") 150 public Directives updateAssociation(@PathParam("id") long assocId, AssociationModel model, 151 @HeaderParam("Cookie") ClientState clientState) { 152 if (model.getId() != -1 && assocId != model.getId()) { 153 throw new RuntimeException("ID mismatch in update request"); 154 } 155 model.setId(assocId); 156 return dms.updateAssociation(model, clientState); 157 } 158 159 @DELETE 160 @Path("/association/{id}") 161 public Directives deleteAssociation(@PathParam("id") long assocId) { 162 return dms.deleteAssociation(assocId); 163 } 164 165 166 167 // === Topic Types === 168 169 @GET 170 @Path("/topictype") 171 public List<String> getTopicTypeUris() { 172 return dms.getTopicTypeUris(); 173 } 174 175 @GET 176 @Path("/topictype/{uri}") 177 public TopicType getTopicType(@PathParam("uri") String uri) { 178 return dms.getTopicType(uri); 179 } 180 181 @GET 182 @Path("/topictype/all") 183 public List<TopicType> getAllTopicTypes() { 184 return dms.getAllTopicTypes(); 185 } 186 187 @POST 188 @Path("/topictype") 189 public TopicType createTopicType(TopicTypeModel topicTypeModel, @HeaderParam("Cookie") ClientState clientState) { 190 return dms.createTopicType(topicTypeModel, clientState); 191 } 192 193 @PUT 194 @Path("/topictype") 195 public Directives updateTopicType(TopicTypeModel model, @HeaderParam("Cookie") ClientState clientState) { 196 return dms.updateTopicType(model, clientState); 197 } 198 199 @DELETE 200 @Path("/topictype/{uri}") 201 public Directives deleteTopicType(@PathParam("uri") String uri) { 202 return dms.deleteTopicType(uri); 203 } 204 205 206 207 // === Association Types === 208 209 @GET 210 @Path("/assoctype") 211 public List<String> getAssociationTypeUris() { 212 return dms.getAssociationTypeUris(); 213 } 214 215 @GET 216 @Path("/assoctype/{uri}") 217 public AssociationType getAssociationType(@PathParam("uri") String uri) { 218 return dms.getAssociationType(uri); 219 } 220 221 @GET 222 @Path("/assoctype/all") 223 public List<AssociationType> getAssociationAllTypes() { 224 return dms.getAllAssociationTypes(); 225 } 226 227 @POST 228 @Path("/assoctype") 229 public AssociationType createAssociationType(AssociationTypeModel assocTypeModel, 230 @HeaderParam("Cookie") ClientState clientState) { 231 return dms.createAssociationType(assocTypeModel, clientState); 232 } 233 234 @PUT 235 @Path("/assoctype") 236 public Directives updateAssociationType(AssociationTypeModel model, 237 @HeaderParam("Cookie") ClientState clientState) { 238 return dms.updateAssociationType(model, clientState); 239 } 240 241 @DELETE 242 @Path("/assoctype/{uri}") 243 public Directives deleteAssociationType(@PathParam("uri") String uri) { 244 return dms.deleteAssociationType(uri); 245 } 246 247 248 249 // === Plugins === 250 251 @GET 252 @Path("/plugin") 253 public List<PluginInfo> getPluginInfo() { 254 return dms.getPluginInfo(); 255 } 256 257 258 259 // ********************** 260 // *** Topic REST API *** 261 // ********************** 262 263 264 265 @GET 266 @Path("/topic/{id}/related_topics") 267 public ResultList<RelatedTopic> getTopicRelatedTopics(@PathParam("id") long topicId, 268 @QueryParam("assoc_type_uri") String assocTypeUri, 269 @QueryParam("my_role_type_uri") String myRoleTypeUri, 270 @QueryParam("others_role_type_uri") String othersRoleTypeUri, 271 @QueryParam("others_topic_type_uri") String othersTopicTypeUri, 272 @QueryParam("max_result_size") int maxResultSize) { 273 Topic topic = dms.getTopic(topicId, false); 274 return getRelatedTopics(topic, "topic", assocTypeUri, myRoleTypeUri, othersRoleTypeUri, 275 othersTopicTypeUri, maxResultSize); 276 } 277 278 279 280 // **************************** 281 // *** Association REST API *** 282 // **************************** 283 284 285 286 @GET 287 @Path("/association/{id}/related_topics") 288 public ResultList<RelatedTopic> getAssociationRelatedTopics(@PathParam("id") long assocId, 289 @QueryParam("assoc_type_uri") String assocTypeUri, 290 @QueryParam("my_role_type_uri") String myRoleTypeUri, 291 @QueryParam("others_role_type_uri") String othersRoleTypeUri, 292 @QueryParam("others_topic_type_uri") String othersTopicTypeUri, 293 @QueryParam("max_result_size") int maxResultSize) { 294 Association assoc = dms.getAssociation(assocId, false); 295 return getRelatedTopics(assoc, "association", assocTypeUri, myRoleTypeUri, othersRoleTypeUri, 296 othersTopicTypeUri, maxResultSize); 297 } 298 299 300 301 // ------------------------------------------------------------------------------------------------- Private Methods 302 303 private ResultList<RelatedTopic> getRelatedTopics(DeepaMehtaObject object, String objectInfo, String assocTypeUri, 304 String myRoleTypeUri, String othersRoleTypeUri, 305 String othersTopicTypeUri, int maxResultSize) { 306 String operation = "Fetching related topics of " + objectInfo + " " + object.getId(); 307 String paramInfo = "(assocTypeUri=\"" + assocTypeUri + "\", myRoleTypeUri=\"" + myRoleTypeUri + 308 "\", othersRoleTypeUri=\"" + othersRoleTypeUri + "\", othersTopicTypeUri=\"" + othersTopicTypeUri + 309 "\", maxResultSize=" + maxResultSize + ")"; 310 try { 311 logger.info(operation + " " + paramInfo); 312 return object.getRelatedTopics(assocTypeUri, myRoleTypeUri, othersRoleTypeUri, othersTopicTypeUri, 313 false, false, maxResultSize); 314 } catch (Exception e) { 315 throw new RuntimeException(operation + " failed " + paramInfo, e); 316 } 317 } 318 }