001/* 002 * Copyright (c) 2004-2010 The Regents of the University of California. 003 * All rights reserved. 004 * 005 * '$Author: welker $' 006 * '$Date: 2010-05-06 05:21:26 +0000 (Thu, 06 May 2010) $' 007 * '$Revision: 24234 $' 008 * 009 * Permission is hereby granted, without written agreement and without 010 * license or royalty fees, to use, copy, modify, and distribute this 011 * software and its documentation for any purpose, provided that the above 012 * copyright notice and the following two paragraphs appear in all copies 013 * of this software. 014 * 015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 019 * SUCH DAMAGE. 020 * 021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 026 * ENHANCEMENTS, OR MODIFICATIONS. 027 * 028 */ 029 030package org.kepler.objectmanager.data.db; 031 032import org.apache.commons.logging.Log; 033import org.apache.commons.logging.LogFactory; 034 035/** 036 * This class represents a foreignkey in table level 037 * 038 * @author Jing Tao 039 * 040 */ 041 042public class ForeignKey implements Constraint { 043 private int type = Constraint.FOREIGNKEY; 044 private String name = null; 045 private String[] keys = null; 046 private String entityReference = null; 047 048 private static Log log; 049 050 static { 051 log = LogFactory.getLog("org.kepler.objectmanager.data.db.ForeignKey"); 052 } 053 054 /** 055 * Default constructor 056 */ 057 public ForeignKey() { 058 059 } 060 061 /** 062 * Method to get type 063 * 064 * @return int 065 */ 066 public int getType() { 067 return type; 068 } 069 070 /** 071 * Method to get constraint name 072 * 073 * @return String 074 */ 075 public String getName() { 076 return name; 077 } 078 079 /** 080 * Method to get keys 081 * 082 * @return String[] 083 */ 084 public String[] getKeys() { 085 return keys; 086 } 087 088 /** 089 * Method to get referenced entity 090 * 091 * @return String 092 */ 093 public String getEntityReference() { 094 return entityReference; 095 } 096 097 /** 098 * Method to set constraint name 099 * 100 * @param constraintName 101 * String 102 */ 103 public void setName(String constraintName) { 104 name = constraintName; 105 } 106 107 /** 108 * Method to set keys 109 * 110 * @param constraintKeys 111 * String[] 112 */ 113 public void setKeys(String[] constraintKeys) { 114 keys = constraintKeys; 115 } 116 117 /** 118 * Method to set referenced entity name 119 * 120 * @param referencedEntity 121 * String 122 */ 123 public void setEntityReference(String referencedEntity) { 124 entityReference = referencedEntity; 125 } 126 127 /** 128 * Method to print out a part sql command about foreign key In foreign key, 129 * you don't need to specify the referenced entity's column name, the 130 * primary key column(s) of the referenced table is used as the referenced 131 * column(s) 132 * 133 * @return String 134 */ 135 public String printString() throws UnWellFormedConstraintException { 136 String sql = null; 137 if (name == null || name.trim().equals("")) { 138 throw new UnWellFormedConstraintException( 139 "No Constraint name assign " + "to Primary key"); 140 } 141 if (keys == null || keys.length == 0) { 142 throw new UnWellFormedConstraintException("No key is specified in " 143 + "primary key"); 144 } 145 146 StringBuffer buffer = new StringBuffer(); 147 buffer.append(Constraint.SPACESTRING); 148 buffer.append(Constraint.CONSTRAINT); 149 buffer.append(Constraint.SPACESTRING); 150 buffer.append(name); 151 buffer.append(Constraint.SPACESTRING); 152 buffer.append(Constraint.FOREIGNKEYSTRING); 153 buffer.append(Constraint.SPACESTRING); 154 155 // add foreign key list 156 buffer.append(Constraint.LEFTPARENTH); 157 // add keys into parenthesis 158 boolean firstKey = true; 159 for (int i = 0; i < keys.length; i++) { 160 String keyName = keys[i]; 161 // if any key is null or empty, we will throw a exception 162 if (keyName == null || keyName.trim().equals("")) { 163 throw new UnWellFormedConstraintException( 164 "key name empty or null in " + "foreign key"); 165 } 166 // if this is not the first key, we need add a comma 167 if (!firstKey) { 168 buffer.append(Constraint.COMMA); 169 } 170 buffer.append(keyName); 171 firstKey = false; 172 }// for 173 buffer.append(Constraint.RIGHTPARENTH); 174 buffer.append(Constraint.SPACESTRING); 175 buffer.append(Constraint.REFERENCESTRING); 176 buffer.append(Constraint.SPACESTRING); 177 buffer.append(entityReference); 178 buffer.append(Constraint.SPACESTRING); 179 sql = buffer.toString(); 180 log.debug("The foreign key part of sql is " + sql); 181 return sql; 182 } 183 184}