DBVIEW
src/org/dbview/resources/softforeignkeydetectors/IdTargetTableName.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: id&lt;name of the target table&gt;
00027  * The target field is always "id".
00028  * @author Denis Beurive
00029  */
00030 public class IdTargetTableName 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() < 3) { return Boolean.FALSE; }
00041         if (in_field_name.startsWith("id")) { 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         return in_field_name.substring(2);
00055     }
00056 
00057     /**
00058      * If the field is a (soft) foreign key, then the method returns the name of the "reference" field.
00059      * @param in_field_name Name of the field.
00060      * @return If the field is a (soft) foreign key, then the method returns the name of the "reference" field.
00061      *         Otherwise, the method returns the value null.
00062      */
00063     public String referenceField(String in_field_name)
00064     {
00065         if (! this.isFk(in_field_name)) { return null; }
00066         return "id";
00067     }
00068 
00069     /**
00070      * This method returns a description of the matcher.
00071      * @return The method returns a description of the matcher.
00072      */
00073     public String description()
00074     {
00075         ArrayList<String> doc = new ArrayList<String>();
00076         doc.add("Pattern:      id{name of the target table}.");
00077         doc.add("Target field: Always \"id\".");
00078         doc.add("Example:      idproduct_category");
00079         doc.add("              -> product_category.id");
00080 
00081         return org.dbview.utils.Strings.joinWithNewLines(doc);
00082     }
00083 }