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 com.fatwire.gst.foundation.facade.sql.table;
18
19 import java.util.Collections;
20 import java.util.LinkedList;
21 import java.util.List;
22
23 import com.fatwire.gst.foundation.facade.sql.table.TableColumn.Type;
24
25 /**
26 * The definition of a ContentServer database table
27 *
28 * @author Dolf Dijkstra
29 *
30 */
31
32 public class TableDef {
33 private final String name;
34
35 private final String acl;
36
37 private final String type;
38
39 private final List<TableColumn> columns = new LinkedList<TableColumn>();
40
41 /**
42 * @param name the name of the table
43 * @param acl the acl for the table
44 * @param type the type of the table
45 */
46 public TableDef(final String name, final String acl, final String type) {
47 super();
48 this.name = name;
49 this.acl = acl;
50 this.type = type;
51 }
52
53 /**
54 * @param col the column to add
55 * @return the current TableDef, this.
56 */
57 public TableDef addColumn(TableColumn col) {
58 if (col.isPrimary()) {
59 for (TableColumn current : columns) {
60 if (current.isPrimary()) {
61 throw new IllegalStateException("Table has already have a primary column");
62 }
63 }
64 }
65 this.columns.add(col);
66 return this;
67 }
68
69 /**
70 * Adds a column to this table.
71 * @param name the name of the column
72 * @param type the type of the column
73 * @param 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 * @param name
92 * @param type
93 * @return the added TableColumn.
94 */
95 public TableColumn addColumn(final String name, final Type type) {
96
97 return addColumn(name, type, false);
98 }
99
100 public Iterable<TableColumn> getColumns() {
101 return Collections.unmodifiableCollection(columns);
102 }
103
104 /**
105 * @return the acl value
106 */
107 public String getAcl() {
108 return acl;
109 }
110
111 /**
112 * @return the name value
113 */
114 public String getName() {
115 return name;
116 }
117
118 /**
119 * @return the type value
120 */
121 public String getType() {
122 return type;
123 }
124
125 }