Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / connectivity / source / commontools / parameters.cxx
blob13859f83b3b59b62c11fae6e7f47ad813b0ab87a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "connectivity/parameters.hxx"
22 #include <com/sun/star/form/DatabaseParameterEvent.hpp>
23 #include <com/sun/star/sdbc/XParameters.hpp>
24 #include <com/sun/star/container/XChild.hpp>
25 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
26 #include <com/sun/star/container/XEnumerationAccess.hpp>
27 #include <com/sun/star/sdb/XParametersSupplier.hpp>
28 #include <com/sun/star/sdb/XInteractionSupplyParameters.hpp>
29 #include <com/sun/star/sdb/ParametersRequest.hpp>
31 #include <connectivity/dbtools.hxx>
32 #include "connectivity/filtermanager.hxx"
33 #include "TConnection.hxx"
35 #include <tools/debug.hxx>
36 #include <tools/diagnose_ex.h>
38 #include <comphelper/uno3.hxx>
39 #include <comphelper/proparrhlp.hxx>
40 #include <comphelper/broadcasthelper.hxx>
41 #include "connectivity/ParameterCont.hxx"
42 #include <rtl/ustrbuf.hxx>
45 namespace dbtools
49 using namespace ::com::sun::star::uno;
50 using namespace ::com::sun::star::sdb;
51 using namespace ::com::sun::star::sdbc;
52 using namespace ::com::sun::star::sdbcx;
53 using namespace ::com::sun::star::lang;
54 using namespace ::com::sun::star::beans;
55 using namespace ::com::sun::star::task;
56 using namespace ::com::sun::star::form;
57 using namespace ::com::sun::star::container;
59 using namespace ::comphelper;
60 using namespace ::connectivity;
63 //= ParameterManager
66 ParameterManager::ParameterManager( ::osl::Mutex& _rMutex, const Reference< XComponentContext >& _rxContext )
67 :m_rMutex ( _rMutex )
68 ,m_aParameterListeners( _rMutex )
69 ,m_xContext ( _rxContext )
70 ,m_pOuterParameters ( NULL )
71 ,m_nInnerCount ( 0 )
72 ,m_bUpToDate ( false )
74 OSL_ENSURE( m_xContext.is(), "ParameterManager::ParameterManager: no service factory!" );
78 void ParameterManager::initialize( const Reference< XPropertySet >& _rxComponent, const Reference< XAggregation >& _rxComponentAggregate )
80 OSL_ENSURE( !m_xComponent.get().is(), "ParameterManager::initialize: already initialized!" );
82 m_xComponent = _rxComponent;
83 m_xAggregatedRowSet = _rxComponentAggregate;
84 if ( m_xAggregatedRowSet.is() )
85 m_xAggregatedRowSet->queryAggregation( ::getCppuType( &m_xInnerParamUpdate ) ) >>= m_xInnerParamUpdate;
86 OSL_ENSURE( m_xComponent.get().is() && m_xInnerParamUpdate.is(), "ParameterManager::initialize: invalid arguments!" );
87 if ( !m_xComponent.get().is() || !m_xInnerParamUpdate.is() )
88 return;
92 void ParameterManager::dispose( )
94 clearAllParameterInformation();
96 m_xComposer.clear();
97 m_xParentComposer.clear();
98 //m_xComponent.clear();
99 m_xInnerParamUpdate.clear();
100 m_xAggregatedRowSet.clear();
104 void ParameterManager::clearAllParameterInformation()
106 m_xInnerParamColumns.clear();
107 if ( m_pOuterParameters.is() )
108 m_pOuterParameters->dispose();
109 m_pOuterParameters = NULL;
110 m_nInnerCount = 0;
111 ParameterInformation aEmptyInfo;
112 m_aParameterInformation.swap( aEmptyInfo );
113 m_aMasterFields.realloc( 0 );
114 m_aDetailFields.realloc( 0 );
115 m_sIdentifierQuoteString = OUString();
116 ::std::vector< bool > aEmptyArray;
117 m_aParametersVisited.swap( aEmptyArray );
118 m_bUpToDate = false;
122 void ParameterManager::disposing( const EventObject& /*_rDisposingEvent*/ )
127 void ParameterManager::setAllParametersNull() SAL_THROW( ( SQLException, RuntimeException ) )
129 OSL_PRECOND( isAlive(), "ParameterManager::setAllParametersNull: not initialized, or already disposed!" );
130 if ( !isAlive() )
131 return;
133 for ( sal_Int32 i = 1; i <= m_nInnerCount; ++i )
134 m_xInnerParamUpdate->setNull( i, DataType::VARCHAR );
138 bool ParameterManager::initializeComposerByComponent( const Reference< XPropertySet >& _rxComponent )
140 OSL_PRECOND( _rxComponent.is(), "ParameterManager::initializeComposerByComponent: invalid !" );
142 m_xComposer.clear();
143 m_xInnerParamColumns.clear();
144 m_nInnerCount = 0;
146 // create and fill a composer
149 // get a query composer for the 's settings
150 m_xComposer.reset( getCurrentSettingsComposer( _rxComponent, m_xContext ), SharedQueryComposer::TakeOwnership );
152 // see if the composer found parameters
153 Reference< XParametersSupplier > xParamSupp( m_xComposer, UNO_QUERY );
154 if ( xParamSupp.is() )
155 m_xInnerParamColumns = xParamSupp->getParameters();
157 if ( m_xInnerParamColumns.is() )
158 m_nInnerCount = m_xInnerParamColumns->getCount();
160 catch( const SQLException& )
164 return m_xInnerParamColumns.is();
168 void ParameterManager::collectInnerParameters( bool _bSecondRun )
170 OSL_PRECOND( m_xInnerParamColumns.is(), "ParameterManager::collectInnerParameters: missing some internal data!" );
171 if ( !m_xInnerParamColumns.is() )
172 return;
174 // strip previous index information
175 if ( _bSecondRun )
177 for ( ParameterInformation::iterator aParamInfo = m_aParameterInformation.begin();
178 aParamInfo != m_aParameterInformation.end();
179 ++aParamInfo
182 aParamInfo->second.aInnerIndexes.clear();
186 // we need to map the parameter names (which is all we get from the 's
187 // MasterFields property) to indices, which are needed by the XParameters
188 // interface of the row set)
189 Reference<XPropertySet> xParam;
190 for ( sal_Int32 i = 0; i < m_nInnerCount; ++i )
194 xParam.clear();
195 m_xInnerParamColumns->getByIndex( i ) >>= xParam;
197 OUString sName;
198 xParam->getPropertyValue( OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME) ) >>= sName;
200 // only append additional parameters when they are not already in the list
201 ParameterInformation::iterator aExistentPos = m_aParameterInformation.find( sName );
202 OSL_ENSURE( !_bSecondRun || ( aExistentPos != m_aParameterInformation.end() ),
203 "ParameterManager::collectInnerParameters: the parameter information should already exist in the second run!" );
205 if ( aExistentPos == m_aParameterInformation.end() )
207 aExistentPos = m_aParameterInformation.insert( ParameterInformation::value_type(
208 sName, xParam ) ).first;
210 else
211 aExistentPos->second.xComposerColumn = xParam;
213 aExistentPos->second.aInnerIndexes.push_back( i );
215 catch( const Exception& )
217 SAL_WARN( "connectivity.commontools", "ParameterManager::collectInnerParameters: caught an exception!" );
223 OUString ParameterManager::createFilterConditionFromColumnLink(
224 const OUString& _rMasterColumn, const OUString& _rDetailLink, OUString& _rNewParamName )
226 OUString sFilter;
228 // format is:
229 // <detail_column> = :<new_param_name>
230 sFilter = quoteName( m_sIdentifierQuoteString, _rDetailLink );
231 sFilter += " = :";
233 // generate a parameter name which is not already used
234 _rNewParamName = "link_from_";
235 _rNewParamName += convertName2SQLName( _rMasterColumn, m_sSpecialCharacters );
236 while ( m_aParameterInformation.find( _rNewParamName ) != m_aParameterInformation.end() )
238 _rNewParamName += "_";
241 return sFilter += _rNewParamName;
245 void ParameterManager::classifyLinks( const Reference< XNameAccess >& _rxParentColumns,
246 const Reference< XNameAccess >& _rxColumns, ::std::vector< OUString >& _out_rAdditionalFilterComponents ) SAL_THROW(( Exception ))
248 OSL_PRECOND( m_aMasterFields.getLength() == m_aDetailFields.getLength(),
249 "ParameterManager::classifyLinks: master and detail fields should have the same length!" );
250 OSL_ENSURE( _rxColumns.is(), "ParameterManager::classifyLinks: invalid columns!" );
252 if ( !_rxColumns.is() )
253 return;
255 // we may need to strip any links which are invalid, so here go the containers
256 // for temporarirly holding the new pairs
257 ::std::vector< OUString > aStrippedMasterFields;
258 ::std::vector< OUString > aStrippedDetailFields;
260 bool bNeedExchangeLinks = false;
262 // classify the links
263 const OUString* pMasterFields = m_aMasterFields.getConstArray();
264 const OUString* pDetailFields = m_aDetailFields.getConstArray();
265 const OUString* pDetailFieldsEnd = pDetailFields + m_aDetailFields.getLength();
266 for ( ; pDetailFields < pDetailFieldsEnd; ++pDetailFields, ++pMasterFields )
268 if ( pMasterFields->isEmpty() || pDetailFields->isEmpty() )
269 continue;
271 // if not even the master part of the relationship exists in the parent , the
272 // link is invalid as a whole
273 // #i63674# / 2006-03-28 / frank.schoenheit@sun.com
274 if ( !_rxParentColumns->hasByName( *pMasterFields ) )
276 bNeedExchangeLinks = true;
277 continue;
280 bool bValidLink = true;
282 // is there an inner parameter with this name? That is, a parameter which is already part of
283 // the very original statement (not the one we create ourselve, with the additional parameters)
284 ParameterInformation::iterator aPos = m_aParameterInformation.find( *pDetailFields );
285 if ( aPos != m_aParameterInformation.end() )
286 { // there is an inner parameter with this name
287 aPos->second.eType = eLinkedByParamName;
288 aStrippedDetailFields.push_back( *pDetailFields );
290 else
292 // does the detail name denote a column?
293 if ( _rxColumns->hasByName( *pDetailFields ) )
295 OUString sNewParamName;
296 const OUString sFilterCondition = createFilterConditionFromColumnLink( *pMasterFields, *pDetailFields, sNewParamName );
297 OSL_PRECOND( !sNewParamName.isEmpty(), "ParameterManager::classifyLinks: createFilterConditionFromColumnLink returned nonsense!" );
299 // remember meta information about this new parameter
300 ::std::pair< ParameterInformation::iterator, bool > aInsertionPos =
301 m_aParameterInformation.insert(
302 ParameterInformation::value_type( sNewParamName, ParameterMetaData( NULL ) )
304 OSL_ENSURE( aInsertionPos.second, "ParameterManager::classifyLinks: there already was a parameter with this name!" );
305 aInsertionPos.first->second.eType = eLinkedByColumnName;
307 // remember the filter component
308 _out_rAdditionalFilterComponents.push_back( sFilterCondition );
310 // remember the new "detail field" for this link
311 aStrippedDetailFields.push_back( sNewParamName );
312 bNeedExchangeLinks = true;
314 else
316 // the detail field neither denotes a column name, nor a parameter name
317 bValidLink = false;
318 bNeedExchangeLinks = true;
322 if ( bValidLink )
323 aStrippedMasterFields.push_back( *pMasterFields );
325 SAL_WARN_IF( aStrippedMasterFields.size() != aStrippedDetailFields.size(),
326 "connectivity.commontools",
327 "ParameterManager::classifyLinks: inconsistency in new link pairs!" );
329 if ( bNeedExchangeLinks )
331 OUString *pFields = aStrippedMasterFields.empty() ? 0 : &aStrippedMasterFields[0];
332 m_aMasterFields = Sequence< OUString >( pFields, aStrippedMasterFields.size() );
333 pFields = aStrippedDetailFields.empty() ? 0 : &aStrippedDetailFields[0];
334 m_aDetailFields = Sequence< OUString >( pFields, aStrippedDetailFields.size() );
339 void ParameterManager::analyzeFieldLinks( FilterManager& _rFilterManager, bool& /* [out] */ _rColumnsInLinkDetails )
341 OSL_PRECOND( isAlive(), "ParameterManager::analyzeFieldLinks: not initialized, or already disposed!" );
342 if ( !isAlive() )
343 return;
345 _rColumnsInLinkDetails = false;
348 // the links as determined by the properties
349 Reference< XPropertySet > xProp = m_xComponent;
350 OSL_ENSURE(xProp.is(),"Some already released my component!");
351 if ( xProp.is() )
353 xProp->getPropertyValue( OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_MASTERFIELDS) ) >>= m_aMasterFields;
354 xProp->getPropertyValue( OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_DETAILFIELDS) ) >>= m_aDetailFields;
358 // normalize to equal length
359 sal_Int32 nMasterLength = m_aMasterFields.getLength();
360 sal_Int32 nDetailLength = m_aDetailFields.getLength();
362 if ( nMasterLength > nDetailLength )
363 m_aMasterFields.realloc( nDetailLength );
364 else if ( nDetailLength > nMasterLength )
365 m_aDetailFields.realloc( nMasterLength );
368 Reference< XNameAccess > xColumns;
369 if ( !getColumns( xColumns, true ) )
370 // already asserted in getColumns
371 return;
373 Reference< XNameAccess > xParentColumns;
374 if ( !getParentColumns( xParentColumns, true ) )
375 return;
377 // classify the links - depending on what the detail fields in each link pair denotes
378 ::std::vector< OUString > aAdditionalFilterComponents;
379 classifyLinks( xParentColumns, xColumns, aAdditionalFilterComponents );
381 // did we find links where the detail field refers to a detail column (instead of a parameter name)?
382 if ( !aAdditionalFilterComponents.empty() )
384 // build a conjunction of all the filter components
385 OUStringBuffer sAdditionalFilter;
386 for ( ::std::vector< OUString >::const_iterator aComponent = aAdditionalFilterComponents.begin();
387 aComponent != aAdditionalFilterComponents.end();
388 ++aComponent
391 if ( !sAdditionalFilter.isEmpty() )
392 sAdditionalFilter.append(" AND ");
394 sAdditionalFilter.appendAscii("( ",((sal_Int32)(sizeof("( ")-1)));
395 sAdditionalFilter.append(*aComponent);
396 sAdditionalFilter.appendAscii(" )",((sal_Int32)(sizeof(" )")-1)));
399 // now set this filter at the 's filter manager
400 _rFilterManager.setFilterComponent( FilterManager::fcLinkFilter, sAdditionalFilter.makeStringAndClear() );
402 _rColumnsInLinkDetails = true;
405 catch( const Exception& )
407 SAL_WARN( "connectivity.commontools", "ParameterManager::analyzeFieldLinks: caught an exception!" );
412 void ParameterManager::createOuterParameters()
414 OSL_PRECOND( !m_pOuterParameters.is(), "ParameterManager::createOuterParameters: outer parameters not initialized!" );
415 OSL_PRECOND( m_xInnerParamUpdate.is(), "ParameterManager::createOuterParameters: no write access to the inner parameters!" );
416 if ( !m_xInnerParamUpdate.is() )
417 return;
419 m_pOuterParameters = new param::ParameterWrapperContainer;
421 #if OSL_DEBUG_LEVEL > 0
422 sal_Int32 nSmallestIndexLinkedByColumnName = -1;
423 sal_Int32 nLargestIndexNotLinkedByColumnName = -1;
424 #endif
425 for ( ParameterInformation::iterator aParam = m_aParameterInformation.begin();
426 aParam != m_aParameterInformation.end();
427 ++aParam
430 #if OSL_DEBUG_LEVEL > 0
431 if ( aParam->second.aInnerIndexes.size() )
433 if ( aParam->second.eType == eLinkedByColumnName )
435 if ( nSmallestIndexLinkedByColumnName == -1 )
436 nSmallestIndexLinkedByColumnName = aParam->second.aInnerIndexes[ 0 ];
438 else
440 nLargestIndexNotLinkedByColumnName = aParam->second.aInnerIndexes[ aParam->second.aInnerIndexes.size() - 1 ];
443 #endif
444 if ( aParam->second.eType != eFilledExternally )
445 continue;
447 // check which of the parameters have already been visited (e.g. filled via XParameters)
448 size_t nAlreadyVisited = 0;
449 for ( ::std::vector< sal_Int32 >::iterator aIndex = aParam->second.aInnerIndexes.begin();
450 aIndex != aParam->second.aInnerIndexes.end();
451 ++aIndex
454 if ( ( m_aParametersVisited.size() > (size_t)*aIndex ) && m_aParametersVisited[ *aIndex ] )
455 { // exclude this index
456 *aIndex = -1;
457 ++nAlreadyVisited;
460 if ( nAlreadyVisited == aParam->second.aInnerIndexes.size() )
461 continue;
463 // need a wrapper for this .... the "inner parameters" as supplied by a result set don't have a "Value"
464 // property, but the parameter listeners expect such a property. So we need an object "aggregating"
465 // xParam and supplying an additional property ("Value")
466 // (it's no real aggregation of course ...)
467 m_pOuterParameters->push_back( new param::ParameterWrapper( aParam->second.xComposerColumn, m_xInnerParamUpdate, aParam->second.aInnerIndexes ) );
470 #if OSL_DEBUG_LEVEL > 0
471 OSL_ENSURE( ( nSmallestIndexLinkedByColumnName == -1 ) || ( nLargestIndexNotLinkedByColumnName == -1 ) ||
472 ( nSmallestIndexLinkedByColumnName > nLargestIndexNotLinkedByColumnName ),
473 "ParameterManager::createOuterParameters: inconsistency!" );
475 // for the master-detail links, where the detail field denoted a column name, we created an additional ("artificial")
476 // filter, and *appended* it to all other (potentially) existing filters of the row set. This means that the indexes
477 // for the parameters resulting from the artificial filter should be larger than any other parameter index, and this
478 // is what the assertion checks.
479 // If the assertion fails, then we would need another handling for the "parameters visited" flags, since they're based
480 // on parameter indexes *without* the artificial filter (because this filter is not visible from the outside).
481 #endif
485 void ParameterManager::updateParameterInfo( FilterManager& _rFilterManager )
487 OSL_PRECOND( isAlive(), "ParameterManager::updateParameterInfo: not initialized, or already disposed!" );
488 if ( !isAlive() )
489 return;
491 clearAllParameterInformation();
492 cacheConnectionInfo();
494 // check whether the is based on a statement/query which requires parameters
495 Reference< XPropertySet > xProp = m_xComponent;
496 OSL_ENSURE(xProp.is(),"Some already released my component!");
497 if ( xProp.is() )
499 if ( !initializeComposerByComponent( xProp ) )
500 { // okay, nothing to do
501 m_bUpToDate = true;
502 return;
503 } // if ( !initializeComposerByComponent( m_xComponent ) )
505 SAL_WARN_IF( !m_xInnerParamColumns.is(),
506 "connectivity.commontools",
507 "ParameterManager::updateParameterInfo: initializeComposerByComponent did nonsense (1)!" );
509 // collect all parameters which are defined by the "inner parameters"
510 collectInnerParameters( false );
512 // analyze the master-detail relationships
513 bool bColumnsInLinkDetails = false;
514 analyzeFieldLinks( _rFilterManager, bColumnsInLinkDetails );
516 if ( bColumnsInLinkDetails )
518 // okay, in this case, analyzeFieldLinks modified the "real" filter at the RowSet, to contain
519 // an additional restriction (which we created ourself)
520 // So we need to update all information about our inner parameter columns
521 Reference< XPropertySet > xDirectRowSetProps;
522 m_xAggregatedRowSet->queryAggregation( ::getCppuType( &xDirectRowSetProps ) ) >>= xDirectRowSetProps;
523 OSL_VERIFY( initializeComposerByComponent( xDirectRowSetProps ) );
524 collectInnerParameters( true );
527 if ( !m_nInnerCount )
528 { // no parameters at all
529 m_bUpToDate = true;
530 return;
533 // for what now remains as outer parameters, create the wrappers for the single
534 // parameter columns
535 createOuterParameters();
537 m_bUpToDate = true;
541 void ParameterManager::fillLinkedParameters( const Reference< XNameAccess >& _rxParentColumns )
543 OSL_PRECOND( isAlive(), "ParameterManager::fillLinkedParameters: not initialized, or already disposed!" );
544 if ( !isAlive() )
545 return;
546 OSL_PRECOND( m_xInnerParamColumns.is(), "ParameterManager::fillLinkedParameters: no inner parameters found!" );
547 OSL_ENSURE ( _rxParentColumns.is(), "ParameterManager::fillLinkedParameters: invalid parent columns!" );
551 // the master and detail field( name)s of the
552 const OUString* pMasterFields = m_aMasterFields.getConstArray();
553 const OUString* pDetailFields = m_aDetailFields.getConstArray();
555 sal_Int32 nMasterLen = m_aMasterFields.getLength();
557 // loop through all master fields. For each of them, get the respective column from the
558 // parent , and forward its current value as parameter value to the (inner) row set
559 for ( sal_Int32 i = 0; i < nMasterLen; ++i, ++pMasterFields, ++pDetailFields )
561 // does the name denote a valid column in the parent?
562 if ( !_rxParentColumns->hasByName( *pMasterFields ) )
564 SAL_WARN( "connectivity.commontools", "ParameterManager::fillLinkedParameters: invalid master names should have been stripped long before!" );
565 continue;
568 // do we, for this name, know where to place the values?
569 ParameterInformation::const_iterator aParamInfo = m_aParameterInformation.find( *pDetailFields );
570 if ( ( aParamInfo == m_aParameterInformation.end() )
571 || ( aParamInfo->second.aInnerIndexes.empty() )
574 SAL_WARN( "connectivity.commontools", "ParameterManager::fillLinkedParameters: nothing known about this detail field!" );
575 continue;
578 // the concrete master field
579 Reference< XPropertySet > xMasterField(_rxParentColumns->getByName( *pMasterFields ),UNO_QUERY);
581 // the positions where we have to fill in values for the current parameter name
582 for ( ::std::vector< sal_Int32 >::const_iterator aPosition = aParamInfo->second.aInnerIndexes.begin();
583 aPosition != aParamInfo->second.aInnerIndexes.end();
584 ++aPosition
587 // the concrete detail field
588 Reference< XPropertySet > xDetailField(m_xInnerParamColumns->getByIndex( *aPosition ),UNO_QUERY);
589 OSL_ENSURE( xDetailField.is(), "ParameterManager::fillLinkedParameters: invalid detail field!" );
590 if ( !xDetailField.is() )
591 continue;
593 // type and scale of the parameter field
594 sal_Int32 nParamType = DataType::VARCHAR;
595 OSL_VERIFY( xDetailField->getPropertyValue( OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE) ) >>= nParamType );
597 sal_Int32 nScale = 0;
598 if ( xDetailField->getPropertySetInfo()->hasPropertyByName( OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_SCALE) ) )
599 OSL_VERIFY( xDetailField->getPropertyValue( OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_SCALE) ) >>= nScale );
601 // transfer the param value
604 m_xInnerParamUpdate->setObjectWithInfo(
605 *aPosition + 1, // parameters are based at 1
606 xMasterField->getPropertyValue( OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_VALUE) ),
607 nParamType,
608 nScale
611 catch( const Exception& )
613 DBG_UNHANDLED_EXCEPTION();
614 SAL_WARN( "connectivity.commontools", "ParameterManager::fillLinkedParameters: master-detail parameter number " <<
615 sal_Int32( *aPosition + 1 ) << " could not be filled!" );
620 catch( const Exception& )
622 DBG_UNHANDLED_EXCEPTION();
627 bool ParameterManager::completeParameters( const Reference< XInteractionHandler >& _rxCompletionHandler, const Reference< XConnection > _rxConnection )
629 OSL_PRECOND( isAlive(), "ParameterManager::completeParameters: not initialized, or already disposed!" );
630 OSL_ENSURE ( _rxCompletionHandler.is(), "ParameterManager::completeParameters: invalid interaction handler!" );
632 // two continuations (Ok and Cancel)
633 OInteractionAbort* pAbort = new OInteractionAbort;
634 OParameterContinuation* pParams = new OParameterContinuation;
636 // the request
637 ParametersRequest aRequest;
638 aRequest.Parameters = m_pOuterParameters.get();
639 aRequest.Connection = _rxConnection;
640 OInteractionRequest* pRequest = new OInteractionRequest( makeAny( aRequest ) );
641 Reference< XInteractionRequest > xRequest( pRequest );
643 // some knittings
644 pRequest->addContinuation( pAbort );
645 pRequest->addContinuation( pParams );
647 // execute the request
650 _rxCompletionHandler->handle( xRequest );
652 catch( const Exception& )
654 SAL_WARN( "connectivity.commontools", "ParameterManager::completeParameters: caught an exception while calling the handler!" );
657 if ( !pParams->wasSelected() )
658 // canceled by the user (i.e. (s)he canceled the dialog)
659 return false;
663 // transfer the values from the continuation object to the parameter columns
664 Sequence< PropertyValue > aFinalValues = pParams->getValues();
665 const PropertyValue* pFinalValues = aFinalValues.getConstArray();
666 for ( sal_Int32 i = 0; i < aFinalValues.getLength(); ++i, ++pFinalValues )
668 Reference< XPropertySet > xParamColumn(aRequest.Parameters->getByIndex( i ),UNO_QUERY);
669 if ( xParamColumn.is() )
671 #ifdef DBG_UTIL
672 OUString sName;
673 xParamColumn->getPropertyValue( OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME) ) >>= sName;
674 OSL_ENSURE( sName == pFinalValues->Name, "ParameterManager::completeParameters: inconsistent parameter names!" );
675 #endif
676 xParamColumn->setPropertyValue( OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_VALUE), pFinalValues->Value );
677 // the property sets are wrapper classes, translating the Value property into a call to
678 // the appropriate XParameters interface
682 catch( const Exception& )
684 SAL_WARN( "connectivity.commontools", "ParameterManager::completeParameters: caught an exception while propagating the values!" );
686 return true;
690 bool ParameterManager::consultParameterListeners( ::osl::ResettableMutexGuard& _rClearForNotifies )
692 bool bCanceled = false;
694 sal_Int32 nParamsLeft = m_pOuterParameters->getParameters().size();
695 // TODO: shouldn't we subtract all the parameters which were already visited?
696 if ( nParamsLeft )
698 ::cppu::OInterfaceIteratorHelper aIter( m_aParameterListeners );
699 Reference< XPropertySet > xProp = m_xComponent;
700 OSL_ENSURE(xProp.is(),"Some already released my component!");
701 DatabaseParameterEvent aEvent( xProp.get(), m_pOuterParameters.get() );
703 _rClearForNotifies.clear();
704 while ( aIter.hasMoreElements() && !bCanceled )
705 bCanceled = !static_cast< XDatabaseParameterListener* >( aIter.next() )->approveParameter( aEvent );
706 _rClearForNotifies.reset();
709 return !bCanceled;
713 bool ParameterManager::fillParameterValues( const Reference< XInteractionHandler >& _rxCompletionHandler, ::osl::ResettableMutexGuard& _rClearForNotifies )
715 OSL_PRECOND( isAlive(), "ParameterManager::fillParameterValues: not initialized, or already disposed!" );
716 if ( !isAlive() )
717 return true;
719 if ( m_nInnerCount == 0 )
720 // no parameters at all
721 return true;
723 // fill the parameters from the master-detail relationship
724 Reference< XNameAccess > xParentColumns;
725 if ( getParentColumns( xParentColumns, false ) && xParentColumns->hasElements() && m_aMasterFields.getLength() )
726 fillLinkedParameters( xParentColumns );
728 // let the user (via the interaction handler) fill all remaining parameters
729 Reference< XConnection > xConnection;
730 getConnection( xConnection );
732 if ( _rxCompletionHandler.is() )
733 return completeParameters( _rxCompletionHandler, xConnection );
735 return consultParameterListeners( _rClearForNotifies );
739 bool ParameterManager::getConnection( Reference< XConnection >& /* [out] */ _rxConnection )
741 OSL_PRECOND( isAlive(), "ParameterManager::getConnection: not initialized, or already disposed!" );
742 if ( !isAlive() )
743 return false;
745 _rxConnection.clear();
748 Reference< XPropertySet > xProp = m_xComponent;
749 OSL_ENSURE(xProp.is(),"Some already released my component!");
750 if ( xProp.is() )
751 xProp->getPropertyValue( OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ACTIVE_CONNECTION) ) >>= _rxConnection;
753 catch( const Exception& )
755 SAL_WARN( "connectivity.commontools", "ParameterManager::getConnection: could not retrieve the connection of the !" );
757 return _rxConnection.is();
761 void ParameterManager::cacheConnectionInfo() SAL_THROW(( ))
765 Reference< XConnection > xConnection;
766 getConnection( xConnection );
767 Reference< XDatabaseMetaData > xMeta;
768 if ( xConnection.is() )
769 xMeta = xConnection->getMetaData();
770 if ( xMeta.is() )
772 m_sIdentifierQuoteString = xMeta->getIdentifierQuoteString();
773 m_sSpecialCharacters = xMeta->getExtraNameCharacters();
776 catch( const Exception& )
778 SAL_WARN( "connectivity.commontools", "ParameterManager::cacheConnectionInfo: caught an exception!" );
783 bool ParameterManager::getColumns( Reference< XNameAccess >& /* [out] */ _rxColumns, bool _bFromComposer ) SAL_THROW(( Exception ))
785 _rxColumns.clear();
787 Reference< XColumnsSupplier > xColumnSupp;
788 if ( _bFromComposer )
789 xColumnSupp = xColumnSupp.query( m_xComposer );
790 else
791 xColumnSupp.set( m_xComponent.get(),UNO_QUERY);
792 if ( xColumnSupp.is() )
793 _rxColumns = xColumnSupp->getColumns();
794 OSL_ENSURE( _rxColumns.is(), "ParameterManager::getColumns: could not retrieve the columns for the detail !" );
796 return _rxColumns.is();
800 bool ParameterManager::getParentColumns( Reference< XNameAccess >& /* [out] */ _out_rxParentColumns, bool _bFromComposer )
802 OSL_PRECOND( isAlive(), "ParameterManager::getParentColumns: not initialized, or already disposed!" );
804 _out_rxParentColumns.clear();
807 // get the parent of the component we're working for
808 Reference< XChild > xAsChild( m_xComponent.get(), UNO_QUERY_THROW );
809 Reference< XPropertySet > xParent( xAsChild->getParent(), UNO_QUERY );
810 if ( !xParent.is() )
811 return false;
813 // the columns supplier: either from a composer, or directly from the
814 Reference< XColumnsSupplier > xParentColSupp;
815 if ( _bFromComposer )
817 // re-create the parent composer all the time. Else, we'd have to bother with
818 // being a listener at its properties, its loaded state, and event the parent-relationship.
819 m_xParentComposer.reset(
820 getCurrentSettingsComposer( xParent, m_xContext ),
821 SharedQueryComposer::TakeOwnership
823 xParentColSupp = xParentColSupp.query( m_xParentComposer );
825 else
826 xParentColSupp = xParentColSupp.query( xParent );
828 // get the columns of the parent
829 if ( xParentColSupp.is() )
830 _out_rxParentColumns = xParentColSupp->getColumns();
832 catch( const Exception& )
834 SAL_WARN( "connectivity.commontools", "ParameterManager::getParentColumns: caught an exception!" );
836 return _out_rxParentColumns.is();
840 void ParameterManager::addParameterListener( const Reference< XDatabaseParameterListener >& _rxListener )
842 if ( _rxListener.is() )
843 m_aParameterListeners.addInterface( _rxListener );
847 void ParameterManager::removeParameterListener( const Reference< XDatabaseParameterListener >& _rxListener )
849 m_aParameterListeners.removeInterface( _rxListener );
853 void ParameterManager::resetParameterValues( ) SAL_THROW(())
855 OSL_PRECOND( isAlive(), "ParameterManager::resetParameterValues: not initialized, or already disposed!" );
856 if ( !isAlive() )
857 return;
859 if ( !m_nInnerCount )
860 // no parameters at all
861 return;
865 Reference< XNameAccess > xColumns;
866 if ( !getColumns( xColumns, false ) )
867 // already asserted in getColumns
868 return;
870 Reference< XNameAccess > xParentColumns;
871 if ( !getParentColumns( xParentColumns, false ) )
872 return;
874 // loop through all links pairs
875 const OUString* pMasterFields = m_aMasterFields.getConstArray();
876 const OUString* pDetailFields = m_aDetailFields.getConstArray();
878 Reference< XPropertySet > xMasterField;
879 Reference< XPropertySet > xDetailField;
881 // now really ....
882 const OUString* pDetailFieldsEnd = pDetailFields + m_aDetailFields.getLength();
883 for ( ; pDetailFields < pDetailFieldsEnd; ++pDetailFields, ++pMasterFields )
885 if ( !xParentColumns->hasByName( *pMasterFields ) )
887 // if this name is unknown in the parent columns, then we don't have a source
888 // for copying the value to the detail columns
889 SAL_WARN( "connectivity.commontools", "ParameterManager::resetParameterValues: this should have been stripped long before!" );
890 continue;
893 // for all inner parameters which are bound to the name as specified by the
894 // slave element of the link, propagate the value from the master column to this
895 // parameter column
896 ParameterInformation::const_iterator aParamInfo = m_aParameterInformation.find( *pDetailFields );
897 if ( ( aParamInfo == m_aParameterInformation.end() )
898 || ( aParamInfo->second.aInnerIndexes.empty() )
901 SAL_WARN( "connectivity.commontools", "ParameterManager::resetParameterValues: nothing known about this detail field!" );
902 continue;
905 xParentColumns->getByName( *pMasterFields ) >>= xMasterField;
906 if ( !xMasterField.is() )
907 continue;
909 for ( ::std::vector< sal_Int32 >::const_iterator aPosition = aParamInfo->second.aInnerIndexes.begin();
910 aPosition != aParamInfo->second.aInnerIndexes.end();
911 ++aPosition
914 Reference< XPropertySet > xInnerParameter;
915 m_xInnerParamColumns->getByIndex( *aPosition ) >>= xInnerParameter;
916 if ( !xInnerParameter.is() )
917 continue;
919 OUString sParamColumnRealName;
920 xInnerParameter->getPropertyValue( OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_REALNAME) ) >>= sParamColumnRealName;
921 if ( xColumns->hasByName( sParamColumnRealName ) )
922 { // our own columns have a column which's name equals the real name of the param column
923 // -> transfer the value property
924 xColumns->getByName( sParamColumnRealName ) >>= xDetailField;
925 if ( xDetailField.is() )
926 xDetailField->setPropertyValue( OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_VALUE), xMasterField->getPropertyValue( OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_VALUE) ) );
931 catch( const Exception& )
933 SAL_WARN( "connectivity.commontools", "ParameterManager::resetParameterValues: caught an exception!" );
939 void ParameterManager::externalParameterVisited( sal_Int32 _nIndex )
941 if ( m_aParametersVisited.size() < (size_t)_nIndex )
943 m_aParametersVisited.reserve( _nIndex );
944 for ( sal_Int32 i = m_aParametersVisited.size(); i < _nIndex; ++i )
945 m_aParametersVisited.push_back( false );
947 m_aParametersVisited[ _nIndex - 1 ] = true;
950 #define VISIT_PARAMETER( method ) \
951 ::osl::MutexGuard aGuard( m_rMutex ); \
952 OSL_ENSURE( m_xInnerParamUpdate.is(), "ParameterManager::XParameters::setXXX: no XParameters access to the RowSet!" ); \
953 if ( !m_xInnerParamUpdate.is() ) \
954 return; \
955 m_xInnerParamUpdate->method; \
956 externalParameterVisited( _nIndex ) \
959 void ParameterManager::setNull( sal_Int32 _nIndex, sal_Int32 sqlType )
961 VISIT_PARAMETER( setNull( _nIndex, sqlType ) );
965 void ParameterManager::setObjectNull( sal_Int32 _nIndex, sal_Int32 sqlType, const OUString& typeName )
967 VISIT_PARAMETER( setObjectNull( _nIndex, sqlType, typeName ) );
971 void ParameterManager::setBoolean( sal_Int32 _nIndex, bool x )
973 VISIT_PARAMETER( setBoolean( _nIndex, x ) );
977 void ParameterManager::setByte( sal_Int32 _nIndex, sal_Int8 x )
979 VISIT_PARAMETER( setByte( _nIndex, x ) );
983 void ParameterManager::setShort( sal_Int32 _nIndex, sal_Int16 x )
985 VISIT_PARAMETER( setShort( _nIndex, x ) );
989 void ParameterManager::setInt( sal_Int32 _nIndex, sal_Int32 x )
991 VISIT_PARAMETER( setInt( _nIndex, x ) );
995 void ParameterManager::setLong( sal_Int32 _nIndex, sal_Int64 x )
997 VISIT_PARAMETER( setLong( _nIndex, x ) );
1001 void ParameterManager::setFloat( sal_Int32 _nIndex, float x )
1003 VISIT_PARAMETER( setFloat( _nIndex, x ) );
1007 void ParameterManager::setDouble( sal_Int32 _nIndex, double x )
1009 VISIT_PARAMETER( setDouble( _nIndex, x ) );
1013 void ParameterManager::setString( sal_Int32 _nIndex, const OUString& x )
1015 VISIT_PARAMETER( setString( _nIndex, x ) );
1019 void ParameterManager::setBytes( sal_Int32 _nIndex, const ::com::sun::star::uno::Sequence< sal_Int8 >& x )
1021 VISIT_PARAMETER( setBytes( _nIndex, x ) );
1025 void ParameterManager::setDate( sal_Int32 _nIndex, const ::com::sun::star::util::Date& x )
1027 VISIT_PARAMETER( setDate( _nIndex, x ) );
1031 void ParameterManager::setTime( sal_Int32 _nIndex, const ::com::sun::star::util::Time& x )
1033 VISIT_PARAMETER( setTime( _nIndex, x ) );
1037 void ParameterManager::setTimestamp( sal_Int32 _nIndex, const ::com::sun::star::util::DateTime& x )
1039 VISIT_PARAMETER( setTimestamp( _nIndex, x ) );
1043 void ParameterManager::setBinaryStream( sal_Int32 _nIndex, const ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream>& x, sal_Int32 length )
1045 VISIT_PARAMETER( setBinaryStream( _nIndex, x, length ) );
1049 void ParameterManager::setCharacterStream( sal_Int32 _nIndex, const ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream>& x, sal_Int32 length )
1051 VISIT_PARAMETER( setCharacterStream( _nIndex, x, length ) );
1055 void ParameterManager::setObject( sal_Int32 _nIndex, const ::com::sun::star::uno::Any& x )
1057 VISIT_PARAMETER( setObject( _nIndex, x ) );
1061 void ParameterManager::setObjectWithInfo( sal_Int32 _nIndex, const ::com::sun::star::uno::Any& x, sal_Int32 targetSqlType, sal_Int32 scale )
1063 VISIT_PARAMETER( setObjectWithInfo( _nIndex, x, targetSqlType, scale ) );
1067 void ParameterManager::setRef( sal_Int32 _nIndex, const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XRef>& x )
1069 VISIT_PARAMETER( setRef( _nIndex, x ) );
1073 void ParameterManager::setBlob( sal_Int32 _nIndex, const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XBlob>& x )
1075 VISIT_PARAMETER( setBlob( _nIndex, x ) );
1079 void ParameterManager::setClob( sal_Int32 _nIndex, const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XClob>& x )
1081 VISIT_PARAMETER( setClob( _nIndex, x ) );
1085 void ParameterManager::setArray( sal_Int32 _nIndex, const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XArray>& x )
1087 VISIT_PARAMETER( setArray( _nIndex, x ) );
1091 void ParameterManager::clearParameters( )
1093 if ( m_xInnerParamUpdate.is() )
1094 m_xInnerParamUpdate->clearParameters( );
1098 //= OParameterContinuation
1101 void SAL_CALL OParameterContinuation::setParameters( const Sequence< PropertyValue >& _rValues ) throw( RuntimeException, std::exception )
1103 m_aValues = _rValues;
1107 } // namespace frm
1110 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */