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