DBVIEW
src/org/dbview/addons/input/mysql/Cli.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 java.util.ArrayList;
00026 import org.dbview.adapter.AbstractCli;
00027 import org.dbview.adapter.CliException;
00028 import org.dbview.adapter.CliParameter;
00029 import org.dbview.adapter.CliParameterTypes;
00030 import org.dbview.addons.input.utils.mysql.CliOptions;
00031 import org.dbview.addons.input.utils.mysql.XML;
00032 import org.dbview.utils.args4j.*;
00033 import org.jdom.Element;
00034 import org.kohsuke.args4j.CmdLineException;
00035 import org.kohsuke.args4j.CmdLineParser;
00036 
00037 /**
00038  * This class is responsible for extracting values from the command line, for the MySql add-on.
00039  *
00040  * @author Denis BEURIVE
00041  */
00042 public class Cli extends AbstractCli
00043 {
00044     /**
00045      * This attribute represents the <i>command line options' container</i> used to parse the command line that applies to the add-on.
00046      * @remark See Args4j. The command line options are declared in a class. We call this class the <i>command line options container</i>
00047      */
00048     private CliOptions __options = new CliOptions();
00049 
00050     /**
00051      * Return the command line options container.
00052      * @remark The returned container is mainly used to detect unexpected arguments in the command line.
00053      * @return The method returns the command line options container.
00054      */
00055     public OptionContainer getOptionsContainer() { return this.__options; }
00056 
00057     /**
00058      * This method returns the information extracted from the command line.
00059      *
00060      * @param in_vargs
00061      *            The command line arguments.
00062      * @param in_strict
00063      *            This parameter indicates whether the CLI parser should check
00064      *            the presence of mandatory options, or not.
00065      *            <ul>
00066      *              <li>TRUE: Check the presence of mandatory options.</li>
00067      *              <li>FALSE: Does not check the presence of mandatory options.</li>
00068      *            </ul>
00069      * @param in_set_default
00070      *            This parameter indicates whether the method should set default
00071      *            values or not.
00072      *            <ul>
00073      *              <li>TRUE: Set default values. When adding a new profile, you should activate the assignment of default values.</li>
00074      *              <li>FALSE: Does not set default values. When updating a profile, you should deactivate the assignment of default values.</li>
00075      *            </ul>
00076      * @return The method returns the information extracted from the command
00077      *         line. The configuration is represented by a list of
00078      *         XML elements. Each element of the returned list will be included
00079      *         in the XML representation of the configuration.
00080      */
00081     protected ArrayList<Element> _getConf_(String[] in_vargs,
00082                                            Boolean in_strict,
00083                                            Boolean in_set_default) throws CliException
00084     {
00085         ArrayList<Element> conf = new ArrayList<Element>();
00086         CmdLineParser parser = new CmdLineParser(this.__options);
00087 
00088         try
00089         {
00090             parser.parseArgument(this.__options.extractArgvForThisSet(in_vargs));
00091 
00092         }
00093         catch (CmdLineException e)
00094         {
00095             throw new CliException(e.getMessage());
00096         }
00097 
00098         // Extract values.
00099         String host       = this.__options.getHost();
00100         String login      = this.__options.getUser();
00101         String password   = this.__options.getPassword();
00102         String db_name    = this.__options.getDbNanme();
00103         String fk_matcher = this.__options.getSoftForeignKeyDetector();
00104         Integer port      = this.__options.getPort();
00105 
00106         // Set default values.
00107         if (in_set_default)
00108         {
00109             host = null == host ? "localhost" : host;
00110             login = null == login ? "root" : login;
00111             port = null == port ? 3306 : port;
00112             password = null == password ? "" : password;
00113             fk_matcher = null == fk_matcher ? "" : fk_matcher;
00114         }
00115 
00116         // Sanity check.
00117         if (in_strict)
00118         {
00119             if (null == db_name)
00120             {
00121                 throw new CliException("The mandatory command line option \"--dbname\" is missing.");
00122             }
00123         }
00124 
00125         // Create the XML element.
00126         if (null != host)
00127         {
00128             conf.add(new Element(XML.HOST).addContent(host));
00129         }
00130         if (null != port)
00131         {
00132             conf.add(new Element(XML.PORT).addContent(port.toString()));
00133         }
00134         if (null != login)
00135         {
00136             conf.add(new Element(XML.LOGIN).addContent(login));
00137         }
00138         if (null != password)
00139         {
00140             conf.add(new Element(XML.PASSWORD).addContent(password));
00141         }
00142         if (null != db_name)
00143         {
00144             conf.add(new Element(XML.DBNAME).addContent(db_name));
00145         }
00146         if (null != fk_matcher)
00147         {
00148             conf.add(new Element(XML.FKMATCHER).addContent(fk_matcher));
00149         }
00150 
00151         return conf;
00152     }
00153 
00154     /**
00155      * This method returns the description of the command line expected by this adaptor.
00156      *
00157      * @return The method returns an array of command line parameter's
00158      *         descriptions.
00159      */
00160     public ArrayList<CliParameter> getOptions()
00161     {
00162         ArrayList<CliParameter> params = new ArrayList<CliParameter>();
00163 
00164         CliParameter p1 = new CliParameter();
00165         p1.parameter = CliOptions.HOST;
00166         p1.type = CliParameterTypes.STRING;
00167         p1.mandatory = Boolean.FALSE;
00168         p1.description = "Host name, or IP address of the MySql server (default: localhost).";
00169 
00170         CliParameter p2 = new CliParameter();
00171         p2.parameter = CliOptions.USER;
00172         p2.type = CliParameterTypes.STRING;
00173         p2.mandatory = Boolean.FALSE;
00174         p2.description = "User's name used to log to the MySql server (default: root).";
00175 
00176         CliParameter p3 = new CliParameter();
00177         p3.parameter = CliOptions.PASSWORD;
00178         p3.type = CliParameterTypes.STRING;
00179         p3.mandatory = Boolean.FALSE;
00180         p3.description = "User's password (default: no password).";
00181 
00182         CliParameter p4 = new CliParameter();
00183         p4.parameter = CliOptions.DBNAME;
00184         p4.type = CliParameterTypes.STRING;
00185         p4.mandatory = Boolean.TRUE;
00186         p4.description = "Name of the database to use.";
00187 
00188         CliParameter p5 = new CliParameter();
00189         p5.parameter = CliOptions.FKMATCHER;
00190         p5.type = CliParameterTypes.STRING;
00191         p5.mandatory = Boolean.FALSE;
00192         p5.description = "Name of the matcher used to find foreign keys if the database engine does not support foreing key constraint (Typically�: MyIsam). Please not that you can use this feature even if you use a database engine that supports foreing key constraint. For example: you are using InnoDb database engine wthout declaring foreign key constraints, but your database� schema presents implicit joins (default: do not use foreign key matcher).";
00193         p5.resource_name = "fkMatchers";
00194 
00195         CliParameter p6 = new CliParameter();
00196         p6.parameter = CliOptions.PORT;
00197         p6.type = CliParameterTypes.INTEGER;
00198         p6.mandatory = Boolean.FALSE;
00199         p6.description = "TCP port number used by the MySql server (default: 3306)";
00200 
00201         params.add(p1);
00202         params.add(p2);
00203         params.add(p3);
00204         params.add(p4);
00205         params.add(p5);
00206         params.add(p6);
00207         return params;
00208     }
00209 }