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