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 
23 package com.sun.star.wizards.db;
24 
25 import java.util.Vector;
26 
27 import com.sun.star.sdbc.SQLException;
28 import com.sun.star.sdbc.XResultSet;
29 import com.sun.star.sdbc.XRow;
30 import com.sun.star.uno.UnoRuntime;
31 import com.sun.star.wizards.common.JavaTools;
32 import com.sun.star.wizards.common.PropertyNames;
33 
34 /**
35  * @author bc93774
36  *
37  * TODO To change the template for this generated type comment go to
38  * Window - Preferences - Java - Code Style - Code Templates
39  */
40 public class RelationController extends CommandName
41 {
42 
43     private int PKTABLE_CAT = 1;
44     private int PKTABLE_SCHEM = 2;
45     private int PKTABLE_NAME = 3;
46     private int PKCOLUMN_NAME = 4;
47     private int FKTABLE_CAT = 5;
48     private int FKTABLE_SCHEM = 6;
49     private int FKTABLE_NAME = 7;
50     private int FKCOLUMN_NAME = 8;
51 
RelationController(CommandMetaData _CommandMetaData, String _CatalogName, String _SchemaName, String _TableName, boolean _baddQuotation)52     public RelationController(CommandMetaData _CommandMetaData, String _CatalogName, String _SchemaName, String _TableName, boolean _baddQuotation)
53     {
54         super(_CommandMetaData, _CatalogName, _SchemaName, _TableName, _baddQuotation);
55     }
56 
RelationController(CommandMetaData _CommandMetaData, String _DisplayName)57     public RelationController(CommandMetaData _CommandMetaData, String _DisplayName)
58     {
59         super(_CommandMetaData, _DisplayName);
60     }
61 
getExportedKeys()62     public String[] getExportedKeys()
63     {
64         String[] sReferencedTableNames = new String[]
65         {
66         };
67         try
68         {
69             String[] sTableNames = super.getCommandMetaData().getTableNames();
70             Vector aReferencedTableVector = new Vector();
71             XResultSet xResultSet = super.getCommandMetaData().xDBMetaData.getExportedKeys((getCatalogName(this)), getSchemaName(), getTableName());
72             XRow xRow = UnoRuntime.queryInterface(XRow.class, xResultSet);
73             while (xResultSet.next())
74             {
75                 String sForeignCatalog = xRow.getString(FKTABLE_CAT);
76                 String sForeignScheme = xRow.getString(FKTABLE_SCHEM);
77                 String sForeignTableName = xRow.getString(FKTABLE_NAME);
78                 CommandName oCommandName = new CommandName(getCommandMetaData(), sForeignCatalog, sForeignScheme, sForeignTableName, false);
79                 aReferencedTableVector.add(oCommandName.getComposedName());
80             }
81             sReferencedTableNames = new String[aReferencedTableVector.size()];
82             aReferencedTableVector.toArray(sReferencedTableNames);
83         }
84         catch (SQLException e)
85         {
86             e.printStackTrace(System.out);
87         }
88         return sReferencedTableNames;
89     }
90 
getCatalogName(CommandName _oCommandName)91     private Object getCatalogName(CommandName _oCommandName)
92     {
93         String sLocCatalog = _oCommandName.getCatalogName();
94         if (sLocCatalog.equals(PropertyNames.EMPTY_STRING))
95         {
96             return null;
97         }
98         else
99         {
100             return sLocCatalog;
101         }
102     }
103 
getImportedKeyColumns(String _sreferencedtablename)104     public String[][] getImportedKeyColumns(String _sreferencedtablename)
105     {
106         String[][] sKeyColumnNames = new String[][]
107         {
108         };
109         try
110         {
111             CommandName oLocCommandName = new CommandName(super.getCommandMetaData(), _sreferencedtablename);
112             XResultSet xResultSet = super.getCommandMetaData().xDBMetaData.getImportedKeys(getCatalogName(oLocCommandName), oLocCommandName.getSchemaName(), oLocCommandName.getTableName());
113             XRow xRow = UnoRuntime.queryInterface(XRow.class, xResultSet);
114             boolean bleaveLoop = false;
115             Vector aMasterFieldNamesVector = new Vector();
116             Vector aSlaveFieldNamesVector = new Vector();
117             while (xResultSet.next() && !bleaveLoop)
118             {
119                 String sPrimaryCatalog = null;
120                 String sPrimarySchema = null;
121                 if (super.getCommandMetaData().xDBMetaData.supportsCatalogsInDataManipulation())
122                 {
123                     sPrimaryCatalog = xRow.getString(PKTABLE_CAT);
124                 }
125                 if (super.getCommandMetaData().xDBMetaData.supportsSchemasInDataManipulation())
126                 {
127                     sPrimarySchema = xRow.getString(PKTABLE_SCHEM);
128                 }
129                 String sPrimaryTableName = xRow.getString(PKTABLE_NAME);
130                 String sPrimaryColumnName = xRow.getString(PKCOLUMN_NAME);
131                 String sForeignColumnName = xRow.getString(FKCOLUMN_NAME);
132                 if (JavaTools.isSame(getTableName(), sPrimaryTableName))
133                 {
134                     if (sPrimarySchema == null || JavaTools.isSame(getSchemaName(), sPrimarySchema))
135                     {
136                         if (JavaTools.isSame(getCatalogName(), sPrimaryCatalog))
137                         {
138                             aSlaveFieldNamesVector.add(sForeignColumnName);
139                             aMasterFieldNamesVector.add(sPrimaryColumnName);
140                             bleaveLoop = true;                  //Only one relation may exist between two tables...
141                         }
142                     }
143 
144                 }
145             }
146             sKeyColumnNames = new String[2][aMasterFieldNamesVector.size()];
147             sKeyColumnNames[0] = new String[aSlaveFieldNamesVector.size()];
148             sKeyColumnNames[1] = new String[aMasterFieldNamesVector.size()];
149             aSlaveFieldNamesVector.toArray(sKeyColumnNames[0]);
150             aMasterFieldNamesVector.toArray(sKeyColumnNames[1]);
151         }
152         catch (Exception e)
153         {
154             e.printStackTrace(System.out);
155         }
156         return sKeyColumnNames;
157     }
158 }
159