KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > ddl > impl > AbstractTableColumn


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.lib.ddl.impl;
21
22 import java.io.Serializable JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import org.openide.util.NbBundle;
27
28 import org.netbeans.lib.ddl.DDLException;
29 import org.netbeans.lib.ddl.util.CommandFormatter;
30
31 /**
32 * Default implementation of database column. It handles name, column name, it's
33 * format and type. If used, column can handle referenced table and column.
34 * User can add custom properties into format.
35 */

36 public class AbstractTableColumn implements Serializable JavaDoc {
37     /** Name and column name. */
38     private String JavaDoc name, cname, format;
39
40     /** Type, usually column, primary or foreign key */
41     private String JavaDoc otype;
42
43     /** Additional properties */
44     private Map JavaDoc addprops;
45
46     /** Referenced table */
47     String JavaDoc reftab;
48
49     /** Referenced column */
50     String JavaDoc refcol;
51
52     static final long serialVersionUID =-5128289937199572117L;
53     /** Returns name of object */
54     public String JavaDoc getObjectName()
55     {
56         return name;
57     }
58
59     /** Sets name of object */
60     public void setObjectName(String JavaDoc oname)
61     {
62         name = oname;
63     }
64
65     /** Returns type of object */
66     public String JavaDoc getObjectType()
67     {
68         return otype;
69     }
70
71     /** Sets name of column */
72     public void setObjectType(String JavaDoc type)
73     {
74         otype = type;
75     }
76
77     /** Returns name of column */
78     public String JavaDoc getColumnName()
79     {
80         return cname;
81     }
82
83     /** Sets name of column */
84     public void setColumnName(String JavaDoc columnName)
85     {
86         cname = columnName;
87     }
88
89     /** Returns name of column */
90     public String JavaDoc getFormat()
91     {
92         return format;
93     }
94
95     /** Sets name of column */
96     public void setFormat(String JavaDoc fmt)
97     {
98         format = fmt;
99     }
100
101     /** Returns referenced table */
102     public String JavaDoc getReferencedTableName()
103     {
104         return reftab;
105     }
106
107     /** Sets referenced table */
108     public void setReferencedTableName(String JavaDoc table)
109     {
110         reftab = table;
111     }
112
113     /** Returns referenced column name */
114     public String JavaDoc getReferencedColumnName()
115     {
116         return refcol;
117     }
118
119     /** Sets referenced column name */
120     public void setReferencedColumnName(String JavaDoc col)
121     {
122         refcol = col;
123     }
124
125     /** Returns custom property identified by name */
126     public Object JavaDoc getProperty(String JavaDoc pname)
127     {
128         return addprops.get(pname);
129     }
130
131     /** Sets property identified by name */
132     public void setProperty(String JavaDoc pname, Object JavaDoc pval)
133     {
134         if (addprops == null) addprops = new HashMap JavaDoc();
135         addprops.put(pname, pval);
136     }
137
138     /** Returns colum properties.
139     * It first copies all custom properties, then sets:
140     * object.name Name of the object
141     * column.name Name of the column
142     * These properties are required; an DDLException will throw if you
143     * forgot to set it up.
144     * fkobject.name Referenced object name
145     * fkcolumn.name Referenced column name
146     */

147     public Map JavaDoc getColumnProperties(AbstractCommand cmd)
148     throws DDLException
149     {
150         HashMap JavaDoc args = new HashMap JavaDoc();
151         String JavaDoc oname = getObjectName();
152         String JavaDoc cname = getColumnName();
153
154         if (addprops != null) args.putAll(addprops);
155         if (oname != null) args.put("object.name", cmd.quote(oname)); // NOI18N
156
else throw new DDLException(NbBundle.getBundle("org.netbeans.lib.ddl.resources.Bundle").getString("EXC_Unknown")); // NOI18N
157
if (cname != null) args.put("column.name", cmd.quote(cname)); // NOI18N
158
else throw new DDLException(NbBundle.getBundle("org.netbeans.lib.ddl.resources.Bundle").getString("EXC_Unknown")); // NOI18N
159

160         if (reftab != null) args.put("fkobject.name", cmd.quote(reftab)); // NOI18N
161
if (refcol != null) args.put("fkcolumn.name", cmd.quote(refcol)); // NOI18N
162

163         return args;
164     }
165
166     /**
167     * Returns full string representation of column. This string needs no
168     * additional formatting. Throws DDLException if format is not specified
169     * or CommandFormatter can't format it (it uses MapFormat to process entire
170     * lines and can solve [] enclosed expressions as optional.
171     */

172     public String JavaDoc getCommand(AbstractCommand cmd)
173     throws DDLException
174     {
175         Map JavaDoc cprops;
176         if (format == null) throw new DDLException(NbBundle.getBundle("org.netbeans.lib.ddl.resources.Bundle").getString("EXC_NoFormatSpec")); //NOI18N
177
try {
178             cprops = getColumnProperties(cmd);
179             return CommandFormatter.format(format, cprops);
180         } catch (Exception JavaDoc e) {
181             throw new DDLException(e.getMessage());
182         }
183     }
184
185     /** Reads object from stream */
186     public void readObject(java.io.ObjectInputStream JavaDoc in)
187     throws java.io.IOException JavaDoc, ClassNotFoundException JavaDoc
188     {
189         name = (String JavaDoc)in.readObject();
190         cname = (String JavaDoc)in.readObject();
191         format = (String JavaDoc)in.readObject();
192         otype = (String JavaDoc)in.readObject();
193         addprops = (Map JavaDoc)in.readObject();
194         reftab = (String JavaDoc)in.readObject();
195         refcol = (String JavaDoc)in.readObject();
196     }
197
198     /** Writes object to stream */
199     public void writeObject(java.io.ObjectOutputStream JavaDoc out)
200     throws java.io.IOException JavaDoc
201     {
202         out.writeObject(name);
203         out.writeObject(cname);
204         out.writeObject(format);
205         out.writeObject(otype);
206         out.writeObject(addprops);
207         out.writeObject(reftab);
208         out.writeObject(refcol);
209     }
210 }
211
Popular Tags