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 ************************************************************************/
31 #include "WCopyTable.hxx"
33 #include "defaultobjectnamecheck.hxx"
34 #include <tools/debug.hxx>
35 #include "dbaccess_helpid.hrc"
36 #include "dbu_misc.hrc"
37 #include "WizardPages.hrc"
38 #include <com/sun/star/sdbc/XResultSet.hpp>
39 #include <com/sun/star/sdb/CommandType.hpp>
40 #include <com/sun/star/sdbc/XRow.hpp>
41 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
42 #include <com/sun/star/sdbcx/XViewsSupplier.hpp>
43 #include <com/sun/star/sdb/application/CopyTableOperation.hpp>
44 #include <vcl/msgbox.hxx>
45 #include <connectivity/dbexception.hxx>
46 #include <connectivity/dbtools.hxx>
47 #include "UITools.hxx"
48 #include "moduledbu.hxx"
49 #include <cppuhelper/exc_hlp.hxx>
51 using namespace ::dbaui
;
52 using namespace ::dbtools
;
53 using namespace ::com::sun::star::uno
;
54 using namespace ::com::sun::star::beans
;
55 using namespace ::com::sun::star::container
;
56 using namespace ::com::sun::star::util
;
57 using namespace ::com::sun::star::sdb
;
58 using namespace ::com::sun::star::sdbc
;
59 using namespace ::com::sun::star::sdbcx
;
61 namespace CopyTableOperation
= ::com::sun::star::sdb::application::CopyTableOperation
;
63 //========================================================================
65 //========================================================================
67 //------------------------------------------------------------------------
68 OCopyTable::OCopyTable( Window
* pParent
)
69 :OWizardPage( pParent
, ModuleRes(TAB_WIZ_COPYTABLE
) )
70 ,m_ftTableName( this, ModuleRes( FT_TABLENAME
) )
71 ,m_edTableName( this, ModuleRes( ET_TABLENAME
) )
72 ,m_aFL_Options( this, ModuleRes( FL_OPTIONS
) )
73 ,m_aRB_DefData( this, ModuleRes( RB_DEFDATA
) )
74 ,m_aRB_Def( this, ModuleRes( RB_DEF
) )
75 ,m_aRB_View( this, ModuleRes( RB_VIEW
) )
76 ,m_aRB_AppendData( this, ModuleRes( RB_APPENDDATA
) )
77 ,m_aCB_UseHeaderLine( this, ModuleRes( CB_USEHEADERLINE
) )
78 ,m_aCB_PrimaryColumn( this, ModuleRes( CB_PRIMARY_COLUMN
) )
79 ,m_aFT_KeyName( this, ModuleRes( FT_KEYNAME
) )
80 ,m_edKeyName( this, ModuleRes( ET_KEYNAME
) )
83 ,m_bUseHeaderAllowed(sal_True
)
85 DBG_CTOR(OCopyTable
,NULL
);
87 m_edTableName
.SetMaxTextLen( EDIT_NOLIMIT
);
89 if ( m_pParent
->m_xDestConnection
.is() )
91 if ( !m_pParent
->supportsViews() )
94 m_aCB_UseHeaderLine
.Check(sal_True
);
95 m_bPKeyAllowed
= m_pParent
->supportsPrimaryKey();
97 m_aCB_PrimaryColumn
.Enable(m_bPKeyAllowed
);
99 m_aRB_AppendData
.SetClickHdl( LINK( this, OCopyTable
, AppendDataClickHdl
) );
101 m_aRB_DefData
.SetClickHdl( LINK( this, OCopyTable
, RadioChangeHdl
) );
102 m_aRB_Def
.SetClickHdl( LINK( this, OCopyTable
, RadioChangeHdl
) );
103 m_aRB_View
.SetClickHdl( LINK( this, OCopyTable
, RadioChangeHdl
) );
105 m_aCB_PrimaryColumn
.SetClickHdl(LINK( this, OCopyTable
, KeyClickHdl
) );
107 m_aFT_KeyName
.Enable(sal_False
);
108 m_edKeyName
.Enable(sal_False
);
109 ::rtl::OUString
sKeyName(RTL_CONSTASCII_USTRINGPARAM("ID"));
110 sKeyName
= m_pParent
->createUniqueName(sKeyName
);
111 m_edKeyName
.SetText(sKeyName
);
113 sal_Int32 nMaxLen
= m_pParent
->getMaxColumnNameLength();
114 m_edKeyName
.SetMaxTextLen(nMaxLen
? (xub_StrLen
)nMaxLen
: EDIT_NOLIMIT
);
119 SetText(String(ModuleRes(STR_COPYTABLE_TITLE_COPY
)));
122 //------------------------------------------------------------------------
123 OCopyTable::~OCopyTable()
125 DBG_DTOR(OCopyTable
,NULL
);
127 //------------------------------------------------------------------------
128 IMPL_LINK( OCopyTable
, AppendDataClickHdl
, Button
*, /*pButton*/ )
130 DBG_CHKTHIS(OCopyTable
,NULL
);
132 SetAppendDataRadio();
136 void OCopyTable::SetAppendDataRadio()
138 m_pParent
->EnableButton(OCopyTableWizard::WIZARD_NEXT
,sal_True
);
139 m_aFT_KeyName
.Enable(sal_False
);
140 m_aCB_PrimaryColumn
.Enable(sal_False
);
141 m_edKeyName
.Enable(sal_False
);
142 m_pParent
->setOperation(CopyTableOperation::AppendData
);
145 //------------------------------------------------------------------------
146 IMPL_LINK( OCopyTable
, RadioChangeHdl
, Button
*, pButton
)
148 DBG_CHKTHIS(OCopyTable
,NULL
);
149 m_pParent
->EnableButton(OCopyTableWizard::WIZARD_NEXT
,pButton
!= &m_aRB_View
);
150 sal_Bool bKey
= m_bPKeyAllowed
&& pButton
!= &m_aRB_View
;
151 m_aFT_KeyName
.Enable(bKey
&& m_aCB_PrimaryColumn
.IsChecked());
152 m_edKeyName
.Enable(bKey
&& m_aCB_PrimaryColumn
.IsChecked());
153 m_aCB_PrimaryColumn
.Enable(bKey
);
154 m_aCB_UseHeaderLine
.Enable(m_bUseHeaderAllowed
&& IsOptionDefData());
156 // set typ what to do
157 if( IsOptionDefData() )
158 m_pParent
->setOperation( CopyTableOperation::CopyDefinitionAndData
);
159 else if( IsOptionDef() )
160 m_pParent
->setOperation( CopyTableOperation::CopyDefinitionOnly
);
161 else if( IsOptionView() )
162 m_pParent
->setOperation( CopyTableOperation::CreateAsView
);
166 //------------------------------------------------------------------------
167 IMPL_LINK( OCopyTable
, KeyClickHdl
, Button
*, /*pButton*/ )
169 DBG_CHKTHIS(OCopyTable
,NULL
);
170 m_edKeyName
.Enable(m_aCB_PrimaryColumn
.IsChecked());
171 m_aFT_KeyName
.Enable(m_aCB_PrimaryColumn
.IsChecked());
174 //------------------------------------------------------------------------
175 sal_Bool
OCopyTable::LeavePage()
177 DBG_CHKTHIS(OCopyTable
,NULL
);
178 m_pParent
->m_bCreatePrimaryKeyColumn
= (m_bPKeyAllowed
&& m_aCB_PrimaryColumn
.IsEnabled()) ? m_aCB_PrimaryColumn
.IsChecked() : sal_False
;
179 m_pParent
->m_aKeyName
= m_pParent
->m_bCreatePrimaryKeyColumn
? m_edKeyName
.GetText() : String();
180 m_pParent
->setUseHeaderLine( m_aCB_UseHeaderLine
.IsChecked() );
182 // first check if the table already exists in the database
183 if( m_pParent
->getOperation() != CopyTableOperation::AppendData
)
185 m_pParent
->clearDestColumns();
186 DynamicTableOrQueryNameCheck
aNameCheck( m_pParent
->m_xDestConnection
, CommandType::TABLE
);
187 SQLExceptionInfo aErrorInfo
;
188 if ( !aNameCheck
.isNameValid( m_edTableName
.GetText(), aErrorInfo
) )
190 aErrorInfo
.append( SQLExceptionInfo::SQL_CONTEXT
, String( ModuleRes( STR_SUGGEST_APPEND_TABLE_DATA
) ) );
191 m_pParent
->showError(aErrorInfo
.get());
196 // have to check the length of the table name
197 Reference
< XDatabaseMetaData
> xMeta
= m_pParent
->m_xDestConnection
->getMetaData();
198 ::rtl::OUString sCatalog
;
199 ::rtl::OUString sSchema
;
200 ::rtl::OUString sTable
;
201 ::dbtools::qualifiedNameComponents( xMeta
,
202 m_edTableName
.GetText(),
206 ::dbtools::eInDataManipulation
);
207 sal_Int32 nMaxLength
= xMeta
->getMaxTableNameLength();
208 if ( nMaxLength
&& sTable
.getLength() > nMaxLength
)
210 String
sError(ModuleRes(STR_INVALID_TABLE_NAME_LENGTH
));
211 m_pParent
->showError(sError
);
215 // now we have to check if the name of the primary key already exists
216 if ( m_pParent
->m_bCreatePrimaryKeyColumn
217 && m_pParent
->m_aKeyName
!= m_pParent
->createUniqueName(m_pParent
->m_aKeyName
) )
219 String
aInfoString( ModuleRes(STR_WIZ_PKEY_ALREADY_DEFINED
) );
220 aInfoString
+= String(' ');
221 aInfoString
+= String(m_pParent
->m_aKeyName
);
222 m_pParent
->showError(aInfoString
);
227 if ( !m_edTableName
.GetSavedValue().Equals(m_edTableName
.GetText()) )
228 { // table exists and name has changed
229 if ( m_pParent
->getOperation() == CopyTableOperation::AppendData
)
231 if(!checkAppendData())
234 else if ( m_nOldOperation
== CopyTableOperation::AppendData
)
236 m_edTableName
.SaveValue();
241 { // table exist and is not new or doesn't exist and so on
242 if ( CopyTableOperation::AppendData
== m_pParent
->getOperation() )
244 if( !checkAppendData() )
248 m_pParent
->m_sName
= m_edTableName
.GetText();
249 m_edTableName
.SaveValue();
251 if(m_pParent
->m_sName
.isEmpty())
253 String
sError(ModuleRes(STR_INVALID_TABLE_NAME
));
254 m_pParent
->showError(sError
);
260 //------------------------------------------------------------------------
261 void OCopyTable::ActivatePage()
263 DBG_CHKTHIS(OCopyTable
,NULL
);
264 m_pParent
->GetOKButton().Enable( sal_True
);
265 m_nOldOperation
= m_pParent
->getOperation();
266 m_edTableName
.GrabFocus();
267 m_aCB_UseHeaderLine
.Check(m_pParent
->UseHeaderLine());
269 //------------------------------------------------------------------------
270 String
OCopyTable::GetTitle() const
272 DBG_CHKTHIS(OCopyTable
,NULL
);
273 return String(ModuleRes(STR_WIZ_TABLE_COPY
));
275 //------------------------------------------------------------------------
276 void OCopyTable::Reset()
278 DBG_CHKTHIS(OCopyTable
,NULL
);
279 m_bFirstTime
= sal_False
;
281 m_edTableName
.SetText( m_pParent
->m_sName
);
282 m_edTableName
.SaveValue();
284 //------------------------------------------------------------------------
285 sal_Bool
OCopyTable::checkAppendData()
287 DBG_CHKTHIS(OCopyTable
,NULL
);
288 m_pParent
->clearDestColumns();
289 Reference
< XPropertySet
> xTable
;
290 Reference
< XTablesSupplier
> xSup( m_pParent
->m_xDestConnection
, UNO_QUERY
);
291 Reference
<XNameAccess
> xTables
;
293 xTables
= xSup
->getTables();
294 if(xTables
.is() && xTables
->hasByName(m_edTableName
.GetText()))
296 const ODatabaseExport::TColumnVector
* pSrcColumns
= m_pParent
->getSrcVector();
297 const sal_uInt32 nSrcSize
= pSrcColumns
->size();
298 m_pParent
->m_vColumnPos
.resize( nSrcSize
, ODatabaseExport::TPositions::value_type( COLUMN_POSITION_NOT_FOUND
, COLUMN_POSITION_NOT_FOUND
) );
299 m_pParent
->m_vColumnTypes
.resize( nSrcSize
, COLUMN_POSITION_NOT_FOUND
);
301 // set new destination
302 xTables
->getByName( m_edTableName
.GetText() ) >>= xTable
;
303 ObjectCopySource
aTableCopySource( m_pParent
->m_xDestConnection
, xTable
);
304 m_pParent
->loadData( aTableCopySource
, m_pParent
->m_vDestColumns
, m_pParent
->m_aDestVec
);
305 const ODatabaseExport::TColumnVector
* pDestColumns
= m_pParent
->getDestVector();
306 ODatabaseExport::TColumnVector::const_iterator aDestIter
= pDestColumns
->begin();
307 ODatabaseExport::TColumnVector::const_iterator aDestEnd
= pDestColumns
->end();
308 const sal_uInt32 nDestSize
= pDestColumns
->size();
309 sal_Bool bNotConvert
;
311 for(sal_Int32 nPos
= 1;aDestIter
!= aDestEnd
&& i
< nDestSize
&& i
< nSrcSize
;++aDestIter
,++nPos
,++i
)
313 bNotConvert
= sal_True
;
314 m_pParent
->m_vColumnPos
[i
] = ODatabaseExport::TPositions::value_type(nPos
,nPos
);
315 TOTypeInfoSP pTypeInfo
= m_pParent
->convertType((*aDestIter
)->second
->getSpecialTypeInfo(),bNotConvert
);
318 m_pParent
->showColumnTypeNotSupported((*aDestIter
)->first
);
322 if ( pTypeInfo
.get() )
323 m_pParent
->m_vColumnTypes
[i
] = pTypeInfo
->nType
;
325 m_pParent
->m_vColumnTypes
[i
] = DataType::VARCHAR
;
332 String
sError(ModuleRes(STR_INVALID_TABLE_NAME
));
333 m_pParent
->showError(sError
);
338 // -----------------------------------------------------------------------------
339 void OCopyTable::setCreatePrimaryKey( bool _bDoCreate
, const ::rtl::OUString
& _rSuggestedName
)
341 bool bCreatePK
= m_bPKeyAllowed
&& _bDoCreate
;
342 m_aCB_PrimaryColumn
.Check( bCreatePK
);
343 m_edKeyName
.SetText( _rSuggestedName
);
345 m_aFT_KeyName
.Enable( bCreatePK
);
346 m_edKeyName
.Enable( bCreatePK
);
349 // -----------------------------------------------------------------------------
350 void OCopyTable::setCreateStyleAction()
352 // reselect the last action before
353 switch(m_pParent
->getOperation())
355 case CopyTableOperation::CopyDefinitionAndData
:
356 m_aRB_DefData
.Check(sal_True
);
357 RadioChangeHdl(&m_aRB_DefData
);
359 case CopyTableOperation::CopyDefinitionOnly
:
360 m_aRB_Def
.Check(sal_True
);
361 RadioChangeHdl(&m_aRB_Def
);
363 case CopyTableOperation::AppendData
:
364 m_aRB_AppendData
.Check(sal_True
);
365 SetAppendDataRadio();
367 case CopyTableOperation::CreateAsView
:
368 if ( m_aRB_View
.IsEnabled() )
370 m_aRB_View
.Check(sal_True
);
371 RadioChangeHdl(&m_aRB_View
);
375 m_aRB_DefData
.Check(sal_True
);
376 RadioChangeHdl(&m_aRB_DefData
);
381 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */