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