1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/filtermanager.hxx>
22 #include <com/sun/star/sdb/XSQLQueryComposerFactory.hpp>
23 #include "TConnection.hxx"
24 #include <osl/diagnose.h>
25 #include <connectivity/dbtools.hxx>
26 #include <tools/diagnose_ex.h>
27 #include <rtl/ustrbuf.hxx>
34 using namespace ::com::sun::star::uno
;
35 using namespace ::com::sun::star::sdbc
;
36 using namespace ::com::sun::star::sdb
;
37 using namespace ::com::sun::star::lang
;
38 using namespace ::com::sun::star::beans
;
39 using namespace connectivity
;
41 FilterManager::FilterManager( )
42 :m_aFilterComponents( FC_COMPONENT_COUNT
)
43 ,m_bApplyPublicFilter( true )
48 void FilterManager::initialize( const Reference
< XPropertySet
>& _rxComponentAggregate
)
50 m_xComponentAggregate
= _rxComponentAggregate
;
51 OSL_ENSURE( m_xComponentAggregate
.is(), "FilterManager::initialize: invalid arguments!" );
53 if ( m_xComponentAggregate
.is() )
54 m_xComponentAggregate
->setPropertyValue( OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_APPLYFILTER
), makeAny( true ) );
58 void FilterManager::dispose( )
60 m_xComponentAggregate
.clear();
64 const OUString
& FilterManager::getFilterComponent( FilterComponent _eWhich
) const
66 return m_aFilterComponents
[ _eWhich
];
70 void FilterManager::setFilterComponent( FilterComponent _eWhich
, const OUString
& _rComponent
)
72 m_aFilterComponents
[ _eWhich
] = _rComponent
;
75 if ( m_xComponentAggregate
.is() && (( _eWhich
!= fcPublicFilter
) || m_bApplyPublicFilter
) )
76 m_xComponentAggregate
->setPropertyValue( OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_FILTER
), makeAny( getComposedFilter() ) );
78 catch( const Exception
& )
80 DBG_UNHANDLED_EXCEPTION();
85 void FilterManager::setApplyPublicFilter( bool _bApply
)
87 if ( m_bApplyPublicFilter
== _bApply
)
90 m_bApplyPublicFilter
= _bApply
;
94 if ( m_xComponentAggregate
.is() && !getFilterComponent( fcPublicFilter
).isEmpty() )
95 { // only if there changed something
96 m_xComponentAggregate
->setPropertyValue( OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_FILTER
), makeAny( getComposedFilter() ) );
99 catch( const Exception
& )
101 DBG_UNHANDLED_EXCEPTION();
106 void FilterManager::appendFilterComponent( OUStringBuffer
& io_appendTo
, const OUString
& i_component
)
108 if ( !io_appendTo
.isEmpty() )
110 io_appendTo
.insert( 0, '(' );
111 io_appendTo
.insert( 1, ' ' );
112 io_appendTo
.appendAscii( " ) AND " );
115 io_appendTo
.appendAscii( "( " );
116 io_appendTo
.append( i_component
);
117 io_appendTo
.appendAscii( " )" );
121 bool FilterManager::isThereAtMostOneComponent( OUStringBuffer
& o_singleComponent
) const
123 sal_Int32 nOnlyNonEmpty
= -1;
125 for ( i
= getFirstApplicableFilterIndex(); i
< FC_COMPONENT_COUNT
; ++i
)
127 if ( !m_aFilterComponents
[ i
].isEmpty() )
129 if ( nOnlyNonEmpty
!= -1 )
130 // it's the second non-empty component
136 if ( nOnlyNonEmpty
== -1 )
138 o_singleComponent
.makeStringAndClear();
142 if ( i
== FC_COMPONENT_COUNT
)
144 // we found only one non-empty filter component
145 o_singleComponent
= m_aFilterComponents
[ nOnlyNonEmpty
];
152 OUString
FilterManager::getComposedFilter( ) const
154 OUStringBuffer aComposedFilter
;
156 // if we have only one non-empty component, then there's no need to compose anything
157 if ( !isThereAtMostOneComponent( aComposedFilter
) )
159 // append the single components
160 for ( sal_Int32 i
= getFirstApplicableFilterIndex(); i
< FC_COMPONENT_COUNT
; ++i
)
161 appendFilterComponent( aComposedFilter
, m_aFilterComponents
[ i
] );
164 return aComposedFilter
.makeStringAndClear();
168 } // namespace dbtools
171 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */