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 join condtion (join two tables without 037 * primary/foreign key) in table level 038 * 039 * @author Jing Tao 040 * 041 */ 042 043public class JoinConditionConstraint implements Constraint { 044 private int type = Constraint.JOINCONDITIONCONSTRAINT; 045 private String name = null; 046 private String[] keys = null; 047 private String entityReference = null; 048 private String[] referencedKeys = null; 049 050 private static Log log; 051 052 static { 053 log = LogFactory 054 .getLog("org.kepler.objectmanager.data.db.JoinConditionConstraint"); 055 } 056 057 /** 058 * Default constructor 059 */ 060 public JoinConditionConstraint() { 061 062 } 063 064 /** 065 * Method to get type 066 * 067 * @return int 068 */ 069 public int getType() { 070 return type; 071 } 072 073 /** 074 * Method to get constraint name 075 * 076 * @return String 077 */ 078 public String getName() { 079 return name; 080 } 081 082 /** 083 * Method to get keys 084 * 085 * @return String[] 086 */ 087 public String[] getKeys() { 088 return keys; 089 } 090 091 /** 092 * Method to get referenced entity(parent table) 093 * 094 * @return String 095 */ 096 public String getEntityReference() { 097 return entityReference; 098 } 099 100 /** 101 * Method to get referenced key in parent table 102 * 103 * @return String[] 104 */ 105 public String[] getReferencedKeys() { 106 return referencedKeys; 107 } 108 109 /** 110 * Method to set constraint name 111 * 112 * @param constraintName 113 * String 114 */ 115 public void setName(String constraintName) { 116 name = constraintName; 117 } 118 119 /** 120 * Method to set keys 121 * 122 * @param constraintKeys 123 * String[] 124 */ 125 public void setKeys(String[] constraintKeys) { 126 keys = constraintKeys; 127 } 128 129 /** 130 * Method to set referenced entity name 131 * 132 * @param referencedEntity 133 * String 134 */ 135 public void setEntityReference(String referencedEntity) { 136 entityReference = referencedEntity; 137 } 138 139 /** 140 * Method to set up referenced key in parent table(referencedEntity) 141 * 142 * @param keyList 143 * String[] 144 */ 145 146 public void setReferencedKeys(String[] keyList) { 147 referencedKeys = keyList; 148 } 149 150 /** 151 * Method to print out a part sql command about joincondition. In 152 * joinCondition, we need to specify the referenced entity's column name. It 153 * is same as foreign key except to specify referenced key name in parent 154 * table 155 * 156 * @return String 157 */ 158 public String printString() throws UnWellFormedConstraintException { 159 String sql = null; 160 if (referencedKeys == null || referencedKeys.length == 0) { 161 throw new UnWellFormedConstraintException( 162 "The refrenced key in parent " 163 + "table is empty in join condition constaint"); 164 } 165 // in joincondition the first part is as same as foreign key 166 // so we can first create a foreginkey object and print out the sql 167 ForeignKey foreignKey = new ForeignKey(); 168 foreignKey.setName(name); 169 foreignKey.setKeys(keys); 170 foreignKey.setEntityReference(entityReference); 171 String foreignKeySql = foreignKey.printString(); 172 173 StringBuffer buffer = new StringBuffer(foreignKeySql); 174 175 // add referencedkey list in parent table 176 buffer.append(Constraint.LEFTPARENTH); 177 // add keys into parenthesis 178 boolean firstKey = true; 179 for (int i = 0; i < referencedKeys.length; i++) { 180 String keyName = keys[i]; 181 // if any key is null or empty, we will throw a exception 182 if (keyName == null || keyName.trim().equals("")) { 183 throw new UnWellFormedConstraintException( 184 "refernced key name in " 185 + "parent table is empty or null in join condition"); 186 } 187 // if this is not the first key, we need add a comma 188 if (!firstKey) { 189 buffer.append(Constraint.COMMA); 190 } 191 buffer.append(keyName); 192 firstKey = false; 193 }// for 194 buffer.append(Constraint.RIGHTPARENTH); 195 buffer.append(Constraint.SPACESTRING); 196 sql = buffer.toString(); 197 log.debug("The foreign key part of sql is " + sql); 198 return sql; 199 } 200 201}