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