DBVIEW
src/org/dbview/addons/input/mysql/Loader.java
00001 /*
00002         DbView - Graph Visualization
00003     Copyright (C) 2012  Denis BEURIVE
00004 
00005     This program is free software: you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation, either version 3 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017 */
00018 
00019 /**
00020  * @author Denis Beurive
00021  */
00022 package org.dbview.addons.input.mysql;
00023 
00024 import org.dbview.addons.input.utils.mysql.XML;
00025 import org.dbview.input_addons.*;
00026 import org.dbview.resources.*;
00027 import org.jdom.*;
00028 
00029 /**
00030  * @class Loader
00031  * This class implements the loader adapter for the Mysql add-on.
00032  * @remark The loader is used to gain information from the input data source.
00033  * @author Denis Beurive
00034  */
00035 public class Loader extends AbstractLoader
00036 {
00037     /**
00038      * This method returns the URL used to open a connection to the database.
00039      * @param in_data XML element that contains the data required to open the connection.
00040      * @return The method returns the URL used to open a connection to the database.
00041      * @throws Exception
00042      */
00043     protected String _getUrl_(Element in_data) throws Exception
00044     {
00045         // System.out.println("Call _getUrl_");
00046         // System.out.println(in_data.getName());
00047 
00048         if (0 != in_data.getName().compareTo("data"))
00049         { throw new Exception("Invalid data for connection: " + org.dbview.utils.Jdom.print(in_data)); }
00050 
00051         String host      = org.dbview.utils.Jdom.getTextOrCdata(in_data.getChild(XML.HOST));
00052         String port      = org.dbview.utils.Jdom.getTextOrCdata(in_data.getChild(XML.PORT));
00053         String login     = org.dbview.utils.Jdom.getTextOrCdata(in_data.getChild(XML.LOGIN));
00054         String password  = org.dbview.utils.Jdom.getTextOrCdata(in_data.getChild(XML.PASSWORD));
00055         String db_name   = org.dbview.utils.Jdom.getTextOrCdata(in_data.getChild(XML.DBNAME));
00056         String URL       = "jdbc:mysql://" + host + ":" + port + "/" + db_name + "?user=" + login;
00057 
00058         if (0 < password.length()) { URL += "&password=" + password; }
00059 
00060         // System.out.println(URL);
00061 
00062         return URL;
00063     }
00064 
00065     /**
00066      * This method returns an instance of a soft foreign key detector (if it is defined in the profile).
00067      * @param in_profile Profile of the input target.
00068      * @return The method returns an instance of a soft foreign key detector, or null if no detector is defined.
00069      */
00070     protected AbstractSotfForeignKeyDetector _getSoftForeignKeyDetector_(Element in_profile) throws Exception
00071     {
00072         if (0 != in_profile.getName().compareTo("data"))
00073         { throw new Exception("Invalid data for soft key detector instantiation: " + org.dbview.utils.Jdom.print(in_profile)); }
00074 
00075         // System.out.println(org.dbview.utils.Jdom.print(in_profile));
00076         
00077         String matcher = org.dbview.utils.Jdom.getTextOrCdata(in_profile.getChild(XML.FKMATCHER));
00078         if (org.dbview.utils.Strings.isEmpty(matcher)) { return null; }
00079 
00080         // System.out.println("(" + matcher + ")");
00081 
00082         SotfForeignKeyDetectorCatalog catalog = new SotfForeignKeyDetectorCatalog();
00083         AbstractSotfForeignKeyDetector d = catalog.getFkMatcherByCliName(matcher);
00084 
00085         if (null == d)
00086         { throw new Exception("Invalid soft key detector " + matcher + " (this detector does not exist!)."); }
00087 
00088         return d;
00089     }
00090 }