View Javadoc

1   /*
2    * Copyright (C) 2004 TiongHiang Lee
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this library; if not,  write to the Free Software
16   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17   *
18   * Email: thlee@onemindsoft.org
19   */
20  package org.onemind.commons.java.sql;
21  
22  import java.util.*;
23  /***
24   * A MetaData contains metadata about a database table
25   * @author TiongHiang Lee (thlee@onemindsoft.org)
26   * @version $Id: MetaData.java,v 1.2 2004/08/26 12:33:18 thlee Exp $ $Name:  $
27   */
28  public class MetaData
29  {
30  
31      /*** the fields * */
32      private Map _fields = new LinkedHashMap();
33  
34      /*** the name * */
35      private String _name;
36  
37      /*** the primary key field name * */
38      private String _idFieldName;
39  
40      /***
41       * create a MetaData with id <i>id </i>
42       * @param name the name
43       */
44      public MetaData(String name)
45      {
46          this._name = name;
47      }
48  
49      /***
50       * create a MetaData with id <i>id </i> and primaryke <i>pj </i>
51       * @param name the name
52       * @param idFieldName the unique identifier field name
53       */
54      public MetaData(String name, String idFieldName)
55      {
56          this(name);
57          _idFieldName = idFieldName;
58      }
59  
60      /***
61       * add a new field
62       * @param field the field
63       */
64      public void addField(Field field)
65      {
66          _fields.put(field.getName(), field);
67      }
68  
69      /***
70       * get the field with id <i>name </i>
71       * @param name the name
72       * @return the field, or null
73       */
74      public Field getField(String name)
75      {
76          return (Field) _fields.get(name);
77      }
78  
79      /***
80       * get the fields in this MetaData
81       * @return Map the map
82       */
83      public Map getFields()
84      {
85          return Collections.unmodifiableMap(_fields);
86      }
87  
88      /***
89       * return the id of the MetaData
90       * @return the id
91       */
92      public String getId()
93      {
94          return _name;
95      }
96  
97      /***
98       * return the primary key field
99       * @return the id field
100      */
101     public Field getIdField()
102     {
103         return (Field) _fields.get(getIdField());
104     }
105 
106     /***
107      * Return the id field name
108      * @return the id field name
109      */
110     public String getIdFieldName()
111     {
112         return _idFieldName;
113     }
114 
115     /***
116      * return whether there's a field with id <i>name </i>
117      * @param name the field name
118      * @return true if has the field
119      */
120     public boolean hasField(String name)
121     {
122         return _fields.containsKey(name);
123     }
124 
125     /***
126      * set the fields of the MetaData
127      * @param fields the fields
128      */
129     public void setFields(Map fields)
130     {
131         _fields.clear();
132         _fields.putAll(fields);
133     }
134 
135     /***
136      * Set the id field name
137      * @param string the id field name
138      */
139     public void setIdFieldName(String string)
140     {
141         _idFieldName = string;
142     }
143 }