1*96de5490SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*96de5490SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*96de5490SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*96de5490SAndrew Rist  * distributed with this work for additional information
6*96de5490SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*96de5490SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*96de5490SAndrew Rist  * "License"); you may not use this file except in compliance
9*96de5490SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*96de5490SAndrew Rist  *
11*96de5490SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*96de5490SAndrew Rist  *
13*96de5490SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*96de5490SAndrew Rist  * software distributed under the License is distributed on an
15*96de5490SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*96de5490SAndrew Rist  * KIND, either express or implied.  See the License for the
17*96de5490SAndrew Rist  * specific language governing permissions and limitations
18*96de5490SAndrew Rist  * under the License.
19*96de5490SAndrew Rist  *
20*96de5490SAndrew Rist  *************************************************************/
21*96de5490SAndrew Rist 
22*96de5490SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_dbaccess.hxx"
26cdf0e10cSrcweir #ifndef DBAUI_QUERYORDER_HRC
27cdf0e10cSrcweir #include "queryorder.hrc"
28cdf0e10cSrcweir #endif
29cdf0e10cSrcweir #ifndef DBAUI_QUERYORDER_HXX
30cdf0e10cSrcweir #include "queryorder.hxx"
31cdf0e10cSrcweir #endif
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #ifndef DBACCESS_SHARED_DBUSTRINGS_HRC
34cdf0e10cSrcweir #include "dbustrings.hrc"
35cdf0e10cSrcweir #endif
36cdf0e10cSrcweir 
37cdf0e10cSrcweir #ifndef _COM_SUN_STAR_SDB_XSINGLESELECTQUERYCOMPOSER_HPP_
38cdf0e10cSrcweir #include <com/sun/star/sdb/XSingleSelectQueryComposer.hpp>
39cdf0e10cSrcweir #endif
40cdf0e10cSrcweir #ifndef _COM_SUN_STAR_SDBC_COLUMNSEARCH_HPP_
41cdf0e10cSrcweir #include <com/sun/star/sdbc/ColumnSearch.hpp>
42cdf0e10cSrcweir #endif
43cdf0e10cSrcweir #ifndef _COM_SUN_STAR_CONTAINER_XNAMEACCESS_HPP_
44cdf0e10cSrcweir #include <com/sun/star/container/XNameAccess.hpp>
45cdf0e10cSrcweir #endif
46cdf0e10cSrcweir #ifndef _TOOLS_DEBUG_HXX
47cdf0e10cSrcweir #include <tools/debug.hxx>
48cdf0e10cSrcweir #endif
49cdf0e10cSrcweir #ifndef _DBAUI_MODULE_DBU_HXX_
50cdf0e10cSrcweir #include "moduledbu.hxx"
51cdf0e10cSrcweir #endif
52cdf0e10cSrcweir #ifndef _CONNECTIVITY_PARSE_SQLITERATOR_HXX_
53cdf0e10cSrcweir #include <connectivity/sqliterator.hxx>
54cdf0e10cSrcweir #endif
55cdf0e10cSrcweir #ifndef _CONNECTIVITY_DBTOOLS_HXX_
56cdf0e10cSrcweir #include <connectivity/dbtools.hxx>
57cdf0e10cSrcweir #endif
58cdf0e10cSrcweir #ifndef _COMPHELPER_EXTRACT_HXX_
59cdf0e10cSrcweir #include <comphelper/extract.hxx>
60cdf0e10cSrcweir #endif
61cdf0e10cSrcweir #ifndef _COM_SUN_STAR_SDBCX_XCOLUMNSSUPPLIER_HPP_
62cdf0e10cSrcweir #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
63cdf0e10cSrcweir #endif
64cdf0e10cSrcweir 
65cdf0e10cSrcweir #ifndef TOOLS_DIAGNOSE_EX_H
66cdf0e10cSrcweir #include <tools/diagnose_ex.h>
67cdf0e10cSrcweir #endif
68cdf0e10cSrcweir 
69cdf0e10cSrcweir #include <algorithm>
70cdf0e10cSrcweir 
71cdf0e10cSrcweir 
72cdf0e10cSrcweir using namespace dbaui;
73cdf0e10cSrcweir using namespace connectivity;
74cdf0e10cSrcweir using namespace ::com::sun::star::uno;
75cdf0e10cSrcweir using namespace ::com::sun::star::container;
76cdf0e10cSrcweir using namespace ::com::sun::star::util;
77cdf0e10cSrcweir using namespace ::com::sun::star::sdb;
78cdf0e10cSrcweir using namespace ::com::sun::star::sdbc;
79cdf0e10cSrcweir using namespace ::com::sun::star::sdbcx;
80cdf0e10cSrcweir using namespace ::com::sun::star::beans;
81cdf0e10cSrcweir 
82cdf0e10cSrcweir DBG_NAME(DlgOrderCrit)
83cdf0e10cSrcweir //------------------------------------------------------------------------------
84cdf0e10cSrcweir DlgOrderCrit::DlgOrderCrit(	Window * pParent,
85cdf0e10cSrcweir 							const Reference< XConnection>& _rxConnection,
86cdf0e10cSrcweir 							const Reference< XSingleSelectQueryComposer >& _rxComposer,
87cdf0e10cSrcweir 							const Reference< XNameAccess>& _rxCols)
88cdf0e10cSrcweir 			 :ModalDialog( pParent, ModuleRes(DLG_ORDERCRIT) )
89cdf0e10cSrcweir 			,aLB_ORDERFIELD1(	this, ModuleRes( LB_ORDERFIELD1 ) )
90cdf0e10cSrcweir 			,aLB_ORDERVALUE1(	this, ModuleRes( LB_ORDERVALUE1 ) )
91cdf0e10cSrcweir 			,aLB_ORDERFIELD2(	this, ModuleRes( LB_ORDERFIELD2 ) )
92cdf0e10cSrcweir 			,aLB_ORDERVALUE2(	this, ModuleRes( LB_ORDERVALUE2 ) )
93cdf0e10cSrcweir 			,aLB_ORDERFIELD3(	this, ModuleRes( LB_ORDERFIELD3 ) )
94cdf0e10cSrcweir 			,aLB_ORDERVALUE3(	this, ModuleRes( LB_ORDERVALUE3 ) )
95cdf0e10cSrcweir 			,aFT_ORDERFIELD(	this, ModuleRes( FT_ORDERFIELD ) )
96cdf0e10cSrcweir 			,aFT_ORDERAFTER1(	this, ModuleRes( FT_ORDERAFTER1 ) )
97cdf0e10cSrcweir 			,aFT_ORDERAFTER2(	this, ModuleRes( FT_ORDERAFTER2 ) )
98cdf0e10cSrcweir 			,aFT_ORDEROPER(		this, ModuleRes( FT_ORDEROPER ) )
99cdf0e10cSrcweir 			,aFT_ORDERDIR(		this, ModuleRes( FT_ORDERDIR ) )
100cdf0e10cSrcweir 			,aBT_OK(			this, ModuleRes( BT_OK ) )
101cdf0e10cSrcweir 			,aBT_CANCEL(		this, ModuleRes( BT_CANCEL ) )
102cdf0e10cSrcweir 			,aBT_HELP(			this, ModuleRes( BT_HELP ) )
103cdf0e10cSrcweir             ,aFL_ORDER(         this, ModuleRes( FL_ORDER ) )
104cdf0e10cSrcweir 			,aSTR_NOENTRY(		ModuleRes( STR_NOENTRY ) )
105cdf0e10cSrcweir 			,m_xQueryComposer( _rxComposer )
106cdf0e10cSrcweir 			,m_xColumns(_rxCols)
107cdf0e10cSrcweir 			,m_xConnection(_rxConnection)
108cdf0e10cSrcweir {
109cdf0e10cSrcweir 	DBG_CTOR(DlgOrderCrit,NULL);
110cdf0e10cSrcweir 
111cdf0e10cSrcweir     AllSettings aSettings( GetSettings() );
112cdf0e10cSrcweir     StyleSettings aStyle( aSettings.GetStyleSettings() );
113cdf0e10cSrcweir     aStyle.SetAutoMnemonic( sal_False );
114cdf0e10cSrcweir     aSettings.SetStyleSettings( aStyle );
115cdf0e10cSrcweir     SetSettings( aSettings );
116cdf0e10cSrcweir 
117cdf0e10cSrcweir 	m_aColumnList[0] = &aLB_ORDERFIELD1;
118cdf0e10cSrcweir 	m_aColumnList[1] = &aLB_ORDERFIELD2;
119cdf0e10cSrcweir 	m_aColumnList[2] = &aLB_ORDERFIELD3;
120cdf0e10cSrcweir 
121cdf0e10cSrcweir 	m_aValueList[0] = &aLB_ORDERVALUE1;
122cdf0e10cSrcweir 	m_aValueList[1] = &aLB_ORDERVALUE2;
123cdf0e10cSrcweir 	m_aValueList[2] = &aLB_ORDERVALUE3;
124cdf0e10cSrcweir 
125cdf0e10cSrcweir 	xub_StrLen j;
126cdf0e10cSrcweir 	for(j=0 ; j < DOG_ROWS ; j++ )
127cdf0e10cSrcweir 	{
128cdf0e10cSrcweir 		m_aColumnList[j]->InsertEntry( aSTR_NOENTRY );
129cdf0e10cSrcweir 	}
130cdf0e10cSrcweir 
131cdf0e10cSrcweir 	for( j=0 ; j < DOG_ROWS ; j++ )
132cdf0e10cSrcweir 	{
133cdf0e10cSrcweir 		m_aColumnList[j]->SelectEntryPos(0);
134cdf0e10cSrcweir 		m_aValueList[j]->SelectEntryPos(0);
135cdf0e10cSrcweir 	}
136cdf0e10cSrcweir 	try
137cdf0e10cSrcweir 	{
138cdf0e10cSrcweir 		// ... sowie auch die restlichen Felder
139cdf0e10cSrcweir 		Sequence< ::rtl::OUString> aNames = m_xColumns->getElementNames();
140cdf0e10cSrcweir 		const ::rtl::OUString* pIter = aNames.getConstArray();
141cdf0e10cSrcweir 		const ::rtl::OUString* pEnd   = pIter + aNames.getLength();
142cdf0e10cSrcweir 		Reference<XPropertySet> xColumn;
143cdf0e10cSrcweir 		for(;pIter != pEnd;++pIter)
144cdf0e10cSrcweir 		{
145cdf0e10cSrcweir 			xColumn.set(m_xColumns->getByName(*pIter),UNO_QUERY);
146cdf0e10cSrcweir 			OSL_ENSURE(xColumn.is(),"Column is null!");
147cdf0e10cSrcweir 			if ( xColumn.is() )
148cdf0e10cSrcweir 			{
149cdf0e10cSrcweir 				sal_Int32 nDataType = 0;
150cdf0e10cSrcweir 				xColumn->getPropertyValue(PROPERTY_TYPE) >>= nDataType;
151cdf0e10cSrcweir 				sal_Int32 eColumnSearch = dbtools::getSearchColumnFlag(m_xConnection,nDataType);
152cdf0e10cSrcweir 				if(eColumnSearch != ColumnSearch::NONE)
153cdf0e10cSrcweir 				{
154cdf0e10cSrcweir 					for( j=0 ; j < DOG_ROWS ; j++ )
155cdf0e10cSrcweir 					{
156cdf0e10cSrcweir 						m_aColumnList[j]->InsertEntry(*pIter);
157cdf0e10cSrcweir 					}
158cdf0e10cSrcweir 				}
159cdf0e10cSrcweir 			}
160cdf0e10cSrcweir 		}
161cdf0e10cSrcweir 
162cdf0e10cSrcweir 		m_sOrgOrder = m_xQueryComposer->getOrder();
163cdf0e10cSrcweir         impl_initializeOrderList_nothrow();
164cdf0e10cSrcweir 	}
165cdf0e10cSrcweir 	catch(const Exception&)
166cdf0e10cSrcweir 	{
167cdf0e10cSrcweir         DBG_UNHANDLED_EXCEPTION();
168cdf0e10cSrcweir 	}
169cdf0e10cSrcweir 	EnableLines();
170cdf0e10cSrcweir 
171cdf0e10cSrcweir 	aLB_ORDERFIELD1.SetSelectHdl(LINK(this,DlgOrderCrit,FieldListSelectHdl));
172cdf0e10cSrcweir 	aLB_ORDERFIELD2.SetSelectHdl(LINK(this,DlgOrderCrit,FieldListSelectHdl));
173cdf0e10cSrcweir 
174cdf0e10cSrcweir 	FreeResource();
175cdf0e10cSrcweir 
176cdf0e10cSrcweir }
177cdf0e10cSrcweir 
178cdf0e10cSrcweir //------------------------------------------------------------------------------
179cdf0e10cSrcweir 
180cdf0e10cSrcweir #define LbText(x)		((x).GetSelectEntry())
181cdf0e10cSrcweir #define LbPos(x)		((x).GetSelectEntryPos())
182cdf0e10cSrcweir 
183cdf0e10cSrcweir //------------------------------------------------------------------------------
184cdf0e10cSrcweir DlgOrderCrit::~DlgOrderCrit()
185cdf0e10cSrcweir {
186cdf0e10cSrcweir 	DBG_DTOR(DlgOrderCrit,NULL);
187cdf0e10cSrcweir }
188cdf0e10cSrcweir 
189cdf0e10cSrcweir //------------------------------------------------------------------------------
190cdf0e10cSrcweir IMPL_LINK_INLINE_START( DlgOrderCrit, FieldListSelectHdl, ListBox *, /*pListBox*/ )
191cdf0e10cSrcweir {
192cdf0e10cSrcweir 	DBG_CHKTHIS(DlgOrderCrit,NULL);
193cdf0e10cSrcweir 	EnableLines();
194cdf0e10cSrcweir 	return 0;
195cdf0e10cSrcweir }
196cdf0e10cSrcweir IMPL_LINK_INLINE_END( DlgOrderCrit, FieldListSelectHdl, ListBox *, pListBox )
197cdf0e10cSrcweir 
198cdf0e10cSrcweir //------------------------------------------------------------------------------
199cdf0e10cSrcweir void DlgOrderCrit::impl_initializeOrderList_nothrow()
200cdf0e10cSrcweir {
201cdf0e10cSrcweir     try
202cdf0e10cSrcweir     {
203cdf0e10cSrcweir         const ::rtl::OUString sNameProperty = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Name" ) );
204cdf0e10cSrcweir         const ::rtl::OUString sAscendingProperty = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "IsAscending" ) );
205cdf0e10cSrcweir 
206cdf0e10cSrcweir         Reference< XIndexAccess > xOrderColumns( m_xQueryComposer->getOrderColumns(), UNO_QUERY_THROW );
207cdf0e10cSrcweir         sal_Int32 nColumns = xOrderColumns->getCount();
208cdf0e10cSrcweir         if ( nColumns > DOG_ROWS )
209cdf0e10cSrcweir             nColumns = DOG_ROWS;
210cdf0e10cSrcweir 
211cdf0e10cSrcweir         for ( sal_Int32 i = 0; i < nColumns; ++i )
212cdf0e10cSrcweir         {
213cdf0e10cSrcweir             Reference< XPropertySet > xColumn( xOrderColumns->getByIndex( i ), UNO_QUERY_THROW );
214cdf0e10cSrcweir 
215cdf0e10cSrcweir             ::rtl::OUString sColumnName;
216cdf0e10cSrcweir             sal_Bool        bIsAscending( sal_True );
217cdf0e10cSrcweir 
218cdf0e10cSrcweir             xColumn->getPropertyValue( sNameProperty ) >>= sColumnName;
219cdf0e10cSrcweir             xColumn->getPropertyValue( sAscendingProperty ) >>= bIsAscending;
220cdf0e10cSrcweir 
221cdf0e10cSrcweir             m_aColumnList[i]->SelectEntry( sColumnName );
222cdf0e10cSrcweir             m_aValueList[i]->SelectEntryPos( bIsAscending ? 0 : 1 );
223cdf0e10cSrcweir         }
224cdf0e10cSrcweir     }
225cdf0e10cSrcweir     catch( const Exception& )
226cdf0e10cSrcweir     {
227cdf0e10cSrcweir     	DBG_UNHANDLED_EXCEPTION();
228cdf0e10cSrcweir     }
229cdf0e10cSrcweir }
230cdf0e10cSrcweir 
231cdf0e10cSrcweir //------------------------------------------------------------------------------
232cdf0e10cSrcweir void DlgOrderCrit::EnableLines()
233cdf0e10cSrcweir {
234cdf0e10cSrcweir 	DBG_CHKTHIS(DlgOrderCrit,NULL);
235cdf0e10cSrcweir 
236cdf0e10cSrcweir     if ( aLB_ORDERFIELD1.GetSelectEntryPos() == 0 )
237cdf0e10cSrcweir     {
238cdf0e10cSrcweir 		aLB_ORDERFIELD2.Disable();
239cdf0e10cSrcweir 		aLB_ORDERVALUE2.Disable();
240cdf0e10cSrcweir 
241cdf0e10cSrcweir         aLB_ORDERFIELD2.SelectEntryPos( 0 );
242cdf0e10cSrcweir 		aLB_ORDERVALUE2.SelectEntryPos( 0 );
243cdf0e10cSrcweir     }
244cdf0e10cSrcweir     else
245cdf0e10cSrcweir     {
246cdf0e10cSrcweir 		aLB_ORDERFIELD2.Enable();
247cdf0e10cSrcweir 		aLB_ORDERVALUE2.Enable();
248cdf0e10cSrcweir     }
249cdf0e10cSrcweir 
250cdf0e10cSrcweir     if ( aLB_ORDERFIELD2.GetSelectEntryPos() == 0 )
251cdf0e10cSrcweir     {
252cdf0e10cSrcweir 		aLB_ORDERFIELD3.Disable();
253cdf0e10cSrcweir 		aLB_ORDERVALUE3.Disable();
254cdf0e10cSrcweir 
255cdf0e10cSrcweir         aLB_ORDERFIELD3.SelectEntryPos( 0 );
256cdf0e10cSrcweir 		aLB_ORDERVALUE3.SelectEntryPos( 0 );
257cdf0e10cSrcweir     }
258cdf0e10cSrcweir     else
259cdf0e10cSrcweir     {
260cdf0e10cSrcweir 		aLB_ORDERFIELD3.Enable();
261cdf0e10cSrcweir 		aLB_ORDERVALUE3.Enable();
262cdf0e10cSrcweir     }
263cdf0e10cSrcweir }
264cdf0e10cSrcweir 
265cdf0e10cSrcweir //------------------------------------------------------------------------------
266cdf0e10cSrcweir ::rtl::OUString DlgOrderCrit::GetOrderList( ) const
267cdf0e10cSrcweir {
268cdf0e10cSrcweir 	DBG_CHKTHIS(DlgOrderCrit,NULL);
269cdf0e10cSrcweir 	Reference<XDatabaseMetaData> xMetaData = m_xConnection->getMetaData();
270cdf0e10cSrcweir 	::rtl::OUString sQuote	= xMetaData.is() ? xMetaData->getIdentifierQuoteString() : ::rtl::OUString();
271cdf0e10cSrcweir 	static const ::rtl::OUString sDESC = ::rtl::OUString::createFromAscii(" DESC ");
272cdf0e10cSrcweir 	static const ::rtl::OUString sASC  = ::rtl::OUString::createFromAscii(" ASC ");
273cdf0e10cSrcweir 
274cdf0e10cSrcweir 	Reference< XNameAccess> xColumns = Reference< XColumnsSupplier >(m_xQueryComposer,UNO_QUERY)->getColumns();
275cdf0e10cSrcweir 
276cdf0e10cSrcweir 	::rtl::OUString sOrder;
277cdf0e10cSrcweir 	for( sal_uInt16 i=0 ; i<DOG_ROWS; i++ )
278cdf0e10cSrcweir 	{
279cdf0e10cSrcweir 		if(m_aColumnList[i]->GetSelectEntryPos() != 0)
280cdf0e10cSrcweir 		{
281cdf0e10cSrcweir 			if(sOrder.getLength())
282cdf0e10cSrcweir 				sOrder += ::rtl::OUString::createFromAscii(",");
283cdf0e10cSrcweir 
284cdf0e10cSrcweir 			String sName = m_aColumnList[i]->GetSelectEntry();
285cdf0e10cSrcweir 			try
286cdf0e10cSrcweir 			{
287cdf0e10cSrcweir 				sal_Bool bFunction = sal_False;
288cdf0e10cSrcweir 				Reference< XPropertySet > xColumn;
289cdf0e10cSrcweir 				if ( xColumns.is() && xColumns->hasByName( sName ) && (xColumns->getByName( sName ) >>= xColumn) && xColumn.is() )
290cdf0e10cSrcweir 				{
291cdf0e10cSrcweir 					if ( xColumn->getPropertySetInfo()->hasPropertyByName(PROPERTY_REALNAME) )
292cdf0e10cSrcweir 					{
293cdf0e10cSrcweir 						::rtl::OUString sRealName;
294cdf0e10cSrcweir 						xColumn->getPropertyValue(PROPERTY_REALNAME)	>>= sRealName;
295cdf0e10cSrcweir 						sName = sRealName;
296cdf0e10cSrcweir 						static ::rtl::OUString sAgg(RTL_CONSTASCII_USTRINGPARAM("AggregateFunction"));
297cdf0e10cSrcweir 						static ::rtl::OUString sFunction(RTL_CONSTASCII_USTRINGPARAM("Function"));
298cdf0e10cSrcweir 						if ( xColumn->getPropertySetInfo()->hasPropertyByName(sFunction) )
299cdf0e10cSrcweir 							xColumn->getPropertyValue(sFunction) >>= bFunction;
300cdf0e10cSrcweir 					}
301cdf0e10cSrcweir 				}
302cdf0e10cSrcweir 				if ( bFunction )
303cdf0e10cSrcweir 					sOrder += sName;
304cdf0e10cSrcweir 				else
305cdf0e10cSrcweir 					sOrder += ::dbtools::quoteName(sQuote,sName);
306cdf0e10cSrcweir 			}
307cdf0e10cSrcweir 			catch(Exception)
308cdf0e10cSrcweir 			{
309cdf0e10cSrcweir 			}
310cdf0e10cSrcweir 			if(m_aValueList[i]->GetSelectEntryPos())
311cdf0e10cSrcweir 				sOrder += sDESC;
312cdf0e10cSrcweir 			else
313cdf0e10cSrcweir 				sOrder += sASC;
314cdf0e10cSrcweir 		}
315cdf0e10cSrcweir 	}
316cdf0e10cSrcweir 	return sOrder;
317cdf0e10cSrcweir }
318cdf0e10cSrcweir 
319cdf0e10cSrcweir //------------------------------------------------------------------------------
320cdf0e10cSrcweir void DlgOrderCrit::BuildOrderPart()
321cdf0e10cSrcweir {
322cdf0e10cSrcweir 	DBG_CHKTHIS(DlgOrderCrit,NULL);
323cdf0e10cSrcweir 	m_xQueryComposer->setOrder(GetOrderList());
324cdf0e10cSrcweir }
325cdf0e10cSrcweir // -----------------------------------------------------------------------------
326cdf0e10cSrcweir 
327cdf0e10cSrcweir 
328cdf0e10cSrcweir 
329cdf0e10cSrcweir 
330cdf0e10cSrcweir 
331