KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > dbschema > jdbcimpl > IndexElementImpl


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.dbschema.jdbcimpl;
21
22 import java.sql.*;
23 import java.util.*;
24
25 import org.netbeans.modules.dbschema.*;
26 import org.netbeans.modules.dbschema.util.*;
27
28 public class IndexElementImpl extends DBMemberElementImpl implements IndexElement.Impl {
29
30     private DBElementsCollection columns;
31     private TableElementImpl tei;
32     private boolean _unique;
33
34     /** Creates new IndexElementImpl */
35     public IndexElementImpl() {
36        this(null, null, false);
37     }
38
39     public IndexElementImpl(TableElementImpl tei, String JavaDoc name, boolean unique) {
40         super(name);
41         columns = new DBElementsCollection(tei, new ColumnElement[0]);
42         
43         //workaround for bug #4396371
44
//http://andorra.eng:8080/cgi-bin/ws.exe/bugtraq/bug.hts?where=bugid_value%3D4396371
45
Object JavaDoc hc = String.valueOf(columns.hashCode());
46         while (DBElementsCollection.instances.contains(hc)) {
47             columns = new DBElementsCollection(tei, new ColumnElement[0]);
48             hc = String.valueOf(columns.hashCode());
49         }
50         DBElementsCollection.instances.add(hc);
51
52         this.tei = tei;
53         _unique = unique;
54     }
55   
56     /** Get the unique flag of the index.
57      * @return true if it is a unique index, false otherwise
58      */

59     public boolean isUnique() {
60         return _unique;
61     }
62   
63     /** Set the unique flag of the index.
64      * @param unique the flag
65      * @throws DBException if impossible
66      */

67     public void setUnique(boolean unique) throws DBException {
68         _unique = unique;
69     }
70   
71     /** Change the set of columns.
72      * @param elems the columns to change
73      * @param action one of {@link #ADD}, {@link #REMOVE}, or {@link #SET}
74      * @exception DBException if the action cannot be handled
75      */

76     public void changeColumns(ColumnElement[] elems,int action) throws DBException {
77         columns.changeElements(elems, action);
78     }
79   
80     /** Get all columns.
81      * @return the columns
82      */

83     public ColumnElement[] getColumns() {
84         DBElement[] dbe = columns.getElements();
85         return (ColumnElement[]) Arrays.asList(dbe).toArray(new ColumnElement[dbe.length]);
86     }
87   
88     /** Find a column by name.
89      * @param name the name for which to look
90      * @return the column, or <code>null</code> if it does not exist
91      */

92     public ColumnElement getColumn(DBIdentifier name) {
93         return (ColumnElement) columns.find(name);
94     }
95   
96     protected void initColumns(LinkedList idxs) {
97         LinkedList columnsList = new LinkedList();
98         String JavaDoc name, info;
99         int start, end;
100         
101         try {
102             for (int i = 0; i < idxs.size(); i++) {
103                 info = idxs.get(i).toString();
104                 start = info.indexOf('.');
105                 end = info.lastIndexOf('.');
106
107                 name = info.substring(0, start);
108                 if (name.equals(this.getName().getName()))
109                     columnsList.add(info.substring(start + 1, end));
110             }
111
112             for (int i = 0; i < columnsList.size(); i++) {
113                 ColumnElement c = ((IndexElement) element).getDeclaringTable().getColumn(DBIdentifier.create(columnsList.get(i).toString()));
114                 if (c != null)
115                     changeColumns(new ColumnElement[] {c}, DBElement.Impl.ADD);
116             }
117         } catch (Exception JavaDoc exc) {
118             exc.printStackTrace();
119         }
120     }
121     
122     /** Returns the table collection of this schema element. This method
123      * should only be used internally and for cloning and archiving.
124      * @return the table collection of this schema element
125      */

126     public DBElementsCollection getColumnCollection () {
127         return columns;
128     }
129
130     /** Set the table collection of this claschemass element to the supplied
131      * collection. This method should only be used internally and for
132      * cloning and archiving.
133      * @param collection the table collection of this schema element
134      */

135     public void setColumnCollection (DBElementsCollection collection) {
136         columns = collection;
137     }
138
139 }
140
Popular Tags