1   /*
2    * Copyright 2003 - 2013 The eFaps Team
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   * Revision:        $Rev$
17   * Last Changed:    $Date$
18   * Last Changed By: $Author$
19   */
20  
21  package org.efaps.db.databases.information;
22  
23  import java.io.Serializable;
24  import java.util.Map;
25  import java.util.TreeMap;
26  
27  import org.apache.commons.lang3.builder.ToStringBuilder;
28  
29  /**
30   * The class is used to store information about unique keys within SQL tables.
31   *
32   * @author The eFaps Team
33   * @version $Id$
34   */
35  public class UniqueKeyInformation
36      implements Serializable
37  {
38      /**
39       * Needed for serialization.
40       */
41      private static final long serialVersionUID = 1L;
42  
43      /**
44       * Name of the unique key in upper case.
45       */
46      private final String ukName;
47  
48      /**
49       * Comma separated string of column names in upper case for which this unique
50       * key is defined.
51       */
52      private final Map<Integer, String> columnNames = new TreeMap<Integer, String>();
53  
54      /**
55       * Constructor to initialize all instance variables.
56       *
57       * @param _ukName   name of unique key
58       */
59      protected UniqueKeyInformation(final String _ukName)
60      {
61          this.ukName = _ukName.toUpperCase();
62      }
63  
64      /**
65       * Append a new name of column for which this unique key is defined.
66       *
67       * @param _index    index within the unique key of the column name
68       * @param _colName  further column names which are defined in this unique
69       *                  key
70       */
71      protected void appendColumnName(final int _index,
72                                      final String _colName)
73      {
74          this.columnNames.put(_index, _colName);
75      }
76  
77      /**
78       * Getter method for instance variable {@link #columnNames}.
79       *
80       * @return value of instance variable columnNames
81       * @see #columnNames
82       */
83      public String getColumnNames()
84      {
85          return this.columnNames.toString();
86      }
87  
88      /**
89       * Getter method for the instance variable {@link #ukName}.
90       *
91       * @return value of instance variable {@link #ukName}
92       */
93      public String getUkName()
94      {
95          return this.ukName;
96      }
97  
98      /**
99       * Returns string representation of this class instance. The information
100      * includes {@link #ukName} and {@link #columnNames}.
101      *
102      * @return string representation of the unique key information
103      */
104     @Override
105     public String toString()
106     {
107         return ToStringBuilder.reflectionToString(this);
108     }
109 }