Line data Source code
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 :
20 : #include "connectivity/filtermanager.hxx"
21 :
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>
28 :
29 :
30 : namespace dbtools
31 : {
32 :
33 :
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;
40 :
41 :
42 : //= FilterManager
43 :
44 :
45 0 : FilterManager::FilterManager( )
46 : :m_aFilterComponents( FC_COMPONENT_COUNT )
47 0 : ,m_bApplyPublicFilter( true )
48 : {
49 0 : }
50 :
51 :
52 0 : void FilterManager::initialize( const Reference< XPropertySet >& _rxComponentAggregate )
53 : {
54 0 : m_xComponentAggregate = _rxComponentAggregate;
55 : OSL_ENSURE( m_xComponentAggregate.is(), "FilterManager::initialize: invalid arguments!" );
56 :
57 0 : if ( m_xComponentAggregate.is() )
58 0 : m_xComponentAggregate->setPropertyValue( OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_APPLYFILTER), makeAny( (sal_Bool)sal_True ) );
59 0 : }
60 :
61 :
62 0 : void FilterManager::dispose( )
63 : {
64 0 : m_xComponentAggregate.clear();
65 0 : }
66 :
67 :
68 0 : const OUString& FilterManager::getFilterComponent( FilterComponent _eWhich ) const
69 : {
70 0 : return m_aFilterComponents[ _eWhich ];
71 : }
72 :
73 :
74 0 : void FilterManager::setFilterComponent( FilterComponent _eWhich, const OUString& _rComponent )
75 : {
76 0 : m_aFilterComponents[ _eWhich ] = _rComponent;
77 : try
78 : {
79 0 : if ( m_xComponentAggregate.is() && (( _eWhich != fcPublicFilter ) || m_bApplyPublicFilter ) )
80 0 : m_xComponentAggregate->setPropertyValue( OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_FILTER), makeAny( getComposedFilter() ) );
81 : }
82 0 : catch( const Exception& )
83 : {
84 : DBG_UNHANDLED_EXCEPTION();
85 : }
86 0 : }
87 :
88 :
89 0 : void FilterManager::setApplyPublicFilter( bool _bApply )
90 : {
91 0 : if ( m_bApplyPublicFilter == _bApply )
92 0 : return;
93 :
94 0 : m_bApplyPublicFilter = _bApply;
95 :
96 : try
97 : {
98 0 : if ( m_xComponentAggregate.is() && !getFilterComponent( fcPublicFilter ).isEmpty() )
99 : { // only if there changed something
100 0 : m_xComponentAggregate->setPropertyValue( OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_FILTER), makeAny( getComposedFilter() ) );
101 : }
102 : }
103 0 : catch( const Exception& )
104 : {
105 : DBG_UNHANDLED_EXCEPTION();
106 : }
107 : }
108 :
109 :
110 0 : void FilterManager::appendFilterComponent( OUStringBuffer& io_appendTo, const OUString& i_component ) const
111 : {
112 0 : if ( !io_appendTo.isEmpty() )
113 : {
114 0 : io_appendTo.insert( 0, '(' );
115 0 : io_appendTo.insert( 1, ' ' );
116 0 : io_appendTo.appendAscii( " ) AND " );
117 : }
118 :
119 0 : io_appendTo.appendAscii( "( " );
120 0 : io_appendTo.append( i_component );
121 0 : io_appendTo.appendAscii( " )" );
122 0 : }
123 :
124 :
125 0 : bool FilterManager::isThereAtMostOneComponent( OUStringBuffer& o_singleComponent ) const
126 : {
127 0 : sal_Int32 nOnlyNonEmpty = -1;
128 : sal_Int32 i;
129 0 : for ( i = getFirstApplicableFilterIndex(); i < FC_COMPONENT_COUNT; ++i )
130 : {
131 0 : if ( !m_aFilterComponents[ i ].isEmpty() )
132 : {
133 0 : if ( nOnlyNonEmpty != -1 )
134 : // it's the second non-empty component
135 0 : break;
136 : else
137 0 : nOnlyNonEmpty = i;
138 : }
139 : }
140 0 : if ( nOnlyNonEmpty == -1 )
141 : {
142 0 : o_singleComponent.makeStringAndClear();
143 0 : return true;
144 : }
145 :
146 0 : if ( i == FC_COMPONENT_COUNT )
147 : {
148 : // we found only one non-empty filter component
149 0 : o_singleComponent = m_aFilterComponents[ nOnlyNonEmpty ];
150 0 : return true;
151 : }
152 0 : return false;
153 : }
154 :
155 :
156 0 : OUString FilterManager::getComposedFilter( ) const
157 : {
158 0 : OUStringBuffer aComposedFilter;
159 :
160 : // if we have only one non-empty component, then there's no need to compose anything
161 0 : if ( !isThereAtMostOneComponent( aComposedFilter ) )
162 : {
163 : // append the single components
164 0 : for ( sal_Int32 i = getFirstApplicableFilterIndex(); i < FC_COMPONENT_COUNT; ++i )
165 0 : appendFilterComponent( aComposedFilter, m_aFilterComponents[ i ] );
166 : }
167 :
168 0 : return aComposedFilter.makeStringAndClear();
169 : }
170 :
171 :
172 : } // namespace dbtools
173 :
174 :
175 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|