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    
200    
201        // === Association Types ===
202    
203        @GET
204        @Path("/assoctype")
205        public List<String> getAssociationTypeUris() {
206            return dms.getAssociationTypeUris();
207        }
208    
209        @GET
210        @Path("/assoctype/{uri}")
211        public AssociationType getAssociationType(@PathParam("uri") String uri) {
212            return dms.getAssociationType(uri);
213        }
214    
215        @GET
216        @Path("/assoctype/all")
217        public List<AssociationType> getAssociationAllTypes() {
218            return dms.getAllAssociationTypes();
219        }
220    
221        @POST
222        @Path("/assoctype")
223        public AssociationType createAssociationType(AssociationTypeModel assocTypeModel,
224                                                     @HeaderParam("Cookie") ClientState clientState) {
225            return dms.createAssociationType(assocTypeModel, clientState);
226        }
227    
228        @PUT
229        @Path("/assoctype")
230        public Directives updateAssociationType(AssociationTypeModel model,
231                                                @HeaderParam("Cookie") ClientState clientState) {
232            return dms.updateAssociationType(model, clientState);
233        }
234    
235    
236    
237        // === Plugins ===
238    
239        @GET
240        @Path("/plugin")
241        public List<PluginInfo> getPluginInfo() {
242            return dms.getPluginInfo();
243        }
244    
245    
246    
247        // **********************
248        // *** Topic REST API ***
249        // **********************
250    
251    
252    
253        @GET
254        @Path("/topic/{id}/related_topics")
255        public ResultList<RelatedTopic> getTopicRelatedTopics(@PathParam("id")                     long topicId,
256                                                             @QueryParam("assoc_type_uri")        String assocTypeUri,
257                                                             @QueryParam("my_role_type_uri")      String myRoleTypeUri,
258                                                             @QueryParam("others_role_type_uri")  String othersRoleTypeUri,
259                                                             @QueryParam("others_topic_type_uri") String othersTopicTypeUri,
260                                                             @QueryParam("max_result_size")       int maxResultSize) {
261            Topic topic = dms.getTopic(topicId, false);
262            return getRelatedTopics(topic, "topic", assocTypeUri, myRoleTypeUri, othersRoleTypeUri,
263                othersTopicTypeUri, maxResultSize);
264        }
265    
266    
267    
268        // ****************************
269        // *** Association REST API ***
270        // ****************************
271    
272    
273    
274        @GET
275        @Path("/association/{id}/related_topics")
276        public ResultList<RelatedTopic> getAssociationRelatedTopics(@PathParam("id")               long assocId,
277                                                             @QueryParam("assoc_type_uri")        String assocTypeUri,
278                                                             @QueryParam("my_role_type_uri")      String myRoleTypeUri,
279                                                             @QueryParam("others_role_type_uri")  String othersRoleTypeUri,
280                                                             @QueryParam("others_topic_type_uri") String othersTopicTypeUri,
281                                                             @QueryParam("max_result_size")       int maxResultSize) {
282            Association assoc = dms.getAssociation(assocId, false);
283            return getRelatedTopics(assoc, "association", assocTypeUri, myRoleTypeUri, othersRoleTypeUri,
284                othersTopicTypeUri, maxResultSize);
285        }
286    
287    
288    
289        // ------------------------------------------------------------------------------------------------- Private Methods
290    
291        private ResultList<RelatedTopic> getRelatedTopics(DeepaMehtaObject object, String objectInfo, String assocTypeUri,
292                                                         String myRoleTypeUri, String othersRoleTypeUri,
293                                                         String othersTopicTypeUri, int maxResultSize) {
294            String operation = "Fetching related topics of " + objectInfo + " " + object.getId();
295            String paramInfo = "(assocTypeUri=\"" + assocTypeUri + "\", myRoleTypeUri=\"" + myRoleTypeUri +
296                "\", othersRoleTypeUri=\"" + othersRoleTypeUri + "\", othersTopicTypeUri=\"" + othersTopicTypeUri +
297                "\", maxResultSize=" + maxResultSize + ")";
298            try {
299                logger.info(operation + " " + paramInfo);
300                return object.getRelatedTopics(assocTypeUri, myRoleTypeUri, othersRoleTypeUri, othersTopicTypeUri,
301                    false, false, maxResultSize);
302            } catch (Exception e) {
303                throw new RuntimeException(operation + " failed " + paramInfo, e);
304            }
305        }
306    }