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 #ifndef DBAUI_TOKENWRITER_HXX
28 #define DBAUI_TOKENWRITER_HXX
29 
30 #include "DExport.hxx"
31 #include "moduledbu.hxx"
32 #include "commontypes.hxx"
33 
34 #include <com/sun/star/awt/FontDescriptor.hpp>
35 #include <com/sun/star/sdbc/XResultSet.hpp>
36 #include <com/sun/star/sdbc/XResultSetUpdate.hpp>
37 #include <com/sun/star/sdbc/XRow.hpp>
38 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
39 #include <com/sun/star/lang/XEventListener.hpp>
40 #include <com/sun/star/frame/XModel.hpp>
41 #include <com/sun/star/beans/PropertyValue.hpp>
42 #include <com/sun/star/sdb/CommandType.hpp>
43 #include <com/sun/star/sdbcx/XRowLocate.hpp>
44 
45 #include <cppuhelper/implbase1.hxx>
46 #include <tools/stream.hxx>
47 #include <svx/dataaccessdescriptor.hxx>
48 
49 #include <memory>
50 
51 namespace com { namespace sun { namespace star {
52     namespace sdbc{
53 	    class XRowUpdate;
54     }
55 }}}
56 
57 namespace dbaui
58 {
59 	// =========================================================================
60 	// ODatabaseImportExport Basisklasse f"ur Import/Export
61 	// =========================================================================
62 	class ODatabaseExport;
63 	typedef ::cppu::WeakImplHelper1< ::com::sun::star::lang::XEventListener> ODatabaseImportExport_BASE;
64 	class ODatabaseImportExport : public ODatabaseImportExport_BASE
65 	{
66     private:
67         void impl_initializeRowMember_throw();
68 
69     protected:
70         ::com::sun::star::lang::Locale	                                                m_aLocale;
71 		::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any>					m_aSelection;
72         sal_Bool                                                                        m_bBookmarkSelection;
73 		SvStream*																		m_pStream;
74 		::com::sun::star::awt::FontDescriptor											m_aFont;
75 		::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >		m_xObject;		// table/query
76 		SharedConnection                                                                m_xConnection;
77 		::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XResultSet >			m_xResultSet;
78 		::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XRow >				m_xRow;
79         ::com::sun::star::uno::Reference< ::com::sun::star::sdbcx::XRowLocate >         m_xRowLocate;
80 		::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XResultSetMetaData >	m_xResultSetMetaData;
81         ::com::sun::star::uno::Reference< ::com::sun::star::container::XIndexAccess >   m_xRowSetColumns;
82 		::com::sun::star::uno::Reference< ::com::sun::star::util::XNumberFormatter > 	m_xFormatter;	// a number formatter working with the connection's NumberFormatsSupplier
83 		::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory> m_xFactory;
84 
85 		::rtl::OUString m_sName;
86 		//dyf add 20070601
87 		//for transfor the tablename
88 		::rtl::OUString m_sDefaultTableName;
89 		//dyf add end
90 		::rtl::OUString m_sDataSourceName;
91 		sal_Int32		m_nCommandType;
92         bool            m_bNeedToReInitialize;
93 
94 #if defined UNX
95 		static const char __FAR_DATA sNewLine;
96 #else
97 		static const char __FAR_DATA sNewLine[];
98 #endif
99 
100 		ODatabaseExport*	m_pReader;
101 		sal_Int32*			m_pRowMarker; // wenn gesetzt, dann nur diese Rows kopieren
102         rtl_TextEncoding    m_eDestEnc;
103 		sal_Bool			m_bInInitialize;
104 		sal_Bool			m_bCheckOnly;
105 
106 		// export data
107 		ODatabaseImportExport(	const ::svx::ODataAccessDescriptor& _aDataDescriptor,
108 								const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rM,
109 								const ::com::sun::star::uno::Reference< ::com::sun::star::util::XNumberFormatter >& _rxNumberF,
110 								const String& rExchange = String());
111 
112 		// import data
113 		ODatabaseImportExport(	const SharedConnection& _rxConnection,
114 								const ::com::sun::star::uno::Reference< ::com::sun::star::util::XNumberFormatter >& _rxNumberF,
115 								const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rM);
116 
117 		virtual ~ODatabaseImportExport();
118 
119 		virtual void initialize();
120 	public:
121 		void setStream(SvStream* _pStream){  m_pStream = _pStream; }
122 
123 		//dyf add 20070601
124 		//for set the tablename
125 		void setSTableName(const ::rtl::OUString &_sTableName){ m_sDefaultTableName = _sTableName; }
126 		//dyf add end
127 
128 		virtual sal_Bool Write(); // Export
129 		virtual sal_Bool Read(); // Import
130 
131 		void initialize(const ::svx::ODataAccessDescriptor& _aDataDescriptor);
132         void dispose();
133 
134 		void enableCheckOnly() { m_bCheckOnly = sal_True; }
135 		sal_Bool isCheckEnabled() const { return m_bCheckOnly; }
136 
137 
138     private:
139         virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source ) throw(::com::sun::star::uno::RuntimeException);
140         void impl_initFromDescriptor( const ::svx::ODataAccessDescriptor& _aDataDescriptor, bool _bPlusDefaultInit );
141 	};
142 
143 	// =========================================================================
144 	// RTF Im- und Export
145 	// =========================================================================
146 
147 	class ORTFImportExport : public ODatabaseImportExport
148 	{
149         void appendRow(::rtl::OString* pHorzChar,sal_Int32 _nColumnCount,sal_Int32& k,sal_Int32& kk);
150 	public:
151 		// export data
152 		ORTFImportExport(	const ::svx::ODataAccessDescriptor& _aDataDescriptor,
153 							const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rM,
154 							const ::com::sun::star::uno::Reference< ::com::sun::star::util::XNumberFormatter >& _rxNumberF,
155 							const String& rExchange = String())
156 							: ODatabaseImportExport(_aDataDescriptor,_rM,_rxNumberF,rExchange) {};
157 
158 		// import data
159 		ORTFImportExport(	const SharedConnection& _rxConnection,
160 							const ::com::sun::star::uno::Reference< ::com::sun::star::util::XNumberFormatter >& _rxNumberF,
161 							const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rM)
162 						: ODatabaseImportExport(_rxConnection,_rxNumberF,_rM)
163 		{}
164 
165 		virtual sal_Bool Write();
166 		virtual sal_Bool Read();
167 	};
168 	// =========================================================================
169 	// HTML Im- und Export
170 	// =========================================================================
171 	#define SBA_HTML_FONTSIZES 7
172 	const sal_Int16 nIndentMax = 23;
173 	class OHTMLImportExport : public ODatabaseImportExport
174 	{
175 		// default HtmlFontSz[1-7]
176 		static const sal_Int16	nDefaultFontSize[SBA_HTML_FONTSIZES];
177 		// HtmlFontSz[1-7] in s*3.ini [user]
178 		static sal_Int16		nFontSize[SBA_HTML_FONTSIZES];
179 		static const sal_Int16	nCellSpacing;
180 		static const char __FAR_DATA sIndentSource[];
181 		char					sIndent[nIndentMax+1];
182 		sal_Int16				m_nIndent;
183 	#ifdef DBG_UTIL
184 		sal_Bool					m_bCheckFont;
185 	#endif
186 
187 		void WriteHeader();
188 		void WriteBody();
189 		void WriteTables();
190 		void WriteCell( sal_Int32 nFormat,sal_Int32 nWidthPixel,sal_Int32 nHeightPixel,const char* pChar,const String& rValue,const char* pHtmlTag);
191 		void IncIndent( sal_Int16 nVal );
192 		const char*			GetIndentStr() { return sIndent; }
193 		void FontOn();
194 		inline void FontOff();
195 
196 	public:
197 		// export data
198 		OHTMLImportExport(	const ::svx::ODataAccessDescriptor& _aDataDescriptor,
199 							const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rM,
200 							const ::com::sun::star::uno::Reference< ::com::sun::star::util::XNumberFormatter >& _rxNumberF,
201 							const String& rExchange = String());
202 		// import data
203 		OHTMLImportExport(	const SharedConnection& _rxConnection,
204 							const ::com::sun::star::uno::Reference< ::com::sun::star::util::XNumberFormatter >& _rxNumberF,
205 							const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rM)
206 						: ODatabaseImportExport(_rxConnection,_rxNumberF,_rM)
207 		{}
208 
209 		virtual sal_Bool Write();
210 		virtual sal_Bool Read();
211 
212 	};
213 	// =========================================================================
214 	// normal RowSet Im- und Export
215 	// =========================================================================
216 
217 	class ORowSetImportExport : public ODatabaseImportExport
218 	{
219 		OModuleClient		m_aModuleClient;
220 		::std::vector<sal_Int32>	m_aColumnMapping;
221 		::std::vector<sal_Int32>	m_aColumnTypes;
222 		::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XResultSetUpdate >	m_xTargetResultSetUpdate;	//
223 		::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XRowUpdate >			m_xTargetRowUpdate;			//
224 		::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XResultSetMetaData >	m_xTargetResultSetMetaData;	//
225 		Window*						m_pParent;
226 		sal_Bool					m_bAlreadyAsked;
227 
228 		sal_Bool insertNewRow();
229 	protected:
230 		virtual void initialize();
231 
232 	public:
233 		// export data
234 		ORowSetImportExport(Window* _pParent,
235 							const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XResultSetUpdate >& _xResultSetUpdate,
236 							const ::svx::ODataAccessDescriptor& _aDataDescriptor,
237 							const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rM,
238 							const String& rExchange = String());
239 
240 		// import data
241 		ORowSetImportExport(const SharedConnection& _rxConnection,
242 							const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rM)
243 						: ODatabaseImportExport(_rxConnection,NULL,_rM)
244 		{}
245 
246 		virtual sal_Bool Write();
247 		virtual sal_Bool Read();
248 
249     private:
250         using ODatabaseImportExport::initialize;
251 	};
252 
253 }
254 #endif // DBAUI_TOKENWRITER_HXX
255 
256 
257 
258