View Javadoc
1   /*
2    * Copyright 2008 FatWire Corporation. All Rights Reserved.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package tools.gsf.facade.sql.table;
18  
19  import tools.gsf.facade.sql.table.TableColumn.Type;
20  
21  import java.util.Collections;
22  import java.util.LinkedList;
23  import java.util.List;
24  
25  /**
26   * The definition of a ContentServer database table
27   *
28   * @author Dolf Dijkstra
29   */
30  
31  public class TableDef {
32      private final String name;
33  
34      private final String acl;
35  
36      private final String type;
37  
38      private final List<TableColumn> columns = new LinkedList<TableColumn>();
39  
40      /**
41       * @param name the name of the table
42       * @param acl  the acl for the table
43       * @param type the type of the table
44       */
45      public TableDef(final String name, final String acl, final String type) {
46          super();
47          this.name = name;
48          this.acl = acl;
49          this.type = type;
50      }
51  
52      /**
53       * @param col the column to add
54       * @return the current TableDef, this.
55       */
56      public TableDef addColumn(TableColumn col) {
57          if (col.isPrimary()) {
58              for (TableColumn current : columns) {
59                  if (current.isPrimary()) {
60                      throw new IllegalStateException("Table has already have a primary column");
61                  }
62              }
63          }
64          this.columns.add(col);
65          return this;
66      }
67  
68      /**
69       * Adds a column to this table.
70       *
71       * @param name    the name of the column
72       * @param type    the type of the column
73       * @param primary boolean flag for primary
74       * @return the added TableColumn.
75       */
76      public TableColumn addColumn(final String name, final Type type, final boolean primary) {
77          TableColumn col = new TableColumn(name, type, primary);
78          if (col.isPrimary()) {
79              for (TableColumn current : columns) {
80                  if (current.isPrimary()) {
81                      throw new IllegalStateException("Table has already have a primary column");
82                  }
83              }
84          }
85          this.columns.add(col);
86          return col;
87      }
88  
89      /**
90       * Adds a non primary column to this table.
91       *
92       * @param name column name
93       * @param type column type
94       * @return the added TableColumn.
95       */
96      public TableColumn addColumn(final String name, final Type type) {
97  
98          return addColumn(name, type, false);
99      }
100 
101     public Iterable<TableColumn> getColumns() {
102         return Collections.unmodifiableCollection(columns);
103     }
104 
105     /**
106      * @return the acl value
107      */
108     public String getAcl() {
109         return acl;
110     }
111 
112     /**
113      * @return the name value
114      */
115     public String getName() {
116         return name;
117     }
118 
119     /**
120      * @return the type value
121      */
122     public String getType() {
123         return type;
124     }
125 
126 }