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