bump product version to 6.3.0.0.beta1
[LibreOffice.git] / connectivity / source / commontools / filtermanager.cxx
blob08489cf8024a66008c344828c4948b22f0ccfa5a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
30 namespace dbtools
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_bApplyPublicFilter( true )
47 void FilterManager::initialize( const Reference< XPropertySet >& _rxComponentAggregate )
49 m_xComponentAggregate = _rxComponentAggregate;
50 OSL_ENSURE( m_xComponentAggregate.is(), "FilterManager::initialize: invalid arguments!" );
52 if ( m_xComponentAggregate.is() )
53 m_xComponentAggregate->setPropertyValue( OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_APPLYFILTER), makeAny( true ) );
57 void FilterManager::dispose( )
59 m_xComponentAggregate.clear();
63 const OUString& FilterManager::getFilterComponent( FilterComponent _eWhich ) const
65 switch (_eWhich)
67 case FilterComponent::PublicFilter:
68 return m_aPublicFilterComponent;
69 case FilterComponent::PublicHaving:
70 return m_aPublicHavingComponent;
71 case FilterComponent::LinkFilter:
72 return m_aLinkFilterComponent;
73 case FilterComponent::LinkHaving:
74 return m_aLinkHavingComponent;
76 assert(false);
78 static const OUString sErr("#FilterManager::getFilterComponent unknown component#");
79 return sErr;
83 void FilterManager::setFilterComponent( FilterComponent _eWhich, const OUString& _rComponent )
85 switch (_eWhich)
87 case FilterComponent::PublicFilter:
88 m_aPublicFilterComponent = _rComponent;
89 break;
90 case FilterComponent::PublicHaving:
91 m_aPublicHavingComponent = _rComponent;
92 break;
93 case FilterComponent::LinkFilter:
94 m_aLinkFilterComponent = _rComponent;
95 break;
96 case FilterComponent::LinkHaving:
97 m_aLinkHavingComponent = _rComponent;
98 break;
102 if ( m_xComponentAggregate.is() )
104 bool propagate(true);
105 switch (_eWhich)
107 case FilterComponent::PublicFilter:
108 propagate = propagate && m_bApplyPublicFilter;
109 [[fallthrough]];
110 case FilterComponent::LinkFilter:
111 if (propagate)
112 m_xComponentAggregate->setPropertyValue( OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_FILTER), makeAny( getComposedFilter() ) );
113 break;
114 case FilterComponent::PublicHaving:
115 propagate = propagate && m_bApplyPublicFilter;
116 [[fallthrough]];
117 case FilterComponent::LinkHaving:
118 if (propagate)
119 m_xComponentAggregate->setPropertyValue( OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_HAVINGCLAUSE), makeAny( getComposedHaving() ) );
120 break;
124 catch( const Exception& )
126 DBG_UNHANDLED_EXCEPTION("connectivity.commontools");
131 void FilterManager::setApplyPublicFilter( bool _bApply )
133 if ( m_bApplyPublicFilter == _bApply )
134 return;
136 m_bApplyPublicFilter = _bApply;
140 if ( m_xComponentAggregate.is())
142 // only where/if something changed
143 if (!getFilterComponent( FilterComponent::PublicFilter ).isEmpty())
144 m_xComponentAggregate->setPropertyValue( OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_FILTER), makeAny( getComposedFilter() ) );
145 if (!getFilterComponent( FilterComponent::PublicHaving ).isEmpty())
146 m_xComponentAggregate->setPropertyValue( OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_HAVINGCLAUSE), makeAny( getComposedHaving() ) );
149 catch( const Exception& )
151 DBG_UNHANDLED_EXCEPTION("connectivity.commontools");
156 void FilterManager::appendFilterComponent( OUStringBuffer& io_appendTo, const OUString& i_component )
158 if ( !io_appendTo.isEmpty() )
160 io_appendTo.insert( 0, '(' );
161 io_appendTo.insert( 1, ' ' );
162 io_appendTo.append( " ) AND " );
165 io_appendTo.append( "( " );
166 io_appendTo.append( i_component );
167 io_appendTo.append( " )" );
171 bool FilterManager::isThereAtMostOneFilterComponent( OUString& o_singleComponent ) const
173 if (m_bApplyPublicFilter) {
174 if (!m_aPublicFilterComponent.isEmpty() && !m_aLinkFilterComponent.isEmpty())
175 return false;
176 if (!m_aPublicFilterComponent.isEmpty())
177 o_singleComponent = m_aPublicFilterComponent;
178 else if (!m_aLinkFilterComponent.isEmpty())
179 o_singleComponent = m_aLinkFilterComponent;
180 else
181 o_singleComponent.clear();
182 return true;
184 else
186 if (m_aLinkFilterComponent.isEmpty())
187 o_singleComponent.clear();
188 else
189 o_singleComponent = m_aLinkFilterComponent;
190 return true;
194 bool FilterManager::isThereAtMostOneHavingComponent( OUString& o_singleComponent ) const
196 if (m_bApplyPublicFilter) {
197 if (!m_aPublicHavingComponent.isEmpty() && !m_aLinkHavingComponent.isEmpty())
198 return false;
199 if (!m_aPublicHavingComponent.isEmpty())
200 o_singleComponent = m_aPublicHavingComponent;
201 else if (!m_aLinkHavingComponent.isEmpty())
202 o_singleComponent = m_aLinkHavingComponent;
203 else
204 o_singleComponent.clear();
205 return true;
207 else
209 if (m_aLinkHavingComponent.isEmpty())
210 o_singleComponent.clear();
211 else
212 o_singleComponent = m_aLinkHavingComponent;
213 return true;
218 OUString FilterManager::getComposedFilter( ) const
220 // if we have only one non-empty component, then there's no need to compose anything
221 OUString singleComponent;
222 if ( isThereAtMostOneFilterComponent( singleComponent ) )
224 return singleComponent;
226 // append the single components
227 OUStringBuffer aComposedFilter(singleComponent);
228 if (m_bApplyPublicFilter)
229 appendFilterComponent( aComposedFilter, m_aPublicFilterComponent );
230 appendFilterComponent( aComposedFilter, m_aLinkFilterComponent );
231 return aComposedFilter.makeStringAndClear();
235 OUString FilterManager::getComposedHaving( ) const
237 // if we have only one non-empty component, then there's no need to compose anything
238 OUString singleComponent;
239 if ( isThereAtMostOneHavingComponent( singleComponent ) )
241 return singleComponent;
243 // append the single components
244 OUStringBuffer aComposedFilter(singleComponent);
245 if (m_bApplyPublicFilter)
246 appendFilterComponent( aComposedFilter, m_aPublicHavingComponent );
247 appendFilterComponent( aComposedFilter, m_aLinkHavingComponent );
248 return aComposedFilter.makeStringAndClear();
252 } // namespace dbtools
255 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */