1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 package org.apache.openoffice.comp.sdbc.dbtools.sdbcx;
23 
24 import java.util.List;
25 
26 import org.apache.openoffice.comp.sdbc.dbtools.comphelper.CompHelper;
27 import org.apache.openoffice.comp.sdbc.dbtools.comphelper.PropertySetAdapter.PropertyGetter;
28 import org.apache.openoffice.comp.sdbc.dbtools.sdbcx.descriptors.SdbcxIndexDescriptor;
29 import org.apache.openoffice.comp.sdbc.dbtools.util.DbTools;
30 import org.apache.openoffice.comp.sdbc.dbtools.util.PropertyIds;
31 
32 import com.sun.star.beans.PropertyAttribute;
33 import com.sun.star.beans.XPropertySet;
34 import com.sun.star.container.ElementExistException;
35 import com.sun.star.container.XNameAccess;
36 import com.sun.star.lang.XServiceInfo;
37 import com.sun.star.sdbc.SQLException;
38 import com.sun.star.sdbcx.XColumnsSupplier;
39 import com.sun.star.sdbcx.XDataDescriptorFactory;
40 import com.sun.star.uno.Type;
41 
42 public class OIndex extends ODescriptor implements XColumnsSupplier, XDataDescriptorFactory, XServiceInfo {
43 
44     private static final String[] services = {
45             "com.sun.star.sdbcx.Index"
46     };
47 
48     protected String catalogName;
49     protected boolean isUnique;
50     protected boolean isPrimaryKeyIndex;
51     protected boolean isClustered;
52     private OTable table;
53     private OContainer columns;
54 
OIndex(String name, boolean isCaseSensitive, String catalogName, boolean isUnique, boolean isPrimaryKeyIndex, boolean isClustered, List<String> columnNames, OTable table)55     public OIndex(String name, boolean isCaseSensitive, String catalogName,
56             boolean isUnique, boolean isPrimaryKeyIndex, boolean isClustered, List<String> columnNames, OTable table) throws ElementExistException {
57         super(name, isCaseSensitive);
58         this.catalogName = catalogName;
59         this.isUnique = isUnique;
60         this.isPrimaryKeyIndex = isPrimaryKeyIndex;
61         this.isClustered = isClustered;
62         this.table = table;
63         columns = new OIndexColumnContainer(this, this, columnNames);
64         registerProperties();
65     }
66 
registerProperties()67     private void registerProperties() {
68         registerProperty(PropertyIds.CATALOG.name, PropertyIds.CATALOG.id, Type.STRING, (short)PropertyAttribute.READONLY,
69                 new PropertyGetter() {
70                     @Override
71                     public Object getValue() {
72                         return catalogName;
73                     }
74                 }, null);
75         registerProperty(PropertyIds.ISPRIMARYKEYINDEX.name, PropertyIds.ISPRIMARYKEYINDEX.id, Type.BOOLEAN, (short)PropertyAttribute.READONLY,
76                 new PropertyGetter() {
77                     @Override
78                     public Object getValue() {
79                         return isPrimaryKeyIndex;
80                     }
81                 }, null);
82         registerProperty(PropertyIds.ISCLUSTERED.name, PropertyIds.ISCLUSTERED.id, Type.BOOLEAN, (short)PropertyAttribute.READONLY,
83                 new PropertyGetter() {
84                     @Override
85                     public Object getValue() {
86                         return isClustered;
87                     }
88                 }, null);
89         registerProperty(PropertyIds.ISUNIQUE.name, PropertyIds.ISUNIQUE.id, Type.BOOLEAN, (short)PropertyAttribute.READONLY,
90                 new PropertyGetter() {
91                     @Override
92                     public Object getValue() {
93                         return isUnique;
94                     }
95                 }, null);
96     }
97 
98     // XServiceInfo
99 
getImplementationName()100     public String getImplementationName() {
101         return getClass().getName();
102     }
103 
104     @Override
getSupportedServiceNames()105     public String[] getSupportedServiceNames() {
106         return services.clone();
107     }
108 
109     @Override
supportsService(String serviceName)110     public boolean supportsService(String serviceName) {
111         for (String service : getSupportedServiceNames()) {
112             if (service.equals(serviceName)) {
113                 return true;
114             }
115         }
116         return false;
117     }
118 
119     // XDataDescriptorFactory
120 
121     @Override
createDataDescriptor()122     public XPropertySet createDataDescriptor() {
123         SdbcxIndexDescriptor descriptor = new SdbcxIndexDescriptor(isCaseSensitive());
124         CompHelper.copyProperties(this, descriptor);
125         try {
126             DbTools.cloneDescriptorColumns(this, descriptor);
127         } catch (SQLException sqlException) {
128         }
129         return descriptor;
130     }
131 
132     @Override
getColumns()133     public XNameAccess getColumns() {
134         return columns;
135     }
136 
getTable()137     public OTable getTable() {
138         return table;
139     }
140 
141     @Override
toString()142     public String toString() {
143         return "OIndex [catalogName=" + catalogName + ", isUnique=" + isUnique + ", isPrimaryKeyIndex=" + isPrimaryKeyIndex + ", isClustered=" + isClustered
144                 + ", name=" + getName() + "]";
145     }
146 }
147