xref: /aoo41x/main/dbaccess/source/ui/misc/WCPage.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_dbaccess.hxx"
30 
31 #include "WCPage.hxx"
32 #include "WCopyTable.hxx"
33 
34 #ifndef DBACCESS_SOURCE_UI_MISC_DEFAULTOBJECTNAMECHECK_HXX
35 #include "defaultobjectnamecheck.hxx"
36 #endif
37 #ifndef _TOOLS_DEBUG_HXX
38 #include <tools/debug.hxx>
39 #endif
40 #ifndef _DBA_DBACCESS_HELPID_HRC_
41 #include "dbaccess_helpid.hrc"
42 #endif
43 #ifndef _DBU_MISC_HRC_
44 #include "dbu_misc.hrc"
45 #endif
46 #ifndef DBAUI_WIZARD_PAGES_HRC
47 #include "WizardPages.hrc"
48 #endif
49 #ifndef _COM_SUN_STAR_SDBC_XRESULTSET_HPP_
50 #include <com/sun/star/sdbc/XResultSet.hpp>
51 #endif
52 #ifndef _COM_SUN_STAR_SDB_COMMANDTYPE_HPP_
53 #include <com/sun/star/sdb/CommandType.hpp>
54 #endif
55 #ifndef _COM_SUN_STAR_SDBC_XROW_HPP_
56 #include <com/sun/star/sdbc/XRow.hpp>
57 #endif
58 #ifndef _COM_SUN_STAR_SDBCX_XTABLESSUPPLIER_HPP_
59 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
60 #endif
61 #ifndef _COM_SUN_STAR_SDBCX_XVIEWSSUPPLIER_HPP_
62 #include <com/sun/star/sdbcx/XViewsSupplier.hpp>
63 #endif
64 #ifndef _COM_SUN_STAR_SDB_APPLICATION_COPYTABLEOPERATION_HPP_
65 #include <com/sun/star/sdb/application/CopyTableOperation.hpp>
66 #endif
67 #ifndef _SV_MSGBOX_HXX
68 #include <vcl/msgbox.hxx>
69 #endif
70 #ifndef _DBHELPER_DBEXCEPTION_HXX_
71 #include <connectivity/dbexception.hxx>
72 #endif
73 #ifndef _CONNECTIVITY_DBTOOLS_HXX_
74 #include <connectivity/dbtools.hxx>
75 #endif
76 #ifndef DBAUI_TOOLS_HXX
77 #include "UITools.hxx"
78 #endif
79 #ifndef _DBAUI_MODULE_DBU_HXX_
80 #include "moduledbu.hxx"
81 #endif
82 #ifndef _CPPUHELPER_EXC_HLP_HXX_
83 #include <cppuhelper/exc_hlp.hxx>
84 #endif
85 
86 using namespace ::dbaui;
87 using namespace ::dbtools;
88 using namespace ::com::sun::star::uno;
89 using namespace ::com::sun::star::beans;
90 using namespace ::com::sun::star::container;
91 using namespace ::com::sun::star::util;
92 using namespace ::com::sun::star::sdb;
93 using namespace ::com::sun::star::sdbc;
94 using namespace ::com::sun::star::sdbcx;
95 
96 namespace CopyTableOperation = ::com::sun::star::sdb::application::CopyTableOperation;
97 
98 //========================================================================
99 // Klasse OCopyTable
100 //========================================================================
101 DBG_NAME(OCopyTable)
102 //------------------------------------------------------------------------
103 OCopyTable::OCopyTable( Window * pParent )
104 	:OWizardPage( pParent, ModuleRes(TAB_WIZ_COPYTABLE) )
105 	,m_ftTableName(			this, ModuleRes( FT_TABLENAME		) )
106 	,m_edTableName(			this, ModuleRes( ET_TABLENAME		) )
107     ,m_aFL_Options(         this, ModuleRes( FL_OPTIONS         ) )
108 	,m_aRB_DefData(			this, ModuleRes( RB_DEFDATA			) )
109 	,m_aRB_Def(				this, ModuleRes( RB_DEF				) )
110 	,m_aRB_View(			this, ModuleRes( RB_VIEW			) )
111     ,m_aRB_AppendData(	    this, ModuleRes( RB_APPENDDATA	    ) )
112     ,m_aCB_UseHeaderLine(	this, ModuleRes( CB_USEHEADERLINE	) )
113 	,m_aCB_PrimaryColumn(	this, ModuleRes( CB_PRIMARY_COLUMN	) )
114 	,m_aFT_KeyName(			this, ModuleRes( FT_KEYNAME			) )
115 	,m_edKeyName(			this, ModuleRes( ET_KEYNAME			) )
116 	,m_pPage2(NULL)
117 	,m_pPage3(NULL)
118     ,m_bUseHeaderAllowed(sal_True)
119 {
120 	DBG_CTOR(OCopyTable,NULL);
121 
122 	m_edTableName.SetMaxTextLen( EDIT_NOLIMIT );
123 
124 	if ( m_pParent->m_xDestConnection.is() )
125 	{
126 		if ( !m_pParent->supportsViews() )
127 			m_aRB_View.Disable();
128 
129         m_aCB_UseHeaderLine.Check(sal_True);
130 		m_bPKeyAllowed = m_pParent->supportsPrimaryKey();
131 
132 		m_aCB_PrimaryColumn.Enable(m_bPKeyAllowed);
133 
134 		m_aRB_AppendData.SetClickHdl(	LINK( this, OCopyTable, AppendDataClickHdl	) );
135 
136 		m_aRB_DefData.SetClickHdl(		LINK( this, OCopyTable, RadioChangeHdl		) );
137 		m_aRB_Def.SetClickHdl(			LINK( this, OCopyTable, RadioChangeHdl		) );
138 		m_aRB_View.SetClickHdl(			LINK( this, OCopyTable, RadioChangeHdl		) );
139 
140 		m_aCB_PrimaryColumn.SetClickHdl(LINK( this, OCopyTable, KeyClickHdl         ) );
141 
142 		m_aFT_KeyName.Enable(sal_False);
143 		m_edKeyName.Enable(sal_False);
144         ::rtl::OUString sKeyName(RTL_CONSTASCII_USTRINGPARAM("ID"));
145 		sKeyName = m_pParent->createUniqueName(sKeyName);
146 		m_edKeyName.SetText(sKeyName);
147 
148 		sal_Int32 nMaxLen = m_pParent->getMaxColumnNameLength();
149 		m_edKeyName.SetMaxTextLen(nMaxLen ? (xub_StrLen)nMaxLen : EDIT_NOLIMIT);
150 	}
151 
152 	FreeResource();
153 
154 	SetText(String(ModuleRes(STR_COPYTABLE_TITLE_COPY)));
155 }
156 
157 //------------------------------------------------------------------------
158 OCopyTable::~OCopyTable()
159 {
160 	DBG_DTOR(OCopyTable,NULL);
161 }
162 //------------------------------------------------------------------------
163 IMPL_LINK( OCopyTable, AppendDataClickHdl, Button*, /*pButton*/ )
164 {
165 	DBG_CHKTHIS(OCopyTable,NULL);
166 
167 	SetAppendDataRadio();
168 	return 0;
169 }
170 //--------dyf ADD
171 void OCopyTable::SetAppendDataRadio()
172 {
173 	m_pParent->EnableButton(OCopyTableWizard::WIZARD_NEXT,sal_True);
174 	m_aFT_KeyName.Enable(sal_False);
175 	m_aCB_PrimaryColumn.Enable(sal_False);
176 	m_edKeyName.Enable(sal_False);
177 	m_pParent->setOperation(CopyTableOperation::AppendData);
178 }
179 
180 //--------add end
181 //------------------------------------------------------------------------
182 IMPL_LINK( OCopyTable, RadioChangeHdl, Button*, pButton )
183 {
184 	DBG_CHKTHIS(OCopyTable,NULL);
185 	m_pParent->EnableButton(OCopyTableWizard::WIZARD_NEXT,pButton != &m_aRB_View);
186 	sal_Bool bKey = m_bPKeyAllowed && pButton != &m_aRB_View;
187 	m_aFT_KeyName.Enable(bKey && m_aCB_PrimaryColumn.IsChecked());
188 	m_edKeyName.Enable(bKey && m_aCB_PrimaryColumn.IsChecked());
189 	m_aCB_PrimaryColumn.Enable(bKey);
190     m_aCB_UseHeaderLine.Enable(m_bUseHeaderAllowed && IsOptionDefData());
191 
192 	// set typ what to do
193 	if( IsOptionDefData() )
194 		m_pParent->setOperation( CopyTableOperation::CopyDefinitionAndData );
195 	else if( IsOptionDef() )
196 		m_pParent->setOperation( CopyTableOperation::CopyDefinitionOnly );
197 	else if( IsOptionView() )
198 		m_pParent->setOperation( CopyTableOperation::CreateAsView );
199 
200 	return 0;
201 }
202 //------------------------------------------------------------------------
203 IMPL_LINK( OCopyTable, KeyClickHdl, Button*, /*pButton*/ )
204 {
205 	DBG_CHKTHIS(OCopyTable,NULL);
206 	m_edKeyName.Enable(m_aCB_PrimaryColumn.IsChecked());
207 	m_aFT_KeyName.Enable(m_aCB_PrimaryColumn.IsChecked());
208 	return 0;
209 }
210 //------------------------------------------------------------------------
211 sal_Bool OCopyTable::LeavePage()
212 {
213 	DBG_CHKTHIS(OCopyTable,NULL);
214 	m_pParent->m_bCreatePrimaryKeyColumn	= (m_bPKeyAllowed && m_aCB_PrimaryColumn.IsEnabled()) ? m_aCB_PrimaryColumn.IsChecked() : sal_False;
215 	m_pParent->m_aKeyName				    = m_pParent->m_bCreatePrimaryKeyColumn ? m_edKeyName.GetText() : String();
216     m_pParent->setUseHeaderLine( m_aCB_UseHeaderLine.IsChecked() );
217 
218 	// first check if the table already exists in the database
219 	if(	m_pParent->getOperation() != CopyTableOperation::AppendData )
220 	{
221         m_pParent->clearDestColumns();
222         DynamicTableOrQueryNameCheck aNameCheck( m_pParent->m_xDestConnection, CommandType::TABLE );
223         SQLExceptionInfo aErrorInfo;
224         if ( !aNameCheck.isNameValid( m_edTableName.GetText(), aErrorInfo ) )
225         {
226             aErrorInfo.append( SQLExceptionInfo::SQL_CONTEXT, String( ModuleRes( STR_SUGGEST_APPEND_TABLE_DATA ) ) );
227             m_pParent->showError(aErrorInfo.get());
228 
229             return sal_False;
230         }
231 
232         // have to check the length of the table name
233 		Reference< XDatabaseMetaData > xMeta = m_pParent->m_xDestConnection->getMetaData();
234 		::rtl::OUString sCatalog;
235 		::rtl::OUString sSchema;
236 		::rtl::OUString sTable;
237 		::dbtools::qualifiedNameComponents(	xMeta,
238 											m_edTableName.GetText(),
239 											sCatalog,
240 											sSchema,
241 											sTable,
242 											::dbtools::eInDataManipulation);
243 		sal_Int32 nMaxLength = xMeta->getMaxTableNameLength();
244 		if ( nMaxLength && sTable.getLength() > nMaxLength )
245 		{
246             String sError(ModuleRes(STR_INVALID_TABLE_NAME_LENGTH));
247 			m_pParent->showError(sError);
248 			return sal_False;
249 		}
250 
251 		// now we have to check if the name of the primary key already exists
252 		if (	m_pParent->m_bCreatePrimaryKeyColumn
253 			&&	m_pParent->m_aKeyName != m_pParent->createUniqueName(m_pParent->m_aKeyName) )
254 		{
255 			String aInfoString( ModuleRes(STR_WIZ_PKEY_ALREADY_DEFINED) );
256 			aInfoString += String(' ');
257 			aInfoString += String(m_pParent->m_aKeyName);
258 			m_pParent->showError(aInfoString);
259 			return sal_False;
260 		}
261 	}
262 
263 	if ( !m_edTableName.GetSavedValue().Equals(m_edTableName.GetText()) )
264 	{ // table exists and name has changed
265 		if ( m_pParent->getOperation() == CopyTableOperation::AppendData )
266 		{
267 			if(!checkAppendData())
268 				return sal_False;
269 		}
270 		else if ( m_nOldOperation == CopyTableOperation::AppendData )
271 		{
272 			m_edTableName.SaveValue();
273 			return LeavePage();
274 		}
275 	}
276 	else
277 	{ // table exist and is not new or doesn't exist and so on
278 		if ( CopyTableOperation::AppendData == m_pParent->getOperation() )
279 		{
280 			if( !checkAppendData() )
281 				return sal_False;
282 		}
283 	}
284 	m_pParent->m_sName = m_edTableName.GetText();
285 	m_edTableName.SaveValue();
286 
287 	if(!m_pParent->m_sName.getLength())
288 	{
289         String sError(ModuleRes(STR_INVALID_TABLE_NAME));
290         m_pParent->showError(sError);
291 		return sal_False;
292 	}
293 
294 	return sal_True;
295 }
296 //------------------------------------------------------------------------
297 void OCopyTable::ActivatePage()
298 {
299 	DBG_CHKTHIS(OCopyTable,NULL);
300 	m_pParent->GetOKButton().Enable( sal_True );
301 	m_nOldOperation = m_pParent->getOperation();
302 	m_edTableName.GrabFocus();
303     m_aCB_UseHeaderLine.Check(m_pParent->UseHeaderLine());
304 }
305 //------------------------------------------------------------------------
306 String OCopyTable::GetTitle() const
307 {
308 	DBG_CHKTHIS(OCopyTable,NULL);
309 	return String(ModuleRes(STR_WIZ_TABLE_COPY));
310 }
311 //------------------------------------------------------------------------
312 void OCopyTable::Reset()
313 {
314 	DBG_CHKTHIS(OCopyTable,NULL);
315 	m_bFirstTime = sal_False;
316 
317 	m_edTableName.SetText( m_pParent->m_sName );
318 	m_edTableName.SaveValue();
319 }
320 //------------------------------------------------------------------------
321 sal_Bool OCopyTable::checkAppendData()
322 {
323 	DBG_CHKTHIS(OCopyTable,NULL);
324 	m_pParent->clearDestColumns();
325     Reference< XPropertySet > xTable;
326 	Reference< XTablesSupplier > xSup( m_pParent->m_xDestConnection, UNO_QUERY );
327 	Reference<XNameAccess> xTables;
328 	if(xSup.is())
329 		xTables = xSup->getTables();
330 	if(xTables.is() && xTables->hasByName(m_edTableName.GetText()))
331 	{
332         const ODatabaseExport::TColumnVector* pSrcColumns = m_pParent->getSrcVector();
333         const sal_uInt32 nSrcSize = pSrcColumns->size();
334         m_pParent->m_vColumnPos.resize( nSrcSize, ODatabaseExport::TPositions::value_type( COLUMN_POSITION_NOT_FOUND, COLUMN_POSITION_NOT_FOUND ) );
335 	    m_pParent->m_vColumnTypes.resize( nSrcSize , COLUMN_POSITION_NOT_FOUND );
336 
337         // set new destination
338 		xTables->getByName( m_edTableName.GetText() ) >>= xTable;
339         ObjectCopySource aTableCopySource( m_pParent->m_xDestConnection, xTable );
340 		m_pParent->loadData( aTableCopySource, m_pParent->m_vDestColumns, m_pParent->m_aDestVec );
341 		// #90027#
342 		const ODatabaseExport::TColumnVector* pDestColumns			= m_pParent->getDestVector();
343 		ODatabaseExport::TColumnVector::const_iterator aDestIter	= pDestColumns->begin();
344         ODatabaseExport::TColumnVector::const_iterator aDestEnd	    = pDestColumns->end();
345 		const sal_uInt32 nDestSize = pDestColumns->size();
346 		sal_Bool bNotConvert;
347         sal_uInt32 i = 0;
348 		for(sal_Int32 nPos = 1;aDestIter != aDestEnd && i < nDestSize && i < nSrcSize;++aDestIter,++nPos,++i)
349 		{
350 			bNotConvert = sal_True;
351 			m_pParent->m_vColumnPos[i] = ODatabaseExport::TPositions::value_type(nPos,nPos);
352 			TOTypeInfoSP pTypeInfo = m_pParent->convertType((*aDestIter)->second->getSpecialTypeInfo(),bNotConvert);
353 			if ( !bNotConvert )
354 			{
355 				m_pParent->showColumnTypeNotSupported((*aDestIter)->first);
356 				return sal_False;
357 			}
358 
359 			if ( pTypeInfo.get() )
360 				m_pParent->m_vColumnTypes[i] = pTypeInfo->nType;
361 			else
362 				m_pParent->m_vColumnTypes[i] = DataType::VARCHAR;
363 		}
364 
365 	}
366 
367 	if ( !xTable.is() )
368 	{
369         String sError(ModuleRes(STR_INVALID_TABLE_NAME));
370         m_pParent->showError(sError);
371 		return sal_False;
372 	}
373 	return sal_True;
374 }
375 // -----------------------------------------------------------------------------
376 void OCopyTable::setCreatePrimaryKey( bool _bDoCreate, const ::rtl::OUString& _rSuggestedName )
377 {
378     bool bCreatePK = m_bPKeyAllowed && _bDoCreate;
379     m_aCB_PrimaryColumn.Check( bCreatePK );
380     m_edKeyName.SetText( _rSuggestedName );
381 
382     m_aFT_KeyName.Enable( bCreatePK );
383 	m_edKeyName.Enable( bCreatePK );
384 }
385 
386 // -----------------------------------------------------------------------------
387 //---dyf add 2006/7/10
388 void OCopyTable::setCreateStyleAction()
389 {
390 	// reselect the last action before
391 	switch(m_pParent->getOperation())
392 	{
393 		case CopyTableOperation::CopyDefinitionAndData:
394 			m_aRB_DefData.Check(sal_True);
395             RadioChangeHdl(&m_aRB_DefData);
396 			break;
397 		case CopyTableOperation::CopyDefinitionOnly:
398 			m_aRB_Def.Check(sal_True);
399             RadioChangeHdl(&m_aRB_Def);
400 			break;
401 		case CopyTableOperation::AppendData:
402 			m_aRB_AppendData.Check(sal_True);
403 			SetAppendDataRadio();
404 			break;
405 		case CopyTableOperation::CreateAsView:
406 			if ( m_aRB_View.IsEnabled() )
407 			{
408 				m_aRB_View.Check(sal_True);
409                 RadioChangeHdl(&m_aRB_View);
410 			}
411 			else
412             {
413 				m_aRB_DefData.Check(sal_True);
414                 RadioChangeHdl(&m_aRB_DefData);
415             }
416 	}
417 }
418 //---add end
419