LibreOffice Module connectivity (master) 1
filtermanager.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
21
22#include <com/sun/star/sdb/XSQLQueryComposerFactory.hpp>
23#include <TConnection.hxx>
24#include <osl/diagnose.h>
26#include <rtl/ustrbuf.hxx>
27
28
29namespace dbtools
30{
31
32
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;
39
41 :m_bApplyPublicFilter( true )
42 {
43 }
44
45
46 void FilterManager::initialize( const Reference< XPropertySet >& _rxComponentAggregate )
47 {
48 m_xComponentAggregate = _rxComponentAggregate;
49 OSL_ENSURE( m_xComponentAggregate.is(), "FilterManager::initialize: invalid arguments!" );
50
51 if ( m_xComponentAggregate.is() )
52 m_xComponentAggregate->setPropertyValue( OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_APPLYFILTER), Any( true ) );
53 }
54
55
57 {
59 }
60
61
62 const OUString& FilterManager::getFilterComponent( FilterComponent _eWhich ) const
63 {
64 switch (_eWhich)
65 {
74 }
75 assert(false);
76
77 static const OUString sErr("#FilterManager::getFilterComponent unknown component#");
78 return sErr;
79 }
80
81
82 void FilterManager::setFilterComponent( FilterComponent _eWhich, const OUString& _rComponent )
83 {
84 switch (_eWhich)
85 {
87 m_aPublicFilterComponent = _rComponent;
88 break;
90 m_aPublicHavingComponent = _rComponent;
91 break;
93 m_aLinkFilterComponent = _rComponent;
94 break;
96 m_aLinkHavingComponent = _rComponent;
97 break;
98 }
99 try
100 {
101 if ( m_xComponentAggregate.is() )
102 {
103 bool propagate(true);
104 switch (_eWhich)
105 {
107 propagate = propagate && m_bApplyPublicFilter;
108 [[fallthrough]];
110 if (propagate)
112 break;
114 propagate = propagate && m_bApplyPublicFilter;
115 [[fallthrough]];
117 if (propagate)
119 break;
120 }
121 }
122 }
123 catch( const Exception& )
124 {
125 DBG_UNHANDLED_EXCEPTION("connectivity.commontools");
126 }
127 }
128
129
131 {
132 if ( m_bApplyPublicFilter == _bApply )
133 return;
134
135 m_bApplyPublicFilter = _bApply;
136
137 try
138 {
139 if ( m_xComponentAggregate.is())
140 {
141 // only where/if something changed
146 }
147 }
148 catch( const Exception& )
149 {
150 DBG_UNHANDLED_EXCEPTION("connectivity.commontools");
151 }
152 }
153
154
155 void FilterManager::appendFilterComponent( OUStringBuffer& io_appendTo, std::u16string_view i_component )
156 {
157 if ( !io_appendTo.isEmpty() )
158 {
159 io_appendTo.insert( 0, '(' );
160 io_appendTo.insert( 1, ' ' );
161 io_appendTo.append( " ) AND " );
162 }
163
164 io_appendTo.append( "( " );
165 io_appendTo.append( i_component );
166 io_appendTo.append( " )" );
167 }
168
169
170 bool FilterManager::isThereAtMostOneFilterComponent( OUString& o_singleComponent ) const
171 {
173 if (!m_aPublicFilterComponent.isEmpty() && !m_aLinkFilterComponent.isEmpty())
174 return false;
175 if (!m_aPublicFilterComponent.isEmpty())
176 o_singleComponent = m_aPublicFilterComponent;
177 else if (!m_aLinkFilterComponent.isEmpty())
178 o_singleComponent = m_aLinkFilterComponent;
179 else
180 o_singleComponent.clear();
181 return true;
182 }
183 else
184 {
185 if (m_aLinkFilterComponent.isEmpty())
186 o_singleComponent.clear();
187 else
188 o_singleComponent = m_aLinkFilterComponent;
189 return true;
190 }
191 }
192
193 bool FilterManager::isThereAtMostOneHavingComponent( OUString& o_singleComponent ) const
194 {
196 if (!m_aPublicHavingComponent.isEmpty() && !m_aLinkHavingComponent.isEmpty())
197 return false;
198 if (!m_aPublicHavingComponent.isEmpty())
199 o_singleComponent = m_aPublicHavingComponent;
200 else if (!m_aLinkHavingComponent.isEmpty())
201 o_singleComponent = m_aLinkHavingComponent;
202 else
203 o_singleComponent.clear();
204 return true;
205 }
206 else
207 {
208 if (m_aLinkHavingComponent.isEmpty())
209 o_singleComponent.clear();
210 else
211 o_singleComponent = m_aLinkHavingComponent;
212 return true;
213 }
214 }
215
216
218 {
219 // if we have only one non-empty component, then there's no need to compose anything
220 OUString singleComponent;
221 if ( isThereAtMostOneFilterComponent( singleComponent ) )
222 {
223 return singleComponent;
224 }
225 // append the single components
226 OUStringBuffer aComposedFilter(singleComponent);
230 return aComposedFilter.makeStringAndClear();
231 }
232
233
235 {
236 // if we have only one non-empty component, then there's no need to compose anything
237 OUString singleComponent;
238 if ( isThereAtMostOneHavingComponent( singleComponent ) )
239 {
240 return singleComponent;
241 }
242 // append the single components
243 OUStringBuffer aComposedFilter(singleComponent);
247 return aComposedFilter.makeStringAndClear();
248 }
249
250
251} // namespace dbtools
252
253
254/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
::dbtools::OPropertyMap & getPropMap()
Definition: TConnection.cxx:68
OUString getComposedFilter() const
retrieves a filter which is a conjunction of all single filter components
static void appendFilterComponent(OUStringBuffer &io_appendTo, std::u16string_view i_component)
appends one filter component to the statement in our composer
bool isThereAtMostOneHavingComponent(OUString &o_singleComponent) const
void setApplyPublicFilter(bool _bApply)
css::uno::Reference< css::beans::XPropertySet > m_xComponentAggregate
OUString getComposedHaving() const
void setFilterComponent(FilterComponent _eWhich, const OUString &_rComponent)
const OUString & getFilterComponent(FilterComponent _eWhich) const
void initialize(const css::uno::Reference< css::beans::XPropertySet > &_rxComponentAggregate)
late ctor
bool isThereAtMostOneFilterComponent(OUString &o_singleComponent) const
checks whether there is only one (or even no) non-empty filter component
void dispose()
makes the object forgetting the references to the database component
#define DBG_UNHANDLED_EXCEPTION(...)
@ Exception
#define PROPERTY_ID_HAVINGCLAUSE
Definition: propertyids.hxx:95
#define PROPERTY_ID_FILTER
#define PROPERTY_ID_APPLYFILTER