ObjectModelHandler.java
Go to the documentation of this file.
00001 /* \file ObjectModelHandler.java
00002  * \brief Object model handler
00003  *
00004  * The object model handler provides the callbacks for ROS services related to object models.
00005  * 
00006  * This file is part of the RoboEarth ROS re_comm_core package.
00007  * 
00008  * It was originally created for <a href="http://www.roboearth.org/">RoboEarth</a>.
00009  * The research leading to these results has received funding from the 
00010  * European Union Seventh Framework Programme FP7/2007-2013 
00011  * under grant agreement no248942 RoboEarth.
00012  *
00013  * Copyright (C) 2010 by 
00014  * <a href=" mailto:perzylo@cs.tum.edu">Alexander Perzylo</a>
00015  * Technische Universitaet Muenchen
00016  * 
00017  * Redistribution and use in source and binary forms, with or without
00018  * modification, are permitted provided that the following conditions are met:
00019  * 
00020  *    <UL>
00021  *     <LI> Redistributions of source code must retain the above copyright
00022  *       notice, this list of conditions and the following disclaimer.
00023  *     <LI> Redistributions in binary form must reproduce the above copyright
00024  *       notice, this list of conditions and the following disclaimer in the
00025  *       documentation and/or other materials provided with the distribution.
00026  *     <LI> Neither the name of Willow Garage, Inc. nor the names of its
00027  *       contributors may be used to endorse or promote products derived from
00028  *       this software without specific prior written permission.
00029  *    </UL>
00030  * 
00031  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00032  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00033  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00034  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00035  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00036  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00037  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00038  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00039  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00040  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00041  * POSSIBILITY OF SUCH DAMAGE.
00042  *
00043  * \author Alexander Perzylo
00044  * \version 1.0
00045  * \date 2010
00046  * \image html http://www.roboearth.org/sites/default/files/RoboEarth.org_logo.gif
00047  * \image latex http://www.roboearth.org/sites/default/files/RoboEarth.org_logo.gif
00048  */
00049 package roboearth.wp5.module;
00050 
00051 import java.util.ArrayList;
00052 
00053 import roboearth.wp5.conn.REInterface;
00054 import ros.NodeHandle;
00055 import ros.Ros;
00056 import ros.RosException;
00057 import ros.ServiceServer;
00058 import ros.pkg.re_msgs.msg.StringArray;
00059 import ros.pkg.re_srvs.srv.DelObject;
00060 import ros.pkg.re_srvs.srv.GetObject;
00061 import ros.pkg.re_srvs.srv.QueryObjects;
00062 import ros.pkg.re_srvs.srv.SearchObjects;
00063 import ros.pkg.re_srvs.srv.SetObject;
00064 import ros.pkg.re_srvs.srv.UpdateObject;
00065 
00066 public class ObjectModelHandler extends AbstractHandler {
00067 
00074         public ObjectModelHandler(Ros ros, NodeHandle n) throws RosException {
00075 
00076                 super(ros, n);
00077 
00078                 this.n.advertiseService("/re_comm/get_object", new GetObject(), new GetObjectCallback());
00079                 this.n.advertiseService("/re_comm/set_object", new SetObject(), new SetObjectCallback());
00080                 this.n.advertiseService("/re_comm/del_object", new DelObject(), new DelObjectCallback());
00081                 this.n.advertiseService("/re_comm/update_object", new UpdateObject(), new UpdateObjectCallback());
00082                 this.n.advertiseService("/re_comm/search_objects", new SearchObjects(), new SearchObjectsCallback());
00083                 this.n.advertiseService("/re_comm/query_objects", new QueryObjects(), new QueryObjectsCallback());
00084 
00085                 ros.logInfo("Module 'ObjectHandler' loaded.");
00086 
00087         }
00088 
00089 
00090         public static GetObject.Response getObject(GetObject.Request req) {
00091                 
00092                 GetObject.Response res = new GetObject.Response();
00093                 
00094                 REInterface con = getREConnection(guestInterfaceKey);
00095                 ArrayList<String> outFilenames = new ArrayList<String>();
00096                 ArrayList<String> outFileURLs = new ArrayList<String>();
00097                 String ont = con.requestObject(req.objectUID, outFilenames, outFileURLs);
00098                 
00099                 if (ont != null) {
00100                         res.object = ont;
00101                         res.filenames = outFilenames;
00102                         res.fileURLs = outFileURLs;
00103                         res.success = true;
00104                 } else {
00105                         res.success = false;
00106                 }
00107                 
00108                 return res;
00109                 
00110         }
00111         
00112         public static SetObject.Response setObject(SetObject.Request req) {
00113                 
00114                 SetObject.Response res = new SetObject.Response();
00115                 res.success = false;
00116                 
00117                 if (req.apiKey != null && req.apiKey.length() > 0) {
00118                         REInterface con = getREConnection(req.apiKey);
00119                         ArrayList<String> filenames = new ArrayList<String>();
00120                         ArrayList<byte[]> dataArray = new ArrayList<byte[]>();
00121                         for (ros.pkg.re_msgs.msg.File file : req.files) {
00122                                 filenames.add(file.name);
00123                                 dataArray.add(file.data);
00124                         }
00125                         res.success = con.submitObject(req.object, req.cls, req.id, req.description, dataArray, filenames);     
00126                 }
00127                 
00128                 return res;
00129                 
00130         }
00131 
00132         public static DelObject.Response delObject(DelObject.Request req) {
00133 
00134                 DelObject.Response res = new DelObject.Response();
00135                 res.success = false;
00136 
00137                 if (req.apiKey != null && req.apiKey.length() > 0) {
00138 
00139                         REInterface con = getREConnection(req.apiKey);
00140                         res.success = con.deleteObject(req.objectUID);
00141 
00142                 }
00143 
00144                 return res;
00145 
00146         }
00147 
00148         public static UpdateObject.Response updateObject(UpdateObject.Request req) {
00149 
00150                 UpdateObject.Response res = new UpdateObject.Response();
00151                 res.success = false;
00152 
00153                 if (req.apiKey != null && req.apiKey.length() > 0) {
00154 
00155                         REInterface con = getREConnection(req.apiKey);                  
00156                         if (req.object != null) {
00157                                 res.success = con.updateObject(req.uid, req.object, req.description);   
00158                         } else {
00159                                 res.success = false;
00160                         }
00161 
00162                 }
00163 
00164                 return res;
00165 
00166         }
00167 
00168         public static SearchObjects.Response searchObjects(SearchObjects.Request req) {
00169 
00170                 SearchObjects.Response res = new SearchObjects.Response();
00171                 res.success = false;
00172 
00173                 REInterface con = getREConnection(guestInterfaceKey);
00174                 
00175                 ArrayList<String> uids = new ArrayList<String>();
00176                 ArrayList<ArrayList<String>> filenames = new ArrayList<ArrayList<String>>();
00177                 ArrayList<ArrayList<String>> urls = new ArrayList<ArrayList<String>>();
00178 
00179                 String[] onts = con.searchObjects(req.searchID, uids, filenames, urls);
00180                 
00181                 if (onts != null) {
00182                         for (String ont : onts) {
00183                                 StringArray saNames = new StringArray();
00184                                 saNames.list = filenames.remove(0);
00185                                 res.filenames.add(saNames);
00186                                 
00187                                 StringArray saURLs = new StringArray();
00188                                 saURLs.list = urls.remove(0);
00189                                 res.fileURLs.add(saURLs);
00190                                 
00191                                 res.objects.add(ont);
00192                         }
00193                         res.uids = uids;
00194                         res.success = true;
00195                 } else {
00196                         res.success = false;
00197                 }
00198 
00199                 return res;
00200 
00201         }
00202         
00203         public static QueryObjects.Response queryObjects(QueryObjects.Request req) {
00204 
00205                 QueryObjects.Response res = new QueryObjects.Response();
00206                 res.result = "";
00207 
00208                 REInterface con = getREConnection(guestInterfaceKey);
00209                 String result = con.queryObjectDB(req.query);
00210                 if (result != null) {
00211                         res.result = result;
00212                 }
00213 
00214                 return res;
00215 
00216         }
00217         
00225         class GetObjectCallback implements ServiceServer.Callback<GetObject.Request, GetObject.Response> {
00226 
00227                 @Override
00228                 public GetObject.Response call(GetObject.Request req) {
00229 
00230                         GetObject.Response res = getObject(req);
00231                         if (res.success) {
00232                                 ros.logInfo("GetObject (UID: " + req.objectUID + "): Done");
00233                         } else {
00234                                 ros.logInfo("GetObject (UID: " + req.objectUID + "): Failed");
00235                         }
00236 
00237                         return res;
00238 
00239                 }
00240 
00241         }
00242 
00250         class SetObjectCallback implements ServiceServer.Callback<SetObject.Request, SetObject.Response> {
00251 
00252                 @Override
00253                 public SetObject.Response call(SetObject.Request req) {
00254 
00255                         SetObject.Response res = setObject(req);
00256                         
00257                         if (req.apiKey != null && req.apiKey.length() > 0) {
00258 
00259                                 if (res.success) {
00260                                         ros.logInfo("SetObject (UID: " + req.cls+"."+req.id + "): Done");
00261                                 } else {
00262                                         ros.logInfo("SetObject (UID: " + req.cls+"."+req.id + "): Failed");
00263                                 }
00264                                 
00265                         } else {
00266                                 ros.logInfo("SetObject (UID: " + req.cls+"."+req.id + "): API key is missing!");
00267                         }
00268 
00269                         return res;
00270 
00271                 }
00272 
00273         }
00274 
00282         class DelObjectCallback implements ServiceServer.Callback<DelObject.Request, DelObject.Response> {
00283 
00284                 @Override
00285                 public DelObject.Response call(DelObject.Request req) {
00286 
00287                         DelObject.Response res = delObject(req);
00288                         
00289                         if (req.apiKey != null && req.apiKey.length() > 0) {
00290 
00291                                 if (res.success) {
00292                                         ros.logInfo("DelObject (UID: " + req.objectUID + "): Done");
00293                                 } else {
00294                                         ros.logInfo("DelObject (UID: " + req.objectUID + "): Failed");
00295                                 }
00296                                 
00297                         } else {
00298                                 ros.logInfo("DelObject (UID: " + req.objectUID + "): API key is missing!");
00299                         }
00300 
00301                         return res;
00302 
00303                 }
00304 
00305         }
00306 
00314         class UpdateObjectCallback implements ServiceServer.Callback<UpdateObject.Request, UpdateObject.Response> {
00315 
00316                 @Override
00317                 public UpdateObject.Response call(UpdateObject.Request req) {
00318 
00319                         UpdateObject.Response res = updateObject(req);
00320                         
00321                         if (req.apiKey != null && req.apiKey.length() > 0) {
00322 
00323                                 if (res.success) {
00324                                         ros.logInfo("UpdateObject (UID: " + req.uid + "): Done");
00325                                 } else {
00326                                         ros.logInfo("UpdateObject (UID: " + req.uid + "): Failed");
00327                                 }
00328                                 
00329                         } else {
00330                                 ros.logInfo("UpdateObject (UID: " + req.uid + "): API key is missing!");
00331                         }
00332 
00333                         return res;
00334 
00335                 }
00336 
00337         }
00338 
00346         class SearchObjectsCallback implements ServiceServer.Callback<SearchObjects.Request, SearchObjects.Response> {
00347 
00348                 @Override
00349                 public SearchObjects.Response call(SearchObjects.Request req) {
00350 
00351                         SearchObjects.Response res = searchObjects(req);
00352                         if (res.success) {
00353                                 ros.logInfo("SearchObjects (UID: " + req.searchID + "): Done");
00354                         } else {
00355                                 ros.logInfo("SearchObjects (UID: " + req.searchID + "): Failed");
00356                         }
00357 
00358                         return res;
00359 
00360                 }
00361 
00362         }
00363         
00371         class QueryObjectsCallback implements ServiceServer.Callback<QueryObjects.Request, QueryObjects.Response> {
00372 
00373                 @Override
00374                 public QueryObjects.Response call(QueryObjects.Request req) {
00375 
00376                         QueryObjects.Response res = queryObjects(req);
00377                         if (res.result != null && res.result.length() > 0) {
00378                                 ros.logInfo("QueryObjects (query:\n" + req.query + "): Done");
00379                         } else {
00380                                 ros.logInfo("QueryObjects (query:\n" + req.query + "): Failed");
00381                         }
00382 
00383                         return res;
00384 
00385                 }
00386 
00387         }
00388 
00389 }


re_comm_core
Author(s): Alexander Perzylo
autogenerated on Sun Jan 5 2014 11:29:33