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