001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.lib.ddl.impl;
043:
044: import java.io.Serializable;
045: import java.util.HashMap;
046: import java.util.Map;
047:
048: import org.openide.util.NbBundle;
049:
050: import org.netbeans.lib.ddl.DDLException;
051: import org.netbeans.lib.ddl.util.CommandFormatter;
052:
053: /**
054: * Default implementation of database column. It handles name, column name, it's
055: * format and type. If used, column can handle referenced table and column.
056: * User can add custom properties into format.
057: */
058: public class AbstractTableColumn implements Serializable {
059: /** Name and column name. */
060: private String name, cname, format;
061:
062: /** Type, usually column, primary or foreign key */
063: private String otype;
064:
065: /** Additional properties */
066: private Map addprops;
067:
068: /** Referenced table */
069: String reftab;
070:
071: /** Referenced column */
072: String refcol;
073:
074: /** Is this a new column or an existing one */
075: boolean newColumn = false;
076:
077: /** Is this a column for an existing object or one that is being
078: * newly created?
079: */
080: boolean newObject = false;
081:
082: static final long serialVersionUID = -5128289937199572117L;
083:
084: /** Returns name of object */
085: public String getObjectName() {
086: return name;
087: }
088:
089: /** Sets name of object */
090: public void setObjectName(String oname) {
091: name = oname;
092: }
093:
094: /** Returns type of object */
095: public String getObjectType() {
096: return otype;
097: }
098:
099: /** Sets name of column */
100: public void setObjectType(String type) {
101: otype = type;
102: }
103:
104: /** Returns name of column */
105: public String getColumnName() {
106: return cname;
107: }
108:
109: /** Sets name of column */
110: public void setColumnName(String columnName) {
111: cname = columnName;
112: }
113:
114: /** Returns name of column */
115: public String getFormat() {
116: return format;
117: }
118:
119: /** Sets name of column */
120: public void setFormat(String fmt) {
121: format = fmt;
122: }
123:
124: /** Returns referenced table */
125: public String getReferencedTableName() {
126: return reftab;
127: }
128:
129: /** Sets referenced table */
130: public void setReferencedTableName(String table) {
131: reftab = table;
132: }
133:
134: /** Returns referenced column name */
135: public String getReferencedColumnName() {
136: return refcol;
137: }
138:
139: /** Sets referenced column name */
140: public void setReferencedColumnName(String col) {
141: refcol = col;
142: }
143:
144: public boolean isNewColumn() {
145: return newColumn;
146: }
147:
148: public void setNewColumn(boolean newColumn) {
149: this .newColumn = newColumn;
150: }
151:
152: public boolean isNewObject() {
153: return newObject;
154: }
155:
156: public void setNewObject(boolean newObject) {
157: this .newObject = newObject;
158: }
159:
160: /** Returns custom property identified by name */
161: public Object getProperty(String pname) {
162: return addprops.get(pname);
163: }
164:
165: /** Sets property identified by name */
166: public void setProperty(String pname, Object pval) {
167: if (addprops == null)
168: addprops = new HashMap();
169: addprops.put(pname, pval);
170: }
171:
172: /** Returns colum properties.
173: * It first copies all custom properties, then sets:
174: * object.name Name of the object
175: * column.name Name of the column
176: * These properties are required; an DDLException will throw if you
177: * forgot to set it up.
178: * fkobject.name Referenced object name
179: * fkcolumn.name Referenced column name
180: */
181: public Map getColumnProperties(AbstractCommand cmd)
182: throws DDLException {
183: HashMap args = new HashMap();
184: String oname = getObjectName();
185: String cname = getColumnName();
186:
187: if (addprops != null)
188: args.putAll(addprops);
189: if (oname != null)
190: args.put("object.name", newObject ? oname : cmd
191: .quote(oname)); // NOI18N
192: else
193: throw new DDLException(NbBundle.getBundle(
194: "org.netbeans.lib.ddl.resources.Bundle").getString(
195: "EXC_Unknown")); // NOI18N
196: if (cname != null)
197: args.put("column.name", newColumn ? cname : cmd
198: .quote(cname)); // NOI18N
199: else
200: throw new DDLException(NbBundle.getBundle(
201: "org.netbeans.lib.ddl.resources.Bundle").getString(
202: "EXC_Unknown")); // NOI18N
203:
204: if (reftab != null)
205: args.put("fkobject.name", cmd.quote(reftab)); // NOI18N
206: if (refcol != null)
207: args.put("fkcolumn.name", cmd.quote(refcol)); // NOI18N
208:
209: return args;
210: }
211:
212: /**
213: * Returns full string representation of column. This string needs no
214: * additional formatting. Throws DDLException if format is not specified
215: * or CommandFormatter can't format it (it uses MapFormat to process entire
216: * lines and can solve [] enclosed expressions as optional.
217: */
218: public String getCommand(AbstractCommand cmd) throws DDLException {
219: Map cprops;
220: if (format == null)
221: throw new DDLException(NbBundle.getBundle(
222: "org.netbeans.lib.ddl.resources.Bundle").getString(
223: "EXC_NoFormatSpec")); //NOI18N
224: try {
225: cprops = getColumnProperties(cmd);
226: return CommandFormatter.format(format, cprops);
227: } catch (Exception e) {
228: throw new DDLException(e.getMessage());
229: }
230: }
231:
232: /** Reads object from stream */
233: public void readObject(java.io.ObjectInputStream in)
234: throws java.io.IOException, ClassNotFoundException {
235: name = (String) in.readObject();
236: cname = (String) in.readObject();
237: format = (String) in.readObject();
238: otype = (String) in.readObject();
239: addprops = (Map) in.readObject();
240: reftab = (String) in.readObject();
241: refcol = (String) in.readObject();
242: }
243:
244: /** Writes object to stream */
245: public void writeObject(java.io.ObjectOutputStream out)
246: throws java.io.IOException {
247: out.writeObject(name);
248: out.writeObject(cname);
249: out.writeObject(format);
250: out.writeObject(otype);
251: out.writeObject(addprops);
252: out.writeObject(reftab);
253: out.writeObject(refcol);
254: }
255: }
|