DBVIEW
src/org/dbview/addons/input/mysql/Configuration.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 
00023 package org.dbview.addons.input.mysql;
00024 
00025 import org.dbview.addons.input.utils.mysql.XML;
00026 import org.dbview.input_addons.AbstractConfiguration;
00027 import org.dbview.input_addons.ConfigurationException;
00028 import org.jdom.*;
00029 
00030 import java.util.ArrayList;
00031 
00032 /**
00033  * @class Configuration
00034  * This class implements the configuration adapter for the Mysql add-on.
00035  * @author Denis BEURIVE
00036  */
00037 public class Configuration extends AbstractConfiguration
00038 {
00039     /**
00040      * Name of the host that runs the MySql server.
00041      * @author Denis BEURIVE
00042      */
00043     private String __host = null;
00044 
00045     /**
00046      * Port number used to connect to the MySql serveur.
00047      * @author Denis BEURIVE
00048      */
00049     private Integer __port = new Integer(3306);
00050 
00051     /**
00052      * Identifier used to access the database.
00053      * @author Denis BEURIVE
00054      */
00055     private String __login = null;
00056 
00057     /**
00058      * Password used to authenticate to the MySql server.
00059      * @author Denis BEURIVE
00060      */
00061     private String __password = null;
00062 
00063     /**
00064      * Name of the database.
00065      * @author Denis BEURIVE
00066      */
00067     private String __db_name = null;
00068 
00069     /**
00070      * This string represents the name of the class used to detect foreign keys from field's names.
00071      */
00072     private String __soft_foreign_key_detector = null;
00073 
00074     /**
00075      * Constructor.
00076      * @author Denis BEURIVE
00077      */
00078     public Configuration()
00079     {
00080         super();
00081     }
00082 
00083     /**
00084      * This method returns the name of the specific add-on to
00085      * which the configuration is associated.
00086      *
00087      * @return The method returns the name of the specific add-on
00088      *         to which the configuration is associated.
00089      */
00090     protected String _getAddOnName_()
00091     {
00092         return "mysql";
00093     }
00094 
00095     /**
00096      * This method returns a list of XML elements. These XML elements represent
00097      * the configuration for the specific add-on to which the configuration is
00098      * associated (in this case "mysql").
00099      * These XML elements will be included in the configuration.
00100      * @return The method returns a list of XML elements.
00101      */
00102     protected ArrayList<Element> _toXml_()
00103     {
00104         ArrayList<Element> data = new ArrayList<Element>();
00105         data.add(new Element(XML.HOST).addContent(this.__host));
00106         data.add(new Element(XML.PORT).addContent(this.__port.toString()));
00107         data.add(new Element(XML.LOGIN).addContent(this.__login));
00108         data.add(new Element(XML.PASSWORD).addContent(new CDATA(this.__password)));
00109         data.add(new Element(XML.DBNAME).addContent(this.__db_name));
00110         if (null != this.__soft_foreign_key_detector) { data.add(new Element(XML.FKMATCHER).addContent(this.__soft_foreign_key_detector)); }
00111         return data;
00112     }
00113 
00114     /**
00115      * This method initializes the configuration for the specific add-on to
00116      * which the configuration is associated. The initialization is done using
00117      * the content of a given XML element.
00118      *
00119      * @param in_xml
00120      *            Document XML (that represents a configuration).
00121      * @throws ConfigurationException
00122      * @note This method is used with the profile's repository.
00123      */
00124     protected void _fromXml_(Element in_xml) throws ConfigurationException
00125     {
00126         Element e;
00127 
00128         e = in_xml.getChild(XML.HOST);
00129         if (null == e) { throw new ConfigurationException("Missing \"host\"."); }
00130         try { this.__host = org.dbview.utils.Jdom.getTextOrCdata(e); } catch (Exception x) { throw new ConfigurationException(x.getMessage()); }
00131         if (null == this.__host) { throw new ConfigurationException("XML representation of the configuration is not valid. Something is messed up with the host definition!"); }
00132 
00133         e = in_xml.getChild(XML.PORT);
00134         if (null == e) { throw new ConfigurationException("Missing \"port\".");  }
00135         String s = null;
00136         try { s = org.dbview.utils.Jdom.getTextOrCdata(e); } catch (Exception x) { throw new ConfigurationException(x.getMessage() + " XML representation of the configuration is not valid. Something is messed up with the port definition!"); }
00137         this.__port = null == s ? null : Integer.parseInt(s);
00138 
00139         e = in_xml.getChild(XML.LOGIN);
00140         if (null == e) { throw new ConfigurationException("Missing \"login\"."); }
00141         try { this.__login = org.dbview.utils.Jdom.getTextOrCdata(e); } catch (Exception x) { throw new ConfigurationException(x.getMessage() + " XML representation of the configuration is not valid. Something is messed up with the login definition!"); }
00142 
00143         e = in_xml.getChild(XML.FKMATCHER);
00144         if (null != e) { try { this.__soft_foreign_key_detector = org.dbview.utils.Jdom.getTextOrCdata(e); } catch (Exception x) { throw new ConfigurationException(x.getMessage() + " XML representation of the configuration is not valid. Something is messed up with the foreign key matcher definition!"); } }
00145 
00146         e = in_xml.getChild(XML.PASSWORD);
00147         if (null == e) { throw new ConfigurationException("Missing \"password\"."); }
00148         try { this.__password = org.dbview.utils.Jdom.getTextOrCdata(e); } catch (Exception x) { throw new ConfigurationException(x.getMessage() + " XML representation of the configuration is not valid. Something is messed up with the password definition!"); }
00149 
00150         e = in_xml.getChild(XML.DBNAME);
00151         if (null == e) { throw new ConfigurationException("Missing \"db_name\"."); }
00152         try { this.__db_name = org.dbview.utils.Jdom.getTextOrCdata(e); } catch (Exception x) { throw new ConfigurationException(x.getMessage() + " XML representation of the configuration is not valid. Something is messed up with the database's name definition!"); }
00153     }
00154 
00155     /**
00156      * This method returns a structure that can be sued to produce a literal
00157      * representation of the configuration. The returned structure is an array.
00158      * Each element of the returned array is an array that contains 2 strings.
00159      * <ul>
00160      *    <li>The first element of the array is the name of a configuration
00161      *        parameter.</li>
00162      *    <li>The second element of the array is the value of a
00163      *        configuration parameter.</li>
00164      * </ul>
00165      *
00166      * @return The method returns a structure that can be used to produce a
00167      *         literal representation of the configuration.
00168      */
00169     public ArrayList<String[]> toStrings()
00170     {
00171                 ArrayList<String[]> list = new ArrayList<String[]>();
00172 
00173                 String[] host        = { "host",       this.__host};
00174                 String[] port        = { "port",       this.__port.toString()};
00175                 String[] login       = { "login",      this.__login};
00176                 String[] password    = { "password",   this.__password};
00177                 String[] db_name     = { "db name",    this.__db_name};
00178                 String[] fk_matcher  = { "fk matcher", this.__soft_foreign_key_detector};
00179 
00180                 list.add(host);
00181                 list.add(port);
00182                 list.add(login);
00183                 list.add(password);
00184                 list.add(db_name);
00185                 list.add(fk_matcher);
00186 
00187                 return list;
00188     }
00189 
00190 }