DBVIEW
src/org/dbview/resources/softforeignkeydetectors/FkUsTargetTableName.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 package org.dbview.resources.softforeignkeydetectors;
00020 
00021 import org.dbview.resources.AbstractSotfForeignKeyDetector;
00022 import java.util.*;
00023 
00024 /**
00025  * This class implements a (soft) foreign key detector.
00026  * Foreign key looks like: fk_&lt;name of the target table&gt;
00027  * The target field is always "id".
00028  * @author Denis Beurive
00029  */
00030 public class FkUsTargetTableName extends AbstractSotfForeignKeyDetector
00031 {
00032     /**
00033      * This method tests whether a field, identified by its name, is a (soft) foreign key or not.
00034      * @param in_field_name Name of the field to test.
00035      * @return If the field is a (soft) foreign key, then the method returns the value TRUE.
00036      *         Otherwise, it returns the value FALSE.
00037      */
00038     public Boolean isFk(String in_field_name)
00039     {
00040         if (in_field_name.length() < 4) { return Boolean.FALSE; }
00041         if (in_field_name.startsWith("fk_")) { return Boolean.TRUE; }
00042         return Boolean.FALSE;
00043     }
00044 
00045     /**
00046      * If the field is a (soft) foreign key, then the method returns the name of the "reference" table.
00047      * @param in_field_name Name of the field.
00048      * @return If the field is a (soft) foreign key, then the method returns the name of the "reference" table.
00049      *         Otherwise, the method returns the value null.
00050      */
00051     public String referenceTable(String in_field_name)
00052     {
00053         if (! this.isFk(in_field_name)) { return null; }
00054 
00055         ArrayList<String> tokens = new ArrayList<String>(Arrays.asList(in_field_name.split("_")));
00056         tokens.remove(0);
00057 
00058         return org.dbview.utils.Strings.join((tokens), "_");
00059     }
00060 
00061     /**
00062      * If the field is a (soft) foreign key, then the method returns the name of the "reference" field.
00063      * @param in_field_name Name of the field.
00064      * @return If the field is a (soft) foreign key, then the method returns the name of the "reference" field.
00065      *         Otherwise, the method returns the value null.
00066      */
00067     public String referenceField(String in_field_name)
00068     {
00069         if (! this.isFk(in_field_name)) { return null; }
00070         return "id";
00071     }
00072 
00073     /**
00074      * This method returns a description of the matcher.
00075      * @return The method returns a description of the matcher.
00076      */
00077     public String description()
00078     {
00079         ArrayList<String> doc = new ArrayList<String>();
00080         doc.add("Pattern:      fk_{name of the target table}.");
00081         doc.add("Target field: Always \"id\".");
00082         doc.add("Example:      fk_product_category");
00083                 doc.add(        "              -> product_category.id");
00084 
00085         return org.dbview.utils.Strings.joinWithNewLines(doc);
00086     }
00087 }