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