Files
libreoffice/connectivity/source/sdbcx/VCollection.cxx

436 lines
18 KiB
C++
Raw Normal View History

2000-09-18 15:18:56 +00:00
/*************************************************************************
*
* $RCSfile: VCollection.cxx,v $
*
2001-10-12 11:02:56 +00:00
* $Revision: 1.24 $
2000-09-18 15:18:56 +00:00
*
2001-10-12 11:02:56 +00:00
* last change: $Author: oj $ $Date: 2001-10-12 11:53:59 $
2000-09-18 15:18:56 +00:00
*
* The Contents of this file are made available subject to the terms of
* either of the following licenses
*
* - GNU Lesser General Public License Version 2.1
* - Sun Industry Standards Source License Version 1.1
*
* Sun Microsystems Inc., October, 2000
*
* GNU Lesser General Public License Version 2.1
* =============================================
* Copyright 2000 by Sun Microsystems, Inc.
* 901 San Antonio Road, Palo Alto, CA 94303, USA
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
*
* Sun Industry Standards Source License Version 1.1
* =================================================
* The contents of this file are subject to the Sun Industry Standards
* Source License Version 1.1 (the "License"); You may not use this file
* except in compliance with the License. You may obtain a copy of the
* License at http://www.openoffice.org/license.html.
*
* Software provided under this License is provided on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
* WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
* MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
* See the License for the specific provisions governing your rights and
* obligations concerning the Software.
*
* The Initial Developer of the Original Code is: Sun Microsystems, Inc.
*
* Copyright: 2000 by Sun Microsystems, Inc.
*
* All Rights Reserved.
*
* Contributor(s): _______________________________________
*
*
************************************************************************/
2000-11-29 11:28:04 +00:00
#include <stdio.h>
2000-09-18 15:18:56 +00:00
#ifndef _CONNECTIVITY_SDBCX_COLLECTION_HXX_
#include "connectivity/sdbcx/VCollection.hxx"
#endif
2000-11-03 12:36:27 +00:00
#ifndef _CONNECTIVITY_SDBCX_DESCRIPTOR_HXX_
#include "connectivity/sdbcx/VDescriptor.hxx"
#endif
2000-10-19 10:47:14 +00:00
#ifndef _COMPHELPER_ENUMHELPER_HXX_
#include <comphelper/enumhelper.hxx>
2000-09-18 15:18:56 +00:00
#endif
2000-10-09 11:09:06 +00:00
#ifndef _COMPHELPER_CONTAINER_HXX_
#include <comphelper/container.hxx>
#endif
2001-03-12 12:51:27 +00:00
#ifndef _COMPHELPER_TYPES_HXX_
#include <comphelper/types.hxx>
#endif
2001-05-14 10:42:44 +00:00
#ifndef CONNECTIVITY_CONNECTION_HXX
#include "TConnection.hxx"
2001-03-30 13:01:50 +00:00
#endif
2000-09-18 15:18:56 +00:00
using namespace connectivity::sdbcx;
using namespace connectivity;
2000-10-19 10:47:14 +00:00
using namespace comphelper;
2001-02-23 13:55:44 +00:00
using namespace ::cppu;
2000-09-18 15:18:56 +00:00
using namespace ::com::sun::star::beans;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::sdbc;
using namespace ::com::sun::star::container;
using namespace ::com::sun::star::util;
IMPLEMENT_SERVICE_INFO(OCollection,"com.sun.star.sdbcx.VContainer" , "com.sun.star.sdbcx.Container")
OCollection::OCollection(::cppu::OWeakObject& _rParent,sal_Bool _bCase, ::osl::Mutex& _rMutex,const TStringVector &_rVector)
2000-10-17 07:36:20 +00:00
: m_rParent(_rParent)
,m_rMutex(_rMutex)
2001-02-23 13:55:44 +00:00
,m_aContainerListeners(_rMutex)
2000-10-17 07:36:20 +00:00
,m_aRefreshListeners(_rMutex)
,m_aNameMap(_bCase ? true : false)
2000-10-17 07:36:20 +00:00
{
m_aElements.reserve(_rVector.size());
for(TStringVector::const_iterator i=_rVector.begin(); i != _rVector.end();++i)
2001-10-12 11:02:56 +00:00
m_aElements.push_back(m_aNameMap.insert(m_aNameMap.begin(), ObjectMap::value_type(*i,WeakReference< XNamed >())));
2000-10-17 07:36:20 +00:00
}
// -------------------------------------------------------------------------
OCollection::~OCollection()
{
}
2000-11-07 16:15:48 +00:00
// -------------------------------------------------------------------------
void OCollection::clear_NoDispose()
{
::osl::MutexGuard aGuard(m_rMutex);
2000-11-07 16:15:48 +00:00
m_aElements.clear();
m_aNameMap.clear();
::std::vector< ObjectIter >(m_aElements).swap(m_aElements);
ObjectMap(m_aNameMap).swap(m_aNameMap);
2000-11-07 16:15:48 +00:00
}
2000-09-18 15:18:56 +00:00
// -------------------------------------------------------------------------
void OCollection::disposing(void)
{
2001-02-23 13:55:44 +00:00
m_aContainerListeners.disposeAndClear(EventObject(static_cast<XWeak*>(this)));
2000-09-18 15:18:56 +00:00
m_aRefreshListeners.disposeAndClear(EventObject(static_cast<XWeak*>(this)));
::osl::MutexGuard aGuard(m_rMutex);
2001-10-12 11:02:56 +00:00
disposeElements();
::std::vector< ObjectIter >().swap(m_aElements);
ObjectMap().swap(m_aNameMap);
2000-09-18 15:18:56 +00:00
}
// -------------------------------------------------------------------------
Any SAL_CALL OCollection::getByIndex( sal_Int32 Index ) throw(IndexOutOfBoundsException, WrappedTargetException, RuntimeException)
{
::osl::MutexGuard aGuard(m_rMutex);
2001-10-12 11:02:56 +00:00
if (Index < 0 || Index >= static_cast<sal_Int32>(m_aNameMap.size()))
2000-11-14 12:42:37 +00:00
throw IndexOutOfBoundsException(::rtl::OUString::valueOf(Index),*this);
2000-09-18 15:18:56 +00:00
ObjectIter aIter = m_aElements[Index];
2001-10-12 11:02:56 +00:00
return makeAny(getObject(aIter));
2000-09-18 15:18:56 +00:00
}
// -------------------------------------------------------------------------
Any SAL_CALL OCollection::getByName( const ::rtl::OUString& aName ) throw(NoSuchElementException, WrappedTargetException, RuntimeException)
{
::osl::MutexGuard aGuard(m_rMutex);
ObjectIter aIter = m_aNameMap.find(aName);
if(aIter == m_aNameMap.end())
2000-11-14 12:42:37 +00:00
throw NoSuchElementException(aName,*this);
2001-10-12 11:02:56 +00:00
return makeAny(getObject(aIter));
2000-09-18 15:18:56 +00:00
}
// -------------------------------------------------------------------------
Sequence< ::rtl::OUString > SAL_CALL OCollection::getElementNames( ) throw(RuntimeException)
{
::osl::MutexGuard aGuard(m_rMutex);
sal_Int32 nLen = m_aElements.size();
2000-09-18 15:18:56 +00:00
Sequence< ::rtl::OUString > aNameList(nLen);
::rtl::OUString* pStringArray = aNameList.getArray();
2001-10-12 11:02:56 +00:00
for(::std::vector< ObjectIter >::const_iterator aIter = m_aElements.begin(); aIter != m_aElements.end();++aIter,++pStringArray)
*pStringArray = (*aIter)->first;
2000-09-18 15:18:56 +00:00
return aNameList;
}
// -------------------------------------------------------------------------
void SAL_CALL OCollection::refresh( ) throw(RuntimeException)
{
::osl::MutexGuard aGuard(m_rMutex);
2001-10-12 11:02:56 +00:00
disposeElements();
2000-09-18 15:18:56 +00:00
impl_refresh();
EventObject aEvt(static_cast<XWeak*>(this));
NOTIFY_LISTENERS(m_aRefreshListeners, XRefreshListener, refreshed, aEvt);
}
// -----------------------------------------------------------------------------
void OCollection::reFill(const TStringVector &_rVector)
{
OSL_ENSURE(!m_aNameMap.size(),"OCollection::reFill: collection isn't empty");
m_aElements.reserve(_rVector.size());
for(TStringVector::const_iterator i=_rVector.begin(); i != _rVector.end();++i)
2001-10-12 11:02:56 +00:00
m_aElements.push_back(m_aNameMap.insert(m_aNameMap.begin(), ObjectMap::value_type(*i,WeakReference< XNamed >())));
}
2000-09-18 15:18:56 +00:00
// -------------------------------------------------------------------------
// XDataDescriptorFactory
Reference< XPropertySet > SAL_CALL OCollection::createDataDescriptor( ) throw(RuntimeException)
{
::osl::MutexGuard aGuard(m_rMutex);
return createEmptyObject();
}
// -------------------------------------------------------------------------
// XAppend
void SAL_CALL OCollection::appendByDescriptor( const Reference< XPropertySet >& descriptor ) throw(SQLException, ElementExistException, RuntimeException)
{
::osl::MutexGuard aGuard(m_rMutex);
Reference< XNamed > xName(descriptor,UNO_QUERY);
if(xName.is())
{
2001-10-12 11:02:56 +00:00
::rtl::OUString sName = xName->getName();
if(m_aNameMap.find(sName) != m_aNameMap.end())
throw ElementExistException(sName,*this);
2000-09-18 15:18:56 +00:00
2001-10-12 11:02:56 +00:00
appendObject(descriptor);
Reference< XNamed > xNewName = cloneObject(descriptor);
Reference<XUnoTunnel> xTunnel(xNewName,UNO_QUERY);
2000-11-03 12:36:27 +00:00
if(xTunnel.is())
{
ODescriptor* pDescriptor = (ODescriptor*)xTunnel->getSomething(ODescriptor::getUnoTunnelImplementationId());
if(pDescriptor)
pDescriptor->setNew(sal_False);
}
2001-10-12 11:02:56 +00:00
m_aElements.push_back(m_aNameMap.insert(m_aNameMap.begin(), ObjectMap::value_type(sName,WeakReference< XNamed >(xNewName))));
2001-02-23 13:55:44 +00:00
// notify our container listeners
2001-10-12 11:02:56 +00:00
ContainerEvent aEvent(static_cast<XContainer*>(this), makeAny(sName), makeAny(xNewName), Any());
2001-02-23 13:55:44 +00:00
OInterfaceIteratorHelper aListenerLoop(m_aContainerListeners);
while (aListenerLoop.hasMoreElements())
static_cast<XContainerListener*>(aListenerLoop.next())->elementInserted(aEvent);
2000-09-18 15:18:56 +00:00
}
}
// -------------------------------------------------------------------------
// XDrop
void SAL_CALL OCollection::dropByName( const ::rtl::OUString& elementName ) throw(SQLException, NoSuchElementException, RuntimeException)
{
::osl::MutexGuard aGuard(m_rMutex);
ObjectMap::iterator aIter = m_aNameMap.find(elementName);
if( aIter == m_aNameMap.end())
throw NoSuchElementException(elementName,*this);
for(::std::vector< ObjectIter >::size_type i=0;i<m_aElements.size();++i)
{
if(m_aElements[i] == aIter)
{
2001-10-12 11:02:56 +00:00
dropImpl(i);
2001-02-23 13:55:44 +00:00
break; // no duplicates possible
2000-09-18 15:18:56 +00:00
}
}
}
// -------------------------------------------------------------------------
void SAL_CALL OCollection::dropByIndex( sal_Int32 index ) throw(SQLException, IndexOutOfBoundsException, RuntimeException)
{
::osl::MutexGuard aGuard(m_rMutex);
2001-03-30 13:01:50 +00:00
if(index <0 || index >= getCount())
throw IndexOutOfBoundsException(::rtl::OUString::valueOf(index),*this);
2000-09-18 15:18:56 +00:00
2001-10-12 11:02:56 +00:00
dropImpl(index);
}
// -----------------------------------------------------------------------------
void OCollection::dropImpl(sal_Int32 _nIndex)
{
::rtl::OUString elementName = m_aElements[_nIndex]->first;
dropObject(_nIndex,elementName);
::comphelper::disposeComponent(m_aElements[_nIndex]->second);
m_aNameMap.erase(m_aElements[_nIndex]);
m_aElements.erase(m_aElements.begin()+_nIndex);
2001-02-23 13:55:44 +00:00
// notify our container listeners
2001-10-12 11:02:56 +00:00
notifyElementRemoved(elementName);
}
// -----------------------------------------------------------------------------
void OCollection::notifyElementRemoved(const ::rtl::OUString& _sName)
{
ContainerEvent aEvent(static_cast<XContainer*>(this), makeAny(_sName), Any(), Any());
// note that xExistent may be empty, in case somebody removed the data source while it is not alive at this moment
2001-02-23 13:55:44 +00:00
OInterfaceIteratorHelper aListenerLoop(m_aContainerListeners);
while (aListenerLoop.hasMoreElements())
static_cast<XContainerListener*>(aListenerLoop.next())->elementRemoved(aEvent);
2000-09-18 15:18:56 +00:00
}
// -------------------------------------------------------------------------
sal_Int32 SAL_CALL OCollection::findColumn( const ::rtl::OUString& columnName ) throw(SQLException, RuntimeException)
2000-09-18 15:18:56 +00:00
{
ObjectIter aIter = m_aNameMap.find(columnName);
if(aIter == m_aNameMap.end())
2001-05-14 10:42:44 +00:00
throw SQLException(::rtl::OUString::createFromAscii("Unknown column name!"),*this,OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_HY0000),1000,makeAny(NoSuchElementException(columnName,*this)) );
2000-09-18 15:18:56 +00:00
return m_aElements.size() - (m_aElements.end() - ::std::find(m_aElements.begin(),m_aElements.end(),aIter)) +1; // because cloumns start at one
}
// -------------------------------------------------------------------------
Reference< XEnumeration > SAL_CALL OCollection::createEnumeration( ) throw(RuntimeException)
{
::osl::MutexGuard aGuard(m_rMutex);
2000-10-19 10:47:14 +00:00
return new OEnumerationByIndex( static_cast< XIndexAccess*>(this));
2000-09-18 15:18:56 +00:00
}
2001-02-23 13:55:44 +00:00
// -----------------------------------------------------------------------------
void SAL_CALL OCollection::addContainerListener( const Reference< XContainerListener >& _rxListener ) throw(RuntimeException)
{
m_aContainerListeners.addInterface(_rxListener);
}
//------------------------------------------------------------------------------
void SAL_CALL OCollection::removeContainerListener( const Reference< XContainerListener >& _rxListener ) throw(RuntimeException)
{
m_aContainerListeners.removeInterface(_rxListener);
}
// -----------------------------------------------------------------------------
void SAL_CALL OCollection::acquire() throw(RuntimeException)
2001-04-30 08:59:56 +00:00
{
m_rParent.acquire();
}
// -----------------------------------------------------------------------------
void SAL_CALL OCollection::release() throw(RuntimeException)
2001-04-30 08:59:56 +00:00
{
m_rParent.release();
}
// -----------------------------------------------------------------------------
Type SAL_CALL OCollection::getElementType( ) throw(RuntimeException)
2001-04-30 08:59:56 +00:00
{
return::getCppuType(static_cast< Reference< XPropertySet>*>(NULL));
2001-04-30 08:59:56 +00:00
}
// -----------------------------------------------------------------------------
sal_Bool SAL_CALL OCollection::hasElements( ) throw(RuntimeException)
2001-04-30 08:59:56 +00:00
{
::osl::MutexGuard aGuard(m_rMutex);
return !m_aNameMap.empty();
2001-04-30 08:59:56 +00:00
}
// -----------------------------------------------------------------------------
sal_Int32 SAL_CALL OCollection::getCount( ) throw(RuntimeException)
2001-04-30 08:59:56 +00:00
{
::osl::MutexGuard aGuard(m_rMutex);
return m_aNameMap.size();
2001-04-30 08:59:56 +00:00
}
// -----------------------------------------------------------------------------
sal_Bool SAL_CALL OCollection::hasByName( const ::rtl::OUString& aName ) throw(RuntimeException)
2001-04-30 08:59:56 +00:00
{
::osl::MutexGuard aGuard(m_rMutex);
return m_aNameMap.find(aName) != m_aNameMap.end();
}
// -----------------------------------------------------------------------------
void SAL_CALL OCollection::addRefreshListener( const Reference< XRefreshListener >& l ) throw(RuntimeException)
2001-04-30 08:59:56 +00:00
{
m_aRefreshListeners.addInterface(l);
}
// -----------------------------------------------------------------------------
void SAL_CALL OCollection::removeRefreshListener( const Reference< XRefreshListener >& l ) throw(RuntimeException)
2001-04-30 08:59:56 +00:00
{
m_aRefreshListeners.removeInterface(l);
}
// -----------------------------------------------------------------------------
void OCollection::insertElement(const ::rtl::OUString& _sElementName,const Object_BASE& _xElement)
{
OSL_ENSURE(m_aNameMap.find(_sElementName) == m_aNameMap.end(),"Element already exists");
if(m_aNameMap.find(_sElementName) == m_aNameMap.end())
m_aElements.push_back(m_aNameMap.insert(m_aNameMap.begin(), ObjectMap::value_type(_sElementName,_xElement)));
}
// -----------------------------------------------------------------------------
void OCollection::renameObject(const ::rtl::OUString _sOldName,const ::rtl::OUString _sNewName)
{
OSL_ENSURE(m_aNameMap.find(_sOldName) != m_aNameMap.end(),"Element doesn't exist");
OSL_ENSURE(m_aNameMap.find(_sNewName) == m_aNameMap.end(),"Element already exists");
OSL_ENSURE(_sNewName.getLength(),"New name must not be empty!");
OSL_ENSURE(_sOldName.getLength(),"New name must not be empty!");
ObjectMap::iterator aIter = m_aNameMap.find(_sOldName);
if(aIter != m_aNameMap.end())
{
::std::vector< ObjectIter >::iterator aFind = ::std::find(m_aElements.begin(),m_aElements.end(),aIter);
if(m_aElements.end() != aFind)
{
(*aFind) = m_aNameMap.insert(m_aNameMap.begin(), ObjectMap::value_type(_sNewName,(*aFind)->second));
m_aNameMap.erase(aIter);
ContainerEvent aEvent(static_cast<XContainer*>(this), makeAny(_sNewName), makeAny((*aFind)->second),makeAny(_sOldName));
// note that xExistent may be empty, in case somebody removed the data source while it is not alive at this moment
OInterfaceIteratorHelper aListenerLoop(m_aContainerListeners);
while (aListenerLoop.hasMoreElements())
static_cast<XContainerListener*>(aListenerLoop.next())->elementReplaced(aEvent);
}
}
}
// -----------------------------------------------------------------------------
2001-10-12 11:02:56 +00:00
Reference< XNamed > OCollection::getObject(ObjectIter& _rCurrentObject)
{
Reference< XNamed > xName = (*_rCurrentObject).second;
if(!(*_rCurrentObject).second.is())
{
try
{
xName = createObject((*_rCurrentObject).first);
}
catch(const SQLException& e)
{
throw WrappedTargetException(e.Message,*this,makeAny(e));
}
(*_rCurrentObject).second = xName;
}
return xName;
}
// -----------------------------------------------------------------------------
void OCollection::disposeElements()
{
for( ObjectIter aIter = m_aNameMap.begin(); aIter != m_aNameMap.end(); ++aIter)
{
if(aIter->second.is())
{
::comphelper::disposeComponent(aIter->second);
(*aIter).second = NULL;
}
}
m_aNameMap.clear();
m_aElements.clear();
}
// -----------------------------------------------------------------------------
Reference< XPropertySet > OCollection::createEmptyObject()
{
OSL_ASSERT(!"Need to be overloaded when used!");
throw SQLException();
}
// -----------------------------------------------------------------------------
void OCollection::appendObject( const Reference< XPropertySet >& descriptor )
{
}
// -----------------------------------------------------------------------------
Reference< XNamed > OCollection::cloneObject(const Reference< XPropertySet >& _xDescriptor)
{
OSL_ASSERT(!"Need to be overloaded when used!");
throw SQLException();
}
// -----------------------------------------------------------------------------
void OCollection::dropObject(sal_Int32 _nPos,const ::rtl::OUString _sElementName)
{
}
// -----------------------------------------------------------------------------