Coverage report

  %line %branch
org.apache.torque.engine.database.model.ForeignKey
58% 
88% 

 1  
 package org.apache.torque.engine.database.model;
 2  
 
 3  
 /*
 4  
  * Copyright 2001-2005 The Apache Software Foundation.
 5  
  *
 6  
  * Licensed under the Apache License, Version 2.0 (the "License")
 7  
  * you may not use this file except in compliance with the License.
 8  
  * You may obtain a copy of the License at
 9  
  *
 10  
  *     http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 
 19  
 import java.util.ArrayList;
 20  
 import java.util.Hashtable;
 21  
 import java.util.List;
 22  
 import org.xml.sax.Attributes;
 23  
 
 24  
 /**
 25  
  * A class for information about foreign keys of a table.
 26  
  *
 27  
  * @author <a href="mailto:fedor.karpelevitch@home.com">Fedor</a>
 28  
  * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
 29  
  * @version $Id: ForeignKey.java 239624 2005-08-24 12:18:03Z henning $
 30  
  */
 31  27
 public class ForeignKey
 32  
 {
 33  
     private String foreignTableName;
 34  
     private String name;
 35  
     private String onUpdate;
 36  
     private String onDelete;
 37  
     private Table parentTable;
 38  27
     private List localColumns = new ArrayList(3);
 39  27
     private List foreignColumns = new ArrayList(3);
 40  
 
 41  
     // the uppercase equivalent of the onDelete/onUpdate values in the dtd
 42  
     private static final String NONE    = "NONE";
 43  
     private static final String SETNULL = "SETNULL";
 44  
 
 45  
     /**
 46  
      * Imports foreign key from an XML specification
 47  
      *
 48  
      * @param attrib the xml attributes
 49  
      */
 50  
     public void loadFromXML(Attributes attrib)
 51  
     {
 52  27
         foreignTableName = attrib.getValue("foreignTable");
 53  27
         name = attrib.getValue("name");
 54  27
         onUpdate = attrib.getValue("onUpdate");
 55  27
         onDelete = attrib.getValue("onDelete");
 56  27
         onUpdate = normalizeFKey(onUpdate);
 57  27
         onDelete = normalizeFKey(onDelete);
 58  27
     }
 59  
 
 60  
     /**
 61  
      * Normalizes the input of onDelete, onUpdate attributes
 62  
      *
 63  
      * @param attrib the attribute to normalize
 64  
      * @return nomalized form
 65  
      */
 66  
     private String normalizeFKey(String attrib)
 67  
     {
 68  54
         if (attrib == null)
 69  
         {
 70  0
             attrib = NONE;
 71  
         }
 72  
 
 73  54
         attrib = attrib.toUpperCase();
 74  54
         if (attrib.equals(SETNULL))
 75  
         {
 76  9
             attrib = "SET NULL";
 77  
         }
 78  54
         return attrib;
 79  
     }
 80  
 
 81  
     /**
 82  
      * Returns whether or not the onUpdate attribute is set
 83  
      *
 84  
      * @return true if the onUpdate attribute is set
 85  
      */
 86  
     public boolean hasOnUpdate()
 87  
     {
 88  2
        return !onUpdate.equals(NONE);
 89  
     }
 90  
 
 91  
     /**
 92  
      * Returns whether or not the onDelete attribute is set
 93  
      *
 94  
      * @return true if the onDelete attribute is set
 95  
      */
 96  
     public boolean hasOnDelete()
 97  
     {
 98  2
        return !onDelete.equals(NONE);
 99  
     }
 100  
 
 101  
     /**
 102  
      * Returns the onUpdate attribute
 103  
      *
 104  
      * @return the onUpdate attribute
 105  
      */
 106  
     public String getOnUpdate()
 107  
     {
 108  1
        return onUpdate;
 109  
     }
 110  
 
 111  
     /**
 112  
      * Returns the onDelete attribute
 113  
      *
 114  
      * @return the onDelete attribute
 115  
      */
 116  
     public String getOnDelete()
 117  
     {
 118  1
        return onDelete;
 119  
     }
 120  
 
 121  
     /**
 122  
      * Sets the onDelete attribute
 123  
      *
 124  
      * @param value the onDelete attribute
 125  
      */
 126  
     public void setOnDelete(String value)
 127  
     {
 128  0
        onDelete = normalizeFKey(value);
 129  0
     }
 130  
 
 131  
     /**
 132  
      * Sets the onUpdate attribute
 133  
      *
 134  
      * @param value the onUpdate attribute
 135  
      */
 136  
     public void setOnUpdate(String value)
 137  
     {
 138  0
        onUpdate = normalizeFKey(value);
 139  0
     }
 140  
 
 141  
     /**
 142  
      * Returns the name attribute.
 143  
      *
 144  
      * @return the name
 145  
      */
 146  
     public String getName()
 147  
     {
 148  27
         return name;
 149  
     }
 150  
 
 151  
     /**
 152  
      * Sets the name attribute.
 153  
      *
 154  
      * @param name the name
 155  
      */
 156  
     public void setName(String name)
 157  
     {
 158  27
         this.name = name;
 159  27
     }
 160  
 
 161  
     /**
 162  
      * Get the foreignTableName of the FK
 163  
      *
 164  
      * @return the name of the foreign table
 165  
      */
 166  
     public String getForeignTableName()
 167  
     {
 168  83
         return foreignTableName;
 169  
     }
 170  
 
 171  
     /**
 172  
      * Set the foreignTableName of the FK
 173  
      *
 174  
      * @param tableName the name of the foreign table
 175  
      */
 176  
     public void setForeignTableName(String tableName)
 177  
     {
 178  0
         foreignTableName = tableName;
 179  0
     }
 180  
 
 181  
     /**
 182  
      * Set the parent Table of the foreign key
 183  
      *
 184  
      * @param parent the table
 185  
      */
 186  
     public void setTable(Table parent)
 187  
     {
 188  27
         parentTable = parent;
 189  27
     }
 190  
 
 191  
     /**
 192  
      * Get the parent Table of the foreign key
 193  
      *
 194  
      * @return the parent table
 195  
      */
 196  
     public Table getTable()
 197  
     {
 198  0
         return parentTable;
 199  
     }
 200  
 
 201  
     /**
 202  
      * Returns the name of the table the foreign key is in
 203  
      *
 204  
      * @return the name of the table
 205  
      */
 206  
     public String getTableName()
 207  
     {
 208  1
         return parentTable.getName();
 209  
     }
 210  
 
 211  
     /**
 212  
      * Adds a new reference entry to the foreign key
 213  
      *
 214  
      * @param attrib the xml attributes
 215  
      */
 216  
     public void addReference(Attributes attrib)
 217  
     {
 218  36
         addReference(attrib.getValue("local"), attrib.getValue("foreign"));
 219  36
     }
 220  
 
 221  
     /**
 222  
      * Adds a new reference entry to the foreign key
 223  
      *
 224  
      * @param local name of the local column
 225  
      * @param foreign name of the foreign column
 226  
      */
 227  
     public void addReference(String local, String foreign)
 228  
     {
 229  36
         localColumns.add(local);
 230  36
         foreignColumns.add(foreign);
 231  36
     }
 232  
 
 233  
     /**
 234  
      * Returns a comma delimited string of local column names
 235  
      *
 236  
      * @return the local column names
 237  
      */
 238  
     public String getLocalColumnNames()
 239  
     {
 240  0
         return Column.makeList(getLocalColumns());
 241  
     }
 242  
 
 243  
     /**
 244  
      * Returns a comma delimited string of foreign column names
 245  
      *
 246  
      * @return the foreign column names
 247  
      */
 248  
     public String getForeignColumnNames()
 249  
     {
 250  0
         return Column.makeList(getForeignColumns());
 251  
     }
 252  
 
 253  
     /**
 254  
      * Returns the list of local column names. You should not edit this List.
 255  
      *
 256  
      * @return the local columns
 257  
      */
 258  
     public List getLocalColumns()
 259  
     {
 260  27
         return localColumns;
 261  
     }
 262  
 
 263  
     /**
 264  
      * Utility method to get local column names to foreign column names
 265  
      * mapping for this foreign key.
 266  
      *
 267  
      * @return table mapping foreign names to local names
 268  
      */
 269  
     public Hashtable getLocalForeignMapping()
 270  
     {
 271  0
         Hashtable h = new Hashtable();
 272  
 
 273  0
         for (int i = 0; i < localColumns.size(); i++)
 274  
         {
 275  0
             h.put(localColumns.get(i), foreignColumns.get(i));
 276  
         }
 277  
 
 278  0
         return h;
 279  
     }
 280  
 
 281  
     /**
 282  
      * Returns the list of foreign column names. You should not edit this List.
 283  
      *
 284  
      * @return the foreign columns
 285  
      */
 286  
     public List getForeignColumns()
 287  
     {
 288  29
         return foreignColumns;
 289  
     }
 290  
 
 291  
     /**
 292  
      * Utility method to get foreign column names to local column names
 293  
      * mapping for this foreign key.
 294  
      *
 295  
      * @return table mapping local names to foreign names
 296  
      */
 297  
     public Hashtable getForeignLocalMapping()
 298  
     {
 299  0
         Hashtable h = new Hashtable();
 300  
 
 301  0
         for (int i = 0; i < localColumns.size(); i++)
 302  
         {
 303  0
             h.put(foreignColumns.get(i), localColumns.get(i));
 304  
         }
 305  
 
 306  0
         return h;
 307  
     }
 308  
 
 309  
     /**
 310  
      * String representation of the foreign key. This is an xml representation.
 311  
      *
 312  
      * @return string representation in xml
 313  
      */
 314  
     public String toString()
 315  
     {
 316  0
         StringBuffer result = new StringBuffer();
 317  0
         result.append("    <foreign-key foreignTable=\"")
 318  
             .append(getForeignTableName())
 319  
             .append("\" name=\"")
 320  
             .append(getName())
 321  
             .append("\">\n");
 322  
 
 323  0
         for (int i = 0; i < localColumns.size(); i++)
 324  
         {
 325  0
             result.append("        <reference local=\"")
 326  
                 .append(localColumns.get(i))
 327  
                 .append("\" foreign=\"")
 328  
                 .append(foreignColumns.get(i))
 329  
                 .append("\"/>\n");
 330  
         }
 331  0
         result.append("    </foreign-key>\n");
 332  0
         return result.toString();
 333  
     }
 334  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.