1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 package com.sun.star.wizards.db;
28 
29 import java.util.Vector;
30 
31 import com.sun.star.sdbc.SQLException;
32 import com.sun.star.sdbc.XResultSet;
33 import com.sun.star.sdbc.XRow;
34 import com.sun.star.uno.UnoRuntime;
35 import com.sun.star.wizards.common.JavaTools;
36 import com.sun.star.wizards.common.PropertyNames;
37 
38 /**
39  * @author bc93774
40  *
41  * TODO To change the template for this generated type comment go to
42  * Window - Preferences - Java - Code Style - Code Templates
43  */
44 public class RelationController extends CommandName
45 {
46 
47     private int PKTABLE_CAT = 1;
48     private int PKTABLE_SCHEM = 2;
49     private int PKTABLE_NAME = 3;
50     private int PKCOLUMN_NAME = 4;
51     private int FKTABLE_CAT = 5;
52     private int FKTABLE_SCHEM = 6;
53     private int FKTABLE_NAME = 7;
54     private int FKCOLUMN_NAME = 8;
55 
56     public RelationController(CommandMetaData _CommandMetaData, String _CatalogName, String _SchemaName, String _TableName, boolean _baddQuotation)
57     {
58         super(_CommandMetaData, _CatalogName, _SchemaName, _TableName, _baddQuotation);
59     }
60 
61     public RelationController(CommandMetaData _CommandMetaData, String _DisplayName)
62     {
63         super(_CommandMetaData, _DisplayName);
64     }
65 
66     public String[] getExportedKeys()
67     {
68         String[] sReferencedTableNames = new String[]
69         {
70         };
71         try
72         {
73             String[] sTableNames = super.getCommandMetaData().getTableNames();
74             Vector aReferencedTableVector = new Vector();
75             XResultSet xResultSet = super.getCommandMetaData().xDBMetaData.getExportedKeys((getCatalogName(this)), getSchemaName(), getTableName());
76             XRow xRow = UnoRuntime.queryInterface(XRow.class, xResultSet);
77             while (xResultSet.next())
78             {
79                 String sForeignCatalog = xRow.getString(FKTABLE_CAT);
80                 String sForeignScheme = xRow.getString(FKTABLE_SCHEM);
81                 String sForeignTableName = xRow.getString(FKTABLE_NAME);
82                 CommandName oCommandName = new CommandName(getCommandMetaData(), sForeignCatalog, sForeignScheme, sForeignTableName, false);
83                 aReferencedTableVector.add(oCommandName.getComposedName());
84             }
85             sReferencedTableNames = new String[aReferencedTableVector.size()];
86             aReferencedTableVector.toArray(sReferencedTableNames);
87         }
88         catch (SQLException e)
89         {
90             e.printStackTrace(System.out);
91         }
92         return sReferencedTableNames;
93     }
94 
95     private Object getCatalogName(CommandName _oCommandName)
96     {
97         String sLocCatalog = _oCommandName.getCatalogName();
98         if (sLocCatalog.equals(PropertyNames.EMPTY_STRING))
99         {
100             return null;
101         }
102         else
103         {
104             return sLocCatalog;
105         }
106     }
107 
108     public String[][] getImportedKeyColumns(String _sreferencedtablename)
109     {
110         String[][] sKeyColumnNames = new String[][]
111         {
112         };
113         try
114         {
115             CommandName oLocCommandName = new CommandName(super.getCommandMetaData(), _sreferencedtablename);
116             XResultSet xResultSet = super.getCommandMetaData().xDBMetaData.getImportedKeys(getCatalogName(oLocCommandName), oLocCommandName.getSchemaName(), oLocCommandName.getTableName());
117             XRow xRow = UnoRuntime.queryInterface(XRow.class, xResultSet);
118             boolean bleaveLoop = false;
119             Vector aMasterFieldNamesVector = new Vector();
120             Vector aSlaveFieldNamesVector = new Vector();
121             while (xResultSet.next() && !bleaveLoop)
122             {
123                 String sPrimaryCatalog = null;
124                 String sPrimarySchema = null;
125                 if (super.getCommandMetaData().xDBMetaData.supportsCatalogsInDataManipulation())
126                 {
127                     sPrimaryCatalog = xRow.getString(PKTABLE_CAT);
128                 }
129                 if (super.getCommandMetaData().xDBMetaData.supportsSchemasInDataManipulation())
130                 {
131                     sPrimarySchema = xRow.getString(PKTABLE_SCHEM);
132                 }
133                 String sPrimaryTableName = xRow.getString(PKTABLE_NAME);
134                 String sPrimaryColumnName = xRow.getString(PKCOLUMN_NAME);
135                 String sForeignColumnName = xRow.getString(FKCOLUMN_NAME);
136                 if (JavaTools.isSame(getTableName(), sPrimaryTableName))
137                 {
138                     if (sPrimarySchema == null || JavaTools.isSame(getSchemaName(), sPrimarySchema))
139                     {
140                         if (JavaTools.isSame(getCatalogName(), sPrimaryCatalog))
141                         {
142                             aSlaveFieldNamesVector.add(sForeignColumnName);
143                             aMasterFieldNamesVector.add(sPrimaryColumnName);
144                             bleaveLoop = true;                  //Only one relation may exist between two tables...
145                         }
146                     }
147 
148                 }
149             }
150             sKeyColumnNames = new String[2][aMasterFieldNamesVector.size()];
151             sKeyColumnNames[0] = new String[aSlaveFieldNamesVector.size()];
152             sKeyColumnNames[1] = new String[aMasterFieldNamesVector.size()];
153             aSlaveFieldNamesVector.toArray(sKeyColumnNames[0]);
154             aMasterFieldNamesVector.toArray(sKeyColumnNames[1]);
155         }
156         catch (Exception e)
157         {
158             e.printStackTrace(System.out);
159         }
160         return sKeyColumnNames;
161     }
162 }
163