1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_dbaccess.hxx"
26 
27 #include "PropertyForward.hxx"
28 #include "dbastrings.hrc"
29 
30 #include <com/sun/star/beans/PropertyValue.hpp>
31 #include <com/sun/star/sdbcx/XDataDescriptorFactory.hpp>
32 #include <com/sun/star/sdbcx/XAppend.hpp>
33 
34 #include <comphelper/property.hxx>
35 #include <tools/debug.hxx>
36 #include <tools/diagnose_ex.h>
37 
38 
39 //........................................................................
40 namespace dbaccess
41 {
42 //........................................................................
43 
44     using namespace ::com::sun::star::uno;
45 	using namespace ::com::sun::star::beans;
46 	using namespace ::com::sun::star::container;
47 	using namespace ::com::sun::star::sdbcx;
48 	using namespace ::com::sun::star::lang;
49 
DBG_NAME(OPropertyForward)50     DBG_NAME(OPropertyForward)
51 
52     //------------------------------------------------------------------------
53     OPropertyForward::OPropertyForward( const Reference< XPropertySet>& _xSource, const Reference< XNameAccess>& _xDestContainer,
54             const ::rtl::OUString& _sName, const ::std::vector< ::rtl::OUString>& _aPropertyList )
55         :m_xSource( _xSource, UNO_SET_THROW )
56         ,m_xDestContainer( _xDestContainer, UNO_SET_THROW )
57         ,m_sName( _sName )
58         ,m_bInInsert( sal_False )
59     {
60 	    DBG_CTOR(OPropertyForward,NULL);
61 
62         osl_incrementInterlockedCount(&m_refCount);
63 	    try
64 	    {
65 		    if ( _aPropertyList.empty() )
66 			    _xSource->addPropertyChangeListener( ::rtl::OUString(), this );
67 		    else
68 		    {
69 			    ::std::vector< ::rtl::OUString >::const_iterator aIter = _aPropertyList.begin();
70 			    ::std::vector< ::rtl::OUString >::const_iterator aEnd = _aPropertyList.end();
71 			    for (; aIter != aEnd ; ++aIter )
72 				    _xSource->addPropertyChangeListener( *aIter, this );
73 		    }
74 	    }
75 	    catch( const Exception& )
76 	    {
77             DBG_UNHANDLED_EXCEPTION();
78 	    }
79 	    osl_decrementInterlockedCount( &m_refCount );
80     }
81 
82     // -----------------------------------------------------------------------------
~OPropertyForward()83     OPropertyForward::~OPropertyForward()
84     {
85 	    DBG_DTOR(OPropertyForward,NULL);
86     }
87 
88     // -----------------------------------------------------------------------------
propertyChange(const PropertyChangeEvent & evt)89     void SAL_CALL OPropertyForward::propertyChange( const PropertyChangeEvent& evt ) throw(RuntimeException)
90     {
91 	    ::osl::MutexGuard aGuard( m_aMutex );
92 
93         if ( !m_xDestContainer.is() )
94             throw DisposedException( ::rtl::OUString(), *this );
95 
96         try
97         {
98             if ( !m_xDest.is() )
99             {
100 	            if ( m_xDestContainer->hasByName( m_sName ) )
101 	            {
102 		            m_xDest.set( m_xDestContainer->getByName( m_sName ), UNO_QUERY_THROW );
103 	            }
104 	            else
105 	            {
106 		            Reference< XDataDescriptorFactory > xFactory( m_xDestContainer, UNO_QUERY_THROW );
107 		            m_xDest.set( xFactory->createDataDescriptor(), UNO_SET_THROW );
108 
109                     ::comphelper::copyProperties( m_xSource, m_xDest );
110 
111                     m_bInInsert = sal_True;
112 	                Reference< XAppend > xAppend( m_xDestContainer, UNO_QUERY_THROW );
113 	                xAppend->appendByDescriptor( m_xDest );
114 	                m_bInInsert = sal_False;
115 	            }
116 
117 	            m_xDestInfo.set( m_xDest->getPropertySetInfo(), UNO_SET_THROW );
118             }
119 
120             if ( m_xDestInfo->hasPropertyByName( evt.PropertyName ) )
121 	        {
122 		        m_xDest->setPropertyValue( evt.PropertyName, evt.NewValue );
123 	        }
124         }
125         catch( const Exception& )
126         {
127 	        DBG_UNHANDLED_EXCEPTION();
128         }
129     }
130 
131     // -----------------------------------------------------------------------------
disposing(const::com::sun::star::lang::EventObject &)132     void SAL_CALL OPropertyForward::disposing( const ::com::sun::star::lang::EventObject& /*_rSource*/ ) throw (RuntimeException)
133     {
134 	    ::osl::MutexGuard aGuard(m_aMutex);
135 
136         if ( !m_xSource.is() )
137             throw DisposedException( ::rtl::OUString(), *this );
138 
139 	    m_xSource->removePropertyChangeListener( ::rtl::OUString(), this );
140 	    m_xSource = NULL;
141 	    m_xDestContainer = NULL;
142 	    m_xDestInfo = NULL;
143 	    m_xDest = NULL;
144     }
145 
146     // -----------------------------------------------------------------------------
setDefinition(const::com::sun::star::uno::Reference<::com::sun::star::beans::XPropertySet> & _xDest)147     void OPropertyForward::setDefinition( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet>& _xDest )
148     {
149 	    ::osl::MutexGuard aGuard( m_aMutex );
150 	    if ( m_bInInsert )
151             return;
152 
153         OSL_ENSURE( !m_xDest.is(), "OPropertyForward::setDefinition: definition object is already set!" );
154         try
155         {
156 	        m_xDest.set( _xDest, UNO_SET_THROW );
157 	        m_xDestInfo.set( m_xDest->getPropertySetInfo(), UNO_SET_THROW );
158 	        ::comphelper::copyProperties( m_xDest, m_xSource );
159         }
160         catch( const Exception& )
161         {
162     	    DBG_UNHANDLED_EXCEPTION();
163         }
164     }
165 
166 //........................................................................
167 }	// namespace dbaccess
168 //........................................................................
169 
170