LCOV - code coverage report
Current view: top level - dbaccess/source/ui/misc - WCPage.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 1 198 0.5 %
Date: 2015-06-13 12:38:46 Functions: 2 20 10.0 %
Legend: Lines: hit not hit

          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             : #include "WCPage.hxx"
      21             : #include "WCopyTable.hxx"
      22             : #include "WColumnSelect.hxx"
      23             : #include "WExtendPages.hxx"
      24             : 
      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>
      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             : // Klasse OCopyTable
      55           0 : OCopyTable::OCopyTable(vcl::Window * pParent)
      56             :     : OWizardPage(pParent, "CopyTablePage", "dbaccess/ui/copytablepage.ui")
      57             :     , m_nOldOperation(0)
      58             :     , m_pPage2(NULL)
      59             :     , m_pPage3(NULL)
      60             :     , m_bPKeyAllowed(false)
      61           0 :     , m_bUseHeaderAllowed(true)
      62             : {
      63           0 :     get(m_pEdTableName, "name");
      64           0 :     get(m_pRB_DefData, "defdata");
      65           0 :     get(m_pRB_Def, "def");
      66           0 :     get(m_pRB_View, "view");
      67           0 :     get(m_pRB_AppendData, "data");
      68           0 :     get(m_pCB_UseHeaderLine, "firstline");
      69           0 :     get(m_pCB_PrimaryColumn, "primarykey");
      70           0 :     get(m_pFT_KeyName, "keynamelabel");
      71           0 :     get(m_pEdKeyName, "keyname");
      72             : 
      73           0 :     m_pEdTableName->SetMaxTextLen(EDIT_NOLIMIT);
      74             : 
      75           0 :     if ( m_pParent->m_xDestConnection.is() )
      76             :     {
      77           0 :         if ( !m_pParent->supportsViews() )
      78           0 :             m_pRB_View->Disable();
      79             : 
      80           0 :         m_pCB_UseHeaderLine->Check(true);
      81           0 :         m_bPKeyAllowed = m_pParent->supportsPrimaryKey();
      82             : 
      83           0 :         m_pCB_PrimaryColumn->Enable(m_bPKeyAllowed);
      84             : 
      85           0 :         m_pRB_AppendData->SetClickHdl(   LINK( this, OCopyTable, AppendDataClickHdl  ) );
      86             : 
      87           0 :         m_pRB_DefData->SetClickHdl(      LINK( this, OCopyTable, RadioChangeHdl      ) );
      88           0 :         m_pRB_Def->SetClickHdl(          LINK( this, OCopyTable, RadioChangeHdl      ) );
      89           0 :         m_pRB_View->SetClickHdl(         LINK( this, OCopyTable, RadioChangeHdl      ) );
      90             : 
      91           0 :         m_pCB_PrimaryColumn->SetClickHdl(LINK( this, OCopyTable, KeyClickHdl         ) );
      92             : 
      93           0 :         m_pFT_KeyName->Enable(false);
      94           0 :         m_pEdKeyName->Enable(false);
      95           0 :         OUString sKeyName("ID");
      96           0 :         sKeyName = m_pParent->createUniqueName(sKeyName);
      97           0 :         m_pEdKeyName->SetText(sKeyName);
      98             : 
      99           0 :         sal_Int32 nMaxLen = m_pParent->getMaxColumnNameLength();
     100           0 :         m_pEdKeyName->SetMaxTextLen(nMaxLen ? nMaxLen : EDIT_NOLIMIT);
     101             :     }
     102             : 
     103           0 :     SetText(ModuleRes(STR_COPYTABLE_TITLE_COPY));
     104           0 : }
     105             : 
     106           0 : OCopyTable::~OCopyTable()
     107             : {
     108           0 :     disposeOnce();
     109           0 : }
     110             : 
     111           0 : void OCopyTable::dispose()
     112             : {
     113           0 :     m_pEdTableName.clear();
     114           0 :     m_pRB_DefData.clear();
     115           0 :     m_pRB_Def.clear();
     116           0 :     m_pRB_View.clear();
     117           0 :     m_pRB_AppendData.clear();
     118           0 :     m_pCB_UseHeaderLine.clear();
     119           0 :     m_pCB_PrimaryColumn.clear();
     120           0 :     m_pFT_KeyName.clear();
     121           0 :     m_pEdKeyName.clear();
     122           0 :     m_pPage2.clear();
     123           0 :     m_pPage3.clear();
     124           0 :     OWizardPage::dispose();
     125           0 : }
     126             : 
     127           0 : IMPL_LINK( OCopyTable, AppendDataClickHdl, Button*, /*pButton*/ )
     128             : {
     129             : 
     130           0 :     SetAppendDataRadio();
     131           0 :     return 0;
     132             : }
     133             : 
     134           0 : void OCopyTable::SetAppendDataRadio()
     135             : {
     136           0 :     m_pParent->EnableButton(OCopyTableWizard::WIZARD_NEXT,true);
     137           0 :     m_pFT_KeyName->Enable(false);
     138           0 :     m_pCB_PrimaryColumn->Enable(false);
     139           0 :     m_pEdKeyName->Enable(false);
     140           0 :     m_pParent->setOperation(CopyTableOperation::AppendData);
     141           0 : }
     142             : 
     143           0 : IMPL_LINK( OCopyTable, RadioChangeHdl, Button*, pButton )
     144             : {
     145           0 :     m_pParent->EnableButton(OCopyTableWizard::WIZARD_NEXT,pButton != m_pRB_View);
     146           0 :     bool bKey = m_bPKeyAllowed && pButton != m_pRB_View;
     147           0 :     m_pFT_KeyName->Enable(bKey && m_pCB_PrimaryColumn->IsChecked());
     148           0 :     m_pEdKeyName->Enable(bKey && m_pCB_PrimaryColumn->IsChecked());
     149           0 :     m_pCB_PrimaryColumn->Enable(bKey);
     150           0 :     m_pCB_UseHeaderLine->Enable(m_bUseHeaderAllowed && IsOptionDefData());
     151             : 
     152             :     // set typ what to do
     153           0 :     if( IsOptionDefData() )
     154           0 :         m_pParent->setOperation( CopyTableOperation::CopyDefinitionAndData );
     155           0 :     else if( IsOptionDef() )
     156           0 :         m_pParent->setOperation( CopyTableOperation::CopyDefinitionOnly );
     157           0 :     else if( IsOptionView() )
     158           0 :         m_pParent->setOperation( CopyTableOperation::CreateAsView );
     159             : 
     160           0 :     return 0;
     161             : }
     162             : 
     163           0 : IMPL_LINK( OCopyTable, KeyClickHdl, Button*, /*pButton*/ )
     164             : {
     165           0 :     m_pEdKeyName->Enable(m_pCB_PrimaryColumn->IsChecked());
     166           0 :     m_pFT_KeyName->Enable(m_pCB_PrimaryColumn->IsChecked());
     167           0 :     return 0;
     168             : }
     169             : 
     170           0 : bool OCopyTable::LeavePage()
     171             : {
     172           0 :     m_pParent->m_bCreatePrimaryKeyColumn    = m_bPKeyAllowed && m_pCB_PrimaryColumn->IsEnabled() && m_pCB_PrimaryColumn->IsChecked();
     173           0 :     m_pParent->m_aKeyName                   = m_pParent->m_bCreatePrimaryKeyColumn ? m_pEdKeyName->GetText() : OUString();
     174           0 :     m_pParent->setUseHeaderLine( m_pCB_UseHeaderLine->IsChecked() );
     175             : 
     176             :     // first check if the table already exists in the database
     177           0 :     if( m_pParent->getOperation() != CopyTableOperation::AppendData )
     178             :     {
     179           0 :         m_pParent->clearDestColumns();
     180           0 :         DynamicTableOrQueryNameCheck aNameCheck( m_pParent->m_xDestConnection, CommandType::TABLE );
     181           0 :         SQLExceptionInfo aErrorInfo;
     182           0 :         if ( !aNameCheck.isNameValid( m_pEdTableName->GetText(), aErrorInfo ) )
     183             :         {
     184           0 :             aErrorInfo.append( SQLExceptionInfo::SQL_CONTEXT, ModuleRes( STR_SUGGEST_APPEND_TABLE_DATA ) );
     185           0 :             m_pParent->showError(aErrorInfo.get());
     186             : 
     187           0 :             return false;
     188             :         }
     189             : 
     190             :         // have to check the length of the table name
     191           0 :         Reference< XDatabaseMetaData > xMeta = m_pParent->m_xDestConnection->getMetaData();
     192           0 :         OUString sCatalog;
     193           0 :         OUString sSchema;
     194           0 :         OUString sTable;
     195             :         ::dbtools::qualifiedNameComponents( xMeta,
     196           0 :                                             m_pEdTableName->GetText(),
     197             :                                             sCatalog,
     198             :                                             sSchema,
     199             :                                             sTable,
     200           0 :                                             ::dbtools::eInDataManipulation);
     201           0 :         sal_Int32 nMaxLength = xMeta->getMaxTableNameLength();
     202           0 :         if ( nMaxLength && sTable.getLength() > nMaxLength )
     203             :         {
     204           0 :             OUString sError(ModuleRes(STR_INVALID_TABLE_NAME_LENGTH));
     205           0 :             m_pParent->showError(sError);
     206           0 :             return false;
     207             :         }
     208             : 
     209             :         // now we have to check if the name of the primary key already exists
     210           0 :         if (    m_pParent->m_bCreatePrimaryKeyColumn
     211           0 :             &&  m_pParent->m_aKeyName != m_pParent->createUniqueName(m_pParent->m_aKeyName) )
     212             :         {
     213           0 :             OUString aInfoString( ModuleRes(STR_WIZ_PKEY_ALREADY_DEFINED) );
     214           0 :             aInfoString += " ";
     215           0 :             aInfoString += m_pParent->m_aKeyName;
     216           0 :             m_pParent->showError(aInfoString);
     217           0 :             return false;
     218           0 :         }
     219             :     }
     220             : 
     221           0 :     if ( m_pEdTableName->IsValueChangedFromSaved() )
     222             :     { // table exists and name has changed
     223           0 :         if ( m_pParent->getOperation() == CopyTableOperation::AppendData )
     224             :         {
     225           0 :             if(!checkAppendData())
     226           0 :                 return false;
     227             :         }
     228           0 :         else if ( m_nOldOperation == CopyTableOperation::AppendData )
     229             :         {
     230           0 :             m_pEdTableName->SaveValue();
     231           0 :             return LeavePage();
     232             :         }
     233             :     }
     234             :     else
     235             :     { // table exist and is not new or doesn't exist and so on
     236           0 :         if ( CopyTableOperation::AppendData == m_pParent->getOperation() )
     237             :         {
     238           0 :             if( !checkAppendData() )
     239           0 :                 return false;
     240             :         }
     241             :     }
     242           0 :     m_pParent->m_sName = m_pEdTableName->GetText();
     243           0 :     m_pEdTableName->SaveValue();
     244             : 
     245           0 :     if(m_pParent->m_sName.isEmpty())
     246             :     {
     247           0 :         OUString sError(ModuleRes(STR_INVALID_TABLE_NAME));
     248           0 :         m_pParent->showError(sError);
     249           0 :         return false;
     250             :     }
     251             : 
     252           0 :     return true;
     253             : }
     254             : 
     255           0 : void OCopyTable::ActivatePage()
     256             : {
     257           0 :     m_pParent->GetOKButton().Enable( true );
     258           0 :     m_nOldOperation = m_pParent->getOperation();
     259           0 :     m_pEdTableName->GrabFocus();
     260           0 :     m_pCB_UseHeaderLine->Check(m_pParent->UseHeaderLine());
     261           0 : }
     262             : 
     263           0 : OUString OCopyTable::GetTitle() const
     264             : {
     265           0 :     return ModuleRes(STR_WIZ_TABLE_COPY);
     266             : }
     267             : 
     268           0 : void OCopyTable::Reset()
     269             : {
     270           0 :     m_bFirstTime = false;
     271             : 
     272           0 :     m_pEdTableName->SetText( m_pParent->m_sName );
     273           0 :     m_pEdTableName->SaveValue();
     274           0 : }
     275             : 
     276           0 : bool OCopyTable::checkAppendData()
     277             : {
     278           0 :     m_pParent->clearDestColumns();
     279           0 :     Reference< XPropertySet > xTable;
     280           0 :     Reference< XTablesSupplier > xSup( m_pParent->m_xDestConnection, UNO_QUERY );
     281           0 :     Reference<XNameAccess> xTables;
     282           0 :     if(xSup.is())
     283           0 :         xTables = xSup->getTables();
     284           0 :     if(xTables.is() && xTables->hasByName(m_pEdTableName->GetText()))
     285             :     {
     286           0 :         const ODatabaseExport::TColumnVector& rSrcColumns = m_pParent->getSrcVector();
     287           0 :         const sal_uInt32 nSrcSize = rSrcColumns.size();
     288           0 :         m_pParent->m_vColumnPos.resize( nSrcSize, ODatabaseExport::TPositions::value_type( COLUMN_POSITION_NOT_FOUND, COLUMN_POSITION_NOT_FOUND ) );
     289           0 :         m_pParent->m_vColumnTypes.resize( nSrcSize , COLUMN_POSITION_NOT_FOUND );
     290             : 
     291             :         // set new destination
     292           0 :         xTables->getByName( m_pEdTableName->GetText() ) >>= xTable;
     293           0 :         ObjectCopySource aTableCopySource( m_pParent->m_xDestConnection, xTable );
     294           0 :         m_pParent->loadData( aTableCopySource, m_pParent->m_vDestColumns, m_pParent->m_aDestVec );
     295           0 :         const ODatabaseExport::TColumnVector& rDestColumns          = m_pParent->getDestVector();
     296           0 :         ODatabaseExport::TColumnVector::const_iterator aDestIter    = rDestColumns.begin();
     297           0 :         ODatabaseExport::TColumnVector::const_iterator aDestEnd     = rDestColumns.end();
     298           0 :         const sal_uInt32 nDestSize = rDestColumns.size();
     299           0 :         sal_uInt32 i = 0;
     300           0 :         for(sal_Int32 nPos = 1;aDestIter != aDestEnd && i < nDestSize && i < nSrcSize;++aDestIter,++nPos,++i)
     301             :         {
     302           0 :             bool bNotConvert = true;
     303           0 :             m_pParent->m_vColumnPos[i] = ODatabaseExport::TPositions::value_type(nPos,nPos);
     304           0 :             TOTypeInfoSP pTypeInfo = m_pParent->convertType((*aDestIter)->second->getSpecialTypeInfo(),bNotConvert);
     305           0 :             if ( !bNotConvert )
     306             :             {
     307           0 :                 m_pParent->showColumnTypeNotSupported((*aDestIter)->first);
     308           0 :                 return false;
     309             :             }
     310             : 
     311           0 :             if ( pTypeInfo.get() )
     312           0 :                 m_pParent->m_vColumnTypes[i] = pTypeInfo->nType;
     313             :             else
     314           0 :                 m_pParent->m_vColumnTypes[i] = DataType::VARCHAR;
     315           0 :         }
     316             : 
     317             :     }
     318             : 
     319           0 :     if ( !xTable.is() )
     320             :     {
     321           0 :         OUString sError(ModuleRes(STR_INVALID_TABLE_NAME));
     322           0 :         m_pParent->showError(sError);
     323           0 :         return false;
     324             :     }
     325           0 :     return true;
     326             : }
     327             : 
     328           0 : void OCopyTable::setCreatePrimaryKey( bool _bDoCreate, const OUString& _rSuggestedName )
     329             : {
     330           0 :     bool bCreatePK = m_bPKeyAllowed && _bDoCreate;
     331           0 :     m_pCB_PrimaryColumn->Check( bCreatePK );
     332           0 :     m_pEdKeyName->SetText( _rSuggestedName );
     333             : 
     334           0 :     m_pFT_KeyName->Enable( bCreatePK );
     335           0 :     m_pEdKeyName->Enable( bCreatePK );
     336           0 : }
     337             : 
     338           0 : void OCopyTable::setCreateStyleAction()
     339             : {
     340             :     // reselect the last action before
     341           0 :     switch(m_pParent->getOperation())
     342             :     {
     343             :         case CopyTableOperation::CopyDefinitionAndData:
     344           0 :             m_pRB_DefData->Check(true);
     345           0 :             RadioChangeHdl(m_pRB_DefData);
     346           0 :             break;
     347             :         case CopyTableOperation::CopyDefinitionOnly:
     348           0 :             m_pRB_Def->Check(true);
     349           0 :             RadioChangeHdl(m_pRB_Def);
     350           0 :             break;
     351             :         case CopyTableOperation::AppendData:
     352           0 :             m_pRB_AppendData->Check(true);
     353           0 :             SetAppendDataRadio();
     354           0 :             break;
     355             :         case CopyTableOperation::CreateAsView:
     356           0 :             if ( m_pRB_View->IsEnabled() )
     357             :             {
     358           0 :                 m_pRB_View->Check(true);
     359           0 :                 RadioChangeHdl(m_pRB_View);
     360             :             }
     361             :             else
     362             :             {
     363           0 :                 m_pRB_DefData->Check(true);
     364           0 :                 RadioChangeHdl(m_pRB_DefData);
     365             :             }
     366             :     }
     367          36 : }
     368             : 
     369             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11