1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: filtermanager.cxx,v $
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_connectivity.hxx"
33 #include "connectivity/filtermanager.hxx"
35 /** === begin UNO includes === **/
36 #include <com/sun/star/sdb/XSQLQueryComposerFactory.hpp>
37 /** === end UNO includes === **/
38 #include "TConnection.hxx"
39 #include <osl/diagnose.h>
40 #include "connectivity/dbtools.hxx"
42 //........................................................................
45 //........................................................................
47 using namespace ::com::sun::star::uno
;
48 using namespace ::com::sun::star::sdbc
;
49 using namespace ::com::sun::star::sdb
;
50 using namespace ::com::sun::star::lang
;
51 using namespace ::com::sun::star::beans
;
52 using namespace connectivity
;
54 //====================================================================
56 //====================================================================
57 //--------------------------------------------------------------------
58 FilterManager::FilterManager( const Reference
< XMultiServiceFactory
>& _rxORB
)
60 ,m_aFilterComponents( FC_COMPONENT_COUNT
)
61 ,m_bApplyPublicFilter( true )
65 //--------------------------------------------------------------------
66 void FilterManager::initialize( const Reference
< XPropertySet
>& _rxComponentAggregate
)
68 m_xComponentAggregate
= _rxComponentAggregate
;
69 OSL_ENSURE( m_xComponentAggregate
.is(), "FilterManager::initialize: invalid arguments!" );
71 if ( m_xComponentAggregate
.is() )
72 m_xComponentAggregate
->setPropertyValue( OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_APPLYFILTER
), makeAny( (sal_Bool
)sal_True
) );
75 //--------------------------------------------------------------------
76 void FilterManager::dispose( )
78 m_xComponentAggregate
.clear();
81 //--------------------------------------------------------------------
82 const ::rtl::OUString
& FilterManager::getFilterComponent( FilterComponent _eWhich
) const
84 return m_aFilterComponents
[ _eWhich
];
87 //--------------------------------------------------------------------
88 void FilterManager::setFilterComponent( FilterComponent _eWhich
, const ::rtl::OUString
& _rComponent
)
90 m_aFilterComponents
[ _eWhich
] = _rComponent
;
93 if ( m_xComponentAggregate
.is() && (( _eWhich
!= fcPublicFilter
) || m_bApplyPublicFilter
) )
94 m_xComponentAggregate
->setPropertyValue( OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_FILTER
), makeAny( getComposedFilter() ) );
96 catch( const Exception
& )
98 OSL_ENSURE( sal_False
, "FilterManager::setFilterComponent: setting the filter failed!" );
102 //--------------------------------------------------------------------
103 void FilterManager::setApplyPublicFilter( sal_Bool _bApply
)
105 if ( m_bApplyPublicFilter
== _bApply
)
108 m_bApplyPublicFilter
= _bApply
;
112 if ( m_xComponentAggregate
.is() && getFilterComponent( fcPublicFilter
).getLength() )
113 { // only if there changed something
114 m_xComponentAggregate
->setPropertyValue( OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_FILTER
), makeAny( getComposedFilter() ) );
117 catch( const Exception
& )
119 OSL_ENSURE( sal_False
, "FilterManager::setApplyPublicFilter: setting the filter failed!" );
123 //--------------------------------------------------------------------
126 void lcl_ensureBracketed( ::rtl::OUString
& /* [inout] */ _rExpression
)
128 OSL_ENSURE( _rExpression
.getLength(), "lcl_ensureBracketed: expression is empty!" );
129 if ( _rExpression
.getLength() )
131 if ( ( _rExpression
.getStr()[0] != '(' ) || ( _rExpression
.getStr()[ _rExpression
.getLength() - 1 ] != ')' ) )
133 ::rtl::OUString
sComposed( RTL_CONSTASCII_USTRINGPARAM( "(" ) );
134 sComposed
+= _rExpression
;
135 sComposed
+= ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ")" ) );
136 _rExpression
= sComposed
;
141 //--------------------------------------------------------------------
142 void FilterManager::appendFilterComponent( ::rtl::OUString
& /* [inout] */ _rAppendTo
, const ::rtl::OUString
& _rComponent
) const
144 if ( _rAppendTo
.getLength() )
145 _rAppendTo
+= ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( " AND " ) );
147 ::rtl::OUString
sComponent( _rComponent
);
148 lcl_ensureBracketed( sComponent
);
149 _rAppendTo
+= sComponent
;
152 //--------------------------------------------------------------------
153 bool FilterManager::isThereAtMostOneComponent( ::rtl::OUString
& _rOnlyComponent
) const
155 sal_Int32 nOnlyNonEmpty
= -1;
157 for ( i
= getFirstApplicableFilterIndex(); i
< FC_COMPONENT_COUNT
; ++i
)
159 if ( m_aFilterComponents
[ i
].getLength() )
161 if ( nOnlyNonEmpty
!= -1 )
162 // it's the second non-empty component
168 if ( nOnlyNonEmpty
== -1 )
170 _rOnlyComponent
= ::rtl::OUString();
174 if ( i
== FC_COMPONENT_COUNT
)
176 // we found only one non-empty filter component
177 _rOnlyComponent
= m_aFilterComponents
[ nOnlyNonEmpty
];
183 //--------------------------------------------------------------------
184 ::rtl::OUString
FilterManager::getComposedFilter( ) const
186 ::rtl::OUString sComposedFilter
;
188 // if we have only one non-empty component, then there's no need to compose anything
189 if ( isThereAtMostOneComponent( sComposedFilter
) )
190 return sComposedFilter
;
192 // append the single components
193 for ( sal_Int32 i
= getFirstApplicableFilterIndex(); i
< FC_COMPONENT_COUNT
; ++i
)
194 appendFilterComponent( sComposedFilter
, m_aFilterComponents
[ i
] );
196 return sComposedFilter
;
199 //........................................................................
200 } // namespace dbtools
201 //........................................................................