LCOV - code coverage report
Current view: top level - libreoffice/sw/source/ui/shells - textsh2.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 1 125 0.8 %
Date: 2012-12-27 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             : #define DB_DD_DELIM 0x0b
      78             : 
      79           0 : struct DBTextStruct_Impl
      80             : {
      81             :     SwDBData aDBData;
      82             :     Sequence<Any> aSelection;
      83             :     Reference<XResultSet>   xCursor;
      84             :     Reference<XConnection>  xConnection;
      85             : };
      86             : 
      87           0 : void SwTextShell::ExecDB(SfxRequest &rReq)
      88             : {
      89           0 :     const SfxItemSet *pArgs = rReq.GetArgs();
      90           0 :     SwNewDBMgr* pNewDBMgr = GetShell().GetNewDBMgr();
      91           0 :     sal_uInt16 nSlot = rReq.GetSlot();
      92           0 :     ::rtl::OUString sSourceArg, sCommandArg;
      93           0 :     sal_Int32 nCommandTypeArg = 0;
      94             : 
      95           0 :     const SfxPoolItem* pSourceItem = 0;
      96           0 :     const SfxPoolItem* pCursorItem = 0;
      97           0 :     const SfxPoolItem* pConnectionItem = 0;
      98           0 :     const SfxPoolItem* pCommandItem = 0;
      99           0 :     const SfxPoolItem* pCommandTypeItem = 0;
     100           0 :     const SfxPoolItem* pSelectionItem = 0;
     101             : 
     102             :     // first get the selection of rows to be inserted
     103           0 :     pArgs->GetItemState(FN_DB_DATA_SELECTION_ANY, sal_False, &pSelectionItem);
     104             : 
     105           0 :     Sequence<Any> aSelection;
     106           0 :     if(pSelectionItem)
     107           0 :         ((SfxUsrAnyItem*)pSelectionItem)->GetValue() >>= aSelection;
     108             : 
     109             :     // get the data source name
     110           0 :     pArgs->GetItemState(FN_DB_DATA_SOURCE_ANY, sal_False, &pSourceItem);
     111           0 :     if(pSourceItem)
     112           0 :         ((const SfxUsrAnyItem*)pSourceItem)->GetValue() >>= sSourceArg;
     113             : 
     114             :     // get the command
     115           0 :     pArgs->GetItemState(FN_DB_DATA_COMMAND_ANY, sal_False, &pCommandItem);
     116           0 :     if(pCommandItem)
     117           0 :         ((const SfxUsrAnyItem*)pCommandItem)->GetValue() >>= sCommandArg;
     118             : 
     119             :     // get the command type
     120           0 :     pArgs->GetItemState(FN_DB_DATA_COMMAND_TYPE_ANY, sal_False, &pCommandTypeItem);
     121           0 :     if(pCommandTypeItem)
     122           0 :         ((const SfxUsrAnyItem*)pCommandTypeItem)->GetValue() >>= nCommandTypeArg;
     123             : 
     124           0 :     Reference<XConnection> xConnection;
     125           0 :     pArgs->GetItemState(FN_DB_CONNECTION_ANY, sal_False, &pConnectionItem);
     126           0 :     if ( pConnectionItem )
     127           0 :         ((const SfxUsrAnyItem*)pConnectionItem)->GetValue() >>= xConnection;
     128             :     // may be we even get no connection
     129           0 :     if ( !xConnection.is() )
     130             :     {
     131           0 :         Reference<XDataSource> xSource;
     132           0 :         xConnection = pNewDBMgr->GetConnection(sSourceArg, xSource);
     133             :     }
     134           0 :     if(!xConnection.is())
     135             :         return ;
     136             : 
     137             :     // get the cursor, we use to travel, may be NULL
     138           0 :     Reference<XResultSet> xCursor;
     139           0 :     pArgs->GetItemState(FN_DB_DATA_CURSOR_ANY, sal_False, &pCursorItem);
     140           0 :     if ( pCursorItem )
     141           0 :         ((const SfxUsrAnyItem*)pCursorItem)->GetValue() >>= xCursor;
     142             : 
     143           0 :     switch (nSlot)
     144             :     {
     145             :         case FN_QRY_INSERT:
     146             :             {
     147           0 :                 if(pSourceItem && pCommandItem && pCommandTypeItem)
     148             :                 {
     149           0 :                     DBTextStruct_Impl* pNew     = new DBTextStruct_Impl;
     150           0 :                     pNew->aDBData.sDataSource   = sSourceArg;
     151           0 :                     pNew->aDBData.sCommand      = sCommandArg;
     152           0 :                     pNew->aDBData.nCommandType  = nCommandTypeArg;
     153           0 :                     pNew->aSelection            = aSelection;
     154             :                     //if the cursor is NULL, it must be created inside InsertDBTextHdl
     155             :                     // because it called via a PostUserEvent
     156           0 :                     pNew->xCursor               = xCursor;
     157           0 :                     pNew->xConnection           = xConnection;
     158             : 
     159             :                     Application::PostUserEvent( STATIC_LINK( this, SwBaseShell,
     160           0 :                                             InsertDBTextHdl ), pNew );
     161             :                     // the pNew will be removed in InsertDBTextHdl !!
     162             :                 }
     163             :             }
     164           0 :             break;
     165             : 
     166             :         case FN_QRY_MERGE_FIELD:
     167             :             {
     168             :                 // we don't get any cursor, so we must create our own
     169           0 :                 sal_Bool bDisposeResultSet = sal_False;
     170           0 :                 if ( !xCursor.is() )
     171             :                 {
     172           0 :                     xCursor = SwNewDBMgr::createCursor(sSourceArg,sCommandArg,nCommandTypeArg,xConnection);
     173           0 :                     bDisposeResultSet = xCursor.is();
     174             :                 }
     175             : 
     176           0 :                 ODataAccessDescriptor aDescriptor;
     177           0 :                 aDescriptor.setDataSource(sSourceArg);
     178           0 :                 aDescriptor[daCommand]      <<= sCommandArg;
     179           0 :                 aDescriptor[daCursor]       <<= xCursor;
     180           0 :                 aDescriptor[daSelection]    <<= aSelection;
     181           0 :                 aDescriptor[daCommandType]  <<= nCommandTypeArg;
     182             : 
     183           0 :                 SwMergeDescriptor aMergeDesc( DBMGR_MERGE, *GetShellPtr(), aDescriptor );
     184           0 :                 pNewDBMgr->MergeNew(aMergeDesc);
     185             : 
     186           0 :                 if ( bDisposeResultSet )
     187           0 :                     ::comphelper::disposeComponent(xCursor);
     188             :             }
     189           0 :             break;
     190             : 
     191             :         case FN_QRY_INSERT_FIELD:
     192             :             {
     193           0 :                 const SfxPoolItem* pColumnItem = 0;
     194           0 :                 const SfxPoolItem* pColumnNameItem = 0;
     195             : 
     196           0 :                 pArgs->GetItemState(FN_DB_COLUMN_ANY, sal_False, &pColumnItem);
     197           0 :                 pArgs->GetItemState(FN_DB_DATA_COLUMN_NAME_ANY, sal_False, &pColumnNameItem);
     198             : 
     199           0 :                 ::rtl::OUString sColumnName;
     200           0 :                 if(pColumnNameItem)
     201           0 :                     ((SfxUsrAnyItem*)pColumnNameItem)->GetValue() >>= sColumnName;
     202           0 :                 String sDBName = sSourceArg;
     203           0 :                 sDBName += DB_DELIM;
     204           0 :                 sDBName += (String)sCommandArg;
     205           0 :                 sDBName += DB_DELIM;
     206           0 :                 sDBName += String::CreateFromInt32(nCommandTypeArg);
     207           0 :                 sDBName += DB_DELIM;
     208           0 :                 sDBName += (String)sColumnName;
     209             : 
     210           0 :                 SwFldMgr aFldMgr(GetShellPtr());
     211           0 :                 SwInsertFld_Data aData(TYP_DBFLD, 0, sDBName, aEmptyStr, 0);
     212           0 :                 if(pConnectionItem)
     213           0 :                     aData.aDBConnection = ((SfxUsrAnyItem*)pConnectionItem)->GetValue();
     214           0 :                 if(pColumnItem)
     215           0 :                     aData.aDBColumn = ((SfxUsrAnyItem*)pColumnItem)->GetValue();
     216           0 :                 aFldMgr.InsertFld(aData);
     217           0 :                 SfxViewFrame* pViewFrame = GetView().GetViewFrame();
     218             :                 uno::Reference< XDispatchRecorder > xRecorder =
     219           0 :                         pViewFrame->GetBindings().GetRecorder();
     220           0 :                 if ( xRecorder.is() )
     221             :                 {
     222           0 :                     SfxRequest aReq( pViewFrame, FN_INSERT_DBFIELD );
     223           0 :                     aReq.AppendItem( SfxUInt16Item(FN_PARAM_FIELD_TYPE, TYP_DBFLD));
     224           0 :                     aReq.AppendItem( SfxStringItem( FN_INSERT_DBFIELD, sDBName ));
     225           0 :                     aReq.AppendItem( SfxStringItem( FN_PARAM_1, sCommandArg ));
     226           0 :                     aReq.AppendItem( SfxStringItem( FN_PARAM_2, sColumnName ));
     227           0 :                     aReq.AppendItem( SfxInt32Item( FN_PARAM_3, nCommandTypeArg));
     228           0 :                     aReq.Done();
     229           0 :                 }
     230             :             }
     231           0 :             break;
     232             : 
     233             :         default:
     234             :             OSL_ENSURE(!this, "wrong dispatcher");
     235             :             return;
     236           0 :     }
     237             : }
     238             : 
     239           0 : IMPL_STATIC_LINK( SwBaseShell, InsertDBTextHdl, DBTextStruct_Impl*, pDBStruct )
     240             : {
     241           0 :     if( pDBStruct )
     242             :     {
     243           0 :         sal_Bool bDispose = sal_False;
     244           0 :         Reference< sdbc::XConnection> xConnection = pDBStruct->xConnection;
     245           0 :         Reference<XDataSource> xSource = SwNewDBMgr::getDataSourceAsParent(xConnection,pDBStruct->aDBData.sDataSource);
     246             :         // #111987# the connection is disposed an so no parent has been found
     247           0 :         if(xConnection.is() && !xSource.is())
     248           0 :             return 0;
     249             : 
     250           0 :         if ( !xConnection.is()  )
     251             :         {
     252           0 :             xConnection = SwNewDBMgr::GetConnection(pDBStruct->aDBData.sDataSource, xSource);
     253           0 :             bDispose = sal_True;
     254             :         }
     255             : 
     256           0 :         Reference< XColumnsSupplier> xColSupp;
     257           0 :         if(xConnection.is())
     258             :             xColSupp = SwNewDBMgr::GetColumnSupplier(xConnection,
     259             :                                     pDBStruct->aDBData.sCommand,
     260             :                                     pDBStruct->aDBData.nCommandType == CommandType::QUERY ?
     261           0 :                                         SW_DB_SELECT_QUERY : SW_DB_SELECT_TABLE);
     262             : 
     263           0 :         if( xColSupp.is() )
     264             :         {
     265           0 :             SwDBData aDBData = pDBStruct->aDBData;
     266           0 :             SwAbstractDialogFactory* pFact = SwAbstractDialogFactory::Create();
     267             :             OSL_ENSURE(pFact, "SwAbstractDialogFactory fail!");
     268           0 :             ::std::auto_ptr<AbstractSwInsertDBColAutoPilot>pDlg (pFact->CreateSwInsertDBColAutoPilot( pThis->GetView(),
     269             :                                                                                                 xSource,
     270             :                                                                                                 xColSupp,
     271             :                                                                                                 aDBData,
     272           0 :                                                                                                 DLG_AP_INSERT_DB_SEL ));
     273           0 :             if( RET_OK == pDlg->Execute() )
     274             :             {
     275           0 :                 Reference <XResultSet> xResSet = pDBStruct->xCursor;
     276           0 :                 pDlg->DataToDoc( pDBStruct->aSelection, xSource, xConnection, xResSet);
     277           0 :             }
     278             :         }
     279           0 :         if ( bDispose )
     280           0 :             ::comphelper::disposeComponent(xConnection);
     281             :     }
     282             : 
     283           0 :     delete pDBStruct;
     284           0 :     return 0;
     285          30 : }
     286             : 
     287             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10