View Javadoc

1   /* ==============================================================================
2    *   JDBCAdmin, data management software.
3    *   Copyright (C) 2005  Norsys S.A
4    *
5    *   This library is free software; you can redistribute it and/or
6    *   modify it under the terms of the GNU Lesser General Public
7    *   License as published by the Free Software Foundation; either
8    *   version 2.1 of the License, or (at your option) any later version.
9    *
10   *   This library is distributed in the hope that it will be useful,
11   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   *   Lesser General Public License for more details.
14   *
15   *   You should have received a copy of the GNU Lesser General Public
16   *   License along with this library; if not, write to the Free Software
17   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   * ==============================================================================
19   */
20  package jdbcadmin.core.data;
21  
22  import java.io.Serializable;
23  import java.util.ArrayList;
24  import java.util.Collections;
25  import java.util.HashMap;
26  import java.util.Iterator;
27  import java.util.List;
28  import java.util.Map;
29  import java.util.Set;
30  
31  import jdbcadmin.core.exceptions.TechnicalException;
32  import jdbcadmin.core.util.SQLTypeConvertor;
33  import jdbcadmin.core.util.StringUtil;
34  
35  /***
36   * Represente une table
37   */
38  public class Table implements Comparable, Serializable {
39  
40      ///////////////// Constantes ////////////////
41      /*** Minimum col size for display. */
42      private static final int DISPLAY_MINI_COL_SIZE = 4;
43      /*** Separator SCHEMA/TABLE. */
44      private static final String DOT = ".";
45  
46      ///////////////////// Attributs ////////////////////
47      /*** Columns of the table, key : column name. */
48      private Map colMap = new HashMap();
49      /*** Ordered columns of the table */
50      private List colLst = new ArrayList();
51      /*** Ordered column names of the table */
52      private List colNamesLst = new ArrayList();
53      /*** List of the primary key columns. */
54      private List pkColumns = new ArrayList();
55      /*** Table name. */
56      private String name;
57      /*** Table's schema. */
58      private Schema schema;
59      /*** Computed columns display sizes. */
60      private Map columnSizes;
61      /*** Is the table completely filled. */
62      private boolean filled = false;
63  
64  
65      //////////////////// Constructeurs /////////////////
66      /***
67       * Constructor
68       * @param aNom table name
69       * @param aSchema schema
70       */
71      public Table (String aNom, Schema aSchema) {
72          name = aNom;
73          schema = aSchema;
74      }
75  
76      //////////////////// java.lang.object Override /////
77      /*** {@inheritDoc} */
78      public String toString() {
79          Iterator colsIt = colLst.iterator();
80          StringBuffer buff = new StringBuffer();
81          buff.append("TABLE : ");
82          buff.append(name);
83          buff.append("\n");
84          while (colsIt.hasNext()) {
85              buff.append("\t");
86              buff.append(colsIt.next());
87              buff.append("\n");
88          }
89          return buff.toString();
90      }
91  
92  
93      //////////////////// Methodes publiques ////////////
94  
95      /***
96       * Adds a primarykey column
97       * @param aPKColName name of the pk column
98       * @throws IllegalArgumentException if there is no columns with this name in the table
99       */
100     public void addPKColumn(String aPKColName) throws IllegalArgumentException {
101         Column col = (Column) colMap.get(aPKColName);
102         if (col == null) {
103             throw new IllegalArgumentException ("The column |" + aPKColName
104                     + "| does not belong to the table |" + name + "|");
105         }
106         pkColumns.add(col);
107     }
108 
109     /***
110      * Adds a column
111      * @param aCol the column to had
112      */
113     public void addColumn (Column aCol) {
114         colMap.put(aCol.getName(), aCol);
115         colLst.add(aCol);
116         colNamesLst.add(aCol.getName());
117     }
118 
119 
120     /***
121      * Indicates whether or not the table contains the specified column
122      * @param aColName name of the searched column
123      * @return true if exists, false otherwise
124      */
125     public boolean containsColumn(String aColName) {
126         return (colMap.containsKey(aColName));
127     }
128 
129     /***
130      * Inidactes whether or not the specified column is part of the primary key
131      * @param aColName name of the searched column
132      * @return true if belongs to the PK, false otherwise
133      */
134     public boolean isPKColumn(String aColName) {
135         return pkColumns.contains(colMap.get(aColName));
136     }
137 
138     /***
139      * Iterate the columns.
140      * @return iterator on columns
141      */
142     public Iterator iterateColumns() {
143         return colLst.iterator();
144     }
145 
146     /***
147      * Iterate the column names.
148      * @return iterator on column names
149      */
150     public Iterator iterateColumnNames() {
151         return colNamesLst.iterator();
152     }
153 
154     /***
155      * Retrieves a Map associating column names with their display size.
156      * @return the map
157      */
158     public Map getColumnSizes() {
159         if (columnSizes == null) {
160 
161             columnSizes = new HashMap();
162 
163             // For each column, take the max between the column name size and the column's size
164             Iterator it = iterateColumns();
165             while (it.hasNext()) {
166                 Column col = (Column) it.next();
167                 int taille;
168                 if (col.getName().length() > col.getSize()) {
169                     taille = col.getName().length();
170                 } else {
171                     taille = col.getSize();
172                 }
173                 if (taille < DISPLAY_MINI_COL_SIZE) {
174                     taille = DISPLAY_MINI_COL_SIZE;
175                 } else {
176                     taille += 2;
177                 }
178                 columnSizes.put(col.getName(), new Integer(taille));
179             }
180         }
181         return columnSizes;
182     }
183 
184     /***
185      * Retrieves a String with column namles, ready to display.
186      * @return the header string
187      * @throws TechnicalException technical error
188      */
189     public String getHeaderString() throws TechnicalException {
190         StringBuffer buff = new StringBuffer();
191         Iterator it = iterateColumnNames();
192         while (it.hasNext()) {
193             String nomCol = (String) it.next();
194             StringUtil.append(buff, null, null, nomCol, ((Integer) getColumnSizes().get(nomCol)).intValue(), "|", ' ');
195         }
196         buff.append("\n");
197         it = iterateColumns();
198         while (it.hasNext()) {
199             Column col = (Column) it.next();
200             String sample = SQLTypeConvertor.getSampleForColumn(col);
201             StringUtil.append(buff, null, null, sample,
202                     ((Integer) getColumnSizes().get(col.getName())).intValue(), "|", ' ');
203         }
204         return buff.toString();
205     }
206 
207     /***
208      * @return the numner of columns
209      */
210     public int getColumnNumber() {
211         return colMap.size();
212     }
213 
214     /***
215      * Retrieves a column
216      * @param aNom column name
217      * @return the column
218      */
219     public Column getColumn(String aNom) {
220         return (Column) colMap.get(aNom);
221     }
222 
223     /***
224      * @return the column names list
225      */
226     public List getColumnNames() {
227         return colNamesLst;
228     }
229 
230     /***
231      * @return the colums list
232      */
233     public List getColMap() {
234         return colLst;
235     }
236 
237     /***
238      * Indicates whether or not the table has got a primary key
239      * @return true is has a primary kay, false otherwise
240      */
241     public boolean hasPrimaryKey() {
242         return pkColumns.size() > 0;
243     }
244 
245     /***
246      * @return the complete name (prefixed by the schema name)
247      */
248     public String getCompleteName() {
249         if (schema.getName() == null || schema.getName().trim().length() == 0) {
250             return name;
251         } else {
252             return schema.getName() + DOT + name;
253         }
254     }
255 
256     /***
257      * @throws IllegalArgumentException if <code>aObj</code> is not a Table
258      * {@inheritDoc}
259      */
260     public int compareTo(Object aObj) {
261         if (aObj instanceof Table) {
262             return name.compareTo(((Table) aObj).getName());
263         } else {
264             throw new IllegalArgumentException("Impossible to compare a table with an other object");
265         }
266     }
267 
268     //////////////////// Getters et setters /////////////
269 
270     /***
271      * @return Returns the filled.
272      */
273     public boolean isFilled() {
274         return filled;
275     }
276     /***
277      * @param aFilled The filled to set.
278      */
279     public void setFilled(boolean aFilled) {
280         filled = aFilled;
281     }
282     /***
283      * @return Returns the name.
284      */
285     public String getName() {
286         return name;
287     }
288 
289     /***
290      * @return Returns the pkColumns.
291      */
292     public List getPkColumns() {
293         return pkColumns;
294     }
295     /***
296      * @param aPkColumns The pkColumns to set.
297      */
298     public void setPkColumns(List aPkColumns) {
299         pkColumns = aPkColumns;
300     }
301     /***
302      * @return Returns the schema.
303      */
304     public Schema getSchema() {
305         return schema;
306     }
307 }