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