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 <svx/dataaccessdescriptor.hxx>
21 : #include <comphelper/propertysetinfo.hxx>
22 : #include <comphelper/genericpropertyset.hxx>
23 : #include <osl/diagnose.h>
24 : #include <com/sun/star/sdbc/XConnection.hpp>
25 : #include <com/sun/star/ucb/XContent.hpp>
26 : #include <com/sun/star/beans/PropertyAttribute.hpp>
27 : #include <tools/urlobj.hxx>
28 :
29 :
30 : namespace svx
31 : {
32 :
33 :
34 : using namespace ::com::sun::star::uno;
35 : using namespace ::com::sun::star::sdbc;
36 : using namespace ::com::sun::star::beans;
37 : using namespace ::com::sun::star::ucb;
38 : using namespace ::comphelper;
39 :
40 172 : class ODADescriptorImpl
41 : {
42 : protected:
43 : bool m_bSetOutOfDate : 1;
44 : bool m_bSequenceOutOfDate : 1;
45 :
46 : public:
47 : typedef ::std::map< DataAccessDescriptorProperty, Any > DescriptorValues;
48 : DescriptorValues m_aValues;
49 : Sequence< PropertyValue > m_aAsSequence;
50 : Reference< XPropertySet > m_xAsSet;
51 :
52 : typedef ::std::map< OUString, PropertyMapEntry const * > MapString2PropertyEntry;
53 :
54 : public:
55 : ODADescriptorImpl();
56 : ODADescriptorImpl(const ODADescriptorImpl& _rSource);
57 :
58 : void invalidateExternRepresentations();
59 :
60 : void updateSequence();
61 :
62 : /** builds the descriptor from a property value sequence
63 : @return <TRUE/>
64 : if and only if the sequence contained valid properties only
65 : */
66 : bool buildFrom( const Sequence< PropertyValue >& _rValues );
67 :
68 : /** builds the descriptor from a property set
69 : @return <TRUE/>
70 : if and only if the set contained valid properties only
71 : */
72 : bool buildFrom( const Reference< XPropertySet >& _rValues );
73 :
74 : protected:
75 : static PropertyValue buildPropertyValue( const DescriptorValues::const_iterator& _rPos );
76 : static const MapString2PropertyEntry& getPropertyMap( );
77 : static PropertyMapEntry const * getPropertyMapEntry( const DescriptorValues::const_iterator& _rPos );
78 : };
79 :
80 :
81 172 : ODADescriptorImpl::ODADescriptorImpl()
82 : :m_bSetOutOfDate(true)
83 172 : ,m_bSequenceOutOfDate(true)
84 : {
85 172 : }
86 :
87 :
88 4 : ODADescriptorImpl::ODADescriptorImpl(const ODADescriptorImpl& _rSource)
89 : :m_bSetOutOfDate( _rSource.m_bSetOutOfDate )
90 : ,m_bSequenceOutOfDate( _rSource.m_bSequenceOutOfDate )
91 4 : ,m_aValues( _rSource.m_aValues )
92 : {
93 4 : if (!m_bSetOutOfDate)
94 0 : m_xAsSet = _rSource.m_xAsSet;
95 4 : if (!m_bSequenceOutOfDate)
96 4 : m_aAsSequence = _rSource.m_aAsSequence;
97 4 : }
98 :
99 :
100 8 : bool ODADescriptorImpl::buildFrom( const Sequence< PropertyValue >& _rValues )
101 : {
102 8 : const MapString2PropertyEntry& rProperties = getPropertyMap();
103 :
104 8 : bool bValidPropsOnly = true;
105 :
106 : // loop through the sequence, and fill our m_aValues
107 8 : const PropertyValue* pValues = _rValues.getConstArray();
108 8 : const PropertyValue* pValuesEnd = pValues + _rValues.getLength();
109 130 : for (;pValues != pValuesEnd; ++pValues)
110 : {
111 122 : MapString2PropertyEntry::const_iterator aPropPos = rProperties.find( pValues->Name );
112 122 : if ( aPropPos != rProperties.end() )
113 : {
114 30 : DataAccessDescriptorProperty eProperty = (DataAccessDescriptorProperty)aPropPos->second->mnHandle;
115 30 : m_aValues[eProperty] = pValues->Value;
116 : }
117 : else
118 : // unknown property
119 92 : bValidPropsOnly = false;
120 : }
121 :
122 8 : if (bValidPropsOnly)
123 : {
124 6 : m_aAsSequence = _rValues;
125 6 : m_bSequenceOutOfDate = false;
126 : }
127 : else
128 2 : m_bSequenceOutOfDate = true;
129 :
130 8 : return bValidPropsOnly;
131 : }
132 :
133 :
134 2 : bool ODADescriptorImpl::buildFrom( const Reference< XPropertySet >& _rxValues )
135 : {
136 2 : Reference< XPropertySetInfo > xPropInfo;
137 2 : if (_rxValues.is())
138 2 : xPropInfo = _rxValues->getPropertySetInfo();
139 2 : if (!xPropInfo.is())
140 : {
141 : OSL_FAIL("ODADescriptorImpl::buildFrom: invalid property set!");
142 0 : return false;
143 : }
144 :
145 : // build a PropertyValue sequence with the current values
146 4 : Sequence< Property > aProperties = xPropInfo->getProperties();
147 2 : const Property* pProperty = aProperties.getConstArray();
148 2 : const Property* pPropertyEnd = pProperty + aProperties.getLength();
149 :
150 4 : Sequence< PropertyValue > aValues(aProperties.getLength());
151 2 : PropertyValue* pValues = aValues.getArray();
152 :
153 106 : for (;pProperty != pPropertyEnd; ++pProperty, ++pValues)
154 : {
155 104 : pValues->Name = pProperty->Name;
156 104 : pValues->Value = _rxValues->getPropertyValue(pProperty->Name);
157 : }
158 :
159 2 : bool bValidPropsOnly = buildFrom(aValues);
160 2 : if (bValidPropsOnly)
161 : {
162 0 : m_xAsSet = _rxValues;
163 0 : m_bSetOutOfDate = false;
164 : }
165 : else
166 2 : m_bSetOutOfDate = true;
167 :
168 4 : return bValidPropsOnly;
169 : }
170 :
171 :
172 94 : void ODADescriptorImpl::invalidateExternRepresentations()
173 : {
174 94 : m_bSetOutOfDate = true;
175 94 : m_bSequenceOutOfDate = true;
176 94 : }
177 :
178 :
179 20 : const ODADescriptorImpl::MapString2PropertyEntry& ODADescriptorImpl::getPropertyMap( )
180 : {
181 : // the properties we know
182 20 : static MapString2PropertyEntry s_aProperties;
183 20 : if ( s_aProperties.empty() )
184 : {
185 : static PropertyMapEntry const s_aDesriptorProperties[] =
186 : {
187 2 : { OUString("ActiveConnection"), daConnection, cppu::UnoType<XConnection>::get(), PropertyAttribute::TRANSIENT, 0 },
188 2 : { OUString("BookmarkSelection"), daBookmarkSelection, ::getBooleanCppuType( ), PropertyAttribute::TRANSIENT, 0 },
189 2 : { OUString("Column"), daColumnObject, cppu::UnoType<XPropertySet>::get(), PropertyAttribute::TRANSIENT, 0 },
190 2 : { OUString("ColumnName"), daColumnName, ::cppu::UnoType<OUString>::get(), PropertyAttribute::TRANSIENT, 0 },
191 2 : { OUString("Command"), daCommand, ::cppu::UnoType<OUString>::get(), PropertyAttribute::TRANSIENT, 0 },
192 2 : { OUString("CommandType"), daCommandType, ::cppu::UnoType<sal_Int32>::get(), PropertyAttribute::TRANSIENT, 0 },
193 2 : { OUString("Component"), daComponent, cppu::UnoType<XContent>::get(), PropertyAttribute::TRANSIENT, 0 },
194 2 : { OUString("ConnectionResource"), daConnectionResource, ::cppu::UnoType<OUString>::get(), PropertyAttribute::TRANSIENT, 0 },
195 2 : { OUString("Cursor"), daCursor, cppu::UnoType<XResultSet>::get(), PropertyAttribute::TRANSIENT, 0 },
196 2 : { OUString("DataSourceName"), daDataSource, ::cppu::UnoType<OUString>::get(), PropertyAttribute::TRANSIENT, 0 },
197 2 : { OUString("DatabaseLocation"), daDatabaseLocation, ::cppu::UnoType<OUString>::get(), PropertyAttribute::TRANSIENT, 0 },
198 2 : { OUString("EscapeProcessing"), daEscapeProcessing, ::getBooleanCppuType( ), PropertyAttribute::TRANSIENT, 0 },
199 2 : { OUString("Filter"), daFilter, ::cppu::UnoType<OUString>::get(), PropertyAttribute::TRANSIENT, 0 },
200 2 : { OUString("Selection"), daSelection, ::getCppuType( static_cast< Sequence< Any >* >(NULL) ), PropertyAttribute::TRANSIENT, 0 },
201 : { OUString(), 0, css::uno::Type(), 0, 0 }
202 32 : };
203 :
204 2 : PropertyMapEntry const * pEntry = s_aDesriptorProperties;
205 32 : while ( !pEntry->maName.isEmpty() )
206 : {
207 28 : s_aProperties[ pEntry->maName ] = pEntry;
208 28 : ++pEntry;
209 : }
210 : }
211 :
212 20 : return s_aProperties;
213 : }
214 :
215 :
216 12 : PropertyMapEntry const * ODADescriptorImpl::getPropertyMapEntry( const DescriptorValues::const_iterator& _rPos )
217 : {
218 12 : const MapString2PropertyEntry& rProperties = getPropertyMap();
219 :
220 12 : sal_Int32 nNeededHandle = (sal_Int32)(_rPos->first);
221 :
222 252 : for ( MapString2PropertyEntry::const_iterator loop = rProperties.begin();
223 168 : loop != rProperties.end();
224 : ++loop
225 : )
226 : {
227 84 : if ( nNeededHandle == loop->second->mnHandle )
228 24 : return loop->second;
229 : }
230 0 : throw RuntimeException();
231 : }
232 :
233 :
234 12 : PropertyValue ODADescriptorImpl::buildPropertyValue( const DescriptorValues::const_iterator& _rPos )
235 : {
236 : // the map entry
237 12 : PropertyMapEntry const * pProperty = getPropertyMapEntry( _rPos );
238 :
239 : // build the property value
240 12 : PropertyValue aReturn;
241 12 : aReturn.Name = pProperty->maName;
242 12 : aReturn.Handle = pProperty->mnHandle;
243 12 : aReturn.Value = _rPos->second;
244 12 : aReturn.State = PropertyState_DIRECT_VALUE;
245 :
246 : // outta here
247 12 : return aReturn;
248 : }
249 :
250 :
251 4 : void ODADescriptorImpl::updateSequence()
252 : {
253 4 : if (!m_bSequenceOutOfDate)
254 4 : return;
255 :
256 4 : m_aAsSequence.realloc(m_aValues.size());
257 4 : PropertyValue* pValue = m_aAsSequence.getArray();
258 :
259 : // loop through all our values
260 48 : for ( DescriptorValues::const_iterator aLoop = m_aValues.begin();
261 32 : aLoop != m_aValues.end();
262 : ++aLoop, ++pValue
263 : )
264 : {
265 12 : *pValue = buildPropertyValue(aLoop);
266 : }
267 :
268 : // don't need to rebuild next time
269 4 : m_bSequenceOutOfDate = false;
270 : }
271 :
272 166 : ODataAccessDescriptor::ODataAccessDescriptor()
273 166 : :m_pImpl(new ODADescriptorImpl)
274 : {
275 166 : }
276 :
277 :
278 0 : ODataAccessDescriptor::ODataAccessDescriptor( const ODataAccessDescriptor& _rSource )
279 0 : :m_pImpl(new ODADescriptorImpl(*_rSource.m_pImpl))
280 : {
281 0 : }
282 :
283 :
284 4 : const ODataAccessDescriptor& ODataAccessDescriptor::operator=(const ODataAccessDescriptor& _rSource)
285 : {
286 4 : delete m_pImpl;
287 4 : m_pImpl = new ODADescriptorImpl(*_rSource.m_pImpl);
288 4 : return *this;
289 : }
290 :
291 :
292 2 : ODataAccessDescriptor::ODataAccessDescriptor( const Reference< XPropertySet >& _rValues )
293 2 : :m_pImpl(new ODADescriptorImpl)
294 : {
295 2 : m_pImpl->buildFrom(_rValues);
296 2 : }
297 :
298 :
299 0 : ODataAccessDescriptor::ODataAccessDescriptor( const Any& _rValues )
300 0 : :m_pImpl(new ODADescriptorImpl)
301 : {
302 : // check if we know the format in the Any
303 0 : Sequence< PropertyValue > aValues;
304 0 : Reference< XPropertySet > xValues;
305 0 : if ( _rValues >>= aValues )
306 0 : m_pImpl->buildFrom( aValues );
307 0 : else if ( _rValues >>= xValues )
308 0 : m_pImpl->buildFrom( xValues );
309 0 : }
310 :
311 :
312 4 : ODataAccessDescriptor::ODataAccessDescriptor( const Sequence< PropertyValue >& _rValues )
313 4 : :m_pImpl(new ODADescriptorImpl)
314 : {
315 4 : m_pImpl->buildFrom(_rValues);
316 4 : }
317 :
318 :
319 168 : ODataAccessDescriptor::~ODataAccessDescriptor()
320 : {
321 168 : delete m_pImpl;
322 168 : }
323 :
324 :
325 2 : void ODataAccessDescriptor::clear()
326 : {
327 2 : m_pImpl->m_aValues.clear();
328 2 : }
329 :
330 :
331 0 : void ODataAccessDescriptor::erase(DataAccessDescriptorProperty _eWhich)
332 : {
333 : OSL_ENSURE(has(_eWhich), "ODataAccessDescriptor::erase: invalid call!");
334 0 : if (has(_eWhich))
335 0 : m_pImpl->m_aValues.erase(_eWhich);
336 0 : }
337 :
338 :
339 192 : bool ODataAccessDescriptor::has(DataAccessDescriptorProperty _eWhich) const
340 : {
341 192 : return m_pImpl->m_aValues.find(_eWhich) != m_pImpl->m_aValues.end();
342 : }
343 :
344 :
345 84 : const Any& ODataAccessDescriptor::operator [] ( DataAccessDescriptorProperty _eWhich ) const
346 : {
347 84 : if (!has(_eWhich))
348 : {
349 : OSL_FAIL("ODataAccessDescriptor::operator[]: invalid acessor!");
350 0 : static const Any aDummy;
351 0 : return aDummy;
352 : }
353 :
354 84 : return m_pImpl->m_aValues[_eWhich];
355 : }
356 :
357 :
358 94 : Any& ODataAccessDescriptor::operator[] ( DataAccessDescriptorProperty _eWhich )
359 : {
360 94 : m_pImpl->invalidateExternRepresentations();
361 94 : return m_pImpl->m_aValues[_eWhich];
362 : }
363 :
364 :
365 2 : void ODataAccessDescriptor::initializeFrom(const Sequence< PropertyValue >& _rValues, bool _bClear)
366 : {
367 2 : if (_bClear)
368 2 : clear();
369 2 : m_pImpl->buildFrom(_rValues);
370 2 : }
371 :
372 :
373 4 : Sequence< PropertyValue > ODataAccessDescriptor::createPropertyValueSequence()
374 : {
375 4 : m_pImpl->updateSequence();
376 4 : return m_pImpl->m_aAsSequence;
377 : }
378 :
379 :
380 54 : OUString ODataAccessDescriptor::getDataSource() const
381 : {
382 54 : OUString sDataSourceName;
383 54 : if ( has(daDataSource) )
384 52 : (*this)[daDataSource] >>= sDataSourceName;
385 2 : else if ( has(daDatabaseLocation) )
386 0 : (*this)[daDatabaseLocation] >>= sDataSourceName;
387 54 : return sDataSourceName;
388 : }
389 :
390 18 : void ODataAccessDescriptor::setDataSource(const OUString& _sDataSourceNameOrLocation)
391 : {
392 18 : if ( !_sDataSourceNameOrLocation.isEmpty() )
393 : {
394 4 : INetURLObject aURL(_sDataSourceNameOrLocation);
395 4 : (*this)[ (( aURL.GetProtocol() == INET_PROT_FILE ) ? daDatabaseLocation : daDataSource)] <<= _sDataSourceNameOrLocation;
396 : }
397 : else
398 14 : (*this)[ daDataSource ] <<= OUString();
399 18 : }
400 :
401 :
402 : } // namespace svx
403 :
404 :
405 :
406 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|