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.HashMap;
24  import java.util.Iterator;
25  import java.util.Map;
26  
27  import jdbcadmin.core.exceptions.TechnicalException;
28  import jdbcadmin.core.util.StringUtil;
29  
30  /***
31   * Represents a table line.
32   */
33  public class Line implements Serializable{
34  
35  
36      //////////////// Attributs //////////////////
37  
38      /*** Line values : Map column name/value as String. */
39      private Map values;
40  
41      /*** Line's table */
42      private Table table;
43  
44      //////////////// Constructeurs //////////////////
45  
46      /***
47       * Constructor which specifies the line's table.
48       * @param aTable the line's table
49       */
50      public Line (Table aTable) {
51          table = aTable;
52          // The map is initialized with null values for all tables's columns
53          values = new HashMap(table.getColumnNumber());
54          Iterator it = table.iterateColumnNames();
55          while (it.hasNext() ) {
56              values.put(it.next(), null);
57          }
58      }
59  
60      //////////////// Methodes publiques  //////////////////
61  
62      /***
63       * Adds a value
64       * @param aNomColonne column name
65       * @param aVal value
66       * @throws TechnicalException Technical error
67       */
68      public void addValue (String aNomColonne, String aVal) throws TechnicalException {
69  
70          // We test whether the column exists in the table
71          if (!table.containsColumn(aNomColonne)) {
72              throw new TechnicalException("La table " + table.getName()
73                      + " ne contient pas de colonne nommée |" + aNomColonne + "|");
74          }
75  
76          // Put the value in the map
77          values.put(aNomColonne, aVal);
78      }
79  
80      /***
81       * Retrieves a value for athe specified columns
82       * @param aColName column name
83       * @return the value
84       */
85      public String getValue(String aColName) {
86          return (String) values.get(aColName);
87      }
88  
89      /***
90       * Retrieves a Map containing values, the key of the map is the column name.
91       * @return the values
92       */
93      public Map getValues() {
94          return values;
95      }
96  
97      //////////////// surcharge  java.lang.Object //////////////////
98  
99      /***
100      * Represente la ligne sous forme de String
101      * {@inheritDoc}
102      * */
103     public String toString() {
104         StringBuffer buff = new StringBuffer();
105         Iterator it = table.iterateColumnNames();
106         while (it.hasNext()) {
107             String nomCol = (String) it.next();
108             String val    = (String) values.get(nomCol);
109             if (val != null) {
110                 val = "<" + val + ">";
111             }
112             StringUtil.append(buff, null, null, val,
113                     ((Integer) table.getColumnSizes().get(nomCol)).intValue(), "|", ' ');
114         }
115         return buff.toString();
116     }
117     /***
118      * Returns the table.
119      * @return Table
120      */
121     public Table getTable() {
122         return table;
123     }
124 
125 }