LibreOffice Module svx (master)  1
dataaccessdescriptor.cxx
Go to the documentation of this file.
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 
21 #include <osl/diagnose.h>
22 #include <com/sun/star/ucb/XContent.hpp>
23 #include <com/sun/star/beans/PropertyValue.hpp>
24 #include <com/sun/star/beans/XPropertySet.hpp>
25 #include <tools/urlobj.hxx>
26 #include <map>
27 
28 namespace svx
29 {
30  using namespace ::com::sun::star::uno;
31  using namespace ::com::sun::star::beans;
32  using namespace ::com::sun::star::ucb;
33 
34  typedef std::pair<OUString const, DataAccessDescriptorProperty> PropertyMapEntry;
35 
37  {
38  protected:
39  bool m_bSetOutOfDate : 1;
41 
42  public:
43  typedef ::std::map< DataAccessDescriptorProperty, Any > DescriptorValues;
44  DescriptorValues m_aValues;
45  Sequence< PropertyValue > m_aAsSequence;
46 
47  typedef ::std::map< OUString, DataAccessDescriptorProperty > MapString2PropertyEntry;
48 
49  public:
51  ODADescriptorImpl(const ODADescriptorImpl& _rSource);
52 
54 
55  void updateSequence();
56 
61  bool buildFrom( const Sequence< PropertyValue >& _rValues );
62 
67  bool buildFrom( const Reference< XPropertySet >& _rValues );
68 
69  protected:
70  static PropertyValue buildPropertyValue( const DescriptorValues::const_iterator& _rPos );
71  static const MapString2PropertyEntry& getPropertyMap( );
72  static PropertyMapEntry const * getPropertyMapEntry( const DescriptorValues::const_iterator& _rPos );
73  };
74 
76  :m_bSetOutOfDate(true)
77  ,m_bSequenceOutOfDate(true)
78  {
79  }
80 
82  :m_bSetOutOfDate( _rSource.m_bSetOutOfDate )
83  ,m_bSequenceOutOfDate( _rSource.m_bSequenceOutOfDate )
84  ,m_aValues( _rSource.m_aValues )
85  {
87  m_aAsSequence = _rSource.m_aAsSequence;
88  }
89 
90  bool ODADescriptorImpl::buildFrom( const Sequence< PropertyValue >& _rValues )
91  {
92  const MapString2PropertyEntry& rProperties = getPropertyMap();
93 
94  bool bValidPropsOnly = true;
95 
96  // loop through the sequence, and fill our m_aValues
97  for (const PropertyValue& rValue : _rValues)
98  {
99  MapString2PropertyEntry::const_iterator aPropPos = rProperties.find( rValue.Name );
100  if ( aPropPos != rProperties.end() )
101  {
102  DataAccessDescriptorProperty eProperty = aPropPos->second;
103  m_aValues[eProperty] = rValue.Value;
104  }
105  else
106  // unknown property
107  bValidPropsOnly = false;
108  }
109 
110  if (bValidPropsOnly)
111  {
112  m_aAsSequence = _rValues;
113  m_bSequenceOutOfDate = false;
114  }
115  else
116  m_bSequenceOutOfDate = true;
117 
118  return bValidPropsOnly;
119  }
120 
121  bool ODADescriptorImpl::buildFrom( const Reference< XPropertySet >& _rxValues )
122  {
123  Reference< XPropertySetInfo > xPropInfo;
124  if (_rxValues.is())
125  xPropInfo = _rxValues->getPropertySetInfo();
126  if (!xPropInfo.is())
127  {
128  OSL_FAIL("ODADescriptorImpl::buildFrom: invalid property set!");
129  return false;
130  }
131 
132  // build a PropertyValue sequence with the current values
133  const Sequence< Property > aProperties = xPropInfo->getProperties();
134 
135  Sequence< PropertyValue > aValues(aProperties.getLength());
136  PropertyValue* pValues = aValues.getArray();
137 
138  for (const Property& rProperty : aProperties)
139  {
140  pValues->Name = rProperty.Name;
141  pValues->Value = _rxValues->getPropertyValue(rProperty.Name);
142  ++pValues;
143  }
144 
145  bool bValidPropsOnly = buildFrom(aValues);
146  m_bSetOutOfDate = !bValidPropsOnly;
147 
148  return bValidPropsOnly;
149  }
150 
152  {
153  m_bSetOutOfDate = true;
154  m_bSequenceOutOfDate = true;
155  }
156 
158  {
159  // the properties we know
160  static MapString2PropertyEntry s_aProperties
161  {
162  { OUString("ActiveConnection"), DataAccessDescriptorProperty::Connection, },
163  { OUString("BookmarkSelection"), DataAccessDescriptorProperty::BookmarkSelection, },
164  { OUString("Column"), DataAccessDescriptorProperty::ColumnObject, },
165  { OUString("ColumnName"), DataAccessDescriptorProperty::ColumnName, },
166  { OUString("Command"), DataAccessDescriptorProperty::Command, },
167  { OUString("CommandType"), DataAccessDescriptorProperty::CommandType, },
168  { OUString("Component"), DataAccessDescriptorProperty::Component, },
169  { OUString("ConnectionResource"), DataAccessDescriptorProperty::ConnectionResource, },
170  { OUString("Cursor"), DataAccessDescriptorProperty::Cursor, },
171  { OUString("DataSourceName"), DataAccessDescriptorProperty::DataSource, },
172  { OUString("DatabaseLocation"), DataAccessDescriptorProperty::DatabaseLocation, },
173  { OUString("EscapeProcessing"), DataAccessDescriptorProperty::EscapeProcessing, },
174  { OUString("Filter"), DataAccessDescriptorProperty::Filter, },
175  { OUString("Selection"), DataAccessDescriptorProperty::Selection, }
176  };
177 
178  return s_aProperties;
179  }
180 
181  PropertyMapEntry const * ODADescriptorImpl::getPropertyMapEntry( const DescriptorValues::const_iterator& _rPos )
182  {
183  const MapString2PropertyEntry& rProperties = getPropertyMap();
184 
185  DataAccessDescriptorProperty nNeededHandle = _rPos->first;
186 
187  auto loop = std::find_if(rProperties.begin(), rProperties.end(),
188  [&nNeededHandle](const MapString2PropertyEntry::value_type& rProp) { return nNeededHandle == rProp.second; });
189  if (loop != rProperties.end())
190  return &*loop;
191  throw RuntimeException();
192  }
193 
194  PropertyValue ODADescriptorImpl::buildPropertyValue( const DescriptorValues::const_iterator& _rPos )
195  {
196  // the map entry
197  PropertyMapEntry const * pProperty = getPropertyMapEntry( _rPos );
198 
199  // build the property value
200  PropertyValue aReturn;
201  aReturn.Name = pProperty->first;
202  aReturn.Handle = static_cast<sal_Int32>(pProperty->second);
203  aReturn.Value = _rPos->second;
204  aReturn.State = PropertyState_DIRECT_VALUE;
205 
206  // outta here
207  return aReturn;
208  }
209 
211  {
213  return;
214 
215  m_aAsSequence.realloc(m_aValues.size());
216  PropertyValue* pValue = m_aAsSequence.getArray();
217 
218  // loop through all our values
219  for ( DescriptorValues::const_iterator aLoop = m_aValues.begin();
220  aLoop != m_aValues.end();
221  ++aLoop, ++pValue
222  )
223  {
224  *pValue = buildPropertyValue(aLoop);
225  }
226 
227  // don't need to rebuild next time
228  m_bSequenceOutOfDate = false;
229  }
230 
233  {
234  }
235 
237  :m_pImpl(new ODADescriptorImpl(*_rSource.m_pImpl))
238  {
239  }
240 
242  :m_pImpl(std::move(_rSource.m_pImpl))
243  {
244  }
245 
247  {
248  if (this != &_rSource)
249  m_pImpl.reset(new ODADescriptorImpl(*_rSource.m_pImpl));
250  return *this;
251  }
252 
254  {
255  m_pImpl = std::move(_rSource.m_pImpl);
256  return *this;
257  }
258 
259  ODataAccessDescriptor::ODataAccessDescriptor( const Reference< XPropertySet >& _rValues )
261  {
262  m_pImpl->buildFrom(_rValues);
263  }
264 
265  ODataAccessDescriptor::ODataAccessDescriptor( const Any& _rValues )
266  :m_pImpl(new ODADescriptorImpl)
267  {
268  // check if we know the format in the Any
269  Sequence< PropertyValue > aValues;
270  Reference< XPropertySet > xValues;
271  if ( _rValues >>= aValues )
272  m_pImpl->buildFrom( aValues );
273  else if ( _rValues >>= xValues )
274  m_pImpl->buildFrom( xValues );
275  }
276 
277  ODataAccessDescriptor::ODataAccessDescriptor( const Sequence< PropertyValue >& _rValues )
278  :m_pImpl(new ODADescriptorImpl)
279  {
280  m_pImpl->buildFrom(_rValues);
281  }
282 
284  {
285  }
286 
288  {
289  m_pImpl->m_aValues.clear();
290  }
291 
293  {
294  OSL_ENSURE(has(_eWhich), "ODataAccessDescriptor::erase: invalid call!");
295  if (has(_eWhich))
296  m_pImpl->m_aValues.erase(_eWhich);
297  }
298 
300  {
301  return m_pImpl->m_aValues.find(_eWhich) != m_pImpl->m_aValues.end();
302  }
303 
305  {
306  if (!has(_eWhich))
307  {
308  OSL_FAIL("ODataAccessDescriptor::operator[]: invalid accessor!");
309  static const Any aDummy;
310  return aDummy;
311  }
312 
313  return m_pImpl->m_aValues[_eWhich];
314  }
315 
317  {
318  m_pImpl->invalidateExternRepresentations();
319  return m_pImpl->m_aValues[_eWhich];
320  }
321 
322  void ODataAccessDescriptor::initializeFrom(const Sequence< PropertyValue >& _rValues)
323  {
324  clear();
325  m_pImpl->buildFrom(_rValues);
326  }
327 
328  Sequence< PropertyValue > const & ODataAccessDescriptor::createPropertyValueSequence()
329  {
330  m_pImpl->updateSequence();
331  return m_pImpl->m_aAsSequence;
332  }
333 
335  {
336  OUString sDataSourceName;
338  (*this)[DataAccessDescriptorProperty::DataSource] >>= sDataSourceName;
340  (*this)[DataAccessDescriptorProperty::DatabaseLocation] >>= sDataSourceName;
341  return sDataSourceName;
342  }
343 
344  void ODataAccessDescriptor::setDataSource(const OUString& _sDataSourceNameOrLocation)
345  {
346  if ( !_sDataSourceNameOrLocation.isEmpty() )
347  {
348  INetURLObject aURL(_sDataSourceNameOrLocation);
349  (*this)[ (( aURL.GetProtocol() == INetProtocol::File ) ? DataAccessDescriptorProperty::DatabaseLocation : DataAccessDescriptorProperty::DataSource)] <<= _sDataSourceNameOrLocation;
350  }
351  else
352  (*this)[ DataAccessDescriptorProperty::DataSource ] <<= OUString();
353  }
354 }
355 
356 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
URL aURL
ODataAccessDescriptor & operator=(const ODataAccessDescriptor &_rSource)
void setDataSource(const OUString &_sDataSourceNameOrLocation)
set the data source name, if it is not file URL
::std::map< OUString, DataAccessDescriptorProperty > MapString2PropertyEntry
void initializeFrom(const css::uno::Sequence< css::beans::PropertyValue > &_rValues)
initialized the descriptor from the property values given The descriptor will clear all its current s...
css::uno::Sequence< css::beans::PropertyValue > const & createPropertyValueSequence()
returns the descriptor as property value sequence
escape processing (boolean)
css::uno::Any const & rValue
::std::map< DataAccessDescriptorProperty, Any > DescriptorValues
std::unique_ptr< ODADescriptorImpl > m_pImpl
PropertiesInfo aProperties
selection are bookmarks? (boolean)
static PropertyValue buildPropertyValue(const DescriptorValues::const_iterator &_rPos)
void clear()
empties the descriptor
DataAccessDescriptorProperty
void erase(DataAccessDescriptorProperty _eWhich)
erases the given property from the descriptor
std::pair< OUString const, DataAccessDescriptorProperty > PropertyMapEntry
OUString getDataSource() const
returns either the data source name if given or the database location
Sequence< PropertyValue > m_aValues
const PropertyValue * pValues
class encapsulating the css::sdb::DataAccessDescriptor service.
bool has(DataAccessDescriptorProperty _eWhich) const
checks whether or not a given property is present in the descriptor
Sequence< PropertyValue > m_aAsSequence
INetProtocol GetProtocol() const
const css::uno::Any & operator[](DataAccessDescriptorProperty _eWhich) const
return the value of a given property
static const MapString2PropertyEntry & getPropertyMap()
static PropertyMapEntry const * getPropertyMapEntry(const DescriptorValues::const_iterator &_rPos)
::std::unique_ptr< XmlIdRegistry_Impl > m_pImpl
bool buildFrom(const Sequence< PropertyValue > &_rValues)
builds the descriptor from a property value sequence