1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "queryorder.hrc"
21 #include "queryorder.hxx"
22 #include "dbustrings.hrc"
23 #include <com/sun/star/sdb/XSingleSelectQueryComposer.hpp>
24 #include <com/sun/star/sdbc/ColumnSearch.hpp>
25 #include <com/sun/star/container/XNameAccess.hpp>
26 #include <tools/debug.hxx>
27 #include "moduledbu.hxx"
28 #include <connectivity/sqliterator.hxx>
29 #include <connectivity/dbtools.hxx>
30 #include <comphelper/extract.hxx>
31 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
32 #include <tools/diagnose_ex.h>
35 using namespace dbaui
;
36 using namespace connectivity
;
37 using namespace ::com::sun::star::uno
;
38 using namespace ::com::sun::star::container
;
39 using namespace ::com::sun::star::util
;
40 using namespace ::com::sun::star::sdb
;
41 using namespace ::com::sun::star::sdbc
;
42 using namespace ::com::sun::star::sdbcx
;
43 using namespace ::com::sun::star::beans
;
45 DBG_NAME(DlgOrderCrit
)
47 DlgOrderCrit::DlgOrderCrit( Window
* pParent
,
48 const Reference
< XConnection
>& _rxConnection
,
49 const Reference
< XSingleSelectQueryComposer
>& _rxComposer
,
50 const Reference
< XNameAccess
>& _rxCols
)
51 :ModalDialog( pParent
, ModuleRes(DLG_ORDERCRIT
) )
52 ,aLB_ORDERFIELD1( this, ModuleRes( LB_ORDERFIELD1
) )
53 ,aLB_ORDERVALUE1( this, ModuleRes( LB_ORDERVALUE1
) )
54 ,aLB_ORDERFIELD2( this, ModuleRes( LB_ORDERFIELD2
) )
55 ,aLB_ORDERVALUE2( this, ModuleRes( LB_ORDERVALUE2
) )
56 ,aLB_ORDERFIELD3( this, ModuleRes( LB_ORDERFIELD3
) )
57 ,aLB_ORDERVALUE3( this, ModuleRes( LB_ORDERVALUE3
) )
58 ,aFT_ORDERFIELD( this, ModuleRes( FT_ORDERFIELD
) )
59 ,aFT_ORDERAFTER1( this, ModuleRes( FT_ORDERAFTER1
) )
60 ,aFT_ORDERAFTER2( this, ModuleRes( FT_ORDERAFTER2
) )
61 ,aFT_ORDEROPER( this, ModuleRes( FT_ORDEROPER
) )
62 ,aFT_ORDERDIR( this, ModuleRes( FT_ORDERDIR
) )
63 ,aBT_OK( this, ModuleRes( BT_OK
) )
64 ,aBT_CANCEL( this, ModuleRes( BT_CANCEL
) )
65 ,aBT_HELP( this, ModuleRes( BT_HELP
) )
66 ,aFL_ORDER( this, ModuleRes( FL_ORDER
) )
67 ,aSTR_NOENTRY( ModuleRes( STR_NOENTRY
) )
68 ,m_xQueryComposer( _rxComposer
)
70 ,m_xConnection(_rxConnection
)
72 DBG_CTOR(DlgOrderCrit
,NULL
);
74 AllSettings
aSettings( GetSettings() );
75 StyleSettings
aStyle( aSettings
.GetStyleSettings() );
76 aStyle
.SetAutoMnemonic( sal_False
);
77 aSettings
.SetStyleSettings( aStyle
);
78 SetSettings( aSettings
);
80 m_aColumnList
[0] = &aLB_ORDERFIELD1
;
81 m_aColumnList
[1] = &aLB_ORDERFIELD2
;
82 m_aColumnList
[2] = &aLB_ORDERFIELD3
;
84 m_aValueList
[0] = &aLB_ORDERVALUE1
;
85 m_aValueList
[1] = &aLB_ORDERVALUE2
;
86 m_aValueList
[2] = &aLB_ORDERVALUE3
;
89 for(j
=0 ; j
< DOG_ROWS
; j
++ )
91 m_aColumnList
[j
]->InsertEntry( aSTR_NOENTRY
);
94 for( j
=0 ; j
< DOG_ROWS
; j
++ )
96 m_aColumnList
[j
]->SelectEntryPos(0);
97 m_aValueList
[j
]->SelectEntryPos(0);
101 // ... also the remaining fields
102 Sequence
< OUString
> aNames
= m_xColumns
->getElementNames();
103 const OUString
* pIter
= aNames
.getConstArray();
104 const OUString
* pEnd
= pIter
+ aNames
.getLength();
105 Reference
<XPropertySet
> xColumn
;
106 for(;pIter
!= pEnd
;++pIter
)
108 xColumn
.set(m_xColumns
->getByName(*pIter
),UNO_QUERY
);
109 OSL_ENSURE(xColumn
.is(),"Column is null!");
112 sal_Int32 nDataType
= 0;
113 xColumn
->getPropertyValue(PROPERTY_TYPE
) >>= nDataType
;
114 sal_Int32 eColumnSearch
= dbtools::getSearchColumnFlag(m_xConnection
,nDataType
);
115 if(eColumnSearch
!= ColumnSearch::NONE
)
117 for( j
=0 ; j
< DOG_ROWS
; j
++ )
119 m_aColumnList
[j
]->InsertEntry(*pIter
);
125 m_sOrgOrder
= m_xQueryComposer
->getOrder();
126 impl_initializeOrderList_nothrow();
128 catch(const Exception
&)
130 DBG_UNHANDLED_EXCEPTION();
134 aLB_ORDERFIELD1
.SetSelectHdl(LINK(this,DlgOrderCrit
,FieldListSelectHdl
));
135 aLB_ORDERFIELD2
.SetSelectHdl(LINK(this,DlgOrderCrit
,FieldListSelectHdl
));
140 DlgOrderCrit::~DlgOrderCrit()
142 DBG_DTOR(DlgOrderCrit
,NULL
);
145 IMPL_LINK_INLINE_START( DlgOrderCrit
, FieldListSelectHdl
, ListBox
*, /*pListBox*/ )
147 DBG_CHKTHIS(DlgOrderCrit
,NULL
);
151 IMPL_LINK_INLINE_END( DlgOrderCrit
, FieldListSelectHdl
, ListBox
*, pListBox
)
153 void DlgOrderCrit::impl_initializeOrderList_nothrow()
157 const OUString sNameProperty
= OUString( "Name" );
158 const OUString sAscendingProperty
= OUString( "IsAscending" );
160 Reference
< XIndexAccess
> xOrderColumns( m_xQueryComposer
->getOrderColumns(), UNO_QUERY_THROW
);
161 sal_Int32 nColumns
= xOrderColumns
->getCount();
162 if ( nColumns
> DOG_ROWS
)
165 for ( sal_Int32 i
= 0; i
< nColumns
; ++i
)
167 Reference
< XPropertySet
> xColumn( xOrderColumns
->getByIndex( i
), UNO_QUERY_THROW
);
169 OUString sColumnName
;
170 sal_Bool
bIsAscending( sal_True
);
172 xColumn
->getPropertyValue( sNameProperty
) >>= sColumnName
;
173 xColumn
->getPropertyValue( sAscendingProperty
) >>= bIsAscending
;
175 m_aColumnList
[i
]->SelectEntry( sColumnName
);
176 m_aValueList
[i
]->SelectEntryPos( bIsAscending
? 0 : 1 );
179 catch( const Exception
& )
181 DBG_UNHANDLED_EXCEPTION();
185 void DlgOrderCrit::EnableLines()
187 DBG_CHKTHIS(DlgOrderCrit
,NULL
);
189 if ( aLB_ORDERFIELD1
.GetSelectEntryPos() == 0 )
191 aLB_ORDERFIELD2
.Disable();
192 aLB_ORDERVALUE2
.Disable();
194 aLB_ORDERFIELD2
.SelectEntryPos( 0 );
195 aLB_ORDERVALUE2
.SelectEntryPos( 0 );
199 aLB_ORDERFIELD2
.Enable();
200 aLB_ORDERVALUE2
.Enable();
203 if ( aLB_ORDERFIELD2
.GetSelectEntryPos() == 0 )
205 aLB_ORDERFIELD3
.Disable();
206 aLB_ORDERVALUE3
.Disable();
208 aLB_ORDERFIELD3
.SelectEntryPos( 0 );
209 aLB_ORDERVALUE3
.SelectEntryPos( 0 );
213 aLB_ORDERFIELD3
.Enable();
214 aLB_ORDERVALUE3
.Enable();
218 OUString
DlgOrderCrit::GetOrderList( ) const
220 DBG_CHKTHIS(DlgOrderCrit
,NULL
);
221 Reference
<XDatabaseMetaData
> xMetaData
= m_xConnection
->getMetaData();
222 OUString sQuote
= xMetaData
.is() ? xMetaData
->getIdentifierQuoteString() : OUString();
223 static const OUString
sDESC(" DESC ");
224 static const OUString
sASC(" ASC ");
226 Reference
< XNameAccess
> xColumns
= Reference
< XColumnsSupplier
>(m_xQueryComposer
,UNO_QUERY
)->getColumns();
229 for( sal_uInt16 i
=0 ; i
<DOG_ROWS
; i
++ )
231 if(m_aColumnList
[i
]->GetSelectEntryPos() != 0)
233 if(!sOrder
.isEmpty())
234 sOrder
+= OUString(",");
236 String sName
= m_aColumnList
[i
]->GetSelectEntry();
237 sOrder
+= ::dbtools::quoteName(sQuote
,sName
);
238 if(m_aValueList
[i
]->GetSelectEntryPos())
247 void DlgOrderCrit::BuildOrderPart()
249 DBG_CHKTHIS(DlgOrderCrit
,NULL
);
250 m_xQueryComposer
->setOrder(GetOrderList());
253 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */