Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "dbu_tbl.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>
33 : #include <vcl/settings.hxx>
34 : #include <algorithm>
35 :
36 : using namespace dbaui;
37 : using namespace connectivity;
38 : using namespace ::com::sun::star::uno;
39 : using namespace ::com::sun::star::container;
40 : using namespace ::com::sun::star::util;
41 : using namespace ::com::sun::star::sdb;
42 : using namespace ::com::sun::star::sdbc;
43 : using namespace ::com::sun::star::sdbcx;
44 : using namespace ::com::sun::star::beans;
45 :
46 :
47 0 : DlgOrderCrit::DlgOrderCrit(Window * pParent,
48 : const Reference< XConnection>& _rxConnection,
49 : const Reference< XSingleSelectQueryComposer >& _rxComposer,
50 : const Reference< XNameAccess>& _rxCols)
51 : : ModalDialog(pParent, "SortDialog", "dbaccess/ui/sortdialog.ui")
52 : , aSTR_NOENTRY(ModuleRes(STR_VALUE_NONE))
53 : , m_xQueryComposer(_rxComposer)
54 : , m_xColumns(_rxCols)
55 0 : , m_xConnection(_rxConnection)
56 : {
57 :
58 0 : get(m_pLB_ORDERFIELD1, "field1");
59 0 : get(m_pLB_ORDERVALUE1, "value1");
60 0 : get(m_pLB_ORDERFIELD2, "field2");
61 0 : get(m_pLB_ORDERVALUE2, "value2");
62 0 : get(m_pLB_ORDERFIELD3, "field3");
63 0 : get(m_pLB_ORDERVALUE3, "value3");
64 :
65 0 : AllSettings aSettings( GetSettings() );
66 0 : StyleSettings aStyle( aSettings.GetStyleSettings() );
67 0 : aStyle.SetAutoMnemonic( false );
68 0 : aSettings.SetStyleSettings( aStyle );
69 0 : SetSettings( aSettings );
70 :
71 0 : m_aColumnList[0] = m_pLB_ORDERFIELD1;
72 0 : m_aColumnList[1] = m_pLB_ORDERFIELD2;
73 0 : m_aColumnList[2] = m_pLB_ORDERFIELD3;
74 :
75 0 : m_aValueList[0] = m_pLB_ORDERVALUE1;
76 0 : m_aValueList[1] = m_pLB_ORDERVALUE2;
77 0 : m_aValueList[2] = m_pLB_ORDERVALUE3;
78 :
79 0 : for (int j=0; j < DOG_ROWS; ++j)
80 : {
81 0 : m_aColumnList[j]->InsertEntry( aSTR_NOENTRY );
82 : }
83 :
84 0 : for (int j=0; j < DOG_ROWS; ++j)
85 : {
86 0 : m_aColumnList[j]->SelectEntryPos(0);
87 0 : m_aValueList[j]->SelectEntryPos(0);
88 : }
89 : try
90 : {
91 : // ... also the remaining fields
92 0 : Sequence< OUString> aNames = m_xColumns->getElementNames();
93 0 : const OUString* pIter = aNames.getConstArray();
94 0 : const OUString* pEnd = pIter + aNames.getLength();
95 0 : Reference<XPropertySet> xColumn;
96 0 : for(;pIter != pEnd;++pIter)
97 : {
98 0 : xColumn.set(m_xColumns->getByName(*pIter),UNO_QUERY);
99 : OSL_ENSURE(xColumn.is(),"Column is null!");
100 0 : if ( xColumn.is() )
101 : {
102 0 : sal_Int32 nDataType = 0;
103 0 : xColumn->getPropertyValue(PROPERTY_TYPE) >>= nDataType;
104 0 : sal_Int32 eColumnSearch = dbtools::getSearchColumnFlag(m_xConnection,nDataType);
105 0 : if(eColumnSearch != ColumnSearch::NONE)
106 : {
107 0 : for (int j=0; j < DOG_ROWS; ++j)
108 : {
109 0 : m_aColumnList[j]->InsertEntry(*pIter);
110 : }
111 : }
112 : }
113 : }
114 :
115 0 : m_sOrgOrder = m_xQueryComposer->getOrder();
116 0 : impl_initializeOrderList_nothrow();
117 : }
118 0 : catch(const Exception&)
119 : {
120 : DBG_UNHANDLED_EXCEPTION();
121 : }
122 0 : EnableLines();
123 :
124 0 : m_pLB_ORDERFIELD1->SetSelectHdl(LINK(this,DlgOrderCrit,FieldListSelectHdl));
125 0 : m_pLB_ORDERFIELD2->SetSelectHdl(LINK(this,DlgOrderCrit,FieldListSelectHdl));
126 0 : }
127 :
128 0 : DlgOrderCrit::~DlgOrderCrit()
129 : {
130 0 : }
131 :
132 0 : IMPL_LINK_INLINE_START( DlgOrderCrit, FieldListSelectHdl, ListBox *, /*pListBox*/ )
133 : {
134 0 : EnableLines();
135 0 : return 0;
136 : }
137 :
138 0 : IMPL_LINK_INLINE_END( DlgOrderCrit, FieldListSelectHdl, ListBox *, pListBox )
139 :
140 0 : void DlgOrderCrit::impl_initializeOrderList_nothrow()
141 : {
142 : try
143 : {
144 0 : const OUString sNameProperty = "Name";
145 0 : const OUString sAscendingProperty = "IsAscending";
146 :
147 0 : Reference< XIndexAccess > xOrderColumns( m_xQueryComposer->getOrderColumns(), UNO_QUERY_THROW );
148 0 : sal_Int32 nColumns = xOrderColumns->getCount();
149 0 : if ( nColumns > DOG_ROWS )
150 0 : nColumns = DOG_ROWS;
151 :
152 0 : for ( sal_Int32 i = 0; i < nColumns; ++i )
153 : {
154 0 : Reference< XPropertySet > xColumn( xOrderColumns->getByIndex( i ), UNO_QUERY_THROW );
155 :
156 0 : OUString sColumnName;
157 0 : sal_Bool bIsAscending( sal_True );
158 :
159 0 : xColumn->getPropertyValue( sNameProperty ) >>= sColumnName;
160 0 : xColumn->getPropertyValue( sAscendingProperty ) >>= bIsAscending;
161 :
162 0 : m_aColumnList[i]->SelectEntry( sColumnName );
163 0 : m_aValueList[i]->SelectEntryPos( bIsAscending ? 0 : 1 );
164 0 : }
165 : }
166 0 : catch( const Exception& )
167 : {
168 : DBG_UNHANDLED_EXCEPTION();
169 : }
170 0 : }
171 :
172 0 : void DlgOrderCrit::EnableLines()
173 : {
174 :
175 0 : if ( m_pLB_ORDERFIELD1->GetSelectEntryPos() == 0 )
176 : {
177 0 : m_pLB_ORDERFIELD2->Disable();
178 0 : m_pLB_ORDERVALUE2->Disable();
179 :
180 0 : m_pLB_ORDERFIELD2->SelectEntryPos( 0 );
181 0 : m_pLB_ORDERVALUE2->SelectEntryPos( 0 );
182 : }
183 : else
184 : {
185 0 : m_pLB_ORDERFIELD2->Enable();
186 0 : m_pLB_ORDERVALUE2->Enable();
187 : }
188 :
189 0 : if ( m_pLB_ORDERFIELD2->GetSelectEntryPos() == 0 )
190 : {
191 0 : m_pLB_ORDERFIELD3->Disable();
192 0 : m_pLB_ORDERVALUE3->Disable();
193 :
194 0 : m_pLB_ORDERFIELD3->SelectEntryPos( 0 );
195 0 : m_pLB_ORDERVALUE3->SelectEntryPos( 0 );
196 : }
197 : else
198 : {
199 0 : m_pLB_ORDERFIELD3->Enable();
200 0 : m_pLB_ORDERVALUE3->Enable();
201 : }
202 0 : }
203 :
204 0 : OUString DlgOrderCrit::GetOrderList( ) const
205 : {
206 0 : Reference<XDatabaseMetaData> xMetaData = m_xConnection->getMetaData();
207 0 : OUString sQuote = xMetaData.is() ? xMetaData->getIdentifierQuoteString() : OUString();
208 0 : static const OUString sDESC(" DESC ");
209 0 : static const OUString sASC(" ASC ");
210 :
211 0 : Reference< XNameAccess> xColumns = Reference< XColumnsSupplier >(m_xQueryComposer,UNO_QUERY)->getColumns();
212 :
213 0 : OUString sOrder;
214 0 : for( sal_uInt16 i=0 ; i<DOG_ROWS; i++ )
215 : {
216 0 : if(m_aColumnList[i]->GetSelectEntryPos() != 0)
217 : {
218 0 : if(!sOrder.isEmpty())
219 0 : sOrder += ",";
220 :
221 0 : OUString sName = m_aColumnList[i]->GetSelectEntry();
222 0 : sOrder += ::dbtools::quoteName(sQuote,sName);
223 0 : if(m_aValueList[i]->GetSelectEntryPos())
224 0 : sOrder += sDESC;
225 : else
226 0 : sOrder += sASC;
227 : }
228 : }
229 0 : return sOrder;
230 : }
231 :
232 0 : void DlgOrderCrit::BuildOrderPart()
233 : {
234 0 : m_xQueryComposer->setOrder(GetOrderList());
235 0 : }
236 :
237 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|