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 <tools/diagnose_ex.h>
26 #include <rtl/ustrbuf.hxx>
33 using namespace ::com::sun::star::uno
;
34 using namespace ::com::sun::star::sdbc
;
35 using namespace ::com::sun::star::sdb
;
36 using namespace ::com::sun::star::lang
;
37 using namespace ::com::sun::star::beans
;
38 using namespace connectivity
;
40 FilterManager::FilterManager( )
41 :m_bApplyPublicFilter( true )
46 void FilterManager::initialize( const Reference
< XPropertySet
>& _rxComponentAggregate
)
48 m_xComponentAggregate
= _rxComponentAggregate
;
49 OSL_ENSURE( m_xComponentAggregate
.is(), "FilterManager::initialize: invalid arguments!" );
51 if ( m_xComponentAggregate
.is() )
52 m_xComponentAggregate
->setPropertyValue( OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_APPLYFILTER
), makeAny( true ) );
56 void FilterManager::dispose( )
58 m_xComponentAggregate
.clear();
62 const OUString
& FilterManager::getFilterComponent( FilterComponent _eWhich
) const
66 case FilterComponent::PublicFilter
:
67 return m_aPublicFilterComponent
;
68 case FilterComponent::PublicHaving
:
69 return m_aPublicHavingComponent
;
70 case FilterComponent::LinkFilter
:
71 return m_aLinkFilterComponent
;
72 case FilterComponent::LinkHaving
:
73 return m_aLinkHavingComponent
;
77 static const OUString
sErr("#FilterManager::getFilterComponent unknown component#");
82 void FilterManager::setFilterComponent( FilterComponent _eWhich
, const OUString
& _rComponent
)
86 case FilterComponent::PublicFilter
:
87 m_aPublicFilterComponent
= _rComponent
;
89 case FilterComponent::PublicHaving
:
90 m_aPublicHavingComponent
= _rComponent
;
92 case FilterComponent::LinkFilter
:
93 m_aLinkFilterComponent
= _rComponent
;
95 case FilterComponent::LinkHaving
:
96 m_aLinkHavingComponent
= _rComponent
;
101 if ( m_xComponentAggregate
.is() )
103 bool propagate(true);
106 case FilterComponent::PublicFilter
:
107 propagate
= propagate
&& m_bApplyPublicFilter
;
109 case FilterComponent::LinkFilter
:
111 m_xComponentAggregate
->setPropertyValue( OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_FILTER
), makeAny( getComposedFilter() ) );
113 case FilterComponent::PublicHaving
:
114 propagate
= propagate
&& m_bApplyPublicFilter
;
116 case FilterComponent::LinkHaving
:
118 m_xComponentAggregate
->setPropertyValue( OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_HAVINGCLAUSE
), makeAny( getComposedHaving() ) );
123 catch( const Exception
& )
125 DBG_UNHANDLED_EXCEPTION("connectivity.commontools");
130 void FilterManager::setApplyPublicFilter( bool _bApply
)
132 if ( m_bApplyPublicFilter
== _bApply
)
135 m_bApplyPublicFilter
= _bApply
;
139 if ( m_xComponentAggregate
.is())
141 // only where/if something changed
142 if (!getFilterComponent( FilterComponent::PublicFilter
).isEmpty())
143 m_xComponentAggregate
->setPropertyValue( OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_FILTER
), makeAny( getComposedFilter() ) );
144 if (!getFilterComponent( FilterComponent::PublicHaving
).isEmpty())
145 m_xComponentAggregate
->setPropertyValue( OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_HAVINGCLAUSE
), makeAny( getComposedHaving() ) );
148 catch( const Exception
& )
150 DBG_UNHANDLED_EXCEPTION("connectivity.commontools");
155 void FilterManager::appendFilterComponent( OUStringBuffer
& io_appendTo
, const OUString
& i_component
)
157 if ( !io_appendTo
.isEmpty() )
159 io_appendTo
.insert( 0, '(' );
160 io_appendTo
.insert( 1, ' ' );
161 io_appendTo
.append( " ) AND " );
164 io_appendTo
.append( "( " );
165 io_appendTo
.append( i_component
);
166 io_appendTo
.append( " )" );
170 bool FilterManager::isThereAtMostOneFilterComponent( OUString
& o_singleComponent
) const
172 if (m_bApplyPublicFilter
) {
173 if (!m_aPublicFilterComponent
.isEmpty() && !m_aLinkFilterComponent
.isEmpty())
175 if (!m_aPublicFilterComponent
.isEmpty())
176 o_singleComponent
= m_aPublicFilterComponent
;
177 else if (!m_aLinkFilterComponent
.isEmpty())
178 o_singleComponent
= m_aLinkFilterComponent
;
180 o_singleComponent
.clear();
185 if (m_aLinkFilterComponent
.isEmpty())
186 o_singleComponent
.clear();
188 o_singleComponent
= m_aLinkFilterComponent
;
193 bool FilterManager::isThereAtMostOneHavingComponent( OUString
& o_singleComponent
) const
195 if (m_bApplyPublicFilter
) {
196 if (!m_aPublicHavingComponent
.isEmpty() && !m_aLinkHavingComponent
.isEmpty())
198 if (!m_aPublicHavingComponent
.isEmpty())
199 o_singleComponent
= m_aPublicHavingComponent
;
200 else if (!m_aLinkHavingComponent
.isEmpty())
201 o_singleComponent
= m_aLinkHavingComponent
;
203 o_singleComponent
.clear();
208 if (m_aLinkHavingComponent
.isEmpty())
209 o_singleComponent
.clear();
211 o_singleComponent
= m_aLinkHavingComponent
;
217 OUString
FilterManager::getComposedFilter( ) const
219 // if we have only one non-empty component, then there's no need to compose anything
220 OUString singleComponent
;
221 if ( isThereAtMostOneFilterComponent( singleComponent
) )
223 return singleComponent
;
225 // append the single components
226 OUStringBuffer
aComposedFilter(singleComponent
);
227 if (m_bApplyPublicFilter
)
228 appendFilterComponent( aComposedFilter
, m_aPublicFilterComponent
);
229 appendFilterComponent( aComposedFilter
, m_aLinkFilterComponent
);
230 return aComposedFilter
.makeStringAndClear();
234 OUString
FilterManager::getComposedHaving( ) const
236 // if we have only one non-empty component, then there's no need to compose anything
237 OUString singleComponent
;
238 if ( isThereAtMostOneHavingComponent( singleComponent
) )
240 return singleComponent
;
242 // append the single components
243 OUStringBuffer
aComposedFilter(singleComponent
);
244 if (m_bApplyPublicFilter
)
245 appendFilterComponent( aComposedFilter
, m_aPublicHavingComponent
);
246 appendFilterComponent( aComposedFilter
, m_aLinkHavingComponent
);
247 return aComposedFilter
.makeStringAndClear();
251 } // namespace dbtools
254 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */