1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: WColumnSelect.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_dbaccess.hxx"
34 #ifndef DBAUI_WIZ_COLUMNSELECT_HXX
35 #include "WColumnSelect.hxx"
37 #ifndef _DBU_MISC_HRC_
38 #include "dbu_misc.hrc"
40 #ifndef _TOOLS_DEBUG_HXX
41 #include <tools/debug.hxx>
43 #ifndef DBAUI_WIZARD_PAGES_HRC
44 #include "WizardPages.hrc"
46 #ifndef DBAUI_WIZ_COPYTABLEDIALOG_HXX
47 #include "WCopyTable.hxx"
49 #ifndef _COM_SUN_STAR_SDBCX_XDATADESCRIPTORFACTORY_HPP_
50 #include <com/sun/star/sdbcx/XDataDescriptorFactory.hpp>
52 #ifndef _COM_SUN_STAR_SDBCX_XCOLUMNSSUPPLIER_HPP_
53 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
55 #ifndef _COM_SUN_STAR_SDBCX_XAPPEND_HPP_
56 #include <com/sun/star/sdbcx/XAppend.hpp>
58 #ifndef _DBAUI_MODULE_DBU_HXX_
59 #include "moduledbu.hxx"
61 #ifndef _COM_SUN_STAR_SDBC_DATATYPE_HPP_
62 #include <com/sun/star/sdbc/DataType.hpp>
64 #ifndef _COM_SUN_STAR_SDBC_COLUMNVALUE_HPP_
65 #include <com/sun/star/sdbc/ColumnValue.hpp>
67 #ifndef _COM_SUN_STAR_SDB_APPLICATION_COPYTABLEOPERATION_HPP_
68 #include <com/sun/star/sdb/application/CopyTableOperation.hpp>
70 #ifndef DBACCESS_SHARED_DBUSTRINGS_HRC
71 #include "dbustrings.hrc"
75 using namespace ::com::sun::star::uno
;
76 using namespace ::com::sun::star::beans
;
77 using namespace ::com::sun::star::container
;
78 using namespace ::com::sun::star::sdbc
;
79 using namespace ::com::sun::star::sdbcx
;
80 using namespace dbaui
;
82 namespace CopyTableOperation
= ::com::sun::star::sdb::application::CopyTableOperation
;
84 // -----------------------------------------------------------------------
85 String
OWizColumnSelect::GetTitle() const { return String(ModuleRes(STR_WIZ_COLUMN_SELECT_TITEL
)); }
86 // -----------------------------------------------------------------------------
87 OWizardPage::OWizardPage( Window
* pParent
, const ResId
& rResId
)
88 : TabPage(pParent
,rResId
)
89 ,m_pParent(static_cast<OCopyTableWizard
*>(pParent
))
90 ,m_bFirstTime(sal_True
)
93 //========================================================================
95 DBG_NAME(OWizColumnSelect
);
96 //========================================================================
97 OWizColumnSelect::OWizColumnSelect( Window
* pParent
)
98 :OWizardPage( pParent
, ModuleRes( TAB_WIZ_COLUMN_SELECT
))
99 ,m_flColumns( this, ModuleRes( FL_COLUMN_SELECT
) )
100 ,m_lbOrgColumnNames( this, ModuleRes( LB_ORG_COLUMN_NAMES
) )
101 ,m_ibColumn_RH( this, ModuleRes( IB_COLUMN_RH
) )
102 ,m_ibColumns_RH( this, ModuleRes( IB_COLUMNS_RH
) )
103 ,m_ibColumn_LH( this, ModuleRes( IB_COLUMN_LH
) )
104 ,m_ibColumns_LH( this, ModuleRes( IB_COLUMNS_LH
) )
105 ,m_lbNewColumnNames( this, ModuleRes( LB_NEW_COLUMN_NAMES
) )
107 DBG_CTOR(OWizColumnSelect
,NULL
);
108 m_ibColumn_RH
.SetClickHdl(LINK(this,OWizColumnSelect
,ButtonClickHdl
));
109 m_ibColumn_LH
.SetClickHdl(LINK(this,OWizColumnSelect
,ButtonClickHdl
));
110 m_ibColumns_RH
.SetClickHdl(LINK(this,OWizColumnSelect
,ButtonClickHdl
));
111 m_ibColumns_LH
.SetClickHdl(LINK(this,OWizColumnSelect
,ButtonClickHdl
));
113 m_lbOrgColumnNames
.EnableMultiSelection(sal_True
);
114 m_lbNewColumnNames
.EnableMultiSelection(sal_True
);
116 m_lbOrgColumnNames
.SetDoubleClickHdl(LINK(this,OWizColumnSelect
,ListDoubleClickHdl
));
117 m_lbNewColumnNames
.SetDoubleClickHdl(LINK(this,OWizColumnSelect
,ListDoubleClickHdl
));
120 // -----------------------------------------------------------------------
121 OWizColumnSelect::~OWizColumnSelect()
123 DBG_DTOR(OWizColumnSelect
,NULL
);
124 while ( m_lbNewColumnNames
.GetEntryCount() )
126 void* pData
= m_lbNewColumnNames
.GetEntryData(0);
128 delete static_cast<OFieldDescription
*>(pData
);
130 m_lbNewColumnNames
.RemoveEntry(0);
132 m_lbNewColumnNames
.Clear();
135 // -----------------------------------------------------------------------
136 void OWizColumnSelect::Reset()
138 // urspr"unglichen zustand wiederherstellen
139 DBG_CHKTHIS(OWizColumnSelect
,NULL
);
141 clearListBox(m_lbOrgColumnNames
);
142 clearListBox(m_lbNewColumnNames
);
144 // insert the source columns in the left listbox
145 const ODatabaseExport::TColumnVector
* pSrcColumns
= m_pParent
->getSrcVector();
146 ODatabaseExport::TColumnVector::const_iterator aIter
= pSrcColumns
->begin();
147 ODatabaseExport::TColumnVector::const_iterator aEnd
= pSrcColumns
->end();
149 for(;aIter
!= aEnd
;++aIter
)
151 sal_uInt16 nPos
= m_lbOrgColumnNames
.InsertEntry((*aIter
)->first
);
152 m_lbOrgColumnNames
.SetEntryData(nPos
,(*aIter
)->second
);
155 // m_pParent->clearDestColumns();
157 if(m_lbOrgColumnNames
.GetEntryCount())
158 m_lbOrgColumnNames
.SelectEntryPos(0);
160 m_bFirstTime
= sal_False
;
162 // -----------------------------------------------------------------------
163 void OWizColumnSelect::ActivatePage( )
165 DBG_CHKTHIS(OWizColumnSelect
,NULL
);
166 // if there are no dest columns reset the left side with the origibnal columns
167 if(m_pParent
->getDestColumns()->size() == 0)
170 clearListBox(m_lbNewColumnNames
);
172 const ODatabaseExport::TColumnVector
* pDestColumns
= m_pParent
->getDestVector();
174 ODatabaseExport::TColumnVector::const_iterator aIter
= pDestColumns
->begin();
175 ODatabaseExport::TColumnVector::const_iterator aEnd
= pDestColumns
->end();
176 for(;aIter
!= aEnd
;++aIter
)
178 USHORT nPos
= m_lbNewColumnNames
.InsertEntry((*aIter
)->first
);
179 m_lbNewColumnNames
.SetEntryData(nPos
,new OFieldDescription(*((*aIter
)->second
)));
180 m_lbOrgColumnNames
.RemoveEntry((*aIter
)->first
);
182 m_pParent
->GetOKButton().Enable(m_lbNewColumnNames
.GetEntryCount() != 0);
183 m_pParent
->EnableButton(OCopyTableWizard::WIZARD_NEXT
,m_lbNewColumnNames
.GetEntryCount() && m_pParent
->getOperation() != CopyTableOperation::AppendData
);
184 m_ibColumns_RH
.GrabFocus();
186 // -----------------------------------------------------------------------
187 sal_Bool
OWizColumnSelect::LeavePage()
189 DBG_CHKTHIS(OWizColumnSelect
,NULL
);
191 // m_pParent->getColumns()->clear();
192 m_pParent
->clearDestColumns();
194 for(sal_uInt16 i
=0 ; i
< m_lbNewColumnNames
.GetEntryCount();++i
)
196 OFieldDescription
* pField
= static_cast<OFieldDescription
*>(m_lbNewColumnNames
.GetEntryData(i
));
197 OSL_ENSURE(pField
,"The field information can not be null!");
198 m_pParent
->insertColumn(i
,pField
);
201 clearListBox(m_lbNewColumnNames
);
204 if ( m_pParent
->GetPressedButton() == OCopyTableWizard::WIZARD_NEXT
205 || m_pParent
->GetPressedButton() == OCopyTableWizard::WIZARD_FINISH
207 return m_pParent
->getDestColumns()->size() != 0;
211 // -----------------------------------------------------------------------
212 IMPL_LINK( OWizColumnSelect
, ButtonClickHdl
, Button
*, pButton
)
214 MultiListBox
*pLeft
= NULL
;
215 MultiListBox
*pRight
= NULL
;
216 sal_Bool bAll
= sal_False
;
218 if(pButton
== &m_ibColumn_RH
)
220 pLeft
= &m_lbOrgColumnNames
;
221 pRight
= &m_lbNewColumnNames
;
223 else if(pButton
== &m_ibColumn_LH
)
225 pLeft
= &m_lbNewColumnNames
;
226 pRight
= &m_lbOrgColumnNames
;
228 else if(pButton
== &m_ibColumns_RH
)
230 pLeft
= &m_lbOrgColumnNames
;
231 pRight
= &m_lbNewColumnNames
;
234 else if(pButton
== &m_ibColumns_LH
)
236 pLeft
= &m_lbNewColumnNames
;
237 pRight
= &m_lbOrgColumnNames
;
242 Reference
< XDatabaseMetaData
> xMetaData( m_pParent
->m_xDestConnection
->getMetaData() );
243 ::rtl::OUString sExtraChars
= xMetaData
->getExtraNameCharacters();
244 sal_Int32 nMaxNameLen
= m_pParent
->getMaxColumnNameLength();
246 ::comphelper::TStringMixEqualFunctor
aCase(xMetaData
->supportsMixedCaseQuotedIdentifiers());
247 ::std::vector
< ::rtl::OUString
> aRightColumns
;
248 fillColumns(pRight
,aRightColumns
);
253 for(sal_uInt16 i
=0; i
< pLeft
->GetSelectEntryCount(); ++i
)
254 moveColumn(pRight
,pLeft
,aRightColumns
,pLeft
->GetSelectEntry(i
),sExtraChars
,nMaxNameLen
,aCase
);
256 for(sal_uInt16 j
=pLeft
->GetSelectEntryCount(); j
; --j
)
257 pLeft
->RemoveEntry(pLeft
->GetSelectEntry(j
-1));
261 sal_uInt16 nEntries
= pLeft
->GetEntryCount();
262 for(sal_uInt16 i
=0; i
< nEntries
; ++i
)
263 moveColumn(pRight
,pLeft
,aRightColumns
,pLeft
->GetEntry(i
),sExtraChars
,nMaxNameLen
,aCase
);
264 for(sal_uInt16 j
=pLeft
->GetEntryCount(); j
; --j
)
265 pLeft
->RemoveEntry(j
-1);
270 if(m_lbOrgColumnNames
.GetEntryCount())
271 m_lbOrgColumnNames
.SelectEntryPos(0);
275 // -----------------------------------------------------------------------
276 IMPL_LINK( OWizColumnSelect
, ListDoubleClickHdl
, MultiListBox
*, pListBox
)
278 MultiListBox
*pLeft
,*pRight
;
279 if(pListBox
== &m_lbOrgColumnNames
)
281 pLeft
= &m_lbOrgColumnNames
;
282 pRight
= &m_lbNewColumnNames
;
286 pRight
= &m_lbOrgColumnNames
;
287 pLeft
= &m_lbNewColumnNames
;
290 //////////////////////////////////////////////////////////////////////
291 // Wenn Datenbank PrimaryKeys verarbeiten kann, PrimaryKey anlegen
292 Reference
< XDatabaseMetaData
> xMetaData( m_pParent
->m_xDestConnection
->getMetaData() );
293 ::rtl::OUString sExtraChars
= xMetaData
->getExtraNameCharacters();
294 sal_Int32 nMaxNameLen
= m_pParent
->getMaxColumnNameLength();
296 ::comphelper::TStringMixEqualFunctor
aCase(xMetaData
->supportsMixedCaseQuotedIdentifiers());
297 ::std::vector
< ::rtl::OUString
> aRightColumns
;
298 fillColumns(pRight
,aRightColumns
);
301 for(sal_uInt16 i
=0; i
< pLeft
->GetSelectEntryCount(); ++i
)
302 moveColumn(pRight
,pLeft
,aRightColumns
,pLeft
->GetSelectEntry(i
),sExtraChars
,nMaxNameLen
,aCase
);
303 for(sal_uInt16 j
=pLeft
->GetSelectEntryCount(); j
; --j
)
304 pLeft
->RemoveEntry(pLeft
->GetSelectEntry(j
-1));
309 // -----------------------------------------------------------------------------
310 void OWizColumnSelect::clearListBox(MultiListBox
& _rListBox
)
312 while(_rListBox
.GetEntryCount())
313 _rListBox
.RemoveEntry(0);
316 // -----------------------------------------------------------------------------
317 void OWizColumnSelect::fillColumns(ListBox
* pRight
,::std::vector
< ::rtl::OUString
> &_rRightColumns
)
319 sal_uInt16 nCount
= pRight
->GetEntryCount();
320 _rRightColumns
.reserve(nCount
);
321 for(sal_uInt16 i
=0; i
< nCount
;++i
)
322 _rRightColumns
.push_back(pRight
->GetEntry(i
));
324 // -----------------------------------------------------------------------------
325 void OWizColumnSelect::createNewColumn( ListBox
* _pListbox
,
326 OFieldDescription
* _pSrcField
,
327 ::std::vector
< ::rtl::OUString
>& _rRightColumns
,
328 const ::rtl::OUString
& _sColumnName
,
329 const ::rtl::OUString
& _sExtraChars
,
330 sal_Int32 _nMaxNameLen
,
331 const ::comphelper::TStringMixEqualFunctor
& _aCase
)
333 ::rtl::OUString sConvertedName
= m_pParent
->convertColumnName(TMultiListBoxEntryFindFunctor(&_rRightColumns
,_aCase
),
337 OFieldDescription
* pNewField
= new OFieldDescription(*_pSrcField
);
338 pNewField
->SetName(sConvertedName
);
339 sal_Bool bNotConvert
= sal_True
;
340 pNewField
->SetType(m_pParent
->convertType(_pSrcField
->getSpecialTypeInfo(),bNotConvert
));
341 if ( !m_pParent
->supportsPrimaryKey() )
342 pNewField
->SetPrimaryKey(sal_False
);
344 _pListbox
->SetEntryData(_pListbox
->InsertEntry(sConvertedName
),pNewField
);
345 _rRightColumns
.push_back(sConvertedName
);
348 m_pParent
->showColumnTypeNotSupported(sConvertedName
);
350 // -----------------------------------------------------------------------------
351 void OWizColumnSelect::moveColumn( ListBox
* _pRight
,
353 ::std::vector
< ::rtl::OUString
>& _rRightColumns
,
354 const ::rtl::OUString
& _sColumnName
,
355 const ::rtl::OUString
& _sExtraChars
,
356 sal_Int32 _nMaxNameLen
,
357 const ::comphelper::TStringMixEqualFunctor
& _aCase
)
359 if(_pRight
== &m_lbNewColumnNames
)
361 // we copy the column into the new format for the dest
362 OFieldDescription
* pSrcField
= static_cast<OFieldDescription
*>(_pLeft
->GetEntryData(_pLeft
->GetEntryPos(String(_sColumnName
))));
363 createNewColumn(_pRight
,pSrcField
,_rRightColumns
,_sColumnName
,_sExtraChars
,_nMaxNameLen
,_aCase
);
367 // find the new column in the dest name mapping to obtain the old column
368 OCopyTableWizard::TNameMapping::iterator aIter
= ::std::find_if(m_pParent
->m_mNameMapping
.begin(),m_pParent
->m_mNameMapping
.end(),
370 ::std::bind2nd(_aCase
, _sColumnName
),
371 ::std::select2nd
<OCopyTableWizard::TNameMapping::value_type
>())
374 DBG_ASSERT(aIter
!= m_pParent
->m_mNameMapping
.end(),"Column must be defined");
375 if ( aIter
== m_pParent
->m_mNameMapping
.end() )
376 return; // do nothing
377 const ODatabaseExport::TColumns
* pSrcColumns
= m_pParent
->getSourceColumns();
378 ODatabaseExport::TColumns::const_iterator aSrcIter
= pSrcColumns
->find((*aIter
).first
);
379 if ( aSrcIter
!= pSrcColumns
->end() )
381 // we need also the old position of this column to insert it back on that position again
382 const ODatabaseExport::TColumnVector
* pSrcVector
= m_pParent
->getSrcVector();
383 ODatabaseExport::TColumnVector::const_iterator aPos
= ::std::find(pSrcVector
->begin(),pSrcVector
->end(),aSrcIter
);
384 OSL_ENSURE( aPos
!= pSrcVector
->end(),"Invalid position for the iterator here!");
385 ODatabaseExport::TColumnVector::size_type nPos
= (aPos
- pSrcVector
->begin()) - adjustColumnPosition(_pLeft
, _sColumnName
, (aPos
- pSrcVector
->begin()), _aCase
);
387 _pRight
->SetEntryData( _pRight
->InsertEntry( (*aIter
).first
, sal::static_int_cast
< USHORT
>(nPos
)),aSrcIter
->second
);
388 _rRightColumns
.push_back((*aIter
).first
);
389 m_pParent
->removeColumnNameFromNameMap(_sColumnName
);
393 // -----------------------------------------------------------------------------
394 // Simply returning fields back to their original position is
395 // not enough. We need to take into acccount what fields have
396 // been removed earlier and adjust accordingly. Based on the
397 // algorithm employed in moveColumn().
398 USHORT
OWizColumnSelect::adjustColumnPosition( ListBox
* _pLeft
,
399 const ::rtl::OUString
& _sColumnName
,
400 ODatabaseExport::TColumnVector::size_type nCurrentPos
,
401 const ::comphelper::TStringMixEqualFunctor
& _aCase
)
403 USHORT nAdjustedPos
= 0;
405 // if returning all entries to their original position,
406 // then there is no need to adjust the positions.
407 if (m_ibColumns_LH
.HasFocus())
410 sal_uInt16 nCount
= _pLeft
->GetEntryCount();
411 ::rtl::OUString sColumnString
;
412 for(sal_uInt16 i
=0; i
< nCount
; ++i
)
414 sColumnString
= _pLeft
->GetEntry(i
);
415 if(_sColumnName
!= sColumnString
)
417 // find the new column in the dest name mapping to obtain the old column
418 OCopyTableWizard::TNameMapping::iterator aIter
= ::std::find_if(m_pParent
->m_mNameMapping
.begin(),m_pParent
->m_mNameMapping
.end(),
420 ::std::bind2nd(_aCase
, sColumnString
),
421 ::std::select2nd
<OCopyTableWizard::TNameMapping::value_type
>())
424 DBG_ASSERT(aIter
!= m_pParent
->m_mNameMapping
.end(),"Column must be defined");
425 const ODatabaseExport::TColumns
* pSrcColumns
= m_pParent
->getSourceColumns();
426 ODatabaseExport::TColumns::const_iterator aSrcIter
= pSrcColumns
->find((*aIter
).first
);
427 if ( aSrcIter
!= pSrcColumns
->end() )
429 // we need also the old position of this column to insert it back on that position again
430 const ODatabaseExport::TColumnVector
* pSrcVector
= m_pParent
->getSrcVector();
431 ODatabaseExport::TColumnVector::const_iterator aPos
= ::std::find(pSrcVector
->begin(),pSrcVector
->end(),aSrcIter
);
432 ODatabaseExport::TColumnVector::size_type nPos
= aPos
- pSrcVector
->begin();
433 if( nPos
< nCurrentPos
)
443 // -----------------------------------------------------------------------------
444 void OWizColumnSelect::enableButtons()
446 sal_Bool bEntries
= m_lbNewColumnNames
.GetEntryCount() != 0;
448 m_pParent
->m_mNameMapping
.clear();
450 m_pParent
->GetOKButton().Enable(bEntries
);
451 m_pParent
->EnableButton(OCopyTableWizard::WIZARD_NEXT
,bEntries
&& m_pParent
->getOperation() != CopyTableOperation::AppendData
);
453 // -----------------------------------------------------------------------------