1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include "WColumnSelect.hxx"
31 #include "dbu_misc.hrc"
32 #include <osl/diagnose.h>
33 #include "WizardPages.hrc"
34 #include "WCopyTable.hxx"
35 #include <com/sun/star/sdbcx/XDataDescriptorFactory.hpp>
36 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
37 #include <com/sun/star/sdbcx/XAppend.hpp>
38 #include "moduledbu.hxx"
39 #include <com/sun/star/sdbc/DataType.hpp>
40 #include <com/sun/star/sdbc/ColumnValue.hpp>
41 #include <com/sun/star/sdb/application/CopyTableOperation.hpp>
42 #include "dbustrings.hrc"
44 #include <o3tl/compat_functional.hxx>
46 using namespace ::com::sun::star::uno
;
47 using namespace ::com::sun::star::beans
;
48 using namespace ::com::sun::star::container
;
49 using namespace ::com::sun::star::sdbc
;
50 using namespace ::com::sun::star::sdbcx
;
51 using namespace dbaui
;
53 namespace CopyTableOperation
= ::com::sun::star::sdb::application::CopyTableOperation
;
55 // -----------------------------------------------------------------------
56 String
OWizColumnSelect::GetTitle() const { return String(ModuleRes(STR_WIZ_COLUMN_SELECT_TITEL
)); }
57 // -----------------------------------------------------------------------------
58 OWizardPage::OWizardPage( Window
* pParent
, const ResId
& rResId
)
59 : TabPage(pParent
,rResId
)
60 ,m_pParent(static_cast<OCopyTableWizard
*>(pParent
))
61 ,m_bFirstTime(sal_True
)
64 //========================================================================
66 DBG_NAME(OWizColumnSelect
);
67 //========================================================================
68 OWizColumnSelect::OWizColumnSelect( Window
* pParent
)
69 :OWizardPage( pParent
, ModuleRes( TAB_WIZ_COLUMN_SELECT
))
70 ,m_flColumns( this, ModuleRes( FL_COLUMN_SELECT
) )
71 ,m_lbOrgColumnNames( this, ModuleRes( LB_ORG_COLUMN_NAMES
) )
72 ,m_ibColumn_RH( this, ModuleRes( IB_COLUMN_RH
) )
73 ,m_ibColumns_RH( this, ModuleRes( IB_COLUMNS_RH
) )
74 ,m_ibColumn_LH( this, ModuleRes( IB_COLUMN_LH
) )
75 ,m_ibColumns_LH( this, ModuleRes( IB_COLUMNS_LH
) )
76 ,m_lbNewColumnNames( this, ModuleRes( LB_NEW_COLUMN_NAMES
) )
78 DBG_CTOR(OWizColumnSelect
,NULL
);
79 m_ibColumn_RH
.SetClickHdl(LINK(this,OWizColumnSelect
,ButtonClickHdl
));
80 m_ibColumn_LH
.SetClickHdl(LINK(this,OWizColumnSelect
,ButtonClickHdl
));
81 m_ibColumns_RH
.SetClickHdl(LINK(this,OWizColumnSelect
,ButtonClickHdl
));
82 m_ibColumns_LH
.SetClickHdl(LINK(this,OWizColumnSelect
,ButtonClickHdl
));
84 m_lbOrgColumnNames
.EnableMultiSelection(sal_True
);
85 m_lbNewColumnNames
.EnableMultiSelection(sal_True
);
87 m_lbOrgColumnNames
.SetDoubleClickHdl(LINK(this,OWizColumnSelect
,ListDoubleClickHdl
));
88 m_lbNewColumnNames
.SetDoubleClickHdl(LINK(this,OWizColumnSelect
,ListDoubleClickHdl
));
91 // -----------------------------------------------------------------------
92 OWizColumnSelect::~OWizColumnSelect()
94 DBG_DTOR(OWizColumnSelect
,NULL
);
95 while ( m_lbNewColumnNames
.GetEntryCount() )
97 void* pData
= m_lbNewColumnNames
.GetEntryData(0);
99 delete static_cast<OFieldDescription
*>(pData
);
101 m_lbNewColumnNames
.RemoveEntry(0);
103 m_lbNewColumnNames
.Clear();
106 // -----------------------------------------------------------------------
107 void OWizColumnSelect::Reset()
109 // urspr"unglichen zustand wiederherstellen
110 DBG_CHKTHIS(OWizColumnSelect
,NULL
);
112 clearListBox(m_lbOrgColumnNames
);
113 clearListBox(m_lbNewColumnNames
);
114 m_pParent
->m_mNameMapping
.clear();
116 // insert the source columns in the left listbox
117 const ODatabaseExport::TColumnVector
* pSrcColumns
= m_pParent
->getSrcVector();
118 ODatabaseExport::TColumnVector::const_iterator aIter
= pSrcColumns
->begin();
119 ODatabaseExport::TColumnVector::const_iterator aEnd
= pSrcColumns
->end();
121 for(;aIter
!= aEnd
;++aIter
)
123 sal_uInt16 nPos
= m_lbOrgColumnNames
.InsertEntry((*aIter
)->first
);
124 m_lbOrgColumnNames
.SetEntryData(nPos
,(*aIter
)->second
);
127 if(m_lbOrgColumnNames
.GetEntryCount())
128 m_lbOrgColumnNames
.SelectEntryPos(0);
130 m_bFirstTime
= sal_False
;
132 // -----------------------------------------------------------------------
133 void OWizColumnSelect::ActivatePage( )
135 DBG_CHKTHIS(OWizColumnSelect
,NULL
);
136 // if there are no dest columns reset the left side with the origibnal columns
137 if(m_pParent
->getDestColumns()->empty())
140 clearListBox(m_lbNewColumnNames
);
142 const ODatabaseExport::TColumnVector
* pDestColumns
= m_pParent
->getDestVector();
144 ODatabaseExport::TColumnVector::const_iterator aIter
= pDestColumns
->begin();
145 ODatabaseExport::TColumnVector::const_iterator aEnd
= pDestColumns
->end();
146 for(;aIter
!= aEnd
;++aIter
)
148 sal_uInt16 nPos
= m_lbNewColumnNames
.InsertEntry((*aIter
)->first
);
149 m_lbNewColumnNames
.SetEntryData(nPos
,new OFieldDescription(*((*aIter
)->second
)));
150 m_lbOrgColumnNames
.RemoveEntry((*aIter
)->first
);
152 m_pParent
->GetOKButton().Enable(m_lbNewColumnNames
.GetEntryCount() != 0);
153 m_pParent
->EnableButton(OCopyTableWizard::WIZARD_NEXT
,m_lbNewColumnNames
.GetEntryCount() && m_pParent
->getOperation() != CopyTableOperation::AppendData
);
154 m_ibColumns_RH
.GrabFocus();
156 // -----------------------------------------------------------------------
157 sal_Bool
OWizColumnSelect::LeavePage()
159 DBG_CHKTHIS(OWizColumnSelect
,NULL
);
161 m_pParent
->clearDestColumns();
163 for(sal_uInt16 i
=0 ; i
< m_lbNewColumnNames
.GetEntryCount();++i
)
165 OFieldDescription
* pField
= static_cast<OFieldDescription
*>(m_lbNewColumnNames
.GetEntryData(i
));
166 OSL_ENSURE(pField
,"The field information can not be null!");
167 m_pParent
->insertColumn(i
,pField
);
170 clearListBox(m_lbNewColumnNames
);
173 if ( m_pParent
->GetPressedButton() == OCopyTableWizard::WIZARD_NEXT
174 || m_pParent
->GetPressedButton() == OCopyTableWizard::WIZARD_FINISH
176 return m_pParent
->getDestColumns()->size() != 0;
180 // -----------------------------------------------------------------------
181 IMPL_LINK( OWizColumnSelect
, ButtonClickHdl
, Button
*, pButton
)
183 MultiListBox
*pLeft
= NULL
;
184 MultiListBox
*pRight
= NULL
;
185 sal_Bool bAll
= sal_False
;
187 if(pButton
== &m_ibColumn_RH
)
189 pLeft
= &m_lbOrgColumnNames
;
190 pRight
= &m_lbNewColumnNames
;
192 else if(pButton
== &m_ibColumn_LH
)
194 pLeft
= &m_lbNewColumnNames
;
195 pRight
= &m_lbOrgColumnNames
;
197 else if(pButton
== &m_ibColumns_RH
)
199 pLeft
= &m_lbOrgColumnNames
;
200 pRight
= &m_lbNewColumnNames
;
203 else if(pButton
== &m_ibColumns_LH
)
205 pLeft
= &m_lbNewColumnNames
;
206 pRight
= &m_lbOrgColumnNames
;
211 Reference
< XDatabaseMetaData
> xMetaData( m_pParent
->m_xDestConnection
->getMetaData() );
212 ::rtl::OUString sExtraChars
= xMetaData
->getExtraNameCharacters();
213 sal_Int32 nMaxNameLen
= m_pParent
->getMaxColumnNameLength();
215 ::comphelper::TStringMixEqualFunctor
aCase(xMetaData
->supportsMixedCaseQuotedIdentifiers());
216 ::std::vector
< ::rtl::OUString
> aRightColumns
;
217 fillColumns(pRight
,aRightColumns
);
222 for(sal_uInt16 i
=0; i
< pLeft
->GetSelectEntryCount(); ++i
)
223 moveColumn(pRight
,pLeft
,aRightColumns
,pLeft
->GetSelectEntry(i
),sExtraChars
,nMaxNameLen
,aCase
);
225 for(sal_uInt16 j
=pLeft
->GetSelectEntryCount(); j
; --j
)
226 pLeft
->RemoveEntry(pLeft
->GetSelectEntry(j
-1));
230 sal_uInt16 nEntries
= pLeft
->GetEntryCount();
231 for(sal_uInt16 i
=0; i
< nEntries
; ++i
)
232 moveColumn(pRight
,pLeft
,aRightColumns
,pLeft
->GetEntry(i
),sExtraChars
,nMaxNameLen
,aCase
);
233 for(sal_uInt16 j
=pLeft
->GetEntryCount(); j
; --j
)
234 pLeft
->RemoveEntry(j
-1);
239 if(m_lbOrgColumnNames
.GetEntryCount())
240 m_lbOrgColumnNames
.SelectEntryPos(0);
244 // -----------------------------------------------------------------------
245 IMPL_LINK( OWizColumnSelect
, ListDoubleClickHdl
, MultiListBox
*, pListBox
)
247 MultiListBox
*pLeft
,*pRight
;
248 if(pListBox
== &m_lbOrgColumnNames
)
250 pLeft
= &m_lbOrgColumnNames
;
251 pRight
= &m_lbNewColumnNames
;
255 pRight
= &m_lbOrgColumnNames
;
256 pLeft
= &m_lbNewColumnNames
;
259 //////////////////////////////////////////////////////////////////////
260 // Wenn Datenbank PrimaryKeys verarbeiten kann, PrimaryKey anlegen
261 Reference
< XDatabaseMetaData
> xMetaData( m_pParent
->m_xDestConnection
->getMetaData() );
262 ::rtl::OUString sExtraChars
= xMetaData
->getExtraNameCharacters();
263 sal_Int32 nMaxNameLen
= m_pParent
->getMaxColumnNameLength();
265 ::comphelper::TStringMixEqualFunctor
aCase(xMetaData
->supportsMixedCaseQuotedIdentifiers());
266 ::std::vector
< ::rtl::OUString
> aRightColumns
;
267 fillColumns(pRight
,aRightColumns
);
270 for(sal_uInt16 i
=0; i
< pLeft
->GetSelectEntryCount(); ++i
)
271 moveColumn(pRight
,pLeft
,aRightColumns
,pLeft
->GetSelectEntry(i
),sExtraChars
,nMaxNameLen
,aCase
);
272 for(sal_uInt16 j
=pLeft
->GetSelectEntryCount(); j
; --j
)
273 pLeft
->RemoveEntry(pLeft
->GetSelectEntry(j
-1));
278 // -----------------------------------------------------------------------------
279 void OWizColumnSelect::clearListBox(MultiListBox
& _rListBox
)
281 while(_rListBox
.GetEntryCount())
282 _rListBox
.RemoveEntry(0);
285 // -----------------------------------------------------------------------------
286 void OWizColumnSelect::fillColumns(ListBox
* pRight
,::std::vector
< ::rtl::OUString
> &_rRightColumns
)
288 sal_uInt16 nCount
= pRight
->GetEntryCount();
289 _rRightColumns
.reserve(nCount
);
290 for(sal_uInt16 i
=0; i
< nCount
;++i
)
291 _rRightColumns
.push_back(pRight
->GetEntry(i
));
293 // -----------------------------------------------------------------------------
294 void OWizColumnSelect::createNewColumn( ListBox
* _pListbox
,
295 OFieldDescription
* _pSrcField
,
296 ::std::vector
< ::rtl::OUString
>& _rRightColumns
,
297 const ::rtl::OUString
& _sColumnName
,
298 const ::rtl::OUString
& _sExtraChars
,
299 sal_Int32 _nMaxNameLen
,
300 const ::comphelper::TStringMixEqualFunctor
& _aCase
)
302 ::rtl::OUString sConvertedName
= m_pParent
->convertColumnName(TMultiListBoxEntryFindFunctor(&_rRightColumns
,_aCase
),
306 OFieldDescription
* pNewField
= new OFieldDescription(*_pSrcField
);
307 pNewField
->SetName(sConvertedName
);
308 sal_Bool bNotConvert
= sal_True
;
309 pNewField
->SetType(m_pParent
->convertType(_pSrcField
->getSpecialTypeInfo(),bNotConvert
));
310 if ( !m_pParent
->supportsPrimaryKey() )
311 pNewField
->SetPrimaryKey(sal_False
);
313 _pListbox
->SetEntryData(_pListbox
->InsertEntry(sConvertedName
),pNewField
);
314 _rRightColumns
.push_back(sConvertedName
);
317 m_pParent
->showColumnTypeNotSupported(sConvertedName
);
319 // -----------------------------------------------------------------------------
320 void OWizColumnSelect::moveColumn( ListBox
* _pRight
,
322 ::std::vector
< ::rtl::OUString
>& _rRightColumns
,
323 const ::rtl::OUString
& _sColumnName
,
324 const ::rtl::OUString
& _sExtraChars
,
325 sal_Int32 _nMaxNameLen
,
326 const ::comphelper::TStringMixEqualFunctor
& _aCase
)
328 if(_pRight
== &m_lbNewColumnNames
)
330 // we copy the column into the new format for the dest
331 OFieldDescription
* pSrcField
= static_cast<OFieldDescription
*>(_pLeft
->GetEntryData(_pLeft
->GetEntryPos(String(_sColumnName
))));
332 createNewColumn(_pRight
,pSrcField
,_rRightColumns
,_sColumnName
,_sExtraChars
,_nMaxNameLen
,_aCase
);
336 // find the new column in the dest name mapping to obtain the old column
337 OCopyTableWizard::TNameMapping::iterator aIter
= ::std::find_if(m_pParent
->m_mNameMapping
.begin(),m_pParent
->m_mNameMapping
.end(),
339 ::std::bind2nd(_aCase
, _sColumnName
),
340 ::o3tl::select2nd
<OCopyTableWizard::TNameMapping::value_type
>())
343 OSL_ENSURE(aIter
!= m_pParent
->m_mNameMapping
.end(),"Column must be defined");
344 if ( aIter
== m_pParent
->m_mNameMapping
.end() )
345 return; // do nothing
346 const ODatabaseExport::TColumns
* pSrcColumns
= m_pParent
->getSourceColumns();
347 ODatabaseExport::TColumns::const_iterator aSrcIter
= pSrcColumns
->find((*aIter
).first
);
348 if ( aSrcIter
!= pSrcColumns
->end() )
350 // we need also the old position of this column to insert it back on that position again
351 const ODatabaseExport::TColumnVector
* pSrcVector
= m_pParent
->getSrcVector();
352 ODatabaseExport::TColumnVector::const_iterator aPos
= ::std::find(pSrcVector
->begin(),pSrcVector
->end(),aSrcIter
);
353 OSL_ENSURE( aPos
!= pSrcVector
->end(),"Invalid position for the iterator here!");
354 ODatabaseExport::TColumnVector::size_type nPos
= (aPos
- pSrcVector
->begin()) - adjustColumnPosition(_pLeft
, _sColumnName
, (aPos
- pSrcVector
->begin()), _aCase
);
356 _pRight
->SetEntryData( _pRight
->InsertEntry( (*aIter
).first
, sal::static_int_cast
< sal_uInt16
>(nPos
)),aSrcIter
->second
);
357 _rRightColumns
.push_back((*aIter
).first
);
358 m_pParent
->removeColumnNameFromNameMap(_sColumnName
);
362 // -----------------------------------------------------------------------------
363 // Simply returning fields back to their original position is
364 // not enough. We need to take into acccount what fields have
365 // been removed earlier and adjust accordingly. Based on the
366 // algorithm employed in moveColumn().
367 sal_uInt16
OWizColumnSelect::adjustColumnPosition( ListBox
* _pLeft
,
368 const ::rtl::OUString
& _sColumnName
,
369 ODatabaseExport::TColumnVector::size_type nCurrentPos
,
370 const ::comphelper::TStringMixEqualFunctor
& _aCase
)
372 sal_uInt16 nAdjustedPos
= 0;
374 // if returning all entries to their original position,
375 // then there is no need to adjust the positions.
376 if (m_ibColumns_LH
.HasFocus())
379 sal_uInt16 nCount
= _pLeft
->GetEntryCount();
380 ::rtl::OUString sColumnString
;
381 for(sal_uInt16 i
=0; i
< nCount
; ++i
)
383 sColumnString
= _pLeft
->GetEntry(i
);
384 if(_sColumnName
!= sColumnString
)
386 // find the new column in the dest name mapping to obtain the old column
387 OCopyTableWizard::TNameMapping::iterator aIter
= ::std::find_if(m_pParent
->m_mNameMapping
.begin(),m_pParent
->m_mNameMapping
.end(),
389 ::std::bind2nd(_aCase
, sColumnString
),
390 ::o3tl::select2nd
<OCopyTableWizard::TNameMapping::value_type
>())
393 OSL_ENSURE(aIter
!= m_pParent
->m_mNameMapping
.end(),"Column must be defined");
394 const ODatabaseExport::TColumns
* pSrcColumns
= m_pParent
->getSourceColumns();
395 ODatabaseExport::TColumns::const_iterator aSrcIter
= pSrcColumns
->find((*aIter
).first
);
396 if ( aSrcIter
!= pSrcColumns
->end() )
398 // we need also the old position of this column to insert it back on that position again
399 const ODatabaseExport::TColumnVector
* pSrcVector
= m_pParent
->getSrcVector();
400 ODatabaseExport::TColumnVector::const_iterator aPos
= ::std::find(pSrcVector
->begin(),pSrcVector
->end(),aSrcIter
);
401 ODatabaseExport::TColumnVector::size_type nPos
= aPos
- pSrcVector
->begin();
402 if( nPos
< nCurrentPos
)
412 // -----------------------------------------------------------------------------
413 void OWizColumnSelect::enableButtons()
415 sal_Bool bEntries
= m_lbNewColumnNames
.GetEntryCount() != 0;
417 m_pParent
->m_mNameMapping
.clear();
419 m_pParent
->GetOKButton().Enable(bEntries
);
420 m_pParent
->EnableButton(OCopyTableWizard::WIZARD_NEXT
,bEntries
&& m_pParent
->getOperation() != CopyTableOperation::AppendData
);
422 // -----------------------------------------------------------------------------
424 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */