View Javadoc

1   /*
2    * Copyright 2006 Outsource Cafe, Inc.
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  package org.javagen.agile.db.model;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import org.javagen.agile.core.model.AbstractModel;
22  import org.javagen.agile.db.context.Keys;
23  
24  /***
25   * Unique constraints on individual columns or groups of columns within a table.
26   *
27   * TODO UniqueConstraint needs to be added to the Table child nodes
28   * 
29   * @author Richard Easterling
30   */
31  //@XmlRootElement(name="uniqueConstraint")
32  public class UniqueConstraint extends AbstractModel {
33      
34      public static final String DEFAULT_MODEL_TYPE = "UNIQUE";
35  
36      private List<Column> uniqueColumns;
37      
38      public UniqueConstraint() {
39          super();
40          this.setModelType(DEFAULT_MODEL_TYPE);
41      }
42      
43      public void addColumn(Column column) {
44          if (uniqueColumns==null)
45              uniqueColumns = new ArrayList<Column>();
46          uniqueColumns.add(column);
47      }
48      
49      public void setColumns(List<Column> uniqueColumns) {
50          this.uniqueColumns = uniqueColumns;
51      }
52      
53      //@XmlIDREF
54      //@XmlElement(name="uniqueColumn")
55      public List<Column> getColumns() {
56          return uniqueColumns;
57      }
58      
59      public int columnReferencesSize() {
60          return (uniqueColumns!=null) ? uniqueColumns.size() : 0;
61      }
62      
63      public Column lookupColumn(String columnName) {
64          if (uniqueColumns!=null)
65              for(Column column : uniqueColumns) {
66                  if (column.getName().equalsIgnoreCase(columnName))
67                      return column;
68              }
69          return null;
70      }
71      
72      /***
73       * add UniqueConstraint references to effected column contexts for easy reference
74       */
75      @SuppressWarnings("unchecked")
76      public void addConstraintToColumnContext() {
77          if (uniqueColumns!=null)
78              for(Column column : uniqueColumns) {
79                  List<UniqueConstraint> constraints = (List<UniqueConstraint>)column.get(Keys.UNIQUE.toString());
80                  if (constraints==null) {
81                      constraints = new ArrayList<UniqueConstraint>();
82                      column.put(Keys.UNIQUE.toString(), constraints);
83                  }
84                  constraints.add(this);
85              }
86      }
87  
88  }