1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 // MARKER(update_precomp.py): autogen include statement, do not remove 25 #include "precompiled_connectivity.hxx" 26 #include "connectivity/filtermanager.hxx" 27 28 /** === begin UNO includes === **/ 29 #include <com/sun/star/sdb/XSQLQueryComposerFactory.hpp> 30 /** === end UNO includes === **/ 31 #include "TConnection.hxx" 32 #include <osl/diagnose.h> 33 #include "connectivity/dbtools.hxx" 34 #include <tools/diagnose_ex.h> 35 #include <rtl/ustrbuf.hxx> 36 37 //........................................................................ 38 namespace dbtools 39 { 40 //........................................................................ 41 42 using namespace ::com::sun::star::uno; 43 using namespace ::com::sun::star::sdbc; 44 using namespace ::com::sun::star::sdb; 45 using namespace ::com::sun::star::lang; 46 using namespace ::com::sun::star::beans; 47 using namespace connectivity; 48 49 //==================================================================== 50 //= FilterManager 51 //==================================================================== 52 //-------------------------------------------------------------------- FilterManager(const Reference<XMultiServiceFactory> & _rxORB)53 FilterManager::FilterManager( const Reference< XMultiServiceFactory >& _rxORB ) 54 :m_xORB( _rxORB ) 55 ,m_aFilterComponents( FC_COMPONENT_COUNT ) 56 ,m_bApplyPublicFilter( true ) 57 { 58 } 59 60 //-------------------------------------------------------------------- initialize(const Reference<XPropertySet> & _rxComponentAggregate)61 void FilterManager::initialize( const Reference< XPropertySet >& _rxComponentAggregate ) 62 { 63 m_xComponentAggregate = _rxComponentAggregate; 64 OSL_ENSURE( m_xComponentAggregate.is(), "FilterManager::initialize: invalid arguments!" ); 65 66 if ( m_xComponentAggregate.is() ) 67 m_xComponentAggregate->setPropertyValue( OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_APPLYFILTER), makeAny( (sal_Bool)sal_True ) ); 68 } 69 70 //-------------------------------------------------------------------- dispose()71 void FilterManager::dispose( ) 72 { 73 m_xComponentAggregate.clear(); 74 } 75 76 //-------------------------------------------------------------------- getFilterComponent(FilterComponent _eWhich) const77 const ::rtl::OUString& FilterManager::getFilterComponent( FilterComponent _eWhich ) const 78 { 79 return m_aFilterComponents[ _eWhich ]; 80 } 81 82 //-------------------------------------------------------------------- setFilterComponent(FilterComponent _eWhich,const::rtl::OUString & _rComponent)83 void FilterManager::setFilterComponent( FilterComponent _eWhich, const ::rtl::OUString& _rComponent ) 84 { 85 m_aFilterComponents[ _eWhich ] = _rComponent; 86 try 87 { 88 if ( m_xComponentAggregate.is() && (( _eWhich != fcPublicFilter ) || m_bApplyPublicFilter ) ) 89 m_xComponentAggregate->setPropertyValue( OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_FILTER), makeAny( getComposedFilter() ) ); 90 } 91 catch( const Exception& ) 92 { 93 DBG_UNHANDLED_EXCEPTION(); 94 } 95 } 96 97 //-------------------------------------------------------------------- setApplyPublicFilter(sal_Bool _bApply)98 void FilterManager::setApplyPublicFilter( sal_Bool _bApply ) 99 { 100 if ( m_bApplyPublicFilter == _bApply ) 101 return; 102 103 m_bApplyPublicFilter = _bApply; 104 105 try 106 { 107 if ( m_xComponentAggregate.is() && getFilterComponent( fcPublicFilter ).getLength() ) 108 { // only if there changed something 109 m_xComponentAggregate->setPropertyValue( OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_FILTER), makeAny( getComposedFilter() ) ); 110 } 111 } 112 catch( const Exception& ) 113 { 114 DBG_UNHANDLED_EXCEPTION(); 115 } 116 } 117 118 //-------------------------------------------------------------------- appendFilterComponent(::rtl::OUStringBuffer & io_appendTo,const::rtl::OUString & i_component) const119 void FilterManager::appendFilterComponent( ::rtl::OUStringBuffer& io_appendTo, const ::rtl::OUString& i_component ) const 120 { 121 if ( io_appendTo.getLength() > 0 ) 122 { 123 io_appendTo.insert( 0, sal_Unicode( '(' ) ); 124 io_appendTo.insert( 1, sal_Unicode( ' ' ) ); 125 io_appendTo.appendAscii( " ) AND " ); 126 } 127 128 io_appendTo.appendAscii( "( " ); 129 io_appendTo.append( i_component ); 130 io_appendTo.appendAscii( " )" ); 131 } 132 133 //-------------------------------------------------------------------- isThereAtMostOneComponent(::rtl::OUStringBuffer & o_singleComponent) const134 bool FilterManager::isThereAtMostOneComponent( ::rtl::OUStringBuffer& o_singleComponent ) const 135 { 136 sal_Int32 nOnlyNonEmpty = -1; 137 sal_Int32 i; 138 for ( i = getFirstApplicableFilterIndex(); i < FC_COMPONENT_COUNT; ++i ) 139 { 140 if ( m_aFilterComponents[ i ].getLength() ) 141 { 142 if ( nOnlyNonEmpty != -1 ) 143 // it's the second non-empty component 144 break; 145 else 146 nOnlyNonEmpty = i; 147 } 148 } 149 if ( nOnlyNonEmpty == -1 ) 150 { 151 o_singleComponent.makeStringAndClear(); 152 return true; 153 } 154 155 if ( i == FC_COMPONENT_COUNT ) 156 { 157 // we found only one non-empty filter component 158 o_singleComponent = m_aFilterComponents[ nOnlyNonEmpty ]; 159 return true; 160 } 161 return false; 162 } 163 164 //-------------------------------------------------------------------- getComposedFilter() const165 ::rtl::OUString FilterManager::getComposedFilter( ) const 166 { 167 ::rtl::OUStringBuffer aComposedFilter; 168 169 // if we have only one non-empty component, then there's no need to compose anything 170 if ( !isThereAtMostOneComponent( aComposedFilter ) ) 171 { 172 // append the single components 173 for ( sal_Int32 i = getFirstApplicableFilterIndex(); i < FC_COMPONENT_COUNT; ++i ) 174 appendFilterComponent( aComposedFilter, m_aFilterComponents[ i ] ); 175 } 176 177 return aComposedFilter.makeStringAndClear(); 178 } 179 180 //........................................................................ 181 } // namespace dbtools 182 //........................................................................ 183