REConnectionMySQL.java
Go to the documentation of this file.
00001 /* \file REConnectionMySQL.java
00002  * \brief Connection to MySQL based RoboEarthDB 
00003  *
00004  * The connection class for a MySQL based RoboEarthDB installation.
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:tenorth@cs.tum.edu">Moritz Tenorth</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 Moritz Tenorth
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.conn;
00050 
00051 import java.io.File;
00052 import java.sql.Connection;
00053 import java.sql.DriverManager;
00054 import java.sql.PreparedStatement;
00055 import java.sql.ResultSet;
00056 import java.sql.SQLException;
00057 import java.sql.Statement;
00058 import java.util.ArrayList;
00059 
00060 import org.semanticweb.owlapi.model.OWLOntology;
00061 
00062 import roboearth.wp5.owl.OWLIO;
00063 
00064 
00076 public class REConnectionMySQL implements REInterface {
00077 
00078 
00082         public final static String roboEarthMysqlHost = "atradig131.informatik.tu-muenchen.de";
00083 
00087         public final static String roboEarthMysqlDB = "roboearth";
00088 
00092         public final static String roboEarthMysqlUser = "roboearth";
00093 
00097         public final static String roboEarthMysqlPassword = "RoboEarth2010";
00098 
00099 
00100         protected Connection conn = null;
00101 
00102 
00103         public REConnectionMySQL() {
00104 
00105                 try {
00106                         Class.forName("com.mysql.jdbc.Driver").newInstance();
00107                         conn = DriverManager.getConnection("jdbc:mysql://"+roboEarthMysqlHost+"/"+
00108                                         roboEarthMysqlDB+"?" +
00109                                         "user="+roboEarthMysqlUser+
00110                                         "&password="+roboEarthMysqlPassword);
00111 
00112                 } catch (SQLException ex) {
00113                         // handle any errors
00114                         System.out.println("SQLException: " + ex.getMessage());
00115                         System.out.println("SQLState: " + ex.getSQLState());
00116                         System.out.println("VendorError: " + ex.getErrorCode());
00117 
00118                 } catch (Exception ex) {
00119                         ex.printStackTrace();
00120                 }
00121 
00122         }
00123 
00124         
00125         
00126         
00127 
00128 
00129 
00130         @Override
00131         public String requestActionRecipe(String id) {
00132 
00133                 String recipe = null;
00134 
00135                 if (id != null && id.length()>0) {
00136 
00137                         String query = "SELECT title, tags, description, recipe from actionrecipes where title='"+id+"'";
00138                         String owlDescription="";
00139 
00140                         Statement stmt = null;
00141                         ResultSet rs = null;
00142 
00143                         try {
00144                                 stmt = conn.createStatement();
00145                                 rs = stmt.executeQuery(query);
00146 
00147 
00148                                 if (!rs.next()) {
00149                                         System.out.println("No recipe found.");
00150                                 } else {
00151 
00152                                         // create OWL description
00153                                         owlDescription = cleanOWLString(rs.getString(4));
00154                                         recipe = owlDescription;
00155                                 }
00156                         }
00157                         catch (SQLException ex){
00158                                 // handle any errors
00159                                 System.out.println("SQLException: " + ex.getMessage());
00160                                 System.out.println("SQLState: " + ex.getSQLState());
00161                                 System.out.println("VendorError: " + ex.getErrorCode());
00162                         }
00163                         finally {
00164 
00165                                 if (rs != null) {
00166                                         try {
00167                                                 rs.close();
00168                                         } catch (SQLException sqlEx) { } // ignore
00169 
00170                                         rs = null;
00171                                 }
00172 
00173                                 if (stmt != null) {
00174                                         try {
00175                                                 stmt.close();
00176                                         } catch (SQLException sqlEx) { } // ignore
00177 
00178                                         stmt = null;
00179                                 }
00180                         }
00181 
00182 
00183                 }
00184 
00185                 return recipe;
00186 
00187         }
00188 
00189         
00190 
00191         public String requestEnvironment(String id) {
00192 
00193                 String map = null;
00194 
00195                 if (id != null && id.length()>0) {
00196 
00197                         String query = "SELECT title, tags, description, map from maps where title='"+id+"'";
00198                         String owlDescription="";
00199 
00200                         Statement stmt = null;
00201                         ResultSet rs = null;
00202 
00203                         try {
00204                                 stmt = conn.createStatement();
00205                                 rs = stmt.executeQuery(query);
00206 
00207 
00208                                 if (!rs.next()) {
00209                                         System.out.println("No environment found.");
00210                                 } else {
00211 
00212                                         // create OWL description
00213                                         owlDescription = cleanOWLString(rs.getString(4));
00214                                         map = owlDescription;
00215                                 }
00216                         }
00217                         catch (SQLException ex){
00218                                 // handle any errors
00219                                 System.out.println("SQLException: " + ex.getMessage());
00220                                 System.out.println("SQLState: " + ex.getSQLState());
00221                                 System.out.println("VendorError: " + ex.getErrorCode());
00222                         }
00223                         finally {
00224 
00225                                 if (rs != null) {
00226                                         try {
00227                                                 rs.close();
00228                                         } catch (SQLException sqlEx) { } // ignore
00229 
00230                                         rs = null;
00231                                 }
00232 
00233                                 if (stmt != null) {
00234                                         try {
00235                                                 stmt.close();
00236                                         } catch (SQLException sqlEx) { } // ignore
00237 
00238                                         stmt = null;
00239                                 }
00240                         }
00241 
00242 
00243                 }
00244 
00245                 return map;
00246 
00247         }
00248 
00249 
00250         @Override
00251         public boolean submitActionRecipe(OWLOntology actionRecipe, String id, String tags, String description) {
00252 
00253                 boolean ok = false;
00254 
00255                 if (actionRecipe != null && id != null && tags != null && description != null) {
00256 
00257                         try{
00258                                 PreparedStatement stmt = conn.prepareStatement("INSERT INTO actionrecipes(title, tags, description, recipe)" +
00259                                 " VALUES(?, ?, ?, ?)");
00260 
00261                                 stmt.setString(1, id);
00262                                 stmt.setString(2, tags);
00263                                 stmt.setString(3, description);
00264 
00265                                 String owlData = OWLIO.saveOntologyToString(actionRecipe, actionRecipe.getOWLOntologyManager().getOntologyFormat(actionRecipe));
00266                                 stmt.setString(4, owlData);
00267 
00268                                 stmt.executeUpdate();
00269 
00270                                 ok=true;
00271 
00272                         } catch (SQLException e) {
00273                                 e.printStackTrace();
00274                         }
00275                 }
00276 
00277                 return ok;
00278 
00279         }
00280 
00281         @Override
00282         public boolean submitEnvironment(OWLOntology map, String id, String tags, String description) {
00283 
00284                 boolean ok = false;
00285 
00286                 if (map != null && id != null && tags != null && description != null) {
00287 
00288 
00289                         try{
00290                                 PreparedStatement stmt = conn.prepareStatement("INSERT INTO maps(title, tags, description, map)" +
00291                                 " VALUES(?, ?, ?, ?)");
00292 
00293                                 stmt.setString(1, id);
00294                                 stmt.setString(2, tags);
00295                                 stmt.setString(3, description);
00296 
00297                                 String owlData = OWLIO.saveOntologyToString(map, map.getOWLOntologyManager().getOntologyFormat(map));
00298                                 stmt.setString(4, owlData);
00299 
00300                                 stmt.executeUpdate();
00301 
00302                                 ok=true;
00303 
00304                         } catch (SQLException e) {
00305                                 e.printStackTrace();
00306                         }
00307                 }
00308 
00309                 return ok;
00310 
00311         }
00312         
00319         public static String cleanOWLString(String owlData) {
00320 
00321                 return owlData.replaceAll("&lt;", "<").replaceAll("&gt;", ">").replaceAll("&quot;", "\"").replaceAll("\t", "").trim();
00322 
00323         }
00324 
00328         public static void main(String[] args) {
00329 
00330 //              REConnectionMySQL re = new REConnectionMySQL();
00331 //
00332 //              // create dummy ontology
00333 //              OWLOntology ontology = null;
00334 //
00335 //              try{
00336 //                      // Create ontology manager and data factory
00337 //                      OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
00338 //                      OWLDataFactory factory = manager.getOWLDataFactory();
00339 //
00340 //                      // Create empty OWL ontology
00341 //                      ontology = manager.createOntology(IRI.create(IRIDepot.ROBOEARTH));
00342 //                      manager.setOntologyFormat(ontology, new RDFXMLOntologyFormat());
00343 //
00344 //                      // Import KnowRob ontology
00345 //                      OWLImportsDeclaration oid = factory.getOWLImportsDeclaration(IRI.create(IRIDepot.KNOWROB));
00346 //                      AddImport addImp = new AddImport(ontology,oid);
00347 //                      manager.applyChange(addImp);
00348 //              } catch(OWLOntologyCreationException e) { }
00349 //
00350 //              // test action recipes
00351 //              re.submitActionRecipe(ontology, "test_recipe", "bla, bla", "tasty action recipe");
00352 //              OWLOntology res = re.requestActionRecipe("test_recipe");
00353 //              System.out.println(OWLIO.saveOntologytoString(res, res.getOWLOntologyManager().getOntologyFormat(res)));
00354 //
00355 //
00356 //              // test object models
00357 //              re.submitObject(ontology, "Cup", "Cup", new File("icub.jpg"), new File("icub.jpg"));
00358 //              res = re.requestObject("Cup", "out1.jpg", "out2.jpg");
00359 //              System.out.println(OWLIO.saveOntologytoString(res, res.getOWLOntologyManager().getOntologyFormat(res)));
00360 //
00361 //              // test maps
00362 //              re.submitEnvironment(ontology, "test_map", "bla, bla", "large environment map");
00363 //              res = re.requestEnvironment("test_map");
00364 //              System.out.println(OWLIO.saveOntologytoString(res, res.getOWLOntologyManager().getOntologyFormat(res)));
00365 
00366 
00367         }
00368 
00369 
00370 
00371 
00372         @Override
00373         public boolean deleteActionRecipe(String uid) {
00374                 // TODO Auto-generated method stub
00375                 return false;
00376         }
00377 
00378 
00379 
00380 
00381         @Override
00382         public boolean deleteEnvironment(String uid) {
00383                 // TODO Auto-generated method stub
00384                 return false;
00385         }
00386 
00387 
00388 
00389 
00390         @Override
00391         public boolean deleteEnvironmentBinaryFile(String uid, String filename) {
00392                 // TODO Auto-generated method stub
00393                 return false;
00394         }
00395 
00396 
00397 
00398 
00399         @Override
00400         public boolean deleteObject(String uid) {
00401                 // TODO Auto-generated method stub
00402                 return false;
00403         }
00404 
00405 
00406 
00407 
00408         @Override
00409         public boolean deleteObjectBinaryFile(String uid, String filename) {
00410                 // TODO Auto-generated method stub
00411                 return false;
00412         }
00413 
00414 
00415 
00416 
00417         @Override
00418         public String queryActionRecipeDB(String seRQLquery) {
00419                 // TODO Auto-generated method stub
00420                 return null;
00421         }
00422 
00423 
00424 
00425 
00426         @Override
00427         public String queryEnvironmentDB(String seRQLquery) {
00428                 // TODO Auto-generated method stub
00429                 return null;
00430         }
00431 
00432 
00433 
00434 
00435         @Override
00436         public String queryObjectDB(String seRQLquery) {
00437                 // TODO Auto-generated method stub
00438                 return null;
00439         }
00440 
00441 
00442 
00443 
00444         @Override
00445         public String requestEnvironment(String uid,
00446                         ArrayList<String> outFilenames, ArrayList<String> outFileURLs) {
00447                 // TODO Auto-generated method stub
00448                 return null;
00449         }
00450 
00451 
00452 
00453 
00454         @Override
00455         public File requestEnvironmentBinaryFile(String objectUID, String filename,
00456                         String targetPath) {
00457                 // TODO Auto-generated method stub
00458                 return null;
00459         }
00460 
00461 
00462 
00463 
00464         @Override
00465         public byte[] requestEnvironmentBinaryFile(String objectUID, String filename) {
00466                 // TODO Auto-generated method stub
00467                 return null;
00468         }
00469 
00470 
00471 
00472 
00473         @Override
00474         public String requestObject(String uid, ArrayList<String> outFilenames,
00475                         ArrayList<String> outFileURLs) {
00476                 // TODO Auto-generated method stub
00477                 return null;
00478         }
00479 
00480 
00481 
00482 
00483         @Override
00484         public File requestObjectBinaryFile(String objectUID, String filename,
00485                         String targetPath) {
00486                 // TODO Auto-generated method stub
00487                 return null;
00488         }
00489 
00490 
00491 
00492 
00493         @Override
00494         public byte[] requestObjectBinaryFile(String objectUID, String filename) {
00495                 // TODO Auto-generated method stub
00496                 return null;
00497         }
00498 
00499 
00500 
00501 
00502         @Override
00503         public String[] searchActionRecipes(String searchID,
00504                         ArrayList<String> outUIDs) {
00505                 // TODO Auto-generated method stub
00506                 return null;
00507         }
00508 
00509 
00510 
00511 
00512         @Override
00513         public String[] searchEnvironments(String searchID,
00514                         ArrayList<String> outUIDs,
00515                         ArrayList<ArrayList<String>> outFilenames,
00516                         ArrayList<ArrayList<String>> outFileURLs) {
00517                 // TODO Auto-generated method stub
00518                 return null;
00519         }
00520 
00521 
00522 
00523 
00524         @Override
00525         public String[] searchObjects(String searchID, ArrayList<String> outUIDs,
00526                         ArrayList<ArrayList<String>> outFilenames,
00527                         ArrayList<ArrayList<String>> outFileURLs) {
00528                 // TODO Auto-generated method stub
00529                 return null;
00530         }
00531 
00532 
00533 
00534 
00535         @Override
00536         public boolean submitEnvironment(OWLOntology env, String cls, String id,
00537                         String description, ArrayList<File> binaryFiles) {
00538                 // TODO Auto-generated method stub
00539                 return false;
00540         }
00541 
00542 
00543 
00544 
00545         @Override
00546         public boolean submitEnvironment(OWLOntology env, String cls, String id,
00547                         String description, ArrayList<byte[]> binaryData,
00548                         ArrayList<String> filenames) {
00549                 // TODO Auto-generated method stub
00550                 return false;
00551         }
00552 
00553 
00554 
00555 
00556         @Override
00557         public boolean submitEnvironmentBinaryFile(String uid, File file) {
00558                 // TODO Auto-generated method stub
00559                 return false;
00560         }
00561 
00562 
00563 
00564 
00565         @Override
00566         public boolean submitEnvironmentBinaryFile(String uid, byte[] data,
00567                         String filename) {
00568                 // TODO Auto-generated method stub
00569                 return false;
00570         }
00571 
00572 
00573 
00574 
00575         @Override
00576         public boolean submitObject(OWLOntology objectOwl, String cls, String id,
00577                         String description) {
00578                 // TODO Auto-generated method stub
00579                 return false;
00580         }
00581 
00582 
00583 
00584 
00585         @Override
00586         public boolean submitObject(OWLOntology objectOwl, String cls, String id,
00587                         String description, ArrayList<File> binaryFiles) {
00588                 // TODO Auto-generated method stub
00589                 return false;
00590         }
00591 
00592 
00593 
00594 
00595         @Override
00596         public boolean submitObject(OWLOntology objectOwl, String cls, String id,
00597                         String description, ArrayList<byte[]> binaryData,
00598                         ArrayList<String> filenames) {
00599                 // TODO Auto-generated method stub
00600                 return false;
00601         }
00602 
00603 
00604 
00605 
00606         @Override
00607         public boolean submitObjectBinaryFile(String uid, File file) {
00608                 // TODO Auto-generated method stub
00609                 return false;
00610         }
00611 
00612 
00613 
00614 
00615         @Override
00616         public boolean submitObjectBinaryFile(String uid, byte[] data,
00617                         String filename) {
00618                 // TODO Auto-generated method stub
00619                 return false;
00620         }
00621 
00622 
00623 
00624 
00625         @Override
00626         public boolean updateActionRecipe(String uid, OWLOntology actionRecipe,
00627                         String description) {
00628                 // TODO Auto-generated method stub
00629                 return false;
00630         }
00631 
00632 
00633 
00634 
00635         @Override
00636         public boolean updateEnvironment(String uid, OWLOntology env,
00637                         String description) {
00638                 // TODO Auto-generated method stub
00639                 return false;
00640         }
00641 
00642 
00643 
00644 
00645         @Override
00646         public boolean updateEnvironmentBinaryFile(String uid, File file) {
00647                 // TODO Auto-generated method stub
00648                 return false;
00649         }
00650 
00651 
00652 
00653 
00654         @Override
00655         public boolean updateEnvironmentBinaryFile(String uid, byte[] data,
00656                         String filename) {
00657                 // TODO Auto-generated method stub
00658                 return false;
00659         }
00660 
00661 
00662 
00663 
00664         @Override
00665         public boolean updateObject(String uid, OWLOntology objectOwl,
00666                         String description) {
00667                 // TODO Auto-generated method stub
00668                 return false;
00669         }
00670 
00671 
00672 
00673 
00674         @Override
00675         public boolean updateObjectBinaryFile(String uid, File file) {
00676                 // TODO Auto-generated method stub
00677                 return false;
00678         }
00679 
00680 
00681 
00682 
00683         @Override
00684         public boolean updateObjectBinaryFile(String uid, byte[] data,
00685                         String filename) {
00686                 // TODO Auto-generated method stub
00687                 return false;
00688         }
00689 
00690 
00691 
00692 
00693         @Override
00694         public ArrayList<byte[]> request2dMap(String envUid, OWLOntology srdl,
00695                         String baseScannerLink, String simpleMapNameWithoutExt) {
00696                 // TODO Auto-generated method stub
00697                 return null;
00698         }
00699 
00700 
00701         
00702 
00703         @Override
00704         public ArrayList<byte[]> requestProjected2dMap(String envUid, double minZ,
00705                         double maxZ, String simpleMapNameWithoutExt) {
00706                 // TODO Auto-generated method stub
00707                 return null;
00708         }
00709 
00710 
00711 
00712 
00713 
00714 
00715 
00716         @Override
00717         public boolean updateObject(String uid, String objectOwl, String description) {
00718                 // TODO Auto-generated method stub
00719                 return false;
00720         }
00721 
00722 
00723 
00724 
00725 
00726 
00727 
00728         @Override
00729         public String requestRobot(String uid) {
00730                 // TODO Auto-generated method stub
00731                 return null;
00732         }
00733 
00734 
00735 
00736 
00737 
00738 
00739 
00740         @Override
00741         public boolean submitActionRecipe(String actionRecipe, String cls,
00742                         String id, String description) {
00743                 // TODO Auto-generated method stub
00744                 return false;
00745         }
00746 
00747 
00748 
00749 
00750 
00751 
00752 
00753         @Override
00754         public boolean submitEnvironment(String env, String cls, String id,
00755                         String description) {
00756                 // TODO Auto-generated method stub
00757                 return false;
00758         }
00759 
00760 
00761 
00762 
00763 
00764 
00765 
00766         @Override
00767         public boolean submitEnvironment(String env, String cls, String id,
00768                         String description, ArrayList<File> binaryFiles) {
00769                 // TODO Auto-generated method stub
00770                 return false;
00771         }
00772 
00773 
00774 
00775 
00776 
00777 
00778 
00779         @Override
00780         public boolean submitEnvironment(String env, String cls, String id,
00781                         String description, ArrayList<byte[]> binaryData,
00782                         ArrayList<String> filenames) {
00783                 // TODO Auto-generated method stub
00784                 return false;
00785         }
00786 
00787 
00788 
00789 
00790 
00791 
00792 
00793         @Override
00794         public boolean submitObject(String objectOwl, String cls, String id,
00795                         String description) {
00796                 // TODO Auto-generated method stub
00797                 return false;
00798         }
00799 
00800 
00801 
00802 
00803 
00804 
00805 
00806         @Override
00807         public boolean submitObject(String objectOwl, String cls, String id,
00808                         String description, ArrayList<File> binaryFiles) {
00809                 // TODO Auto-generated method stub
00810                 return false;
00811         }
00812 
00813 
00814 
00815 
00816 
00817 
00818 
00819         @Override
00820         public boolean submitObject(String objectOwl, String cls, String id,
00821                         String description, ArrayList<byte[]> binaryData,
00822                         ArrayList<String> filenames) {
00823                 // TODO Auto-generated method stub
00824                 return false;
00825         }
00826 
00827 
00828 
00829 
00830 
00831 
00832 
00833         @Override
00834         public boolean updateActionRecipe(String uid, String actionRecipe,
00835                         String description) {
00836                 // TODO Auto-generated method stub
00837                 return false;
00838         }
00839 
00840 
00841 
00842 
00843 
00844 
00845 
00846         @Override
00847         public boolean updateEnvironment(String uid, String env, String description) {
00848                 // TODO Auto-generated method stub
00849                 return false;
00850         }
00851 
00852 
00853 }


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