update dev300-m58
[ooovba.git] / dbaccess / source / ui / misc / databaseobjectview.cxx
blobca9b3d28b1c1d2a83637b33fe8f7c0a7838f3ae8
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: databaseobjectview.cxx,v $
10 * $Revision: 1.8.6.3 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_dbaccess.hxx"
34 #ifndef DBACCESS_DATABASE_OBJECT_VIEW_HXX
35 #include "databaseobjectview.hxx"
36 #endif
37 #ifndef DBACCESS_SHARED_DBUSTRINGS_HRC
38 #include "dbustrings.hrc"
39 #endif
40 #ifndef DBACCESS_ASYNCMODALDIALOG_HXX
41 #include "asyncmodaldialog.hxx"
42 #endif
44 /** === begin UNO includes === **/
45 #ifndef _COM_SUN_STAR_LANG_XSINGLESERVICEFACTORY_HPP_
46 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
47 #endif
48 #ifndef _COM_SUN_STAR_FRAME_XDISPATCHPROVIDER_HPP_
49 #include <com/sun/star/frame/XDispatchProvider.hpp>
50 #endif
51 #ifndef _COM_SUN_STAR_FRAME_XFRAME_HPP_
52 #include <com/sun/star/frame/XFrame.hpp>
53 #endif
54 #ifndef _COM_SUN_STAR_FRAME_XFRAMES_HPP_
55 #include <com/sun/star/frame/XFrames.hpp>
56 #endif
57 #ifndef _COM_SUN_STAR_FRAME_FRAMESEARCHFLAG_HPP_
58 #include <com/sun/star/frame/FrameSearchFlag.hpp>
59 #endif
60 #ifndef _COM_SUN_STAR_SDB_COMMANDTYPE_HPP_
61 #include <com/sun/star/sdb/CommandType.hpp>
62 #endif
63 #ifndef _COM_SUN_STAR_SDB_APPLICATION_XTABLEUIPROVIDER_HPP_
64 #include <com/sun/star/sdb/application/XTableUIProvider.hpp>
65 #endif
66 #ifndef _COM_SUN_STAR_BEANS_NAMEDVALUE_HPP_
67 #include <com/sun/star/beans/NamedValue.hpp>
68 #endif
69 #ifndef _COM_SUN_STAR_AWT_RECTANGLE_HPP_
70 #include <com/sun/star/awt/Rectangle.hpp>
71 #endif
72 /** === end UNO includes === **/
74 #ifndef _COMPHELPER_EXTRACT_HXX_
75 #include <comphelper/extract.hxx>
76 #endif
77 #ifndef _COMPHELPER_SEQUENCE_HXX_
78 #include <comphelper/sequence.hxx>
79 #endif
80 #ifndef COMPHELPER_NAMEDVALUECOLLECTION_HXX
81 #include <comphelper/namedvaluecollection.hxx>
82 #endif
84 #ifndef _CONNECTIVITY_DBTOOLS_HXX_
85 #include <connectivity/dbtools.hxx>
86 #endif
88 #ifndef _OSL_DIAGNOSE_H_
89 #include <osl/diagnose.h>
90 #endif
91 #ifndef TOOLS_DIAGNOSE_EX_H
92 #include <tools/diagnose_ex.h>
93 #endif
95 // .........................................................................
96 namespace dbaui
98 // .........................................................................
100 using namespace ::com::sun::star::uno;
101 using namespace ::com::sun::star::sdbc;
102 using namespace ::com::sun::star::sdb;
103 using namespace ::com::sun::star::sdb::application;
104 using namespace ::com::sun::star::ui::dialogs;
105 using namespace ::com::sun::star::frame;
106 using namespace ::com::sun::star::lang;
107 using namespace ::com::sun::star::beans;
108 using namespace ::com::sun::star::awt;
110 //======================================================================
111 //= DatabaseObjectView
112 //======================================================================
113 DatabaseObjectView::DatabaseObjectView( const Reference< XMultiServiceFactory >& _rxORB,
114 const Reference< XDatabaseDocumentUI >& _rxApplication,
115 const Reference< XFrame >& _rxParentFrame,
116 const ::rtl::OUString& _rComponentURL )
117 :m_xORB ( _rxORB )
118 ,m_xParentFrame ( _rxParentFrame )
119 ,m_xFrameLoader ( )
120 ,m_xApplication ( _rxApplication )
121 ,m_sComponentURL( _rComponentURL )
123 OSL_ENSURE( m_xORB.is(), "DatabaseObjectView::DatabaseObjectView: invalid service factory!" );
124 OSL_ENSURE( m_xApplication.is(), "DatabaseObjectView::DatabaseObjectView: invalid connection!" );
127 //----------------------------------------------------------------------
128 Reference< XConnection > DatabaseObjectView::getConnection() const
130 Reference< XConnection > xConnection;
131 if ( m_xApplication.is() )
132 xConnection = m_xApplication->getActiveConnection();
133 return xConnection;
136 //----------------------------------------------------------------------
137 Reference< XComponent > DatabaseObjectView::createNew(const Reference< XDataSource >& _xDataSource )
139 return doCreateView( makeAny( _xDataSource ), ::rtl::OUString(), Sequence< PropertyValue >() );
142 //----------------------------------------------------------------------
143 Reference< XComponent > DatabaseObjectView::openExisting( const Any& _rDataSource, const ::rtl::OUString& _rName, const Sequence< PropertyValue >& _rArgs )
145 return doCreateView( _rDataSource, _rName, _rArgs );
148 //----------------------------------------------------------------------
149 Reference< XComponent > DatabaseObjectView::doCreateView( const Any& _rDataSource, const ::rtl::OUString& _rObjectName,
150 const Sequence< PropertyValue >& _rCreationArgs )
152 Sequence< PropertyValue > aDispatchArgs;
153 fillDispatchArgs( aDispatchArgs, _rDataSource, _rObjectName );
155 ::comphelper::NamedValueCollection aDispArgs( aDispatchArgs );
156 aDispArgs.merge( _rCreationArgs, true );
158 return doDispatch( aDispArgs.getPropertyValues() );
161 //----------------------------------------------------------------------
162 Reference< XComponent > DatabaseObjectView::doDispatch( const Sequence< PropertyValue >& _rArgs )
164 Reference< XComponent > xReturn;
165 if ( m_xORB.is() )
169 // if we have no externally provided frame, create one
170 if ( !m_xFrameLoader.is() )
172 Reference< XSingleServiceFactory > xFact(m_xORB->createInstance(::rtl::OUString::createFromAscii("com.sun.star.frame.TaskCreator")), UNO_QUERY_THROW);
173 Sequence< Any > lArgs(2);
174 NamedValue aProp;
175 sal_Int32 nArg = 0;
177 aProp.Name = ::rtl::OUString::createFromAscii("ParentFrame");
178 aProp.Value <<= m_xParentFrame;
179 lArgs[nArg++] <<= aProp;
181 aProp.Name = ::rtl::OUString::createFromAscii("TopWindow");
182 aProp.Value <<= sal_True;
183 lArgs[nArg++] <<= aProp;
185 m_xFrameLoader.set(xFact->createInstanceWithArguments(lArgs), UNO_QUERY_THROW);
188 Reference< XComponentLoader > xFrameLoader( m_xFrameLoader, UNO_QUERY_THROW );
189 xReturn = xFrameLoader->loadComponentFromURL(
190 m_sComponentURL,
191 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("_self")),
193 _rArgs
196 if ( !xReturn.is() )
197 xReturn.set( m_xFrameLoader, UNO_QUERY );
199 catch( const Exception& )
201 DBG_UNHANDLED_EXCEPTION();
204 return xReturn;
207 //----------------------------------------------------------------------
208 void DatabaseObjectView::fillDispatchArgs(
209 Sequence< PropertyValue >& _rDispatchArguments,
210 const Any& _aDataSource,
211 const ::rtl::OUString& /* _rName */
214 sal_Int32 nPos = _rDispatchArguments.getLength();
215 _rDispatchArguments.realloc( nPos + 2 );
217 ::rtl::OUString sDataSource;
218 Reference<XDataSource> xDataSource;
219 if ( _aDataSource >>= sDataSource )
221 _rDispatchArguments[nPos ].Name = PROPERTY_DATASOURCENAME;
222 _rDispatchArguments[nPos++].Value <<= sDataSource;
224 else if ( _aDataSource >>= xDataSource )
226 _rDispatchArguments[nPos ].Name = PROPERTY_DATASOURCE;
227 _rDispatchArguments[nPos++].Value <<= xDataSource;
230 _rDispatchArguments[nPos ].Name = PROPERTY_ACTIVE_CONNECTION;
231 _rDispatchArguments[nPos++].Value <<= getConnection();
234 //======================================================================
235 //= QueryDesigner
236 //======================================================================
237 //----------------------------------------------------------------------
238 QueryDesigner::QueryDesigner( const Reference< XMultiServiceFactory >& _rxORB, const Reference< XDatabaseDocumentUI >& _rxApplication,
239 const Reference< XFrame >& _rxParentFrame,
240 bool _bCreateView, sal_Bool _bPreferSQLView )
241 :DatabaseObjectView( _rxORB, _rxApplication, _rxParentFrame, static_cast< ::rtl::OUString >( _bCreateView ? URL_COMPONENT_VIEWDESIGN : URL_COMPONENT_QUERYDESIGN ) )
242 ,m_nCommandType( _bCreateView ? CommandType::TABLE : CommandType::QUERY )
243 ,m_bPreferSQLView( _bPreferSQLView )
247 //----------------------------------------------------------------------
248 void QueryDesigner::fillDispatchArgs( Sequence< PropertyValue >& _rDispatchArguments, const Any& _aDataSource,
249 const ::rtl::OUString& _rObjectName )
251 DatabaseObjectView::fillDispatchArgs( _rDispatchArguments, _aDataSource, _rObjectName );
253 bool bIncludeQueryName = 0 != _rObjectName.getLength();
254 bool bEditViewAsSQLCommand = ( m_nCommandType == CommandType::TABLE ) && m_bPreferSQLView;
256 sal_Int32 nPos = _rDispatchArguments.getLength();
258 sal_Int32 nNewLen = _rDispatchArguments.getLength() + 2;
259 if ( bIncludeQueryName )
260 ++nNewLen;
261 if ( bEditViewAsSQLCommand )
262 ++nNewLen;
263 _rDispatchArguments.realloc( nNewLen );
265 _rDispatchArguments[nPos ].Name = PROPERTY_GRAPHICAL_DESIGN;
266 _rDispatchArguments[nPos++].Value <<= ::cppu::bool2any( !m_bPreferSQLView );
268 _rDispatchArguments[nPos ].Name = PROPERTY_COMMAND_TYPE;
269 _rDispatchArguments[nPos++].Value <<= m_nCommandType;
271 if ( bIncludeQueryName )
273 _rDispatchArguments[nPos ].Name = PROPERTY_COMMAND;
274 _rDispatchArguments[nPos++].Value <<= _rObjectName;
277 if ( bEditViewAsSQLCommand )
279 _rDispatchArguments[nPos ].Name = PROPERTY_ESCAPE_PROCESSING;
280 _rDispatchArguments[nPos++].Value <<= sal_Bool( sal_False );
284 //======================================================================
285 //= TableDesigner
286 //======================================================================
287 //----------------------------------------------------------------------
288 TableDesigner::TableDesigner( const Reference< XMultiServiceFactory >& _rxORB, const Reference< XDatabaseDocumentUI >& _rxApplication, const Reference< XFrame >& _rxParentFrame )
289 :DatabaseObjectView( _rxORB, _rxApplication, _rxParentFrame, static_cast< ::rtl::OUString >( URL_COMPONENT_TABLEDESIGN ) )
293 //----------------------------------------------------------------------
294 void TableDesigner::fillDispatchArgs( Sequence< PropertyValue >& _rDispatchArguments, const Any& _aDataSource,
295 const ::rtl::OUString& _rObjectName )
297 DatabaseObjectView::fillDispatchArgs( _rDispatchArguments, _aDataSource, _rObjectName );
298 sal_Bool bIncludeName = 0 != _rObjectName.getLength();
300 if (bIncludeName)
302 sal_Int32 nPos = _rDispatchArguments.getLength();
303 _rDispatchArguments.realloc(_rDispatchArguments.getLength() + 1);
304 _rDispatchArguments[nPos ].Name = PROPERTY_CURRENTTABLE;
305 _rDispatchArguments[nPos++].Value <<= _rObjectName;
309 //----------------------------------------------------------------------
310 Reference< XComponent > TableDesigner::doCreateView( const Any& _rDataSource, const ::rtl::OUString& _rObjectName,
311 const Sequence< PropertyValue >& _rCreationArgs )
313 bool bIsNewDesign = ( _rObjectName.getLength() == 0 );
315 // let's see whether the connection can provide a dedicated table desginer
316 Reference< XInterface > xDesigner;
317 if ( !bIsNewDesign )
318 xDesigner = impl_getConnectionProvidedDesigner_nothrow( _rObjectName );
320 if ( !xDesigner.is() )
321 return DatabaseObjectView::doCreateView( _rDataSource, _rObjectName, _rCreationArgs );
323 // try whether the designer is a dialog
324 Reference< XExecutableDialog > xDialog( xDesigner, UNO_QUERY_THROW );
325 if ( xDialog.is() )
327 try { AsyncDialogExecutor::executeModalDialogAsync( xDialog ); }
328 catch( const Exception& ) { DBG_UNHANDLED_EXCEPTION(); }
329 return NULL;
332 Reference< XComponent > xDesignerComponent( xDesigner, UNO_QUERY );
333 OSL_ENSURE( xDesignerComponent.is(), "TableDesigner::doCreateView: a designer which is no dialog and no component?" );
334 return xDesignerComponent;
337 //----------------------------------------------------------------------
338 Reference< XInterface > TableDesigner::impl_getConnectionProvidedDesigner_nothrow( const ::rtl::OUString& _rTableName )
340 Reference< XInterface > xDesigner;
343 Reference< XTableUIProvider > xTableUIProv( getConnection(), UNO_QUERY );
344 if ( xTableUIProv.is() )
345 xDesigner = xTableUIProv->getTableEditor( getApplicationUI(), _rTableName );
347 catch( const Exception& )
349 DBG_UNHANDLED_EXCEPTION();
351 return xDesigner;
354 //======================================================================
355 //= ResultSetBrowser
356 //======================================================================
357 //----------------------------------------------------------------------
358 ResultSetBrowser::ResultSetBrowser( const Reference< XMultiServiceFactory >& _rxORB, const Reference< XDatabaseDocumentUI >& _rxApplication, const Reference< XFrame >& _rxParentFrame,
359 sal_Bool _bTable )
360 :DatabaseObjectView( _rxORB, _rxApplication, _rxParentFrame, static_cast < ::rtl::OUString >( URL_COMPONENT_DATASOURCEBROWSER ) )
361 ,m_bTable(_bTable)
365 //----------------------------------------------------------------------
366 void ResultSetBrowser::fillDispatchArgs(Sequence< PropertyValue >& _rDispatchArguments, const Any& _aDataSource,
367 const ::rtl::OUString& _rQualifiedName)
369 DatabaseObjectView::fillDispatchArgs( _rDispatchArguments, _aDataSource, _rQualifiedName );
370 OSL_ENSURE( 0 != _rQualifiedName.getLength(),"A Table name must be set");
371 ::rtl::OUString sCatalog;
372 ::rtl::OUString sSchema;
373 ::rtl::OUString sTable;
374 if ( m_bTable )
375 ::dbtools::qualifiedNameComponents( getConnection()->getMetaData(), _rQualifiedName, sCatalog, sSchema, sTable, ::dbtools::eInDataManipulation );
377 sal_Int32 nPos = _rDispatchArguments.getLength();
378 _rDispatchArguments.realloc( _rDispatchArguments.getLength() + 3 + ( m_bTable ? 3 : 0 ) );
380 _rDispatchArguments[nPos ].Name = PROPERTY_COMMAND_TYPE;
381 _rDispatchArguments[nPos++].Value <<= (m_bTable ? CommandType::TABLE : CommandType::QUERY);
383 _rDispatchArguments[nPos ].Name = PROPERTY_COMMAND;
384 _rDispatchArguments[nPos++].Value <<= _rQualifiedName;
386 _rDispatchArguments[nPos ].Name = PROPERTY_ENABLE_BROWSER;
387 _rDispatchArguments[nPos++].Value <<= sal_False;
389 if ( m_bTable )
391 _rDispatchArguments[nPos ].Name = PROPERTY_UPDATE_CATALOGNAME;
392 _rDispatchArguments[nPos++].Value <<= sCatalog;
393 _rDispatchArguments[nPos ].Name = PROPERTY_UPDATE_SCHEMANAME;
394 _rDispatchArguments[nPos++].Value <<= sSchema;
395 _rDispatchArguments[nPos ].Name = PROPERTY_UPDATE_TABLENAME;
396 _rDispatchArguments[nPos++].Value <<= sTable;
400 //======================================================================
401 //= RelationDesigner
402 //======================================================================
403 //----------------------------------------------------------------------
404 RelationDesigner::RelationDesigner( const Reference< XMultiServiceFactory >& _rxORB, const Reference< XDatabaseDocumentUI >& _rxApplication, const Reference< XFrame >& _rxParentFrame )
405 :DatabaseObjectView( _rxORB, _rxApplication, _rxParentFrame, static_cast< ::rtl::OUString >( URL_COMPONENT_RELATIONDESIGN ) )
408 //======================================================================
409 //= ReportDesigner
410 //======================================================================
411 //----------------------------------------------------------------------
412 ReportDesigner::ReportDesigner( const Reference< XMultiServiceFactory >& _rxORB, const Reference< XDatabaseDocumentUI >& _rxApplication , const Reference< XFrame >& _rxParentFrame)
413 :DatabaseObjectView( _rxORB, _rxApplication, _rxParentFrame,static_cast< ::rtl::OUString >( URL_COMPONENT_REPORTDESIGN ) )
418 // .........................................................................
419 } // namespace dbaui
420 // .........................................................................