1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_dbaccess.hxx"
30 #ifndef DBAUI_TABLEWINDOWDATA_HXX
31 #include "TableWindowData.hxx"
32 #endif
33 #ifndef _TOOLS_DEBUG_HXX
34 #include <tools/debug.hxx>
35 #endif
36 #include <com/sun/star/sdb/XQueriesSupplier.hpp>
37 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
38 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
39 #include <com/sun/star/sdbcx/XKeysSupplier.hpp>
40 #include <com/sun/star/container/XNameAccess.hpp>
41 #include <com/sun/star/container/XIndexAccess.hpp>
42 
43 using namespace dbaui;
44 using namespace ::com::sun::star::lang;
45 using namespace ::com::sun::star::uno;
46 using namespace ::com::sun::star::sdb;
47 using namespace ::com::sun::star::sdbc;
48 using namespace ::com::sun::star::sdbcx;
49 using namespace ::com::sun::star::beans;
50 using namespace ::com::sun::star::container;
51 
52 //==================================================================
53 // class OTableWindowData
54 //==================================================================
55 DBG_NAME(OTableWindowData)
56 //------------------------------------------------------------------------------
57 OTableWindowData::OTableWindowData( const Reference< XPropertySet>& _xTable
58                                    ,const ::rtl::OUString& _rComposedName
59                                    ,const ::rtl::OUString& rTableName
60                                    ,const ::rtl::OUString& rWinName )
61     :m_xTable(_xTable)
62     ,m_aTableName( rTableName )
63 	,m_aWinName( rWinName )
64 	,m_sComposedName(_rComposedName)
65 	,m_aPosition( Point(-1,-1) )
66 	,m_aSize( Size(-1,-1) )
67 	,m_bShowAll( sal_True )
68     ,m_bIsQuery(false)
69     ,m_bIsValid(true)
70 {
71 	DBG_CTOR(OTableWindowData,NULL);
72 	if( !m_aWinName.getLength() )
73 		m_aWinName = m_aTableName;
74 
75     listen();
76 }
77 
78 //------------------------------------------------------------------------------
79 OTableWindowData::~OTableWindowData()
80 {
81 	DBG_DTOR(OTableWindowData,NULL);
82     Reference<XComponent> xComponent( m_xTable, UNO_QUERY );
83 	if ( xComponent.is() )
84 		stopComponentListening( xComponent );
85 }
86 
87 //------------------------------------------------------------------------------
88 sal_Bool OTableWindowData::HasPosition() const
89 {
90 	return ( (m_aPosition.X() != -1) && (m_aPosition.Y() != -1) );
91 }
92 
93 //------------------------------------------------------------------------------
94 sal_Bool OTableWindowData::HasSize() const
95 {
96 	return ( (m_aSize.Width() != -1) && (m_aSize.Height() !=-1) );
97 }
98 // -----------------------------------------------------------------------------
99 void OTableWindowData::_disposing( const ::com::sun::star::lang::EventObject& /*_rSource*/ )
100 {
101 	::osl::MutexGuard aGuard( m_aMutex );
102 	// it doesn't matter which one was disposed
103     m_xColumns.clear();
104     m_xKeys.clear();
105 	m_xTable.clear();
106 }
107 // -----------------------------------------------------------------------------
108 bool OTableWindowData::init(const Reference< XConnection  >& _xConnection,bool _bAllowQueries)
109 {
110     OSL_ENSURE(!m_xTable.is(),"We are already connected to a table!");
111 
112     ::osl::MutexGuard aGuard( m_aMutex );
113 
114 	Reference< XQueriesSupplier > xSupQueries( _xConnection, UNO_QUERY_THROW );
115     Reference< XNameAccess > xQueries( xSupQueries->getQueries(), UNO_QUERY_THROW );
116     bool bIsKnownQuery = _bAllowQueries && xQueries->hasByName( m_sComposedName );
117 
118     Reference< XTablesSupplier > xSupTables( _xConnection, UNO_QUERY_THROW );
119     Reference< XNameAccess > xTables( xSupTables->getTables(), UNO_QUERY_THROW );
120     bool bIsKnownTable = xTables->hasByName( m_sComposedName );
121 
122     if ( bIsKnownQuery )
123         m_xTable.set( xQueries->getByName( m_sComposedName ), UNO_QUERY );
124     else if ( bIsKnownTable )
125         m_xTable.set( xTables->getByName( m_sComposedName ), UNO_QUERY );
126     else
127         m_bIsValid = false;
128 
129     // if we survived so far, we know whether it's a query
130     m_bIsQuery = bIsKnownQuery;
131 
132     listen();
133 
134     Reference< XIndexAccess > xColumnsAsIndex( m_xColumns,UNO_QUERY );
135     return xColumnsAsIndex.is() && ( xColumnsAsIndex->getCount() > 0 );
136 }
137 // -----------------------------------------------------------------------------
138 void OTableWindowData::listen()
139 {
140     if ( m_xTable.is() )
141     {
142         // listen for the object being disposed
143 	    Reference<XComponent> xComponent( m_xTable, UNO_QUERY );
144 	    if ( xComponent.is() )
145 		    startComponentListening( xComponent );
146 
147         // obtain the columns
148         Reference< XColumnsSupplier > xColumnsSups( m_xTable, UNO_QUERY);
149         if ( xColumnsSups.is() )
150 	        m_xColumns = xColumnsSups->getColumns();
151 
152         Reference<XKeysSupplier> xKeySup(m_xTable,UNO_QUERY);
153 	    if ( xKeySup.is() )
154 		    m_xKeys = xKeySup->getKeys();
155     }
156 }
157 // -----------------------------------------------------------------------------
158