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 :
21 : #include "WColumnSelect.hxx"
22 : #include "dbu_misc.hrc"
23 : #include <osl/diagnose.h>
24 : #include "WizardPages.hrc"
25 : #include "WCopyTable.hxx"
26 : #include <com/sun/star/sdbcx/XDataDescriptorFactory.hpp>
27 : #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
28 : #include <com/sun/star/sdbcx/XAppend.hpp>
29 : #include "moduledbu.hxx"
30 : #include <com/sun/star/sdbc/DataType.hpp>
31 : #include <com/sun/star/sdbc/ColumnValue.hpp>
32 : #include <com/sun/star/sdb/application/CopyTableOperation.hpp>
33 : #include "dbustrings.hrc"
34 : #include <functional>
35 : #include <o3tl/compat_functional.hxx>
36 :
37 : using namespace ::com::sun::star::uno;
38 : using namespace ::com::sun::star::beans;
39 : using namespace ::com::sun::star::container;
40 : using namespace ::com::sun::star::sdbc;
41 : using namespace ::com::sun::star::sdbcx;
42 : using namespace dbaui;
43 :
44 : namespace CopyTableOperation = ::com::sun::star::sdb::application::CopyTableOperation;
45 :
46 : // -----------------------------------------------------------------------
47 0 : String OWizColumnSelect::GetTitle() const { return String(ModuleRes(STR_WIZ_COLUMN_SELECT_TITEL)); }
48 : // -----------------------------------------------------------------------------
49 0 : OWizardPage::OWizardPage( Window* pParent, const ResId& rResId )
50 : : TabPage(pParent,rResId)
51 : ,m_pParent(static_cast<OCopyTableWizard*>(pParent))
52 0 : ,m_bFirstTime(sal_True)
53 : {
54 0 : }
55 : //========================================================================
56 : // OWizColumnSelect
57 : DBG_NAME(OWizColumnSelect);
58 : //========================================================================
59 0 : OWizColumnSelect::OWizColumnSelect( Window* pParent)
60 : :OWizardPage( pParent, ModuleRes( TAB_WIZ_COLUMN_SELECT ))
61 : ,m_flColumns( this, ModuleRes( FL_COLUMN_SELECT ) )
62 : ,m_lbOrgColumnNames( this, ModuleRes( LB_ORG_COLUMN_NAMES ) )
63 : ,m_ibColumn_RH( this, ModuleRes( IB_COLUMN_RH ) )
64 : ,m_ibColumns_RH( this, ModuleRes( IB_COLUMNS_RH ) )
65 : ,m_ibColumn_LH( this, ModuleRes( IB_COLUMN_LH ) )
66 : ,m_ibColumns_LH( this, ModuleRes( IB_COLUMNS_LH ) )
67 0 : ,m_lbNewColumnNames( this, ModuleRes( LB_NEW_COLUMN_NAMES ) )
68 : {
69 : DBG_CTOR(OWizColumnSelect,NULL);
70 0 : m_ibColumn_RH.SetClickHdl(LINK(this,OWizColumnSelect,ButtonClickHdl));
71 0 : m_ibColumn_LH.SetClickHdl(LINK(this,OWizColumnSelect,ButtonClickHdl));
72 0 : m_ibColumns_RH.SetClickHdl(LINK(this,OWizColumnSelect,ButtonClickHdl));
73 0 : m_ibColumns_LH.SetClickHdl(LINK(this,OWizColumnSelect,ButtonClickHdl));
74 :
75 0 : m_lbOrgColumnNames.EnableMultiSelection(sal_True);
76 0 : m_lbNewColumnNames.EnableMultiSelection(sal_True);
77 :
78 0 : m_lbOrgColumnNames.SetDoubleClickHdl(LINK(this,OWizColumnSelect,ListDoubleClickHdl));
79 0 : m_lbNewColumnNames.SetDoubleClickHdl(LINK(this,OWizColumnSelect,ListDoubleClickHdl));
80 0 : FreeResource();
81 0 : }
82 : // -----------------------------------------------------------------------
83 0 : OWizColumnSelect::~OWizColumnSelect()
84 : {
85 : DBG_DTOR(OWizColumnSelect,NULL);
86 0 : while ( m_lbNewColumnNames.GetEntryCount() )
87 : {
88 0 : void* pData = m_lbNewColumnNames.GetEntryData(0);
89 0 : if ( pData )
90 0 : delete static_cast<OFieldDescription*>(pData);
91 :
92 0 : m_lbNewColumnNames.RemoveEntry(0);
93 : }
94 0 : m_lbNewColumnNames.Clear();
95 0 : }
96 :
97 : // -----------------------------------------------------------------------
98 0 : void OWizColumnSelect::Reset()
99 : {
100 : // restore original state
101 : DBG_CHKTHIS(OWizColumnSelect,NULL);
102 :
103 0 : clearListBox(m_lbOrgColumnNames);
104 0 : clearListBox(m_lbNewColumnNames);
105 0 : m_pParent->m_mNameMapping.clear();
106 :
107 : // insert the source columns in the left listbox
108 0 : const ODatabaseExport::TColumnVector* pSrcColumns = m_pParent->getSrcVector();
109 0 : ODatabaseExport::TColumnVector::const_iterator aIter = pSrcColumns->begin();
110 0 : ODatabaseExport::TColumnVector::const_iterator aEnd = pSrcColumns->end();
111 :
112 0 : for(;aIter != aEnd;++aIter)
113 : {
114 0 : sal_uInt16 nPos = m_lbOrgColumnNames.InsertEntry((*aIter)->first);
115 0 : m_lbOrgColumnNames.SetEntryData(nPos,(*aIter)->second);
116 : }
117 :
118 0 : if(m_lbOrgColumnNames.GetEntryCount())
119 0 : m_lbOrgColumnNames.SelectEntryPos(0);
120 :
121 0 : m_bFirstTime = sal_False;
122 0 : }
123 : // -----------------------------------------------------------------------
124 0 : void OWizColumnSelect::ActivatePage( )
125 : {
126 : DBG_CHKTHIS(OWizColumnSelect,NULL);
127 : // if there are no dest columns reset the left side with the origibnal columns
128 0 : if(m_pParent->getDestColumns()->empty())
129 0 : Reset();
130 :
131 0 : clearListBox(m_lbNewColumnNames);
132 :
133 0 : const ODatabaseExport::TColumnVector* pDestColumns = m_pParent->getDestVector();
134 :
135 0 : ODatabaseExport::TColumnVector::const_iterator aIter = pDestColumns->begin();
136 0 : ODatabaseExport::TColumnVector::const_iterator aEnd = pDestColumns->end();
137 0 : for(;aIter != aEnd;++aIter)
138 : {
139 0 : sal_uInt16 nPos = m_lbNewColumnNames.InsertEntry((*aIter)->first);
140 0 : m_lbNewColumnNames.SetEntryData(nPos,new OFieldDescription(*((*aIter)->second)));
141 0 : m_lbOrgColumnNames.RemoveEntry((*aIter)->first);
142 : }
143 0 : m_pParent->GetOKButton().Enable(m_lbNewColumnNames.GetEntryCount() != 0);
144 0 : m_pParent->EnableButton(OCopyTableWizard::WIZARD_NEXT,m_lbNewColumnNames.GetEntryCount() && m_pParent->getOperation() != CopyTableOperation::AppendData);
145 0 : m_ibColumns_RH.GrabFocus();
146 0 : }
147 : // -----------------------------------------------------------------------
148 0 : sal_Bool OWizColumnSelect::LeavePage()
149 : {
150 : DBG_CHKTHIS(OWizColumnSelect,NULL);
151 :
152 0 : m_pParent->clearDestColumns();
153 :
154 0 : for(sal_uInt16 i=0 ; i< m_lbNewColumnNames.GetEntryCount();++i)
155 : {
156 0 : OFieldDescription* pField = static_cast<OFieldDescription*>(m_lbNewColumnNames.GetEntryData(i));
157 : OSL_ENSURE(pField,"The field information can not be null!");
158 0 : m_pParent->insertColumn(i,pField);
159 : }
160 :
161 0 : clearListBox(m_lbNewColumnNames);
162 :
163 :
164 0 : if ( m_pParent->GetPressedButton() == OCopyTableWizard::WIZARD_NEXT
165 0 : || m_pParent->GetPressedButton() == OCopyTableWizard::WIZARD_FINISH
166 : )
167 0 : return m_pParent->getDestColumns()->size() != 0;
168 : else
169 0 : return sal_True;
170 : }
171 : // -----------------------------------------------------------------------
172 0 : IMPL_LINK( OWizColumnSelect, ButtonClickHdl, Button *, pButton )
173 : {
174 0 : MultiListBox *pLeft = NULL;
175 0 : MultiListBox *pRight = NULL;
176 0 : sal_Bool bAll = sal_False;
177 :
178 0 : if(pButton == &m_ibColumn_RH)
179 : {
180 0 : pLeft = &m_lbOrgColumnNames;
181 0 : pRight = &m_lbNewColumnNames;
182 : }
183 0 : else if(pButton == &m_ibColumn_LH)
184 : {
185 0 : pLeft = &m_lbNewColumnNames;
186 0 : pRight = &m_lbOrgColumnNames;
187 : }
188 0 : else if(pButton == &m_ibColumns_RH)
189 : {
190 0 : pLeft = &m_lbOrgColumnNames;
191 0 : pRight = &m_lbNewColumnNames;
192 0 : bAll = sal_True;
193 : }
194 0 : else if(pButton == &m_ibColumns_LH)
195 : {
196 0 : pLeft = &m_lbNewColumnNames;
197 0 : pRight = &m_lbOrgColumnNames;
198 0 : bAll = sal_True;
199 : }
200 : // else ????
201 :
202 0 : Reference< XDatabaseMetaData > xMetaData( m_pParent->m_xDestConnection->getMetaData() );
203 0 : OUString sExtraChars = xMetaData->getExtraNameCharacters();
204 0 : sal_Int32 nMaxNameLen = m_pParent->getMaxColumnNameLength();
205 :
206 0 : ::comphelper::TStringMixEqualFunctor aCase(xMetaData->supportsMixedCaseQuotedIdentifiers());
207 0 : ::std::vector< OUString> aRightColumns;
208 0 : fillColumns(pRight,aRightColumns);
209 :
210 0 : if(!bAll)
211 : {
212 0 : for(sal_uInt16 i=0; i < pLeft->GetSelectEntryCount(); ++i)
213 0 : moveColumn(pRight,pLeft,aRightColumns,pLeft->GetSelectEntry(i),sExtraChars,nMaxNameLen,aCase);
214 :
215 0 : for(sal_uInt16 j=pLeft->GetSelectEntryCount(); j ; --j)
216 0 : pLeft->RemoveEntry(pLeft->GetSelectEntry(j-1));
217 : }
218 : else
219 : {
220 0 : sal_uInt16 nEntries = pLeft->GetEntryCount();
221 0 : for(sal_uInt16 i=0; i < nEntries; ++i)
222 0 : moveColumn(pRight,pLeft,aRightColumns,pLeft->GetEntry(i),sExtraChars,nMaxNameLen,aCase);
223 0 : for(sal_uInt16 j=pLeft->GetEntryCount(); j ; --j)
224 0 : pLeft->RemoveEntry(j-1);
225 : }
226 :
227 0 : enableButtons();
228 :
229 0 : if(m_lbOrgColumnNames.GetEntryCount())
230 0 : m_lbOrgColumnNames.SelectEntryPos(0);
231 :
232 0 : return 0;
233 : }
234 : // -----------------------------------------------------------------------
235 0 : IMPL_LINK( OWizColumnSelect, ListDoubleClickHdl, MultiListBox *, pListBox )
236 : {
237 : MultiListBox *pLeft,*pRight;
238 0 : if(pListBox == &m_lbOrgColumnNames)
239 : {
240 0 : pLeft = &m_lbOrgColumnNames;
241 0 : pRight = &m_lbNewColumnNames;
242 : }
243 : else
244 : {
245 0 : pRight = &m_lbOrgColumnNames;
246 0 : pLeft = &m_lbNewColumnNames;
247 : }
248 :
249 : //////////////////////////////////////////////////////////////////////
250 : // If database is able to process PrimaryKeys, set PrimaryKey
251 0 : Reference< XDatabaseMetaData > xMetaData( m_pParent->m_xDestConnection->getMetaData() );
252 0 : OUString sExtraChars = xMetaData->getExtraNameCharacters();
253 0 : sal_Int32 nMaxNameLen = m_pParent->getMaxColumnNameLength();
254 :
255 0 : ::comphelper::TStringMixEqualFunctor aCase(xMetaData->supportsMixedCaseQuotedIdentifiers());
256 0 : ::std::vector< OUString> aRightColumns;
257 0 : fillColumns(pRight,aRightColumns);
258 :
259 0 : for(sal_uInt16 i=0; i < pLeft->GetSelectEntryCount(); ++i)
260 0 : moveColumn(pRight,pLeft,aRightColumns,pLeft->GetSelectEntry(i),sExtraChars,nMaxNameLen,aCase);
261 0 : for(sal_uInt16 j=pLeft->GetSelectEntryCount(); j ; --j)
262 0 : pLeft->RemoveEntry(pLeft->GetSelectEntry(j-1));
263 :
264 0 : enableButtons();
265 0 : return 0;
266 : }
267 : // -----------------------------------------------------------------------------
268 0 : void OWizColumnSelect::clearListBox(MultiListBox& _rListBox)
269 : {
270 0 : while(_rListBox.GetEntryCount())
271 0 : _rListBox.RemoveEntry(0);
272 0 : _rListBox.Clear();
273 0 : }
274 : // -----------------------------------------------------------------------------
275 0 : void OWizColumnSelect::fillColumns(ListBox* pRight,::std::vector< OUString> &_rRightColumns)
276 : {
277 0 : sal_uInt16 nCount = pRight->GetEntryCount();
278 0 : _rRightColumns.reserve(nCount);
279 0 : for(sal_uInt16 i=0; i < nCount;++i)
280 0 : _rRightColumns.push_back(pRight->GetEntry(i));
281 0 : }
282 : // -----------------------------------------------------------------------------
283 0 : void OWizColumnSelect::createNewColumn( ListBox* _pListbox,
284 : OFieldDescription* _pSrcField,
285 : ::std::vector< OUString>& _rRightColumns,
286 : const OUString& _sColumnName,
287 : const OUString& _sExtraChars,
288 : sal_Int32 _nMaxNameLen,
289 : const ::comphelper::TStringMixEqualFunctor& _aCase)
290 : {
291 : OUString sConvertedName = m_pParent->convertColumnName(TMultiListBoxEntryFindFunctor(&_rRightColumns,_aCase),
292 : _sColumnName,
293 : _sExtraChars,
294 0 : _nMaxNameLen);
295 0 : OFieldDescription* pNewField = new OFieldDescription(*_pSrcField);
296 0 : pNewField->SetName(sConvertedName);
297 0 : sal_Bool bNotConvert = sal_True;
298 0 : pNewField->SetType(m_pParent->convertType(_pSrcField->getSpecialTypeInfo(),bNotConvert));
299 0 : if ( !m_pParent->supportsPrimaryKey() )
300 0 : pNewField->SetPrimaryKey(sal_False);
301 :
302 0 : _pListbox->SetEntryData(_pListbox->InsertEntry(sConvertedName),pNewField);
303 0 : _rRightColumns.push_back(sConvertedName);
304 :
305 0 : if ( !bNotConvert )
306 0 : m_pParent->showColumnTypeNotSupported(sConvertedName);
307 0 : }
308 : // -----------------------------------------------------------------------------
309 0 : void OWizColumnSelect::moveColumn( ListBox* _pRight,
310 : ListBox* _pLeft,
311 : ::std::vector< OUString>& _rRightColumns,
312 : const OUString& _sColumnName,
313 : const OUString& _sExtraChars,
314 : sal_Int32 _nMaxNameLen,
315 : const ::comphelper::TStringMixEqualFunctor& _aCase)
316 : {
317 0 : if(_pRight == &m_lbNewColumnNames)
318 : {
319 : // we copy the column into the new format for the dest
320 0 : OFieldDescription* pSrcField = static_cast<OFieldDescription*>(_pLeft->GetEntryData(_pLeft->GetEntryPos(String(_sColumnName))));
321 0 : createNewColumn(_pRight,pSrcField,_rRightColumns,_sColumnName,_sExtraChars,_nMaxNameLen,_aCase);
322 : }
323 : else
324 : {
325 : // find the new column in the dest name mapping to obtain the old column
326 : OCopyTableWizard::TNameMapping::iterator aIter = ::std::find_if(m_pParent->m_mNameMapping.begin(),m_pParent->m_mNameMapping.end(),
327 : ::o3tl::compose1(
328 : ::std::bind2nd(_aCase, _sColumnName),
329 : ::o3tl::select2nd<OCopyTableWizard::TNameMapping::value_type>())
330 0 : );
331 :
332 : OSL_ENSURE(aIter != m_pParent->m_mNameMapping.end(),"Column must be defined");
333 0 : if ( aIter == m_pParent->m_mNameMapping.end() )
334 0 : return; // do nothing
335 0 : const ODatabaseExport::TColumns* pSrcColumns = m_pParent->getSourceColumns();
336 0 : ODatabaseExport::TColumns::const_iterator aSrcIter = pSrcColumns->find((*aIter).first);
337 0 : if ( aSrcIter != pSrcColumns->end() )
338 : {
339 : // we need also the old position of this column to insert it back on that position again
340 0 : const ODatabaseExport::TColumnVector* pSrcVector = m_pParent->getSrcVector();
341 0 : ODatabaseExport::TColumnVector::const_iterator aPos = ::std::find(pSrcVector->begin(),pSrcVector->end(),aSrcIter);
342 : OSL_ENSURE( aPos != pSrcVector->end(),"Invalid position for the iterator here!");
343 0 : ODatabaseExport::TColumnVector::size_type nPos = (aPos - pSrcVector->begin()) - adjustColumnPosition(_pLeft, _sColumnName, (aPos - pSrcVector->begin()), _aCase);
344 :
345 0 : _pRight->SetEntryData( _pRight->InsertEntry( (*aIter).first, sal::static_int_cast< sal_uInt16 >(nPos)),aSrcIter->second );
346 0 : _rRightColumns.push_back((*aIter).first);
347 0 : m_pParent->removeColumnNameFromNameMap(_sColumnName);
348 : }
349 : }
350 : }
351 : // -----------------------------------------------------------------------------
352 : // Simply returning fields back to their original position is
353 : // not enough. We need to take into acccount what fields have
354 : // been removed earlier and adjust accordingly. Based on the
355 : // algorithm employed in moveColumn().
356 0 : sal_uInt16 OWizColumnSelect::adjustColumnPosition( ListBox* _pLeft,
357 : const OUString& _sColumnName,
358 : ODatabaseExport::TColumnVector::size_type nCurrentPos,
359 : const ::comphelper::TStringMixEqualFunctor& _aCase)
360 : {
361 0 : sal_uInt16 nAdjustedPos = 0;
362 :
363 : // if returning all entries to their original position,
364 : // then there is no need to adjust the positions.
365 0 : if (m_ibColumns_LH.HasFocus())
366 0 : return nAdjustedPos;
367 :
368 0 : sal_uInt16 nCount = _pLeft->GetEntryCount();
369 0 : OUString sColumnString;
370 0 : for(sal_uInt16 i=0; i < nCount; ++i)
371 : {
372 0 : sColumnString = _pLeft->GetEntry(i);
373 0 : if(_sColumnName != sColumnString)
374 : {
375 : // find the new column in the dest name mapping to obtain the old column
376 : OCopyTableWizard::TNameMapping::iterator aIter = ::std::find_if(m_pParent->m_mNameMapping.begin(),m_pParent->m_mNameMapping.end(),
377 : ::o3tl::compose1(
378 : ::std::bind2nd(_aCase, sColumnString),
379 : ::o3tl::select2nd<OCopyTableWizard::TNameMapping::value_type>())
380 0 : );
381 :
382 : OSL_ENSURE(aIter != m_pParent->m_mNameMapping.end(),"Column must be defined");
383 0 : const ODatabaseExport::TColumns* pSrcColumns = m_pParent->getSourceColumns();
384 0 : ODatabaseExport::TColumns::const_iterator aSrcIter = pSrcColumns->find((*aIter).first);
385 0 : if ( aSrcIter != pSrcColumns->end() )
386 : {
387 : // we need also the old position of this column to insert it back on that position again
388 0 : const ODatabaseExport::TColumnVector* pSrcVector = m_pParent->getSrcVector();
389 0 : ODatabaseExport::TColumnVector::const_iterator aPos = ::std::find(pSrcVector->begin(),pSrcVector->end(),aSrcIter);
390 0 : ODatabaseExport::TColumnVector::size_type nPos = aPos - pSrcVector->begin();
391 0 : if( nPos < nCurrentPos)
392 : {
393 0 : nAdjustedPos++;
394 : }
395 : }
396 : }
397 : }
398 :
399 0 : return nAdjustedPos;
400 : }
401 : // -----------------------------------------------------------------------------
402 0 : void OWizColumnSelect::enableButtons()
403 : {
404 0 : sal_Bool bEntries = m_lbNewColumnNames.GetEntryCount() != 0;
405 0 : if(!bEntries)
406 0 : m_pParent->m_mNameMapping.clear();
407 :
408 0 : m_pParent->GetOKButton().Enable(bEntries);
409 0 : m_pParent->EnableButton(OCopyTableWizard::WIZARD_NEXT,bEntries && m_pParent->getOperation() != CopyTableOperation::AppendData);
410 12 : }
411 : // -----------------------------------------------------------------------------
412 :
413 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|