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: WCPage.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"
35 #include "WCopyTable.hxx"
37 #ifndef DBACCESS_SOURCE_UI_MISC_DEFAULTOBJECTNAMECHECK_HXX
38 #include "defaultobjectnamecheck.hxx"
40 #ifndef _TOOLS_DEBUG_HXX
41 #include <tools/debug.hxx>
43 #ifndef _DBA_DBACCESS_HELPID_HRC_
44 #include "dbaccess_helpid.hrc"
46 #ifndef _DBU_MISC_HRC_
47 #include "dbu_misc.hrc"
49 #ifndef DBAUI_WIZARD_PAGES_HRC
50 #include "WizardPages.hrc"
52 #ifndef _COM_SUN_STAR_SDBC_XRESULTSET_HPP_
53 #include <com/sun/star/sdbc/XResultSet.hpp>
55 #ifndef _COM_SUN_STAR_SDB_COMMANDTYPE_HPP_
56 #include <com/sun/star/sdb/CommandType.hpp>
58 #ifndef _COM_SUN_STAR_SDBC_XROW_HPP_
59 #include <com/sun/star/sdbc/XRow.hpp>
61 #ifndef _COM_SUN_STAR_SDBCX_XTABLESSUPPLIER_HPP_
62 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
64 #ifndef _COM_SUN_STAR_SDBCX_XVIEWSSUPPLIER_HPP_
65 #include <com/sun/star/sdbcx/XViewsSupplier.hpp>
67 #ifndef _COM_SUN_STAR_SDB_APPLICATION_COPYTABLEOPERATION_HPP_
68 #include <com/sun/star/sdb/application/CopyTableOperation.hpp>
70 #ifndef _SV_MSGBOX_HXX
71 #include <vcl/msgbox.hxx>
73 #ifndef _DBHELPER_DBEXCEPTION_HXX_
74 #include <connectivity/dbexception.hxx>
76 #ifndef _CONNECTIVITY_DBTOOLS_HXX_
77 #include <connectivity/dbtools.hxx>
79 #ifndef DBAUI_TOOLS_HXX
80 #include "UITools.hxx"
82 #ifndef _DBAUI_MODULE_DBU_HXX_
83 #include "moduledbu.hxx"
85 #ifndef _CPPUHELPER_EXC_HLP_HXX_
86 #include <cppuhelper/exc_hlp.hxx>
89 using namespace ::dbaui
;
90 using namespace ::dbtools
;
91 using namespace ::com::sun::star::uno
;
92 using namespace ::com::sun::star::beans
;
93 using namespace ::com::sun::star::container
;
94 using namespace ::com::sun::star::util
;
95 using namespace ::com::sun::star::sdb
;
96 using namespace ::com::sun::star::sdbc
;
97 using namespace ::com::sun::star::sdbcx
;
99 namespace CopyTableOperation
= ::com::sun::star::sdb::application::CopyTableOperation
;
101 //========================================================================
103 //========================================================================
105 //------------------------------------------------------------------------
106 OCopyTable::OCopyTable( Window
* pParent
)
107 :OWizardPage( pParent
, ModuleRes(TAB_WIZ_COPYTABLE
) )
108 ,m_ftTableName( this, ModuleRes( FT_TABLENAME
) )
109 ,m_edTableName( this, ModuleRes( ET_TABLENAME
) )
110 ,m_aFL_Options( this, ModuleRes( FL_OPTIONS
) )
111 ,m_aRB_DefData( this, ModuleRes( RB_DEFDATA
) )
112 ,m_aRB_Def( this, ModuleRes( RB_DEF
) )
113 ,m_aRB_View( this, ModuleRes( RB_VIEW
) )
114 ,m_aRB_AppendData( this, ModuleRes( RB_APPENDDATA
) )
115 ,m_aCB_UseHeaderLine( this, ModuleRes( CB_USEHEADERLINE
) )
116 ,m_aCB_PrimaryColumn( this, ModuleRes( CB_PRIMARY_COLUMN
) )
117 ,m_aFT_KeyName( this, ModuleRes( FT_KEYNAME
) )
118 ,m_edKeyName( this, ModuleRes( ET_KEYNAME
) )
121 ,m_bUseHeaderAllowed(TRUE
)
123 DBG_CTOR(OCopyTable
,NULL
);
125 m_edTableName
.SetMaxTextLen( EDIT_NOLIMIT
);
127 if ( m_pParent
->m_xDestConnection
.is() )
129 if ( !m_pParent
->supportsViews() )
130 m_aRB_View
.Disable();
132 m_aCB_UseHeaderLine
.Check(TRUE
);
133 m_bPKeyAllowed
= m_pParent
->supportsPrimaryKey();
135 m_aCB_PrimaryColumn
.Enable(m_bPKeyAllowed
);
137 m_aRB_AppendData
.SetClickHdl( LINK( this, OCopyTable
, AppendDataClickHdl
) );
139 m_aRB_DefData
.SetClickHdl( LINK( this, OCopyTable
, RadioChangeHdl
) );
140 m_aRB_Def
.SetClickHdl( LINK( this, OCopyTable
, RadioChangeHdl
) );
141 m_aRB_View
.SetClickHdl( LINK( this, OCopyTable
, RadioChangeHdl
) );
143 m_aCB_PrimaryColumn
.SetClickHdl(LINK( this, OCopyTable
, KeyClickHdl
) );
145 m_aFT_KeyName
.Enable(sal_False
);
146 m_edKeyName
.Enable(sal_False
);
147 ::rtl::OUString
sKeyName(RTL_CONSTASCII_USTRINGPARAM("ID"));
148 sKeyName
= m_pParent
->createUniqueName(sKeyName
);
149 m_edKeyName
.SetText(sKeyName
);
151 sal_Int32 nMaxLen
= m_pParent
->getMaxColumnNameLength();
152 m_edKeyName
.SetMaxTextLen(nMaxLen
? (xub_StrLen
)nMaxLen
: EDIT_NOLIMIT
);
157 SetText(String(ModuleRes(STR_COPYTABLE_TITLE_COPY
)));
160 //------------------------------------------------------------------------
161 OCopyTable::~OCopyTable()
163 DBG_DTOR(OCopyTable
,NULL
);
165 //------------------------------------------------------------------------
166 IMPL_LINK( OCopyTable
, AppendDataClickHdl
, Button
*, /*pButton*/ )
168 DBG_CHKTHIS(OCopyTable
,NULL
);
170 SetAppendDataRadio();
174 void OCopyTable::SetAppendDataRadio()
176 m_pParent
->EnableButton(OCopyTableWizard::WIZARD_NEXT
,sal_True
);
177 m_aFT_KeyName
.Enable(sal_False
);
178 m_aCB_PrimaryColumn
.Enable(sal_False
);
179 m_edKeyName
.Enable(sal_False
);
180 m_pParent
->setOperation(CopyTableOperation::AppendData
);
184 //------------------------------------------------------------------------
185 IMPL_LINK( OCopyTable
, RadioChangeHdl
, Button
*, pButton
)
187 DBG_CHKTHIS(OCopyTable
,NULL
);
188 m_pParent
->EnableButton(OCopyTableWizard::WIZARD_NEXT
,pButton
!= &m_aRB_View
);
189 sal_Bool bKey
= m_bPKeyAllowed
&& pButton
!= &m_aRB_View
;
190 m_aFT_KeyName
.Enable(bKey
&& m_aCB_PrimaryColumn
.IsChecked());
191 m_edKeyName
.Enable(bKey
&& m_aCB_PrimaryColumn
.IsChecked());
192 m_aCB_PrimaryColumn
.Enable(bKey
);
193 m_aCB_UseHeaderLine
.Enable(m_bUseHeaderAllowed
&& IsOptionDefData());
195 // set typ what to do
196 if( IsOptionDefData() )
197 m_pParent
->setOperation( CopyTableOperation::CopyDefinitionAndData
);
198 else if( IsOptionDef() )
199 m_pParent
->setOperation( CopyTableOperation::CopyDefinitionOnly
);
200 else if( IsOptionView() )
201 m_pParent
->setOperation( CopyTableOperation::CreateAsView
);
205 //------------------------------------------------------------------------
206 IMPL_LINK( OCopyTable
, KeyClickHdl
, Button
*, /*pButton*/ )
208 DBG_CHKTHIS(OCopyTable
,NULL
);
209 m_edKeyName
.Enable(m_aCB_PrimaryColumn
.IsChecked());
210 m_aFT_KeyName
.Enable(m_aCB_PrimaryColumn
.IsChecked());
213 //------------------------------------------------------------------------
214 sal_Bool
OCopyTable::LeavePage()
216 DBG_CHKTHIS(OCopyTable
,NULL
);
217 m_pParent
->m_bCreatePrimaryKeyColumn
= (m_bPKeyAllowed
&& m_aCB_PrimaryColumn
.IsEnabled()) ? m_aCB_PrimaryColumn
.IsChecked() : sal_False
;
218 m_pParent
->m_aKeyName
= m_pParent
->m_bCreatePrimaryKeyColumn
? m_edKeyName
.GetText() : String();
219 m_pParent
->setUseHeaderLine( m_aCB_UseHeaderLine
.IsChecked() );
221 // first check if the table already exists in the database
222 if( m_pParent
->getOperation() != CopyTableOperation::AppendData
)
224 m_pParent
->clearDestColumns();
225 DynamicTableOrQueryNameCheck
aNameCheck( m_pParent
->m_xDestConnection
, CommandType::TABLE
);
226 SQLExceptionInfo aErrorInfo
;
227 if ( !aNameCheck
.isNameValid( m_edTableName
.GetText(), aErrorInfo
) )
229 aErrorInfo
.append( SQLExceptionInfo::SQL_CONTEXT
, String( ModuleRes( STR_SUGGEST_APPEND_TABLE_DATA
) ) );
230 ::dbaui::showError( aErrorInfo
, m_pParent
, m_pParent
->m_xFactory
);
234 // have to check the length of the table name
235 Reference
< XDatabaseMetaData
> xMeta
= m_pParent
->m_xDestConnection
->getMetaData();
236 ::rtl::OUString sCatalog
;
237 ::rtl::OUString sSchema
;
238 ::rtl::OUString sTable
;
239 ::dbtools::qualifiedNameComponents( xMeta
,
240 m_edTableName
.GetText(),
244 ::dbtools::eInDataManipulation
);
245 sal_Int32 nMaxLength
= xMeta
->getMaxTableNameLength();
246 if ( nMaxLength
&& sTable
.getLength() > nMaxLength
)
248 ErrorBox(this, ModuleRes(ERROR_INVALID_TABLE_NAME_LENGTH
)).Execute();
252 // now we have to check if the name of the primary key already exists
253 if ( m_pParent
->m_bCreatePrimaryKeyColumn
254 && m_pParent
->m_aKeyName
!= m_pParent
->createUniqueName(m_pParent
->m_aKeyName
) )
256 String
aInfoString( ModuleRes(STR_WIZ_PKEY_ALREADY_DEFINED
) );
257 aInfoString
+= String(' ');
258 aInfoString
+= String(m_pParent
->m_aKeyName
);
259 InfoBox
aNameInfoBox( this, aInfoString
);
260 aNameInfoBox
.Execute();
265 if ( !m_edTableName
.GetSavedValue().Equals(m_edTableName
.GetText()) )
266 { // table exists and name has changed
267 if ( m_pParent
->getOperation() == CopyTableOperation::AppendData
)
269 if(!checkAppendData())
272 else if ( m_nOldOperation
== CopyTableOperation::AppendData
)
274 m_edTableName
.SaveValue();
279 { // table exist and is not new or doesn't exist and so on
280 if ( CopyTableOperation::AppendData
== m_pParent
->getOperation() )
282 if( !checkAppendData() )
286 m_pParent
->m_sName
= m_edTableName
.GetText();
287 m_edTableName
.SaveValue();
289 if(!m_pParent
->m_sName
.getLength())
291 ErrorBox(this, ModuleRes(ERROR_INVALID_TABLE_NAME
)).Execute();
297 //------------------------------------------------------------------------
298 void OCopyTable::ActivatePage()
300 DBG_CHKTHIS(OCopyTable
,NULL
);
301 m_pParent
->GetOKButton().Enable( TRUE
);
302 m_nOldOperation
= m_pParent
->getOperation();
303 m_edTableName
.GrabFocus();
304 m_aCB_UseHeaderLine
.Check(m_pParent
->UseHeaderLine());
306 //------------------------------------------------------------------------
307 String
OCopyTable::GetTitle() const
309 DBG_CHKTHIS(OCopyTable
,NULL
);
310 return String(ModuleRes(STR_WIZ_TABLE_COPY
));
312 //------------------------------------------------------------------------
313 void OCopyTable::Reset()
315 DBG_CHKTHIS(OCopyTable
,NULL
);
316 m_bFirstTime
= sal_False
;
318 m_edTableName
.SetText( m_pParent
->m_sName
);
319 m_edTableName
.SaveValue();
321 //------------------------------------------------------------------------
322 sal_Bool
OCopyTable::checkAppendData()
324 DBG_CHKTHIS(OCopyTable
,NULL
);
325 m_pParent
->clearDestColumns();
326 Reference
< XPropertySet
> xTable
;
327 Reference
< XTablesSupplier
> xSup( m_pParent
->m_xDestConnection
, UNO_QUERY
);
328 Reference
<XNameAccess
> xTables
;
330 xTables
= xSup
->getTables();
331 if(xTables
.is() && xTables
->hasByName(m_edTableName
.GetText()))
333 const ODatabaseExport::TColumnVector
* pSrcColumns
= m_pParent
->getSrcVector();
334 const sal_uInt32 nSrcSize
= pSrcColumns
->size();
335 m_pParent
->m_vColumnPos
.resize( nSrcSize
, ODatabaseExport::TPositions::value_type( COLUMN_POSITION_NOT_FOUND
, COLUMN_POSITION_NOT_FOUND
) );
336 m_pParent
->m_vColumnTypes
.resize( nSrcSize
, COLUMN_POSITION_NOT_FOUND
);
338 // set new destination
339 xTables
->getByName( m_edTableName
.GetText() ) >>= xTable
;
340 ObjectCopySource
aTableCopySource( m_pParent
->m_xDestConnection
, xTable
);
341 m_pParent
->loadData( aTableCopySource
, m_pParent
->m_vDestColumns
, m_pParent
->m_aDestVec
);
343 const ODatabaseExport::TColumnVector
* pDestColumns
= m_pParent
->getDestVector();
344 ODatabaseExport::TColumnVector::const_iterator aDestIter
= pDestColumns
->begin();
345 ODatabaseExport::TColumnVector::const_iterator aDestEnd
= pDestColumns
->end();
346 const sal_uInt32 nDestSize
= pDestColumns
->size();
347 sal_Bool bNotConvert
;
349 for(sal_Int32 nPos
= 1;aDestIter
!= aDestEnd
&& i
< nDestSize
&& i
< nSrcSize
;++aDestIter
,++nPos
,++i
)
351 bNotConvert
= sal_True
;
352 m_pParent
->m_vColumnPos
[i
] = ODatabaseExport::TPositions::value_type(nPos
,nPos
);
353 TOTypeInfoSP pTypeInfo
= m_pParent
->convertType((*aDestIter
)->second
->getSpecialTypeInfo(),bNotConvert
);
356 m_pParent
->showColumnTypeNotSupported((*aDestIter
)->first
);
360 if ( pTypeInfo
.get() )
361 m_pParent
->m_vColumnTypes
[i
] = pTypeInfo
->nType
;
363 m_pParent
->m_vColumnTypes
[i
] = DataType::VARCHAR
;
370 ErrorBox( this, ModuleRes( ERROR_INVALID_TABLE_NAME
) ).Execute();
371 // TODO: shouldn't this be some kind of showError? In case of the UNO service for this wizard,
372 // shouldn't this even be a usage of the service's interaction handler?
377 // -----------------------------------------------------------------------------
378 void OCopyTable::setCreatePrimaryKey( bool _bDoCreate
, const ::rtl::OUString
& _rSuggestedName
)
380 bool bCreatePK
= m_bPKeyAllowed
&& _bDoCreate
;
381 m_aCB_PrimaryColumn
.Check( bCreatePK
);
382 m_edKeyName
.SetText( _rSuggestedName
);
384 m_aFT_KeyName
.Enable( bCreatePK
);
385 m_edKeyName
.Enable( bCreatePK
);
388 // -----------------------------------------------------------------------------
389 //---dyf add 2006/7/10
390 void OCopyTable::setCreateStyleAction()
392 // reselect the last action before
393 switch(m_pParent
->getOperation())
395 case CopyTableOperation::CopyDefinitionAndData
:
396 m_aRB_DefData
.Check(sal_True
);
397 RadioChangeHdl(&m_aRB_DefData
);
399 case CopyTableOperation::CopyDefinitionOnly
:
400 m_aRB_Def
.Check(sal_True
);
401 RadioChangeHdl(&m_aRB_Def
);
403 case CopyTableOperation::AppendData
:
404 m_aRB_AppendData
.Check(sal_True
);
405 SetAppendDataRadio();
407 case CopyTableOperation::CreateAsView
:
408 if ( m_aRB_View
.IsEnabled() )
410 m_aRB_View
.Check(sal_True
);
411 RadioChangeHdl(&m_aRB_View
);
415 m_aRB_DefData
.Check(sal_True
);
416 RadioChangeHdl(&m_aRB_DefData
);