001/*
002 * Copyright (c) 2003-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 java.util.Vector;
033
034/**
035 * Simple Implementation of the DSTableIFace<br>
036 * A Table schema has a list of field schemas. It also has a notion of a primary
037 * and secondary key. When the keys are added they are also added as fields,
038 * meaning if the user calls getPrimaryKey, the object that is returned will
039 * also be in the Vector of fields.
040 */
041public class DSTableDef implements DSTableIFace {
042        protected String mName = "";
043        protected Vector mFields = new Vector();
044
045        protected DSTableKeyDef mPrimaryKey = new DSTableKeyDef();
046        protected Vector mSecondaryKeys = new Vector();
047
048        /**
049         * Construct a DSTableDef
050         * 
051         * @param name
052         *            name of the table
053         */
054        public DSTableDef(String aName) {
055                mName = aName;
056                mPrimaryKey.setTable(this);
057        }
058
059        /**
060         * Returns a Vector of all the fields in a table
061         * 
062         * @return Returns the mFields.
063         */
064        public Vector getFields() {
065                return mFields;
066        }
067
068        /**
069         * Returns a DSTableKeyDef Object representing the Primary Key
070         * 
071         * @return Returns a Vector of DSTableFieldDef objects
072         */
073        public DSTableKeyIFace getPrimaryKey() {
074                return mPrimaryKey;
075        }
076
077        /**
078         * Returns a Vector of DSTableFieldDef Objects where each object is a
079         * secondary Key
080         * 
081         * @return Returns a Vector of DSTableFieldDef objects
082         */
083        public Vector getSecondaryKeys() {
084                return mSecondaryKeys;
085        }
086
087        /**
088         * Gets the name of table
089         * 
090         * @return Returns the mName.
091         */
092        public String getName() {
093                return mName;
094        }
095
096        /**
097         * Gets the name of mapped table
098         * 
099         * @return Returns the mName.
100         */
101        public String getMappedName() {
102                return mName;
103        }
104
105        /**
106         * Sets the nameof the table
107         * 
108         * @param aName
109         *            The name to set.
110         */
111        public void setName(String aName) {
112                mName = aName;
113        }
114
115        /**
116         * Adds a field to the table
117         * 
118         * @param aName
119         *            name of field
120         * @param aDataType
121         *            data type of field as defined by the DataType class
122         * @return returns the new field object
123         */
124        public DSTableFieldDef addField(String aName, String aDataType,
125                        Vector aMissingValue) {
126                DSTableFieldDef fieldDef = new DSTableFieldDef(Integer.toString(mFields
127                                .size()), aName, aDataType, aMissingValue);
128                mFields.add(fieldDef);
129                fieldDef.setTable(this);
130                return fieldDef;
131        }
132
133        /**
134         * Adds a Primary Key to the table
135         * 
136         * @param aName
137         *            name of key
138         * @param aDataType
139         *            data type of field as defined by the DataType class
140         * @return the new primary key object (as a field)
141         */
142        public DSTableFieldDef addPrimaryKey(String aName, String aDataType,
143                        Vector aMissingValue) {
144                DSTableFieldDef fieldDef = new DSTableFieldDef(Integer.toString(mFields
145                                .size()), aName, aDataType, DSTableKeyIFace.PRIMARYKEY,
146                                aMissingValue);
147                mPrimaryKey.add(fieldDef);
148                mFields.add(fieldDef);
149                fieldDef.setTable(this);
150                return fieldDef;
151        }
152
153        /**
154         * Adds a secondary key to the table
155         * 
156         * @param aName
157         *            name of key
158         * @param aDataType
159         *            data type of field as defined by the DataType class
160         * @return the new secondary key object (as a field)
161         */
162        public DSTableFieldDef addSecondaryKey(String aName, String aDataType,
163                        Vector aMissingValue) {
164                DSTableFieldDef fieldDef = new DSTableFieldDef(Integer.toString(mFields
165                                .size()), aName, aDataType, DSTableKeyIFace.SECONDARYKEY,
166                                aMissingValue);
167                mSecondaryKeys.add(fieldDef);
168                mFields.add(fieldDef);
169                fieldDef.setTable(this);
170                return fieldDef;
171        }
172
173        /**
174         * Returns name of table as a string
175         */
176        public String toString() {
177                return mName;
178        }
179}