DBVIEW
src/org/dbview/db/structure/FieldToFieldJoin.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.db.structure;
00024 
00025 /**
00026  * <p>This class represents a join (between two fields). A join is defined by a foreign key (defined in the "dependent" table).</p>
00027  * <p>Please note that we define two kinds of foreign keys :</p>
00028  * <ul>
00029  *      <li>Soft foreign keys. A hard foreign key is associated to a database constraint.</li>
00030  *      <li>Hard foreign keys. A soft foreign key is defined only by its name (this implies the existence of naming conventions). Example : "fk_customer_id" is a foreign key. It joins the field "fk_customer_id" to the field "id" of the table "customer".</li>
00031  * </ul>
00032  * <p>The join connects a "dependent" field - which is the foreign key - (in a dependent table), to a "reference" field (in a reference table).</p>
00033  * <p>A join contains 2 cardinalities :</p>
00034  * <ul> 
00035  *      <li>One cardinality represents the number of "reference" fields potentially associated to one "dependent" field.</li>
00036  *      <li>The other cardinality represents the number of "dependent" fields potentially associated to one "reference" fields.</li>
00037  * </ul>
00038  * <p>Cardinalities are represented by a character.</p>
00039  * <ul>
00040  *      <li>?: Zero or one.</li>
00041  *      <li>*: Zero or more.</li>
00042  *      <li>+: At least one (1,2,3...).</li>
00043  *      <li>1: One.</li>
00044  * </ul>
00045  * 
00046  * @remark Please note that these conventions are inspired by the regular expressions. 
00047  * @author Denis Beurive
00048  */
00049 public class FieldToFieldJoin
00050 {
00051         /**
00052          * Minimum occurrence of the field at the origin - in the dependent table - of the relation (values can be "0" or "1").
00053          * Values:
00054          * <ul>
00055          *     <li>Database.MIN_0</li>
00056          *     <li>Database.MIN_1</li>
00057          * </ul>
00058          */
00059     public int src_min;
00060 
00061     /**
00062      * Maximum occurrence of the field at the origin - in the dependent table - of the relation (values can be "1" or "N").
00063      * Value:
00064      * <ul>
00065      *      <li>Database.MAX_1</li>
00066      *      <li>Database.MAX_N</li>
00067      * </ul>
00068      */
00069     public int src_max;
00070 
00071     /**
00072      * Minimum occurrence of the field at the end - in the reference table - of the relation (values can be "0" or "1").
00073      * Value:
00074      * <ul>
00075      *      <li>Database.MAX_1</li>
00076      *      <li>Database.MAX_N</li>
00077      * </ul>
00078      */
00079     public int dst_min;
00080 
00081     /**
00082      * Maximum occurrence of the field at the end - in the reference table - of the relation (values can be "1" or "N").
00083      * Value:
00084      * <ul>
00085      *      <li>Database.MAX_1</li>
00086      *      <li>Database.MAX_N</li>
00087      * </ul>
00088      */
00089     public int dst_max;
00090 
00091     /**
00092      * Field at the origin of the relation (in the dependent table).
00093      * @remark To get the field's table: src_field.getTable().
00094      */
00095     public Field src_field;
00096 
00097     /**
00098      * Field at the destination of the relation (in the reference table).
00099      * @remark To get the field's table: dst_field.getTable().
00100      */
00101     public Field dst_field;
00102 
00103     /**
00104      * This attribute represents the type of join. It can be:
00105      * <ul>
00106      *      <li>Database.UNDEFINED_LINK</li>
00107      *      <li>Database.SOFT_LINK</li>
00108      *      <li>Database.HARD_LINK</li>
00109      * </ul>
00110      */
00111     public int type = Database.UNDEFINED_LINK;
00112 
00113     /**
00114      * This method returns a textual representation of the type of the join.
00115      * @return The method returns a textual representation of the type of the join.
00116      * @throws Exception
00117      */
00118     public String type() throws Exception
00119     {
00120         switch (this.type)
00121         {
00122             case Database.UNDEFINED_LINK:       return "undefined";
00123             case Database.HARD_LINK:            return "hard";
00124             case Database.SOFT_LINK:            return "soft";
00125             default: throw new Exception("You try to print a relation which type is not set!");
00126         }
00127     }
00128 
00129     /**
00130      * This method returns a textual representation of the cardinality from the "dependent field" to the "reference field".
00131      * @return The method returns a textual representation of the cardinality.
00132      */
00133     public String cardinality_from_dependent_to_reference()
00134     {
00135         // @A est associé à minA..maxB @B
00136         return this.__getSymbol(this.src_min, this.dst_max);
00137     }
00138 
00139     /**
00140      * This method returns a textual representation of the cardinality from the "reference field" to the "dependent field".
00141      * @return The method returns a textual representation of the cardinality.
00142      */
00143     public String cardinality_from_reference_to_dependent()
00144     {
00145         // @B est associé à 0..maxA @A
00146         return this.__getSymbol(Database.MIN_0, this.src_max);
00147     }
00148 
00149     /**
00150      * <p>This method returns the symbol that represents a cadinality.</p>
00151      * <p>Cardinalities are represented by a character.</p>
00152      * <ul>
00153      *      <li>?: Zero or one.</li>
00154      *      <li>*: Zero or more.</li>
00155      *      <li>+: At least one (1,2,3...).</li>
00156      *      <li>1: One.</li>
00157      * </ul>
00158      * @param in_min Minimum occurrence of the field.
00159      * @param in_max Maximtm occurrence of the field.
00160      * @remark Please note that these conventions are inspired by the regular expressions.
00161      * @return This method returns the symbol that represents a cadinality.
00162      */
00163     private String __getSymbol(int in_min, int in_max)
00164     {
00165         if ((Database.MIN_0 == in_min) && (Database.MAX_1 == in_max)) { return "?"; }
00166         if ((Database.MIN_0 == in_min) && (Database.MAX_N == in_max)) { return "*"; }
00167         if ((Database.MIN_1 == in_min) && (Database.MAX_1 == in_max)) { return "1"; }
00168         if ((Database.MIN_1 == in_min) && (Database.MAX_N == in_max)) { return "+"; }
00169         return "!!!";
00170     }
00171 }