Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / dbaccess / source / ui / misc / WCPage.cxx
bloba939c95df7447cb0a7ed000ca3ed6108490f00e8
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "WCPage.hxx"
21 #include "WCopyTable.hxx"
22 #include "WColumnSelect.hxx"
23 #include "WExtendPages.hxx"
25 #include "defaultobjectnamecheck.hxx"
26 #include <tools/debug.hxx>
27 #include "dbaccess_helpid.hrc"
28 #include "dbu_misc.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>
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;
52 namespace CopyTableOperation = css::sdb::application::CopyTableOperation;
54 // Klasse OCopyTable
55 OCopyTable::OCopyTable(vcl::Window * pParent)
56 : OWizardPage(pParent, "CopyTablePage", "dbaccess/ui/copytablepage.ui")
57 , m_nOldOperation(0)
58 , m_pPage2(nullptr)
59 , m_pPage3(nullptr)
60 , m_bPKeyAllowed(false)
61 , m_bUseHeaderAllowed(true)
63 get(m_pEdTableName, "name");
64 get(m_pRB_DefData, "defdata");
65 get(m_pRB_Def, "def");
66 get(m_pRB_View, "view");
67 get(m_pRB_AppendData, "data");
68 get(m_pCB_UseHeaderLine, "firstline");
69 get(m_pCB_PrimaryColumn, "primarykey");
70 get(m_pFT_KeyName, "keynamelabel");
71 get(m_pEdKeyName, "keyname");
73 m_pEdTableName->SetMaxTextLen();
75 if ( m_pParent->m_xDestConnection.is() )
77 if ( !m_pParent->supportsViews() )
78 m_pRB_View->Disable();
80 m_pCB_UseHeaderLine->Check();
81 m_bPKeyAllowed = m_pParent->supportsPrimaryKey();
83 m_pCB_PrimaryColumn->Enable(m_bPKeyAllowed);
85 m_pRB_AppendData->SetClickHdl( LINK( this, OCopyTable, AppendDataClickHdl ) );
87 m_pRB_DefData->SetClickHdl( LINK( this, OCopyTable, RadioChangeHdl ) );
88 m_pRB_Def->SetClickHdl( LINK( this, OCopyTable, RadioChangeHdl ) );
89 m_pRB_View->SetClickHdl( LINK( this, OCopyTable, RadioChangeHdl ) );
91 m_pCB_PrimaryColumn->SetClickHdl(LINK( this, OCopyTable, KeyClickHdl ) );
93 m_pFT_KeyName->Enable(false);
94 m_pEdKeyName->Enable(false);
95 m_pEdKeyName->SetText(m_pParent->createUniqueName("ID"));
97 const sal_Int32 nMaxLen = m_pParent->getMaxColumnNameLength();
98 m_pEdKeyName->SetMaxTextLen(nMaxLen ? nMaxLen : EDIT_NOLIMIT);
101 SetText(ModuleRes(STR_COPYTABLE_TITLE_COPY));
104 OCopyTable::~OCopyTable()
106 disposeOnce();
109 void OCopyTable::dispose()
111 m_pEdTableName.clear();
112 m_pRB_DefData.clear();
113 m_pRB_Def.clear();
114 m_pRB_View.clear();
115 m_pRB_AppendData.clear();
116 m_pCB_UseHeaderLine.clear();
117 m_pCB_PrimaryColumn.clear();
118 m_pFT_KeyName.clear();
119 m_pEdKeyName.clear();
120 m_pPage2.clear();
121 m_pPage3.clear();
122 OWizardPage::dispose();
125 IMPL_LINK_NOARG_TYPED( OCopyTable, AppendDataClickHdl, Button*, void )
127 SetAppendDataRadio();
130 void OCopyTable::SetAppendDataRadio()
132 m_pParent->EnableNextButton(true);
133 m_pFT_KeyName->Enable(false);
134 m_pCB_PrimaryColumn->Enable(false);
135 m_pEdKeyName->Enable(false);
136 m_pParent->setOperation(CopyTableOperation::AppendData);
139 IMPL_LINK_TYPED( OCopyTable, RadioChangeHdl, Button*, pButton, void )
141 m_pParent->EnableNextButton(pButton != m_pRB_View);
142 bool bKey = m_bPKeyAllowed && pButton != m_pRB_View;
143 m_pFT_KeyName->Enable(bKey && m_pCB_PrimaryColumn->IsChecked());
144 m_pEdKeyName->Enable(bKey && m_pCB_PrimaryColumn->IsChecked());
145 m_pCB_PrimaryColumn->Enable(bKey);
146 m_pCB_UseHeaderLine->Enable(m_bUseHeaderAllowed && IsOptionDefData());
148 // set typ what to do
149 if( IsOptionDefData() )
150 m_pParent->setOperation( CopyTableOperation::CopyDefinitionAndData );
151 else if( IsOptionDef() )
152 m_pParent->setOperation( CopyTableOperation::CopyDefinitionOnly );
153 else if( IsOptionView() )
154 m_pParent->setOperation( CopyTableOperation::CreateAsView );
157 IMPL_LINK_NOARG_TYPED( OCopyTable, KeyClickHdl, Button*, void )
159 m_pEdKeyName->Enable(m_pCB_PrimaryColumn->IsChecked());
160 m_pFT_KeyName->Enable(m_pCB_PrimaryColumn->IsChecked());
163 bool OCopyTable::LeavePage()
165 m_pParent->m_bCreatePrimaryKeyColumn = m_bPKeyAllowed && m_pCB_PrimaryColumn->IsEnabled() && m_pCB_PrimaryColumn->IsChecked();
166 m_pParent->m_aKeyName = m_pParent->m_bCreatePrimaryKeyColumn ? m_pEdKeyName->GetText() : OUString();
167 m_pParent->setUseHeaderLine( m_pCB_UseHeaderLine->IsChecked() );
169 // first check if the table already exists in the database
170 if( m_pParent->getOperation() != CopyTableOperation::AppendData )
172 m_pParent->clearDestColumns();
173 DynamicTableOrQueryNameCheck aNameCheck( m_pParent->m_xDestConnection, CommandType::TABLE );
174 SQLExceptionInfo aErrorInfo;
175 if ( !aNameCheck.isNameValid( m_pEdTableName->GetText(), aErrorInfo ) )
177 aErrorInfo.append( SQLExceptionInfo::TYPE::SQLContext, ModuleRes( STR_SUGGEST_APPEND_TABLE_DATA ) );
178 m_pParent->showError(aErrorInfo.get());
180 return false;
183 // have to check the length of the table name
184 Reference< XDatabaseMetaData > xMeta = m_pParent->m_xDestConnection->getMetaData();
185 OUString sCatalog;
186 OUString sSchema;
187 OUString sTable;
188 ::dbtools::qualifiedNameComponents( xMeta,
189 m_pEdTableName->GetText(),
190 sCatalog,
191 sSchema,
192 sTable,
193 ::dbtools::EComposeRule::InDataManipulation);
194 sal_Int32 nMaxLength = xMeta->getMaxTableNameLength();
195 if ( nMaxLength && sTable.getLength() > nMaxLength )
197 m_pParent->showError(ModuleRes(STR_INVALID_TABLE_NAME_LENGTH));
198 return false;
201 // now we have to check if the name of the primary key already exists
202 if ( m_pParent->m_bCreatePrimaryKeyColumn
203 && m_pParent->m_aKeyName != m_pParent->createUniqueName(m_pParent->m_aKeyName) )
205 m_pParent->showError(ModuleRes(STR_WIZ_NAME_ALREADY_DEFINED).toString()+" "+m_pParent->m_aKeyName);
206 return false;
210 if ( m_pEdTableName->IsValueChangedFromSaved() )
211 { // table exists and name has changed
212 if ( m_pParent->getOperation() == CopyTableOperation::AppendData )
214 if(!checkAppendData())
215 return false;
217 else if ( m_nOldOperation == CopyTableOperation::AppendData )
219 m_pEdTableName->SaveValue();
220 return LeavePage();
223 else
224 { // table exist and is not new or doesn't exist and so on
225 if ( CopyTableOperation::AppendData == m_pParent->getOperation() )
227 if( !checkAppendData() )
228 return false;
231 m_pParent->m_sName = m_pEdTableName->GetText();
232 m_pEdTableName->SaveValue();
234 if(m_pParent->m_sName.isEmpty())
236 m_pParent->showError(ModuleRes(STR_INVALID_TABLE_NAME));
237 return false;
240 return true;
243 void OCopyTable::ActivatePage()
245 m_pParent->GetOKButton().Enable();
246 m_nOldOperation = m_pParent->getOperation();
247 m_pEdTableName->GrabFocus();
248 m_pCB_UseHeaderLine->Check(m_pParent->UseHeaderLine());
251 OUString OCopyTable::GetTitle() const
253 return ModuleRes(STR_WIZ_TABLE_COPY);
256 void OCopyTable::Reset()
258 m_bFirstTime = false;
260 m_pEdTableName->SetText( m_pParent->m_sName );
261 m_pEdTableName->SaveValue();
264 bool OCopyTable::checkAppendData()
266 m_pParent->clearDestColumns();
267 Reference< XPropertySet > xTable;
268 Reference< XTablesSupplier > xSup( m_pParent->m_xDestConnection, UNO_QUERY );
269 Reference<XNameAccess> xTables;
270 if(xSup.is())
271 xTables = xSup->getTables();
272 if(xTables.is() && xTables->hasByName(m_pEdTableName->GetText()))
274 const ODatabaseExport::TColumnVector& rSrcColumns = m_pParent->getSrcVector();
275 const sal_uInt32 nSrcSize = rSrcColumns.size();
276 m_pParent->m_vColumnPos.resize( nSrcSize, ODatabaseExport::TPositions::value_type( COLUMN_POSITION_NOT_FOUND, COLUMN_POSITION_NOT_FOUND ) );
277 m_pParent->m_vColumnTypes.resize( nSrcSize , COLUMN_POSITION_NOT_FOUND );
279 // set new destination
280 xTables->getByName( m_pEdTableName->GetText() ) >>= xTable;
281 ObjectCopySource aTableCopySource( m_pParent->m_xDestConnection, xTable );
282 m_pParent->loadData( aTableCopySource, m_pParent->m_vDestColumns, m_pParent->m_aDestVec );
283 const ODatabaseExport::TColumnVector& rDestColumns = m_pParent->getDestVector();
284 ODatabaseExport::TColumnVector::const_iterator aDestIter = rDestColumns.begin();
285 ODatabaseExport::TColumnVector::const_iterator aDestEnd = rDestColumns.end();
286 const sal_uInt32 nDestSize = rDestColumns.size();
287 sal_uInt32 i = 0;
288 for(sal_Int32 nPos = 1;aDestIter != aDestEnd && i < nDestSize && i < nSrcSize;++aDestIter,++nPos,++i)
290 bool bNotConvert = true;
291 m_pParent->m_vColumnPos[i] = ODatabaseExport::TPositions::value_type(nPos,nPos);
292 TOTypeInfoSP pTypeInfo = m_pParent->convertType((*aDestIter)->second->getSpecialTypeInfo(),bNotConvert);
293 if ( !bNotConvert )
295 m_pParent->showColumnTypeNotSupported((*aDestIter)->first);
296 return false;
299 if ( pTypeInfo.get() )
300 m_pParent->m_vColumnTypes[i] = pTypeInfo->nType;
301 else
302 m_pParent->m_vColumnTypes[i] = DataType::VARCHAR;
307 if ( !xTable.is() )
309 m_pParent->showError(ModuleRes(STR_INVALID_TABLE_NAME));
310 return false;
312 return true;
315 void OCopyTable::setCreatePrimaryKey( bool _bDoCreate, const OUString& _rSuggestedName )
317 bool bCreatePK = m_bPKeyAllowed && _bDoCreate;
318 m_pCB_PrimaryColumn->Check( bCreatePK );
319 m_pEdKeyName->SetText( _rSuggestedName );
321 m_pFT_KeyName->Enable( bCreatePK );
322 m_pEdKeyName->Enable( bCreatePK );
325 void OCopyTable::setCreateStyleAction()
327 // reselect the last action before
328 switch(m_pParent->getOperation())
330 case CopyTableOperation::CopyDefinitionAndData:
331 m_pRB_DefData->Check();
332 RadioChangeHdl(m_pRB_DefData);
333 break;
334 case CopyTableOperation::CopyDefinitionOnly:
335 m_pRB_Def->Check();
336 RadioChangeHdl(m_pRB_Def);
337 break;
338 case CopyTableOperation::AppendData:
339 m_pRB_AppendData->Check();
340 SetAppendDataRadio();
341 break;
342 case CopyTableOperation::CreateAsView:
343 if ( m_pRB_View->IsEnabled() )
345 m_pRB_View->Check();
346 RadioChangeHdl(m_pRB_View);
348 else
350 m_pRB_DefData->Check();
351 RadioChangeHdl(m_pRB_DefData);
356 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */