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 com.sun.star.sdbc.SQLException;
26 import com.sun.star.uno.Exception;
27 import com.sun.star.wizards.common.JavaTools;
28 import com.sun.star.wizards.common.PropertyNames;
29 
30 public class CommandName
31 {
32 
33     protected CommandMetaData oCommandMetaData;
34     protected String CatalogName = PropertyNames.EMPTY_STRING;
35     protected String SchemaName = PropertyNames.EMPTY_STRING;
36     protected String TableName = PropertyNames.EMPTY_STRING;
37     protected String DisplayName = PropertyNames.EMPTY_STRING;
38     protected String ComposedName = PropertyNames.EMPTY_STRING;
39     protected String AliasName = PropertyNames.EMPTY_STRING;
40     protected boolean bCatalogAtStart;
41     protected String sCatalogSep;
42     protected String sIdentifierQuote;
43     protected boolean baddQuotation = true;
44 
CommandName(CommandMetaData _CommandMetaData, String _DisplayName)45     public CommandName(CommandMetaData _CommandMetaData, String _DisplayName)
46     {
47         oCommandMetaData = _CommandMetaData;
48         setComposedCommandName(_DisplayName);
49     }
50 
CommandName(CommandMetaData _CommandMetaData, String _CatalogName, String _SchemaName, String _TableName, boolean _baddQuotation)51     public CommandName(CommandMetaData _CommandMetaData, String _CatalogName, String _SchemaName, String _TableName, boolean _baddQuotation)
52     {
53         try
54         {
55             baddQuotation = _baddQuotation;
56             oCommandMetaData = _CommandMetaData;
57             if ((_CatalogName != null) && (oCommandMetaData.xDBMetaData.supportsCatalogsInTableDefinitions()))
58             {
59                 if (!_CatalogName.equals(PropertyNames.EMPTY_STRING))
60                 {
61                     CatalogName = _CatalogName;
62                 }
63             }
64             if ((_SchemaName != null) && (oCommandMetaData.xDBMetaData.supportsSchemasInTableDefinitions()))
65             {
66                 if (!_SchemaName.equals(PropertyNames.EMPTY_STRING))
67                 {
68                     SchemaName = _SchemaName;
69                 }
70             }
71             if (_TableName != null)
72             {
73                 if (!_TableName.equals(PropertyNames.EMPTY_STRING))
74                 {
75                     TableName = _TableName;
76                 }
77             }
78             setComposedCommandName();
79         }
80         catch (SQLException e)
81         {
82             e.printStackTrace(System.out);
83         }
84     }
85 
setComposedCommandName(String _DisplayName)86     private void setComposedCommandName(String _DisplayName)
87     {
88         try
89         {
90             if (this.setMetaDataAttributes())
91             {
92                 this.DisplayName = _DisplayName;
93                 int iIndex;
94                 if (oCommandMetaData.xDBMetaData.supportsCatalogsInDataManipulation())
95                 { // ...dann Catalog mit in TableName
96                     iIndex = _DisplayName.indexOf(sCatalogSep);
97                     if (iIndex >= 0)
98                     {
99                         if (bCatalogAtStart)
100                         {
101                             CatalogName = _DisplayName.substring(0, iIndex);
102                             _DisplayName = _DisplayName.substring(iIndex + 1, _DisplayName.length());
103                         }
104                         else
105                         {
106                             CatalogName = _DisplayName.substring(iIndex + 1, _DisplayName.length());
107                             _DisplayName = _DisplayName.substring(0, iIndex);
108                         }
109                     }
110                 }
111                 if (oCommandMetaData.xDBMetaData.supportsSchemasInDataManipulation())
112                 {
113                     String[] NameList;
114                     NameList = new String[0];
115                     NameList = JavaTools.ArrayoutofString(_DisplayName, ".");
116                     SchemaName = NameList[0];
117                     TableName = NameList[1]; // TODO Was ist mit diesem Fall: CatalogSep = "." und CatalogName = PropertyNames.EMPTY_STRING
118                 }
119                 else
120                 {
121                     TableName = _DisplayName;
122                 }
123                 setComposedCommandName();
124             }
125         }
126         catch (Exception exception)
127         {
128             exception.printStackTrace(System.out);
129         }
130     }
131 
setComposedCommandName()132     public void setComposedCommandName()
133     {
134         if (this.setMetaDataAttributes())
135         {
136             if (CatalogName != null)
137             {
138                 if (!CatalogName.equals(PropertyNames.EMPTY_STRING))
139                 {
140                     if (bCatalogAtStart)
141                     {
142                         ComposedName = quoteName(CatalogName) + sCatalogSep;
143                     }
144                 }
145             }
146             if (SchemaName != null)
147             {
148                 if (!SchemaName.equals(PropertyNames.EMPTY_STRING))
149                 {
150                     ComposedName += quoteName(SchemaName) + ".";
151                 }
152             }
153             if (ComposedName.equals(PropertyNames.EMPTY_STRING))
154             {
155                 ComposedName = quoteName(TableName);
156             }
157             else
158             {
159                 ComposedName += quoteName(TableName);
160             }
161             if ((!bCatalogAtStart) && (CatalogName != null))
162             {
163                 if (!CatalogName.equals(PropertyNames.EMPTY_STRING))
164                 {
165                     ComposedName += sCatalogSep + quoteName(CatalogName);
166                 }
167             }
168         }
169     }
170 
setMetaDataAttributes()171     private boolean setMetaDataAttributes()
172     {
173         try
174         {
175             bCatalogAtStart = oCommandMetaData.xDBMetaData.isCatalogAtStart();
176             sCatalogSep = oCommandMetaData.xDBMetaData.getCatalogSeparator();
177             sIdentifierQuote = oCommandMetaData.xDBMetaData.getIdentifierQuoteString();
178             return true;
179         }
180         catch (SQLException e)
181         {
182             e.printStackTrace(System.out);
183             return false;
184         }
185     }
186 
quoteName(String _sName)187     public String quoteName(String _sName)
188     {
189         if (baddQuotation)
190         {
191             return quoteName(_sName, this.oCommandMetaData.getIdentifierQuote());
192         }
193         else
194         {
195             return _sName;
196         }
197     }
198 
quoteName(String sName, String _sIdentifierQuote)199     public static String quoteName(String sName, String _sIdentifierQuote)
200     {
201         if (sName == null)
202         {
203             sName = PropertyNames.EMPTY_STRING;
204         }
205         return new StringBuilder(_sIdentifierQuote).append(sName).append(_sIdentifierQuote).toString();
206     }
207 
setAliasName(String _AliasName)208     public void setAliasName(String _AliasName)
209     {
210         AliasName = _AliasName;
211     }
212 
getAliasName()213     public String getAliasName()
214     {
215         return AliasName;
216     }
217 
218     /**
219      * @return Returns the catalogName.
220      */
getCatalogName()221     public String getCatalogName()
222     {
223         return CatalogName;
224     }
225 
226     /**
227      * @return Returns the composedName.
228      */
getComposedName()229     public String getComposedName()
230     {
231         return ComposedName;
232     }
233 
234     /**
235      * @return Returns the displayName.
236      */
getDisplayName()237     public String getDisplayName()
238     {
239         return DisplayName;
240     }
241 
242     /**
243      * @return Returns the schemaName.
244      */
getSchemaName()245     public String getSchemaName()
246     {
247         return SchemaName;
248     }
249 
250     /**
251      * @return Returns the tableName.
252      */
getTableName()253     public String getTableName()
254     {
255         return TableName;
256     }
257 
getCommandMetaData()258     public CommandMetaData getCommandMetaData()
259     {
260         return oCommandMetaData;
261     }
262 }
263