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 org.apache.openoffice.comp.sdbc.dbtools.util.ComposeRule;
25 import org.apache.openoffice.comp.sdbc.dbtools.util.DbTools;
26 
27 import com.sun.star.container.XNameAccess;
28 import com.sun.star.lang.XServiceInfo;
29 import com.sun.star.lib.uno.helper.ComponentBase;
30 import com.sun.star.sdbc.SQLException;
31 import com.sun.star.sdbc.XDatabaseMetaData;
32 import com.sun.star.sdbc.XRow;
33 import com.sun.star.sdbcx.XGroupsSupplier;
34 import com.sun.star.sdbcx.XTablesSupplier;
35 import com.sun.star.sdbcx.XUsersSupplier;
36 import com.sun.star.sdbcx.XViewsSupplier;
37 
38 /** Base expects the containers returned by X(Tables/Views/Groups/Users)Supplier
39  * to be the same throughout the lifetime of the catalog!!
40  */
41 public abstract class OCatalog extends ComponentBase
42         implements XTablesSupplier, XViewsSupplier, XUsersSupplier, XGroupsSupplier, XServiceInfo {
43 
44     private static final String[] services = {
45             "com.sun.star.sdbcx.DatabaseDefinition"
46     };
47 
48     // Deleted on destruction, weakly held by caller:
49     protected OContainer tables;
50     protected OContainer views;
51     protected OContainer groups;
52     protected OContainer users;
53     protected XDatabaseMetaData metadata;
54 
OCatalog(XDatabaseMetaData metadata)55     public OCatalog(XDatabaseMetaData metadata) {
56         this.metadata = metadata;
57     }
58 
59     @Override
postDisposing()60     protected synchronized void postDisposing() {
61         if (tables != null) {
62             tables.dispose();
63         }
64         if (views != null) {
65             views.dispose();
66         }
67         if (groups != null) {
68             groups.dispose();
69         }
70         if (users != null) {
71             users.dispose();
72         }
73     }
74 
75     // XServiceInfo
76 
77     @Override
getImplementationName()78     public String getImplementationName() {
79         return getClass().getName();
80     }
81 
82     @Override
getSupportedServiceNames()83     public String[] getSupportedServiceNames() {
84         return services.clone();
85     }
86 
87     @Override
supportsService(String serviceName)88     public boolean supportsService(String serviceName) {
89         for (String service : getSupportedServiceNames()) {
90             if (service.equals(serviceName)) {
91                 return true;
92             }
93         }
94         return false;
95     }
96 
97     // X(Tables/Views/Groups/Users)Supplier
98 
99     @Override
getTables()100     public synchronized XNameAccess getTables() {
101         checkDisposed();
102         if (tables == null) {
103             refreshTables();
104         }
105         return tables;
106     }
107 
108     @Override
getViews()109     public synchronized XNameAccess getViews() {
110         checkDisposed();
111         if (views == null) {
112             refreshViews();
113         }
114         return views;
115     }
116 
117     @Override
getGroups()118     public synchronized XNameAccess getGroups() {
119         checkDisposed();
120         if (groups == null) {
121             refreshGroups();
122         }
123         return groups;
124     }
125 
126     @Override
getUsers()127     public synchronized XNameAccess getUsers() {
128         checkDisposed();
129         if (users == null) {
130             refreshUsers();
131         }
132         return users;
133     }
134 
refreshObjects()135     public synchronized void refreshObjects() {
136         checkDisposed();
137         refreshTables();
138         refreshViews();
139         refreshGroups();
140         refreshUsers();
141     }
142 
143     /**
144      * Builds the name which should be used to access the object later on in the collection.
145      * Will only be called in fillNames.
146      * @param  row The current row from the resultset
147      */
buildName(XRow row)148     protected String buildName(XRow row) throws SQLException {
149         String catalog = row.getString(1);
150         if (row.wasNull()) {
151             catalog = "";
152         }
153         String schema = row.getString(2);
154         if (row.wasNull()) {
155             schema = "";
156         }
157         String table = row.getString(3);
158         if (row.wasNull()) {
159             table = "";
160         }
161         return DbTools.composeTableName(metadata, catalog, schema, table, false, ComposeRule.InDataManipulation);
162     }
163 
refreshTables()164     public abstract void refreshTables();
refreshViews()165     public abstract void refreshViews();
refreshGroups()166     public abstract void refreshGroups();
refreshUsers()167     public abstract void refreshUsers();
168 }
169