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

385 lines
16 KiB
C++
Raw Normal View History

2000-09-18 15:18:56 +00:00
/*************************************************************************
*
* $RCSfile: VCollection.cxx,v $
*
* $Revision: 1.20 $
2000-09-18 15:18:56 +00:00
*
* last change: $Author: oj $ $Date: 2001-08-02 14:24:28 $
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
{
for(TStringVector::const_iterator i=_rVector.begin(); i != _rVector.end();++i)
m_aElements.push_back(m_aNameMap.insert(m_aNameMap.begin(), ObjectMap::value_type(*i,WeakReference< ::com::sun::star::container::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);
for( ObjectIter aIter = m_aNameMap.begin(); aIter != m_aNameMap.end(); ++aIter)
{
if((*aIter).second.is())
{
2001-03-12 12:51:27 +00:00
::comphelper::disposeComponent(aIter->second);
(*aIter).second = NULL;
2000-09-18 15:18:56 +00:00
}
}
m_aElements.clear();
2000-11-14 12:42:37 +00:00
m_aNameMap.clear();
::std::vector< ObjectIter >(m_aElements).swap(m_aElements);
ObjectMap(m_aNameMap).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);
if (Index < 0 || Index >= getCount())
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];
Reference< XNamed > xName = (*aIter).second;
if(!(*aIter).second.is())
{
try
{
xName = createObject((*aIter).first);
}
catch(const SQLException& e)
{
throw WrappedTargetException(e.Message,*this,makeAny(e));
}
2000-09-18 15:18:56 +00:00
(*aIter).second = xName;
}
return makeAny( Reference< XPropertySet >(xName,UNO_QUERY));
}
// -------------------------------------------------------------------------
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);
2000-09-18 15:18:56 +00:00
Reference< XNamed > xName = (*aIter).second;
if(!(*aIter).second.is())
{
try
{
xName = createObject(aIter->first);
}
catch(const SQLException& e)
{
throw WrappedTargetException(e.Message,*this,makeAny(e));
}
2000-09-18 15:18:56 +00:00
(*aIter).second = xName;
}
return makeAny( Reference< XPropertySet >(xName,UNO_QUERY));
}
// -------------------------------------------------------------------------
Sequence< ::rtl::OUString > SAL_CALL OCollection::getElementNames( ) throw(RuntimeException)
{
::osl::MutexGuard aGuard(m_rMutex);
sal_Int32 nLen = getCount();
Sequence< ::rtl::OUString > aNameList(nLen);
sal_Int32 i=0;
::rtl::OUString* pStringArray = aNameList.getArray();
2000-10-11 09:48:50 +00:00
// for( ::std::map< ::rtl::OUString, Object_BASE, ::comphelper::UStringLess> ::const_iterator aIter = m_aNameMap.begin(); aIter != m_aNameMap.end(); ++aIter)
2000-09-18 15:18:56 +00:00
for(::std::vector< ObjectIter >::const_iterator aIter = m_aElements.begin(); aIter != m_aElements.end();++aIter)
pStringArray[i++] = (*aIter)->first;
return aNameList;
}
// -------------------------------------------------------------------------
void SAL_CALL OCollection::refresh( ) throw(RuntimeException)
{
::osl::MutexGuard aGuard(m_rMutex);
2000-10-11 09:48:50 +00:00
for( ::std::map< ::rtl::OUString, Object_BASE, ::comphelper::UStringLess>::iterator aIter = m_aNameMap.begin(); aIter != m_aNameMap.end(); ++aIter)
2000-09-18 15:18:56 +00:00
{
if((*aIter).second.is())
{
2001-03-12 12:51:27 +00:00
::comphelper::disposeComponent(aIter->second);
2000-09-18 15:18:56 +00:00
(*aIter).second = Reference< XNamed >();
}
}
m_aNameMap.clear();
m_aElements.clear();
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");
for(TStringVector::const_iterator i=_rVector.begin(); i != _rVector.end();++i)
m_aElements.push_back(m_aNameMap.insert(m_aNameMap.begin(), ObjectMap::value_type(*i,WeakReference< ::com::sun::star::container::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())
{
if(m_aNameMap.find(xName->getName()) != m_aNameMap.end())
throw ElementExistException(xName->getName(),*this);
2000-11-03 12:36:27 +00:00
Reference<XUnoTunnel> xTunnel(descriptor,UNO_QUERY);
if(xTunnel.is())
{
ODescriptor* pDescriptor = (ODescriptor*)xTunnel->getSomething(ODescriptor::getUnoTunnelImplementationId());
if(pDescriptor)
pDescriptor->setNew(sal_False);
}
2000-09-18 15:18:56 +00:00
m_aElements.push_back(m_aNameMap.insert(m_aNameMap.begin(), ObjectMap::value_type(xName->getName(),WeakReference< XNamed >(xName))));
2001-02-23 13:55:44 +00:00
// notify our container listeners
ContainerEvent aEvent(static_cast<XContainer*>(this), makeAny(xName->getName()), makeAny(xName), Any());
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-03-12 12:51:27 +00:00
::comphelper::disposeComponent(aIter->second);
2000-09-18 15:18:56 +00:00
m_aElements.erase(m_aElements.begin()+i);
m_aNameMap.erase(aIter);
2001-02-23 13:55:44 +00:00
break; // no duplicates possible
2000-09-18 15:18:56 +00:00
}
}
2001-02-23 13:55:44 +00:00
// notify our container listeners
ContainerEvent aEvent(static_cast<XContainer*>(this), makeAny(elementName), Any(), Any());
// 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())->elementRemoved(aEvent);
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-03-12 12:51:27 +00:00
::comphelper::disposeComponent(m_aElements[index]->second);
2001-02-23 13:55:44 +00:00
::rtl::OUString elementName = m_aElements[index]->first;
2000-09-18 15:18:56 +00:00
m_aNameMap.erase(m_aElements[index]);
m_aElements.erase(m_aElements.begin()+index);
2001-02-23 13:55:44 +00:00
// notify our container listeners
ContainerEvent aEvent(static_cast<XContainer*>(this), makeAny(elementName), Any(), Any());
// 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())->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 getCount() > 0;
}
// -----------------------------------------------------------------------------
sal_Int32 SAL_CALL OCollection::getCount( ) throw(RuntimeException)
2001-04-30 08:59:56 +00:00
{
::osl::MutexGuard aGuard(m_rMutex);
return m_aElements.size();
}
// -----------------------------------------------------------------------------
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);
}
// -----------------------------------------------------------------------------
2000-09-18 15:18:56 +00:00