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 primary key constraint in eml 037 * 038 * @author Jing Tao 039 * 040 */ 041 042public class PrimaryKey implements Constraint { 043 private int type = Constraint.PRIMARYKEY; 044 private String name = null; 045 private String[] keys = null; 046 047 private static Log log; 048 049 static { 050 log = LogFactory.getLog("org.kepler.objectmanager.data.db.PrimaryKey"); 051 } 052 053 /* 054 * Default constructor 055 */ 056 public PrimaryKey() { 057 058 } 059 060 /** 061 * Method to get constrain type 062 * 063 * @return int 064 */ 065 public int getType() { 066 return type; 067 } 068 069 /** 070 * Method to get constrain name 071 * 072 * @return String 073 */ 074 public String getName() { 075 return name; 076 } 077 078 /** 079 * Method to get keys 080 * 081 * @return String[] 082 */ 083 public String[] getKeys() { 084 return keys; 085 } 086 087 /** 088 * Method to set constraint name 089 * 090 * @param constraintName 091 * String 092 */ 093 public void setName(String constraintName) { 094 name = constraintName; 095 } 096 097 /** 098 * Method to set keys in constraint 099 * 100 * @param keyList 101 * String[] 102 */ 103 public void setKeys(String[] keyList) { 104 keys = keyList; 105 } 106 107 /** 108 * Method to transfer a primary key into sql command (table constraint) The 109 * string will look like: CONSTRAINT constrain_name Primary Key ( col1, 110 * col2, ...) 111 * 112 * @return String 113 */ 114 public String printString() throws UnWellFormedConstraintException { 115 String sql = null; 116 if (name == null || name.trim().equals("")) { 117 throw new UnWellFormedConstraintException( 118 "No Constraint name assign " + "to Primary key"); 119 } 120 if (keys == null || keys.length == 0) { 121 throw new UnWellFormedConstraintException("No key is specified in " 122 + "primary key"); 123 } 124 125 StringBuffer buffer = new StringBuffer(); 126 buffer.append(Constraint.SPACESTRING); 127 buffer.append(Constraint.CONSTRAINT); 128 buffer.append(Constraint.SPACESTRING); 129 buffer.append(name); 130 buffer.append(Constraint.SPACESTRING); 131 buffer.append(Constraint.PRIMARYKEYSTRING); 132 buffer.append(Constraint.SPACESTRING); 133 buffer.append(Constraint.LEFTPARENTH); 134 // add keys into parenthesis 135 boolean firstKey = true; 136 for (int i = 0; i < keys.length; i++) { 137 String keyName = keys[i]; 138 // if any key is null or empty, we will throw a exception 139 if (keyName == null || keyName.trim().equals("")) { 140 throw new UnWellFormedConstraintException( 141 "key name empty or null in " + "primary key"); 142 } 143 // if this is not the first key, we need add a comma 144 if (!firstKey) { 145 buffer.append(Constraint.COMMA); 146 } 147 buffer.append(keyName); 148 firstKey = false; 149 } 150 buffer.append(Constraint.RIGHTPARENTH); 151 buffer.append(Constraint.SPACESTRING); 152 sql = buffer.toString(); 153 log.debug("Primary key part in sql command is " + sql); 154 return sql; 155 156 } 157 158}