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

Generated by: LCOV version 1.10