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 : ::rtl::OUString sExtraChars = xMetaData->getExtraNameCharacters();
204 0 : sal_Int32 nMaxNameLen = m_pParent->getMaxColumnNameLength();
205 :
206 0 : ::comphelper::TStringMixEqualFunctor aCase(xMetaData->supportsMixedCaseQuotedIdentifiers());
207 0 : ::std::vector< ::rtl::OUString> aRightColumns;
208 0 : fillColumns(pRight,aRightColumns);
209 :
210 0 : String aColumnName;
211 0 : if(!bAll)
212 : {
213 0 : for(sal_uInt16 i=0; i < pLeft->GetSelectEntryCount(); ++i)
214 0 : moveColumn(pRight,pLeft,aRightColumns,pLeft->GetSelectEntry(i),sExtraChars,nMaxNameLen,aCase);
215 :
216 0 : for(sal_uInt16 j=pLeft->GetSelectEntryCount(); j ; --j)
217 0 : pLeft->RemoveEntry(pLeft->GetSelectEntry(j-1));
218 : }
219 : else
220 : {
221 0 : sal_uInt16 nEntries = pLeft->GetEntryCount();
222 0 : for(sal_uInt16 i=0; i < nEntries; ++i)
223 0 : moveColumn(pRight,pLeft,aRightColumns,pLeft->GetEntry(i),sExtraChars,nMaxNameLen,aCase);
224 0 : for(sal_uInt16 j=pLeft->GetEntryCount(); j ; --j)
225 0 : pLeft->RemoveEntry(j-1);
226 : }
227 :
228 0 : enableButtons();
229 :
230 0 : if(m_lbOrgColumnNames.GetEntryCount())
231 0 : m_lbOrgColumnNames.SelectEntryPos(0);
232 :
233 0 : return 0;
234 : }
235 : // -----------------------------------------------------------------------
236 0 : IMPL_LINK( OWizColumnSelect, ListDoubleClickHdl, MultiListBox *, pListBox )
237 : {
238 : MultiListBox *pLeft,*pRight;
239 0 : if(pListBox == &m_lbOrgColumnNames)
240 : {
241 0 : pLeft = &m_lbOrgColumnNames;
242 0 : pRight = &m_lbNewColumnNames;
243 : }
244 : else
245 : {
246 0 : pRight = &m_lbOrgColumnNames;
247 0 : pLeft = &m_lbNewColumnNames;
248 : }
249 :
250 : //////////////////////////////////////////////////////////////////////
251 : // If database is able to process PrimaryKeys, set PrimaryKey
252 0 : Reference< XDatabaseMetaData > xMetaData( m_pParent->m_xDestConnection->getMetaData() );
253 0 : ::rtl::OUString sExtraChars = xMetaData->getExtraNameCharacters();
254 0 : sal_Int32 nMaxNameLen = m_pParent->getMaxColumnNameLength();
255 :
256 0 : ::comphelper::TStringMixEqualFunctor aCase(xMetaData->supportsMixedCaseQuotedIdentifiers());
257 0 : ::std::vector< ::rtl::OUString> aRightColumns;
258 0 : fillColumns(pRight,aRightColumns);
259 :
260 0 : String aColumnName;
261 0 : for(sal_uInt16 i=0; i < pLeft->GetSelectEntryCount(); ++i)
262 0 : moveColumn(pRight,pLeft,aRightColumns,pLeft->GetSelectEntry(i),sExtraChars,nMaxNameLen,aCase);
263 0 : for(sal_uInt16 j=pLeft->GetSelectEntryCount(); j ; --j)
264 0 : pLeft->RemoveEntry(pLeft->GetSelectEntry(j-1));
265 :
266 0 : enableButtons();
267 0 : return 0;
268 : }
269 : // -----------------------------------------------------------------------------
270 0 : void OWizColumnSelect::clearListBox(MultiListBox& _rListBox)
271 : {
272 0 : while(_rListBox.GetEntryCount())
273 0 : _rListBox.RemoveEntry(0);
274 0 : _rListBox.Clear();
275 0 : }
276 : // -----------------------------------------------------------------------------
277 0 : void OWizColumnSelect::fillColumns(ListBox* pRight,::std::vector< ::rtl::OUString> &_rRightColumns)
278 : {
279 0 : sal_uInt16 nCount = pRight->GetEntryCount();
280 0 : _rRightColumns.reserve(nCount);
281 0 : for(sal_uInt16 i=0; i < nCount;++i)
282 0 : _rRightColumns.push_back(pRight->GetEntry(i));
283 0 : }
284 : // -----------------------------------------------------------------------------
285 0 : void OWizColumnSelect::createNewColumn( ListBox* _pListbox,
286 : OFieldDescription* _pSrcField,
287 : ::std::vector< ::rtl::OUString>& _rRightColumns,
288 : const ::rtl::OUString& _sColumnName,
289 : const ::rtl::OUString& _sExtraChars,
290 : sal_Int32 _nMaxNameLen,
291 : const ::comphelper::TStringMixEqualFunctor& _aCase)
292 : {
293 : ::rtl::OUString sConvertedName = m_pParent->convertColumnName(TMultiListBoxEntryFindFunctor(&_rRightColumns,_aCase),
294 : _sColumnName,
295 : _sExtraChars,
296 0 : _nMaxNameLen);
297 0 : OFieldDescription* pNewField = new OFieldDescription(*_pSrcField);
298 0 : pNewField->SetName(sConvertedName);
299 0 : sal_Bool bNotConvert = sal_True;
300 0 : pNewField->SetType(m_pParent->convertType(_pSrcField->getSpecialTypeInfo(),bNotConvert));
301 0 : if ( !m_pParent->supportsPrimaryKey() )
302 0 : pNewField->SetPrimaryKey(sal_False);
303 :
304 0 : _pListbox->SetEntryData(_pListbox->InsertEntry(sConvertedName),pNewField);
305 0 : _rRightColumns.push_back(sConvertedName);
306 :
307 0 : if ( !bNotConvert )
308 0 : m_pParent->showColumnTypeNotSupported(sConvertedName);
309 0 : }
310 : // -----------------------------------------------------------------------------
311 0 : void OWizColumnSelect::moveColumn( ListBox* _pRight,
312 : ListBox* _pLeft,
313 : ::std::vector< ::rtl::OUString>& _rRightColumns,
314 : const ::rtl::OUString& _sColumnName,
315 : const ::rtl::OUString& _sExtraChars,
316 : sal_Int32 _nMaxNameLen,
317 : const ::comphelper::TStringMixEqualFunctor& _aCase)
318 : {
319 0 : if(_pRight == &m_lbNewColumnNames)
320 : {
321 : // we copy the column into the new format for the dest
322 0 : OFieldDescription* pSrcField = static_cast<OFieldDescription*>(_pLeft->GetEntryData(_pLeft->GetEntryPos(String(_sColumnName))));
323 0 : createNewColumn(_pRight,pSrcField,_rRightColumns,_sColumnName,_sExtraChars,_nMaxNameLen,_aCase);
324 : }
325 : else
326 : {
327 : // find the new column in the dest name mapping to obtain the old column
328 : OCopyTableWizard::TNameMapping::iterator aIter = ::std::find_if(m_pParent->m_mNameMapping.begin(),m_pParent->m_mNameMapping.end(),
329 : ::o3tl::compose1(
330 : ::std::bind2nd(_aCase, _sColumnName),
331 : ::o3tl::select2nd<OCopyTableWizard::TNameMapping::value_type>())
332 0 : );
333 :
334 : OSL_ENSURE(aIter != m_pParent->m_mNameMapping.end(),"Column must be defined");
335 0 : if ( aIter == m_pParent->m_mNameMapping.end() )
336 0 : return; // do nothing
337 0 : const ODatabaseExport::TColumns* pSrcColumns = m_pParent->getSourceColumns();
338 0 : ODatabaseExport::TColumns::const_iterator aSrcIter = pSrcColumns->find((*aIter).first);
339 0 : if ( aSrcIter != pSrcColumns->end() )
340 : {
341 : // we need also the old position of this column to insert it back on that position again
342 0 : const ODatabaseExport::TColumnVector* pSrcVector = m_pParent->getSrcVector();
343 0 : ODatabaseExport::TColumnVector::const_iterator aPos = ::std::find(pSrcVector->begin(),pSrcVector->end(),aSrcIter);
344 : OSL_ENSURE( aPos != pSrcVector->end(),"Invalid position for the iterator here!");
345 0 : ODatabaseExport::TColumnVector::size_type nPos = (aPos - pSrcVector->begin()) - adjustColumnPosition(_pLeft, _sColumnName, (aPos - pSrcVector->begin()), _aCase);
346 :
347 0 : _pRight->SetEntryData( _pRight->InsertEntry( (*aIter).first, sal::static_int_cast< sal_uInt16 >(nPos)),aSrcIter->second );
348 0 : _rRightColumns.push_back((*aIter).first);
349 0 : m_pParent->removeColumnNameFromNameMap(_sColumnName);
350 : }
351 : }
352 : }
353 : // -----------------------------------------------------------------------------
354 : // Simply returning fields back to their original position is
355 : // not enough. We need to take into acccount what fields have
356 : // been removed earlier and adjust accordingly. Based on the
357 : // algorithm employed in moveColumn().
358 0 : sal_uInt16 OWizColumnSelect::adjustColumnPosition( ListBox* _pLeft,
359 : const ::rtl::OUString& _sColumnName,
360 : ODatabaseExport::TColumnVector::size_type nCurrentPos,
361 : const ::comphelper::TStringMixEqualFunctor& _aCase)
362 : {
363 0 : sal_uInt16 nAdjustedPos = 0;
364 :
365 : // if returning all entries to their original position,
366 : // then there is no need to adjust the positions.
367 0 : if (m_ibColumns_LH.HasFocus())
368 0 : return nAdjustedPos;
369 :
370 0 : sal_uInt16 nCount = _pLeft->GetEntryCount();
371 0 : ::rtl::OUString sColumnString;
372 0 : for(sal_uInt16 i=0; i < nCount; ++i)
373 : {
374 0 : sColumnString = _pLeft->GetEntry(i);
375 0 : if(_sColumnName != sColumnString)
376 : {
377 : // find the new column in the dest name mapping to obtain the old column
378 : OCopyTableWizard::TNameMapping::iterator aIter = ::std::find_if(m_pParent->m_mNameMapping.begin(),m_pParent->m_mNameMapping.end(),
379 : ::o3tl::compose1(
380 : ::std::bind2nd(_aCase, sColumnString),
381 : ::o3tl::select2nd<OCopyTableWizard::TNameMapping::value_type>())
382 0 : );
383 :
384 : OSL_ENSURE(aIter != m_pParent->m_mNameMapping.end(),"Column must be defined");
385 0 : const ODatabaseExport::TColumns* pSrcColumns = m_pParent->getSourceColumns();
386 0 : ODatabaseExport::TColumns::const_iterator aSrcIter = pSrcColumns->find((*aIter).first);
387 0 : if ( aSrcIter != pSrcColumns->end() )
388 : {
389 : // we need also the old position of this column to insert it back on that position again
390 0 : const ODatabaseExport::TColumnVector* pSrcVector = m_pParent->getSrcVector();
391 0 : ODatabaseExport::TColumnVector::const_iterator aPos = ::std::find(pSrcVector->begin(),pSrcVector->end(),aSrcIter);
392 0 : ODatabaseExport::TColumnVector::size_type nPos = aPos - pSrcVector->begin();
393 0 : if( nPos < nCurrentPos)
394 : {
395 0 : nAdjustedPos++;
396 : }
397 : }
398 : }
399 : }
400 :
401 0 : return nAdjustedPos;
402 : }
403 : // -----------------------------------------------------------------------------
404 0 : void OWizColumnSelect::enableButtons()
405 : {
406 0 : sal_Bool bEntries = m_lbNewColumnNames.GetEntryCount() != 0;
407 0 : if(!bEntries)
408 0 : m_pParent->m_mNameMapping.clear();
409 :
410 0 : m_pParent->GetOKButton().Enable(bEntries);
411 0 : m_pParent->EnableButton(OCopyTableWizard::WIZARD_NEXT,bEntries && m_pParent->getOperation() != CopyTableOperation::AppendData);
412 0 : }
413 : // -----------------------------------------------------------------------------
414 :
415 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|