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