LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/sw/source/ui/shells - textsh2.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 1 129 0.8 %
Date: 2013-07-09 Functions: 2 6 33.3 %
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 <cstdarg>
      21             : 
      22             : #include <svtools/svmedit.hxx>
      23             : #include <svl/eitem.hxx>
      24             : #include <svl/whiter.hxx>
      25             : #include <sfx2/event.hxx>
      26             : #include <sfx2/dispatch.hxx>
      27             : #include <sfx2/viewfrm.hxx>
      28             : #include <vcl/msgbox.hxx>
      29             : #include <svl/stritem.hxx>
      30             : #include <svl/itemset.hxx>
      31             : #include <sfx2/request.hxx>
      32             : #include <com/sun/star/sdb/CommandType.hpp>
      33             : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
      34             : #include <com/sun/star/container/XNameAccess.hpp>
      35             : #include <com/sun/star/sdbc/XDataSource.hpp>
      36             : #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
      37             : #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
      38             : #include <com/sun/star/sdb/XQueriesSupplier.hpp>
      39             : #include <com/sun/star/sdb/XDatabaseAccess.hpp>
      40             : #include <com/sun/star/beans/XPropertySet.hpp>
      41             : #include <com/sun/star/container/XChild.hpp>
      42             : #include <comphelper/processfactory.hxx>
      43             : #include <com/sun/star/sdbc/XRowSet.hpp>
      44             : #include <sfx2/frame.hxx>
      45             : #include <fldmgr.hxx>
      46             : #include <fldbas.hxx>
      47             : #include "dbmgr.hxx"
      48             : #include <comphelper/uno3.hxx>
      49             : #include <svx/dataaccessdescriptor.hxx>
      50             : #include <memory>
      51             : 
      52             : #include <vcl/svapp.hxx>
      53             : 
      54             : #include "view.hxx"
      55             : #include "wrtsh.hxx"
      56             : #include "swtypes.hxx"
      57             : #include "cmdid.h"
      58             : #include "swevent.hxx"
      59             : #include "shells.hrc"
      60             : #include "textsh.hxx"
      61             : #include "swabstdlg.hxx"
      62             : #include "dbui.hrc"
      63             : 
      64             : #include <unomid.h>
      65             : 
      66             : using namespace ::svx;
      67             : using namespace ::com::sun::star;
      68             : using namespace ::com::sun::star::uno;
      69             : using namespace ::com::sun::star::container;
      70             : using namespace ::com::sun::star::lang;
      71             : using namespace ::com::sun::star::sdb;
      72             : using namespace ::com::sun::star::sdbc;
      73             : using namespace ::com::sun::star::sdbcx;
      74             : using namespace ::com::sun::star::beans;
      75             : using namespace ::com::sun::star::frame;
      76             : 
      77           0 : struct DBTextStruct_Impl
      78             : {
      79             :     SwDBData aDBData;
      80             :     Sequence<Any> aSelection;
      81             :     Reference<XResultSet>   xCursor;
      82             :     Reference<XConnection>  xConnection;
      83             : };
      84             : 
      85           0 : void SwTextShell::ExecDB(SfxRequest &rReq)
      86             : {
      87           0 :     const SfxItemSet *pArgs = rReq.GetArgs();
      88           0 :     SwNewDBMgr* pNewDBMgr = GetShell().GetNewDBMgr();
      89           0 :     sal_uInt16 nSlot = rReq.GetSlot();
      90           0 :     OUString sSourceArg, sCommandArg;
      91           0 :     sal_Int32 nCommandTypeArg = 0;
      92             : 
      93           0 :     const SfxPoolItem* pSourceItem = 0;
      94           0 :     const SfxPoolItem* pCursorItem = 0;
      95           0 :     const SfxPoolItem* pConnectionItem = 0;
      96           0 :     const SfxPoolItem* pCommandItem = 0;
      97           0 :     const SfxPoolItem* pCommandTypeItem = 0;
      98           0 :     const SfxPoolItem* pSelectionItem = 0;
      99             : 
     100             :     // first get the selection of rows to be inserted
     101           0 :     pArgs->GetItemState(FN_DB_DATA_SELECTION_ANY, sal_False, &pSelectionItem);
     102             : 
     103           0 :     Sequence<Any> aSelection;
     104           0 :     if(pSelectionItem)
     105           0 :         ((SfxUsrAnyItem*)pSelectionItem)->GetValue() >>= aSelection;
     106             : 
     107             :     // get the data source name
     108           0 :     pArgs->GetItemState(FN_DB_DATA_SOURCE_ANY, sal_False, &pSourceItem);
     109           0 :     if(pSourceItem)
     110           0 :         ((const SfxUsrAnyItem*)pSourceItem)->GetValue() >>= sSourceArg;
     111             : 
     112             :     // get the command
     113           0 :     pArgs->GetItemState(FN_DB_DATA_COMMAND_ANY, sal_False, &pCommandItem);
     114           0 :     if(pCommandItem)
     115           0 :         ((const SfxUsrAnyItem*)pCommandItem)->GetValue() >>= sCommandArg;
     116             : 
     117             :     // get the command type
     118           0 :     pArgs->GetItemState(FN_DB_DATA_COMMAND_TYPE_ANY, sal_False, &pCommandTypeItem);
     119           0 :     if(pCommandTypeItem)
     120           0 :         ((const SfxUsrAnyItem*)pCommandTypeItem)->GetValue() >>= nCommandTypeArg;
     121             : 
     122           0 :     Reference<XConnection> xConnection;
     123           0 :     pArgs->GetItemState(FN_DB_CONNECTION_ANY, sal_False, &pConnectionItem);
     124           0 :     if ( pConnectionItem )
     125           0 :         ((const SfxUsrAnyItem*)pConnectionItem)->GetValue() >>= xConnection;
     126             :     // may be we even get no connection
     127           0 :     if ( !xConnection.is() )
     128             :     {
     129           0 :         Reference<XDataSource> xSource;
     130           0 :         xConnection = pNewDBMgr->GetConnection(sSourceArg, xSource);
     131             :     }
     132           0 :     if(!xConnection.is())
     133           0 :         return ;
     134             : 
     135             :     // get the cursor, we use to travel, may be NULL
     136           0 :     Reference<XResultSet> xCursor;
     137           0 :     pArgs->GetItemState(FN_DB_DATA_CURSOR_ANY, sal_False, &pCursorItem);
     138           0 :     if ( pCursorItem )
     139           0 :         ((const SfxUsrAnyItem*)pCursorItem)->GetValue() >>= xCursor;
     140             : 
     141           0 :     switch (nSlot)
     142             :     {
     143             :         case FN_QRY_INSERT:
     144             :             {
     145           0 :                 if(pSourceItem && pCommandItem && pCommandTypeItem)
     146             :                 {
     147           0 :                     DBTextStruct_Impl* pNew     = new DBTextStruct_Impl;
     148           0 :                     pNew->aDBData.sDataSource   = sSourceArg;
     149           0 :                     pNew->aDBData.sCommand      = sCommandArg;
     150           0 :                     pNew->aDBData.nCommandType  = nCommandTypeArg;
     151           0 :                     pNew->aSelection            = aSelection;
     152             :                     //if the cursor is NULL, it must be created inside InsertDBTextHdl
     153             :                     // because it called via a PostUserEvent
     154           0 :                     pNew->xCursor               = xCursor;
     155           0 :                     pNew->xConnection           = xConnection;
     156             : 
     157             :                     Application::PostUserEvent( STATIC_LINK( this, SwBaseShell,
     158           0 :                                             InsertDBTextHdl ), pNew );
     159             :                     // the pNew will be removed in InsertDBTextHdl !!
     160             :                 }
     161             :             }
     162           0 :             break;
     163             : 
     164             :         case FN_QRY_MERGE_FIELD:
     165             :             {
     166             :                 // we don't get any cursor, so we must create our own
     167           0 :                 sal_Bool bDisposeResultSet = sal_False;
     168           0 :                 if ( !xCursor.is() )
     169             :                 {
     170           0 :                     xCursor = SwNewDBMgr::createCursor(sSourceArg,sCommandArg,nCommandTypeArg,xConnection);
     171           0 :                     bDisposeResultSet = xCursor.is();
     172             :                 }
     173             : 
     174           0 :                 ODataAccessDescriptor aDescriptor;
     175           0 :                 aDescriptor.setDataSource(sSourceArg);
     176           0 :                 aDescriptor[daCommand]      <<= sCommandArg;
     177           0 :                 aDescriptor[daCursor]       <<= xCursor;
     178           0 :                 aDescriptor[daSelection]    <<= aSelection;
     179           0 :                 aDescriptor[daCommandType]  <<= nCommandTypeArg;
     180             : 
     181           0 :                 SwMergeDescriptor aMergeDesc( DBMGR_MERGE, *GetShellPtr(), aDescriptor );
     182           0 :                 pNewDBMgr->MergeNew(aMergeDesc);
     183             : 
     184           0 :                 if ( bDisposeResultSet )
     185           0 :                     ::comphelper::disposeComponent(xCursor);
     186             :             }
     187           0 :             break;
     188             : 
     189             :         case FN_QRY_INSERT_FIELD:
     190             :             {
     191           0 :                 const SfxPoolItem* pColumnItem = 0;
     192           0 :                 const SfxPoolItem* pColumnNameItem = 0;
     193             : 
     194           0 :                 pArgs->GetItemState(FN_DB_COLUMN_ANY, sal_False, &pColumnItem);
     195           0 :                 pArgs->GetItemState(FN_DB_DATA_COLUMN_NAME_ANY, sal_False, &pColumnNameItem);
     196             : 
     197           0 :                 OUString sColumnName;
     198           0 :                 if(pColumnNameItem)
     199           0 :                     ((SfxUsrAnyItem*)pColumnNameItem)->GetValue() >>= sColumnName;
     200           0 :                 String sDBName = sSourceArg;
     201           0 :                 sDBName += DB_DELIM;
     202           0 :                 sDBName += (String)sCommandArg;
     203           0 :                 sDBName += DB_DELIM;
     204           0 :                 sDBName += OUString::number(nCommandTypeArg);
     205           0 :                 sDBName += DB_DELIM;
     206           0 :                 sDBName += (String)sColumnName;
     207             : 
     208           0 :                 SwFldMgr aFldMgr(GetShellPtr());
     209           0 :                 SwInsertFld_Data aData(TYP_DBFLD, 0, sDBName, aEmptyStr, 0);
     210           0 :                 if(pConnectionItem)
     211           0 :                     aData.aDBConnection = ((SfxUsrAnyItem*)pConnectionItem)->GetValue();
     212           0 :                 if(pColumnItem)
     213           0 :                     aData.aDBColumn = ((SfxUsrAnyItem*)pColumnItem)->GetValue();
     214           0 :                 aFldMgr.InsertFld(aData);
     215           0 :                 SfxViewFrame* pViewFrame = GetView().GetViewFrame();
     216             :                 uno::Reference< XDispatchRecorder > xRecorder =
     217           0 :                         pViewFrame->GetBindings().GetRecorder();
     218           0 :                 if ( xRecorder.is() )
     219             :                 {
     220           0 :                     SfxRequest aReq( pViewFrame, FN_INSERT_DBFIELD );
     221           0 :                     aReq.AppendItem( SfxUInt16Item(FN_PARAM_FIELD_TYPE, TYP_DBFLD));
     222           0 :                     aReq.AppendItem( SfxStringItem( FN_INSERT_DBFIELD, sDBName ));
     223           0 :                     aReq.AppendItem( SfxStringItem( FN_PARAM_1, sCommandArg ));
     224           0 :                     aReq.AppendItem( SfxStringItem( FN_PARAM_2, sColumnName ));
     225           0 :                     aReq.AppendItem( SfxInt32Item( FN_PARAM_3, nCommandTypeArg));
     226           0 :                     aReq.Done();
     227           0 :                 }
     228             :             }
     229           0 :             break;
     230             : 
     231             :         default:
     232             :             OSL_ENSURE(!this, "wrong dispatcher");
     233           0 :             return;
     234           0 :     }
     235             : }
     236             : 
     237           0 : IMPL_STATIC_LINK( SwBaseShell, InsertDBTextHdl, DBTextStruct_Impl*, pDBStruct )
     238             : {
     239           0 :     if( pDBStruct )
     240             :     {
     241           0 :         bool bDispose = false;
     242           0 :         Reference< sdbc::XConnection> xConnection = pDBStruct->xConnection;
     243           0 :         Reference<XDataSource> xSource = SwNewDBMgr::getDataSourceAsParent(xConnection,pDBStruct->aDBData.sDataSource);
     244             :         // #111987# the connection is disposed an so no parent has been found
     245           0 :         if(xConnection.is() && !xSource.is())
     246           0 :             return 0;
     247             : 
     248           0 :         if ( !xConnection.is()  )
     249             :         {
     250           0 :             xConnection = SwNewDBMgr::GetConnection(pDBStruct->aDBData.sDataSource, xSource);
     251           0 :             bDispose = true;
     252             :         }
     253             : 
     254           0 :         Reference< XColumnsSupplier> xColSupp;
     255           0 :         if(xConnection.is())
     256           0 :             xColSupp = SwNewDBMgr::GetColumnSupplier(xConnection,
     257             :                                     pDBStruct->aDBData.sCommand,
     258           0 :                                     pDBStruct->aDBData.nCommandType == CommandType::QUERY ?
     259           0 :                                         SW_DB_SELECT_QUERY : SW_DB_SELECT_TABLE);
     260             : 
     261           0 :         if( xColSupp.is() )
     262             :         {
     263           0 :             SwDBData aDBData = pDBStruct->aDBData;
     264           0 :             SwAbstractDialogFactory* pFact = SwAbstractDialogFactory::Create();
     265             :             OSL_ENSURE(pFact, "SwAbstractDialogFactory fail!");
     266           0 :             ::std::auto_ptr<AbstractSwInsertDBColAutoPilot>pDlg (pFact->CreateSwInsertDBColAutoPilot( pThis->GetView(),
     267             :                                                                                                 xSource,
     268             :                                                                                                 xColSupp,
     269             :                                                                                                 aDBData,
     270           0 :                                                                                                 DLG_AP_INSERT_DB_SEL ));
     271           0 :             if( RET_OK == pDlg->Execute() )
     272             :             {
     273           0 :                 Reference <XResultSet> xResSet = pDBStruct->xCursor;
     274           0 :                 pDlg->DataToDoc( pDBStruct->aSelection, xSource, xConnection, xResSet);
     275           0 :             }
     276             :         }
     277           0 :         if ( bDispose )
     278           0 :             ::comphelper::disposeComponent(xConnection);
     279             :     }
     280             : 
     281           0 :     delete pDBStruct;
     282           0 :     return 0;
     283          99 : }
     284             : 
     285             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10