xref: /trunk/main/wizards/com/sun/star/wizards/db/SQLQueryComposer.java (revision a1b4a26b27259a85a9821d1e8083147f257d71ca)
1*a1b4a26bSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*a1b4a26bSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*a1b4a26bSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*a1b4a26bSAndrew Rist  * distributed with this work for additional information
6*a1b4a26bSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*a1b4a26bSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*a1b4a26bSAndrew Rist  * "License"); you may not use this file except in compliance
9*a1b4a26bSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*a1b4a26bSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*a1b4a26bSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*a1b4a26bSAndrew Rist  * software distributed under the License is distributed on an
15*a1b4a26bSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*a1b4a26bSAndrew Rist  * KIND, either express or implied.  See the License for the
17*a1b4a26bSAndrew Rist  * specific language governing permissions and limitations
18*a1b4a26bSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*a1b4a26bSAndrew Rist  *************************************************************/
21*a1b4a26bSAndrew Rist 
22*a1b4a26bSAndrew Rist 
23cdf0e10cSrcweir package com.sun.star.wizards.db;
24cdf0e10cSrcweir 
25cdf0e10cSrcweir 
26cdf0e10cSrcweir // import com.sun.star.lang.IllegalArgumentException;
27cdf0e10cSrcweir // import com.sun.star.lang.WrappedTargetException;
28cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
29cdf0e10cSrcweir import com.sun.star.beans.*;
30cdf0e10cSrcweir // import com.sun.star.container.NoSuchElementException;
31cdf0e10cSrcweir import com.sun.star.container.XIndexAccess;
32cdf0e10cSrcweir import com.sun.star.container.XNameAccess;
33cdf0e10cSrcweir import com.sun.star.sdbcx.XColumnsSupplier;
34cdf0e10cSrcweir // import com.sun.star.sdb.XColumn;
35cdf0e10cSrcweir import com.sun.star.sdb.XSingleSelectQueryComposer;
36cdf0e10cSrcweir import com.sun.star.sdb.XSingleSelectQueryAnalyzer;
37cdf0e10cSrcweir import com.sun.star.ui.dialogs.XExecutableDialog;
38cdf0e10cSrcweir import com.sun.star.uno.AnyConverter;
39cdf0e10cSrcweir import com.sun.star.uno.Exception;
40cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
41cdf0e10cSrcweir import com.sun.star.sdbc.SQLException;
42cdf0e10cSrcweir import com.sun.star.lang.XInitialization;
43cdf0e10cSrcweir import com.sun.star.awt.XWindow;
44cdf0e10cSrcweir import com.sun.star.sdb.SQLFilterOperator;
45cdf0e10cSrcweir 
46cdf0e10cSrcweir import com.sun.star.wizards.common.*;
47cdf0e10cSrcweir import java.util.ArrayList;
48cdf0e10cSrcweir 
49cdf0e10cSrcweir public class SQLQueryComposer
50cdf0e10cSrcweir {
51cdf0e10cSrcweir 
52cdf0e10cSrcweir     public XColumnsSupplier xColSuppl;
53cdf0e10cSrcweir     // XSQLQueryComposer xSQLQueryComposer;
54cdf0e10cSrcweir     QueryMetaData CurDBMetaData;
55cdf0e10cSrcweir     // String m_sSelectClause;
56cdf0e10cSrcweir     // String m_sFromClause;
57cdf0e10cSrcweir     public XSingleSelectQueryAnalyzer m_xQueryAnalyzer;
58cdf0e10cSrcweir     ArrayList<CommandName> composedCommandNames = new ArrayList<CommandName>(1);
59cdf0e10cSrcweir     private XSingleSelectQueryComposer m_queryComposer;
60cdf0e10cSrcweir     XMultiServiceFactory xMSF;
61cdf0e10cSrcweir     boolean bincludeGrouping = true;
62cdf0e10cSrcweir 
63cdf0e10cSrcweir     public SQLQueryComposer(QueryMetaData _CurDBMetaData)
64cdf0e10cSrcweir     {
65cdf0e10cSrcweir         try
66cdf0e10cSrcweir         {
67cdf0e10cSrcweir             this.CurDBMetaData = _CurDBMetaData;
68cdf0e10cSrcweir             xMSF = UnoRuntime.queryInterface(XMultiServiceFactory.class, CurDBMetaData.DBConnection);
69cdf0e10cSrcweir             final Object oQueryComposer = xMSF.createInstance("com.sun.star.sdb.SingleSelectQueryComposer");
70cdf0e10cSrcweir             m_xQueryAnalyzer = UnoRuntime.queryInterface(XSingleSelectQueryAnalyzer.class, oQueryComposer);
71cdf0e10cSrcweir             m_queryComposer = UnoRuntime.queryInterface(XSingleSelectQueryComposer.class, m_xQueryAnalyzer);
72cdf0e10cSrcweir         }
73cdf0e10cSrcweir         catch (Exception exception)
74cdf0e10cSrcweir         {
75cdf0e10cSrcweir             exception.printStackTrace(System.out);
76cdf0e10cSrcweir         }
77cdf0e10cSrcweir     }
78cdf0e10cSrcweir 
79cdf0e10cSrcweir     private boolean addtoSelectClause(String DisplayFieldName) throws SQLException
80cdf0e10cSrcweir     {
81cdf0e10cSrcweir         return !(bincludeGrouping && CurDBMetaData.xDBMetaData.supportsGroupByUnrelated() && CurDBMetaData.GroupFieldNames != null && JavaTools.FieldInList(CurDBMetaData.GroupFieldNames, DisplayFieldName) > -1);
82cdf0e10cSrcweir         }
83cdf0e10cSrcweir 
84cdf0e10cSrcweir     public String getSelectClause(boolean _baddAliasFieldNames) throws SQLException
85cdf0e10cSrcweir     {
86cdf0e10cSrcweir         String sSelectBaseClause = "SELECT ";
87cdf0e10cSrcweir         String sSelectClause = sSelectBaseClause;
88cdf0e10cSrcweir         for (int i = 0; i < CurDBMetaData.FieldColumns.length; i++)
89cdf0e10cSrcweir         {
90cdf0e10cSrcweir             if (addtoSelectClause(CurDBMetaData.FieldColumns[i].getDisplayFieldName()))
91cdf0e10cSrcweir             {
92cdf0e10cSrcweir                 int iAggregate = CurDBMetaData.getAggregateIndex(CurDBMetaData.FieldColumns[i].getDisplayFieldName());
93cdf0e10cSrcweir                 if (iAggregate > -1)
94cdf0e10cSrcweir                 {
95cdf0e10cSrcweir                     sSelectClause += CurDBMetaData.AggregateFieldNames[iAggregate][1] + "(" + getComposedAliasFieldName(CurDBMetaData.AggregateFieldNames[iAggregate][0]) + ")";
96cdf0e10cSrcweir                     if (_baddAliasFieldNames)
97cdf0e10cSrcweir                     {
98cdf0e10cSrcweir                         sSelectClause += getAliasFieldNameClause(CurDBMetaData.AggregateFieldNames[iAggregate][0]);
99cdf0e10cSrcweir                     }
100cdf0e10cSrcweir                 }
101cdf0e10cSrcweir                 else
102cdf0e10cSrcweir                 {
103cdf0e10cSrcweir                     sSelectClause += getComposedAliasFieldName(CurDBMetaData.FieldColumns[i].getDisplayFieldName());
104cdf0e10cSrcweir                     if (_baddAliasFieldNames)
105cdf0e10cSrcweir                     {
106cdf0e10cSrcweir                         sSelectClause += getAliasFieldNameClause(CurDBMetaData.FieldColumns[i].getDisplayFieldName());
107cdf0e10cSrcweir                     }
108cdf0e10cSrcweir                 }
109cdf0e10cSrcweir                 sSelectClause += ", ";
110cdf0e10cSrcweir             }
111cdf0e10cSrcweir         }
112cdf0e10cSrcweir         // TODO: little bit unhandy version of remove the append 'comma' at the end
113cdf0e10cSrcweir         if (sSelectClause.equals(sSelectBaseClause))
114cdf0e10cSrcweir         {
115cdf0e10cSrcweir             sSelectClause = sSelectClause.substring(0, sSelectClause.length() - 1);
116cdf0e10cSrcweir         }
117cdf0e10cSrcweir         else
118cdf0e10cSrcweir         {
119cdf0e10cSrcweir             sSelectClause = sSelectClause.substring(0, sSelectClause.length() - 2);
120cdf0e10cSrcweir         }
121cdf0e10cSrcweir         return sSelectClause;
122cdf0e10cSrcweir     }
123cdf0e10cSrcweir 
124cdf0e10cSrcweir     public String getAliasFieldNameClause(String _FieldName)
125cdf0e10cSrcweir     {
126cdf0e10cSrcweir         String FieldTitle = CurDBMetaData.getFieldTitle(_FieldName);
127cdf0e10cSrcweir         if (!FieldTitle.equals(_FieldName))
128cdf0e10cSrcweir         {
129cdf0e10cSrcweir             return " AS " + CommandName.quoteName(FieldTitle, CurDBMetaData.getIdentifierQuote());
130cdf0e10cSrcweir         }
131cdf0e10cSrcweir         else
132cdf0e10cSrcweir         {
133cdf0e10cSrcweir             return "";
134cdf0e10cSrcweir         }
135cdf0e10cSrcweir     }
136cdf0e10cSrcweir 
137cdf0e10cSrcweir     public void appendFilterConditions() throws SQLException
138cdf0e10cSrcweir     {
139cdf0e10cSrcweir         try
140cdf0e10cSrcweir         {
141cdf0e10cSrcweir             for (int i = 0; i < CurDBMetaData.getFilterConditions().length; i++)
142cdf0e10cSrcweir             {
143cdf0e10cSrcweir                 m_queryComposer.setStructuredFilter(CurDBMetaData.getFilterConditions());
144cdf0e10cSrcweir             }
145cdf0e10cSrcweir         }
146cdf0e10cSrcweir         catch (Exception exception)
147cdf0e10cSrcweir         {
148cdf0e10cSrcweir             exception.printStackTrace(System.out);
149cdf0e10cSrcweir         }
150cdf0e10cSrcweir     }
151cdf0e10cSrcweir 
152cdf0e10cSrcweir     public void prependSortingCriteria() throws SQLException
153cdf0e10cSrcweir     {
154cdf0e10cSrcweir         XIndexAccess xColumnIndexAccess = m_xQueryAnalyzer.getOrderColumns();
155cdf0e10cSrcweir         m_queryComposer.setOrder("");
156cdf0e10cSrcweir         for (int i = 0; i < CurDBMetaData.getSortFieldNames().length; i++)
157cdf0e10cSrcweir         {
158cdf0e10cSrcweir             appendSortingCriterion(i, false);
159cdf0e10cSrcweir         }
160cdf0e10cSrcweir         for (int i = 0; i < xColumnIndexAccess.getCount(); i++)
161cdf0e10cSrcweir         {
162cdf0e10cSrcweir             try
163cdf0e10cSrcweir             {
164cdf0e10cSrcweir                 XPropertySet xColumnPropertySet = UnoRuntime.queryInterface(XPropertySet.class, xColumnIndexAccess.getByIndex(i));
165cdf0e10cSrcweir                 String sName = (String) xColumnPropertySet.getPropertyValue(PropertyNames.PROPERTY_NAME);
166cdf0e10cSrcweir                 if (JavaTools.FieldInTable(CurDBMetaData.getSortFieldNames(), sName) == -1)
167cdf0e10cSrcweir                 {
168cdf0e10cSrcweir                     boolean bascend = AnyConverter.toBoolean(xColumnPropertySet.getPropertyValue("IsAscending"));
169cdf0e10cSrcweir                     m_queryComposer.appendOrderByColumn(xColumnPropertySet, bascend);
170cdf0e10cSrcweir                 }
171cdf0e10cSrcweir             }
172cdf0e10cSrcweir             catch (Exception e)
173cdf0e10cSrcweir             {
174cdf0e10cSrcweir                 e.printStackTrace(System.out);
175cdf0e10cSrcweir             }
176cdf0e10cSrcweir         }
177cdf0e10cSrcweir     }
178cdf0e10cSrcweir 
179cdf0e10cSrcweir     private void appendSortingCriterion(int _SortIndex, boolean _baddAliasFieldNames) throws SQLException
180cdf0e10cSrcweir     {
181cdf0e10cSrcweir         String sSortValue = CurDBMetaData.getSortFieldNames()[_SortIndex][0];
182cdf0e10cSrcweir         XPropertySet xColumn = CurDBMetaData.getColumnObjectByFieldName(sSortValue, _baddAliasFieldNames);
183cdf0e10cSrcweir 
184cdf0e10cSrcweir         String sSort = CurDBMetaData.getSortFieldNames()[_SortIndex][1];
185cdf0e10cSrcweir         boolean bascend = (sSort.equals("ASC"));
186cdf0e10cSrcweir         m_queryComposer.appendOrderByColumn(xColumn, bascend);
187cdf0e10cSrcweir     }
188cdf0e10cSrcweir 
189cdf0e10cSrcweir     public void appendSortingcriteria(boolean _baddAliasFieldNames) throws SQLException
190cdf0e10cSrcweir     {
191cdf0e10cSrcweir         String sOrder = "";
192cdf0e10cSrcweir         m_queryComposer.setOrder("");
193cdf0e10cSrcweir         for (int i = 0; i < CurDBMetaData.getSortFieldNames().length; i++)
194cdf0e10cSrcweir         {
195cdf0e10cSrcweir             String sSortValue = CurDBMetaData.getSortFieldNames()[i][0];
196cdf0e10cSrcweir             int iAggregate = CurDBMetaData.getAggregateIndex(sSortValue);
197cdf0e10cSrcweir             if (iAggregate > -1)
198cdf0e10cSrcweir             {
199cdf0e10cSrcweir                 sOrder = m_xQueryAnalyzer.getOrder();
200cdf0e10cSrcweir                 if (sOrder.length() > 0)
201cdf0e10cSrcweir                 {
202cdf0e10cSrcweir                     sOrder += ", ";
203cdf0e10cSrcweir                 }
204cdf0e10cSrcweir                 sOrder += CurDBMetaData.AggregateFieldNames[iAggregate][1] + "(" + CurDBMetaData.AggregateFieldNames[iAggregate][0] + ")";
205cdf0e10cSrcweir                 sOrder += " " + CurDBMetaData.getSortFieldNames()[i][1];
206cdf0e10cSrcweir                 m_queryComposer.setOrder(sOrder);
207cdf0e10cSrcweir             }
208cdf0e10cSrcweir             else
209cdf0e10cSrcweir             {
210cdf0e10cSrcweir                 appendSortingCriterion(i, _baddAliasFieldNames);
211cdf0e10cSrcweir             }
212cdf0e10cSrcweir             sOrder = m_xQueryAnalyzer.getOrder();
213cdf0e10cSrcweir         }
214cdf0e10cSrcweir         // just for debug!
215cdf0e10cSrcweir         sOrder = m_queryComposer.getOrder();
216cdf0e10cSrcweir     }
217cdf0e10cSrcweir 
218cdf0e10cSrcweir     public void appendGroupByColumns(boolean _baddAliasFieldNames) throws SQLException
219cdf0e10cSrcweir     {
220cdf0e10cSrcweir         for (int i = 0; i < CurDBMetaData.GroupFieldNames.length; i++)
221cdf0e10cSrcweir         {
222cdf0e10cSrcweir             XPropertySet xColumn = CurDBMetaData.getColumnObjectByFieldName(CurDBMetaData.GroupFieldNames[i], _baddAliasFieldNames);
223cdf0e10cSrcweir             m_queryComposer.appendGroupByColumn(xColumn);
224cdf0e10cSrcweir         }
225cdf0e10cSrcweir     }
226cdf0e10cSrcweir 
227cdf0e10cSrcweir     public void setDBMetaData(QueryMetaData _oDBMetaData)
228cdf0e10cSrcweir     {
229cdf0e10cSrcweir         this.CurDBMetaData = _oDBMetaData;
230cdf0e10cSrcweir     }
231cdf0e10cSrcweir 
232cdf0e10cSrcweir     private PropertyValue[][] replaceConditionsByAlias(PropertyValue _filterconditions[][])
233cdf0e10cSrcweir     {
234cdf0e10cSrcweir         XColumnsSupplier columnSup = UnoRuntime.queryInterface(XColumnsSupplier.class, m_xQueryAnalyzer);
235cdf0e10cSrcweir         XNameAccess columns = columnSup.getColumns();
236cdf0e10cSrcweir         for (int n = 0; n < _filterconditions.length; n++)
237cdf0e10cSrcweir         {
238cdf0e10cSrcweir             for (int m = 0; m < _filterconditions[n].length; m++)
239cdf0e10cSrcweir             {
240cdf0e10cSrcweir             //  _filterconditions[n][m].Name = getComposedAliasFieldName(_filterconditions[n][m].Name);
241cdf0e10cSrcweir                 final String aliasName = getComposedAliasFieldName(_filterconditions[n][m].Name);
242cdf0e10cSrcweir                 if ( columns.hasByName(aliasName))
243cdf0e10cSrcweir                     _filterconditions[n][m].Name = aliasName;
244cdf0e10cSrcweir             }
245cdf0e10cSrcweir         }
246cdf0e10cSrcweir         return _filterconditions;
247cdf0e10cSrcweir     }
248cdf0e10cSrcweir 
249cdf0e10cSrcweir     public String getQuery()
250cdf0e10cSrcweir     {
251cdf0e10cSrcweir         return m_xQueryAnalyzer.getQuery();
252cdf0e10cSrcweir     }
253cdf0e10cSrcweir 
254cdf0e10cSrcweir     public StringBuilder getFromClause()
255cdf0e10cSrcweir     {
256cdf0e10cSrcweir         StringBuilder sFromClause = new StringBuilder("FROM");
257cdf0e10cSrcweir         composedCommandNames.clear();
258cdf0e10cSrcweir         String[] sCommandNames = CurDBMetaData.getIncludedCommandNames();
259cdf0e10cSrcweir         for (int i = 0; i < sCommandNames.length; i++)
260cdf0e10cSrcweir         {
261cdf0e10cSrcweir             CommandName curCommandName = new CommandName(CurDBMetaData, sCommandNames[i]); //(setComposedCommandName)
262cdf0e10cSrcweir             curCommandName.setAliasName(getuniqueAliasName(curCommandName.getTableName()));
263cdf0e10cSrcweir             sFromClause.append(" ").append(curCommandName.getComposedName()).append(" ").append(quoteName(curCommandName.getAliasName()));
264cdf0e10cSrcweir             if (i < sCommandNames.length - 1)
265cdf0e10cSrcweir             {
266cdf0e10cSrcweir                 sFromClause.append(", ");
267cdf0e10cSrcweir             }
268cdf0e10cSrcweir             // fill composedCommandNames
269cdf0e10cSrcweir             composedCommandNames.add(curCommandName);
270cdf0e10cSrcweir         }
271cdf0e10cSrcweir         return sFromClause;
272cdf0e10cSrcweir     }
273cdf0e10cSrcweir 
274cdf0e10cSrcweir     public boolean setQueryCommand(XWindow _xParentWindow, boolean _bincludeGrouping, boolean _baddAliasFieldNames)
275cdf0e10cSrcweir     {
276cdf0e10cSrcweir         return setQueryCommand(_xParentWindow, _bincludeGrouping, _baddAliasFieldNames, true);
277cdf0e10cSrcweir     }
278cdf0e10cSrcweir 
279cdf0e10cSrcweir     public boolean setQueryCommand(XWindow _xParentWindow, boolean _bincludeGrouping, boolean _baddAliasFieldNames, boolean addQuery)
280cdf0e10cSrcweir     {
281cdf0e10cSrcweir         try
282cdf0e10cSrcweir         {
283cdf0e10cSrcweir             bincludeGrouping = _bincludeGrouping;
284cdf0e10cSrcweir             if (addQuery)
285cdf0e10cSrcweir             {
286cdf0e10cSrcweir                 StringBuilder fromClause = getFromClause();
287cdf0e10cSrcweir                 String sSelectClause = getSelectClause(_baddAliasFieldNames);
288cdf0e10cSrcweir                 StringBuilder queryclause = new StringBuilder(sSelectClause).append(" ").append(fromClause);
289cdf0e10cSrcweir                 m_xQueryAnalyzer.setQuery(queryclause.toString());
290cdf0e10cSrcweir                 if (CurDBMetaData.getFilterConditions() != null && CurDBMetaData.getFilterConditions().length > 0)
291cdf0e10cSrcweir                 {
292cdf0e10cSrcweir                     CurDBMetaData.setFilterConditions(replaceConditionsByAlias(CurDBMetaData.getFilterConditions()));
293cdf0e10cSrcweir                     m_queryComposer.setStructuredFilter(CurDBMetaData.getFilterConditions());
294cdf0e10cSrcweir                 }
295cdf0e10cSrcweir             }
296cdf0e10cSrcweir             if (_bincludeGrouping)
297cdf0e10cSrcweir             {
298cdf0e10cSrcweir                 appendGroupByColumns(_baddAliasFieldNames);
299cdf0e10cSrcweir                 if (CurDBMetaData.GroupByFilterConditions.length > 0)
300cdf0e10cSrcweir                 {
301cdf0e10cSrcweir                     m_queryComposer.setStructuredHavingClause(CurDBMetaData.GroupByFilterConditions);
302cdf0e10cSrcweir                 }
303cdf0e10cSrcweir             }
304cdf0e10cSrcweir             appendSortingcriteria(_baddAliasFieldNames);
305cdf0e10cSrcweir 
306cdf0e10cSrcweir             return true;
307cdf0e10cSrcweir         }
308cdf0e10cSrcweir         catch (Exception exception)
309cdf0e10cSrcweir         {
310cdf0e10cSrcweir             exception.printStackTrace(System.out);
311cdf0e10cSrcweir             displaySQLErrorDialog(exception, _xParentWindow);
312cdf0e10cSrcweir             return false;
313cdf0e10cSrcweir         }
314cdf0e10cSrcweir     }
315cdf0e10cSrcweir 
316cdf0e10cSrcweir     private String getComposedAliasFieldName(String _fieldname)
317cdf0e10cSrcweir     {
318cdf0e10cSrcweir         FieldColumn CurFieldColumn = CurDBMetaData.getFieldColumnByDisplayName(_fieldname);
319cdf0e10cSrcweir         CommandName curComposedCommandName = getComposedCommandByDisplayName(CurFieldColumn.getCommandName());
320cdf0e10cSrcweir         if (curComposedCommandName == null)
321cdf0e10cSrcweir         {
322cdf0e10cSrcweir             return _fieldname;
323cdf0e10cSrcweir         }
324cdf0e10cSrcweir         String curAliasName = curComposedCommandName.getAliasName();
325cdf0e10cSrcweir         return quoteName(curAliasName) + "." + quoteName(CurFieldColumn.getFieldName());
326cdf0e10cSrcweir     }
327cdf0e10cSrcweir 
328cdf0e10cSrcweir     private CommandName getComposedCommandByAliasName(String _AliasName)
329cdf0e10cSrcweir     {
330cdf0e10cSrcweir         if (composedCommandNames != null)
331cdf0e10cSrcweir         {
332cdf0e10cSrcweir             for (CommandName commandName : composedCommandNames)
333cdf0e10cSrcweir             {
334cdf0e10cSrcweir                 if (commandName.getAliasName().equals(_AliasName))
335cdf0e10cSrcweir                 {
336cdf0e10cSrcweir                     return commandName;
337cdf0e10cSrcweir                 }
338cdf0e10cSrcweir             }
339cdf0e10cSrcweir         }
340cdf0e10cSrcweir         return null;
341cdf0e10cSrcweir     }
342cdf0e10cSrcweir 
343cdf0e10cSrcweir     public CommandName getComposedCommandByDisplayName(String _DisplayName)
344cdf0e10cSrcweir     {
345cdf0e10cSrcweir         if (composedCommandNames != null)
346cdf0e10cSrcweir         {
347cdf0e10cSrcweir             for (CommandName commandName : composedCommandNames)
348cdf0e10cSrcweir             {
349cdf0e10cSrcweir                 if (commandName.getDisplayName().equals(_DisplayName))
350cdf0e10cSrcweir                 {
351cdf0e10cSrcweir                     return commandName;
352cdf0e10cSrcweir                 }
353cdf0e10cSrcweir             }
354cdf0e10cSrcweir         }
355cdf0e10cSrcweir         return null;
356cdf0e10cSrcweir     }
357cdf0e10cSrcweir 
358cdf0e10cSrcweir     public String getuniqueAliasName(String _TableName)
359cdf0e10cSrcweir     {
360cdf0e10cSrcweir         int a = 0;
361cdf0e10cSrcweir         String AliasName = "";
362cdf0e10cSrcweir         boolean bAliasNameexists = true;
363cdf0e10cSrcweir         String locAliasName = _TableName;
364cdf0e10cSrcweir         while (bAliasNameexists)
365cdf0e10cSrcweir         {
366cdf0e10cSrcweir             bAliasNameexists = (getComposedCommandByAliasName(locAliasName) != null);
367cdf0e10cSrcweir             if (bAliasNameexists)
368cdf0e10cSrcweir             {
369cdf0e10cSrcweir                 a++;
370cdf0e10cSrcweir                 locAliasName = _TableName + "_" + String.valueOf(a);
371cdf0e10cSrcweir             }
372cdf0e10cSrcweir             else
373cdf0e10cSrcweir             {
374cdf0e10cSrcweir                 AliasName = locAliasName;
375cdf0e10cSrcweir             }
376cdf0e10cSrcweir         }
377cdf0e10cSrcweir         return AliasName;
378cdf0e10cSrcweir     }
379cdf0e10cSrcweir 
380cdf0e10cSrcweir     private String quoteName(String _sname)
381cdf0e10cSrcweir     {
382cdf0e10cSrcweir         return CommandName.quoteName(_sname, CurDBMetaData.getIdentifierQuote());
383cdf0e10cSrcweir     }
384cdf0e10cSrcweir 
385cdf0e10cSrcweir     public void displaySQLErrorDialog(Exception _exception, XWindow _xParentWindow)
386cdf0e10cSrcweir     {
387cdf0e10cSrcweir         try
388cdf0e10cSrcweir         {
389cdf0e10cSrcweir             Object oErrorDialog = CurDBMetaData.xMSF.createInstance("com.sun.star.sdb.ErrorMessageDialog");
390cdf0e10cSrcweir             XInitialization xInitialize = UnoRuntime.queryInterface(XInitialization.class, oErrorDialog);
391cdf0e10cSrcweir             XExecutableDialog xExecute = UnoRuntime.queryInterface(XExecutableDialog.class, oErrorDialog);
392cdf0e10cSrcweir             PropertyValue[] rDispatchArguments = new PropertyValue[3];
393cdf0e10cSrcweir             rDispatchArguments[0] = Properties.createProperty(PropertyNames.PROPERTY_TITLE, Configuration.getProductName(CurDBMetaData.xMSF) + " Base");
394cdf0e10cSrcweir             rDispatchArguments[1] = Properties.createProperty("ParentWindow", _xParentWindow);
395cdf0e10cSrcweir             rDispatchArguments[2] = Properties.createProperty("SQLException", _exception);
396cdf0e10cSrcweir             xInitialize.initialize(rDispatchArguments);
397cdf0e10cSrcweir             xExecute.execute();
398cdf0e10cSrcweir             //TODO dispose???
399cdf0e10cSrcweir         }
400cdf0e10cSrcweir         catch (Exception typeexception)
401cdf0e10cSrcweir         {
402cdf0e10cSrcweir             typeexception.printStackTrace(System.out);
403cdf0e10cSrcweir         }
404cdf0e10cSrcweir     }
405cdf0e10cSrcweir 
406cdf0e10cSrcweir     /**
407cdf0e10cSrcweir      * retrieves a normalized structured filter
408cdf0e10cSrcweir      *
409cdf0e10cSrcweir      * <p>XSingleSelectQueryComposer.getStructuredFilter has a strange habit of returning the predicate (equal, not equal, etc)
410cdf0e10cSrcweir      * effectively twice: Once as SQLFilterOperator, and once in the value. That is, if you have a term "column <> 3", then
411cdf0e10cSrcweir      * you'll get an SQLFilterOperator.NOT_EQUAL (which is fine), <strong>and</strong> the textual value of the condition
412cdf0e10cSrcweir      * will read "<> 3". The latter is strange enough, but even more strange is that this behavior is not even consistent:
413cdf0e10cSrcweir      * for SQLFilterOperator.EQUAL, the "=" sign is not include in the textual value.</p>
414cdf0e10cSrcweir      *
415cdf0e10cSrcweir      * <p>To abstract from this weirdness, use this function here, which strips the unwanted tokens from the textual value
416cdf0e10cSrcweir      * representation.</p>
417cdf0e10cSrcweir      */
418cdf0e10cSrcweir     public PropertyValue[][] getNormalizedStructuredFilter()
419cdf0e10cSrcweir     {
420cdf0e10cSrcweir         final PropertyValue[][] structuredFilter = m_queryComposer.getStructuredFilter();
421cdf0e10cSrcweir         for (int i = 0; i < structuredFilter.length; ++i)
422cdf0e10cSrcweir         {
423cdf0e10cSrcweir             for (int j = 0; j < structuredFilter[i].length; ++j)
424cdf0e10cSrcweir             {
425cdf0e10cSrcweir                 if (!(structuredFilter[i][j].Value instanceof String))
426cdf0e10cSrcweir                 {
427cdf0e10cSrcweir                     continue;
428cdf0e10cSrcweir                 }
429cdf0e10cSrcweir                 final StringBuffer textualValue = new StringBuffer((String) structuredFilter[i][j].Value);
430cdf0e10cSrcweir                 switch (structuredFilter[i][j].Handle)
431cdf0e10cSrcweir                 {
432cdf0e10cSrcweir                     case SQLFilterOperator.EQUAL:
433cdf0e10cSrcweir                         break;
434cdf0e10cSrcweir                     case SQLFilterOperator.NOT_EQUAL:
435cdf0e10cSrcweir                     case SQLFilterOperator.LESS_EQUAL:
436cdf0e10cSrcweir                     case SQLFilterOperator.GREATER_EQUAL:
437cdf0e10cSrcweir                         textualValue.delete(0, 2);
438cdf0e10cSrcweir                         break;
439cdf0e10cSrcweir                     case SQLFilterOperator.LESS:
440cdf0e10cSrcweir                     case SQLFilterOperator.GREATER:
441cdf0e10cSrcweir                         textualValue.delete(0, 1);
442cdf0e10cSrcweir                         break;
443cdf0e10cSrcweir                     case SQLFilterOperator.NOT_LIKE:
444cdf0e10cSrcweir                         textualValue.delete(0, 8);
445cdf0e10cSrcweir                         break;
446cdf0e10cSrcweir                     case SQLFilterOperator.LIKE:
447cdf0e10cSrcweir                         textualValue.delete(0, 4);
448cdf0e10cSrcweir                         break;
449cdf0e10cSrcweir                     case SQLFilterOperator.SQLNULL:
450cdf0e10cSrcweir                         textualValue.delete(0, 7);
451cdf0e10cSrcweir                         break;
452cdf0e10cSrcweir                     case SQLFilterOperator.NOT_SQLNULL:
453cdf0e10cSrcweir                         textualValue.delete(0, 11);
454cdf0e10cSrcweir                         break;
455cdf0e10cSrcweir                 }
456cdf0e10cSrcweir                 structuredFilter[i][j].Value = textualValue.toString().trim();
457cdf0e10cSrcweir             }
458cdf0e10cSrcweir         }
459cdf0e10cSrcweir         return structuredFilter;
460cdf0e10cSrcweir     }
461cdf0e10cSrcweir 
462cdf0e10cSrcweir     public XSingleSelectQueryComposer getQueryComposer()
463cdf0e10cSrcweir     {
464cdf0e10cSrcweir         return m_queryComposer;
465cdf0e10cSrcweir     }
466cdf0e10cSrcweir }
467