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 #ifndef DBAUI_TABLEWINDOWDATA_HXX
27 #include "TableWindowData.hxx"
28 #endif
29 #ifndef _TOOLS_DEBUG_HXX
30 #include <tools/debug.hxx>
31 #endif
32 #include <com/sun/star/sdb/XQueriesSupplier.hpp>
33 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
34 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
35 #include <com/sun/star/sdbcx/XKeysSupplier.hpp>
36 #include <com/sun/star/container/XNameAccess.hpp>
37 #include <com/sun/star/container/XIndexAccess.hpp>
38 
39 using namespace dbaui;
40 using namespace ::com::sun::star::lang;
41 using namespace ::com::sun::star::uno;
42 using namespace ::com::sun::star::sdb;
43 using namespace ::com::sun::star::sdbc;
44 using namespace ::com::sun::star::sdbcx;
45 using namespace ::com::sun::star::beans;
46 using namespace ::com::sun::star::container;
47 
48 //==================================================================
49 // class OTableWindowData
50 //==================================================================
51 DBG_NAME(OTableWindowData)
52 //------------------------------------------------------------------------------
53 OTableWindowData::OTableWindowData( const Reference< XPropertySet>& _xTable
54                                    ,const ::rtl::OUString& _rComposedName
55                                    ,const ::rtl::OUString& rTableName
56                                    ,const ::rtl::OUString& rWinName )
57     :m_xTable(_xTable)
58     ,m_aTableName( rTableName )
59 	,m_aWinName( rWinName )
60 	,m_sComposedName(_rComposedName)
61 	,m_aPosition( Point(-1,-1) )
62 	,m_aSize( Size(-1,-1) )
63 	,m_bShowAll( sal_True )
64     ,m_bIsQuery(false)
65     ,m_bIsValid(true)
66 {
67 	DBG_CTOR(OTableWindowData,NULL);
68 	if( !m_aWinName.getLength() )
69 		m_aWinName = m_aTableName;
70 
71     listen();
72 }
73 
74 //------------------------------------------------------------------------------
75 OTableWindowData::~OTableWindowData()
76 {
77 	DBG_DTOR(OTableWindowData,NULL);
78     Reference<XComponent> xComponent( m_xTable, UNO_QUERY );
79 	if ( xComponent.is() )
80 		stopComponentListening( xComponent );
81 }
82 
83 //------------------------------------------------------------------------------
84 sal_Bool OTableWindowData::HasPosition() const
85 {
86 	return ( (m_aPosition.X() != -1) && (m_aPosition.Y() != -1) );
87 }
88 
89 //------------------------------------------------------------------------------
90 sal_Bool OTableWindowData::HasSize() const
91 {
92 	return ( (m_aSize.Width() != -1) && (m_aSize.Height() !=-1) );
93 }
94 // -----------------------------------------------------------------------------
95 void OTableWindowData::_disposing( const ::com::sun::star::lang::EventObject& /*_rSource*/ )
96 {
97 	::osl::MutexGuard aGuard( m_aMutex );
98 	// it doesn't matter which one was disposed
99     m_xColumns.clear();
100     m_xKeys.clear();
101 	m_xTable.clear();
102 }
103 // -----------------------------------------------------------------------------
104 bool OTableWindowData::init(const Reference< XConnection  >& _xConnection,bool _bAllowQueries)
105 {
106     OSL_ENSURE(!m_xTable.is(),"We are already connected to a table!");
107 
108     ::osl::MutexGuard aGuard( m_aMutex );
109 
110 	Reference< XQueriesSupplier > xSupQueries( _xConnection, UNO_QUERY_THROW );
111     Reference< XNameAccess > xQueries( xSupQueries->getQueries(), UNO_QUERY_THROW );
112     bool bIsKnownQuery = _bAllowQueries && xQueries->hasByName( m_sComposedName );
113 
114     Reference< XTablesSupplier > xSupTables( _xConnection, UNO_QUERY_THROW );
115     Reference< XNameAccess > xTables( xSupTables->getTables(), UNO_QUERY_THROW );
116     bool bIsKnownTable = xTables->hasByName( m_sComposedName );
117 
118     if ( bIsKnownQuery )
119         m_xTable.set( xQueries->getByName( m_sComposedName ), UNO_QUERY );
120     else if ( bIsKnownTable )
121         m_xTable.set( xTables->getByName( m_sComposedName ), UNO_QUERY );
122     else
123         m_bIsValid = false;
124 
125     // if we survived so far, we know whether it's a query
126     m_bIsQuery = bIsKnownQuery;
127 
128     listen();
129 
130     Reference< XIndexAccess > xColumnsAsIndex( m_xColumns,UNO_QUERY );
131     return xColumnsAsIndex.is() && ( xColumnsAsIndex->getCount() > 0 );
132 }
133 // -----------------------------------------------------------------------------
134 void OTableWindowData::listen()
135 {
136     if ( m_xTable.is() )
137     {
138         // listen for the object being disposed
139 	    Reference<XComponent> xComponent( m_xTable, UNO_QUERY );
140 	    if ( xComponent.is() )
141 		    startComponentListening( xComponent );
142 
143         // obtain the columns
144         Reference< XColumnsSupplier > xColumnsSups( m_xTable, UNO_QUERY);
145         if ( xColumnsSups.is() )
146 	        m_xColumns = xColumnsSups->getColumns();
147 
148         Reference<XKeysSupplier> xKeySup(m_xTable,UNO_QUERY);
149 	    if ( xKeySup.is() )
150 		    m_xKeys = xKeySup->getKeys();
151     }
152 }
153 // -----------------------------------------------------------------------------
154