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