xref: /aoo42x/main/sw/inc/dbmgr.hxx (revision 1d2dbeb0)
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 #ifndef _SWDBMGR_HXX
24 #define _SWDBMGR_HXX
25 
26 
27 #include <tools/string.hxx>
28 #include <tools/link.hxx>
29 #include <svl/svarray.hxx>
30 #include <com/sun/star/util/Date.hpp>
31 #include "swdllapi.h"
32 #include <swdbdata.hxx>
33 #include <com/sun/star/uno/Reference.h>
34 #include <com/sun/star/uno/Sequence.hxx>
35 #include <com/sun/star/lang/Locale.hpp>
36 #include <com/sun/star/beans/PropertyValue.hpp>
37 namespace com{namespace sun{namespace star{
38 	namespace sdbc{
39 		class XConnection;
40 		class XStatement;
41 		class XDataSource;
42 		class XResultSet;
43 	}
44 	namespace beans{
45 
46 		class XPropertySet;
47 		struct PropertyValue;
48 	}
49 	namespace sdbcx{
50 		class XColumnsSupplier;
51 	}
52 	namespace util{
53 		class XNumberFormatter;
54 	}
55     namespace mail{
56         class XSmtpService;
57     }
58 }}}
59 namespace svx {
60 	class ODataAccessDescriptor;
61 }
62 
63 struct SwDBFormatData
64 {
65 	com::sun::star::util::Date aNullDate;
66 	com::sun::star::uno::Reference< com::sun::star::util::XNumberFormatter> xFormatter;
67 	com::sun::star::lang::Locale aLocale;
68 };
69 
70 class SwView;
71 class SwWrtShell;
72 class SfxProgress;
73 class ListBox;
74 class Button;
75 class SvNumberFormatter;
76 class SwDbtoolsClient;
77 class SwXMailMerge;
78 class SwMailMergeConfigItem;
79 
80 // -----------------------------------------------------------------------
81 
82 enum DBMgrOptions
83 {
84 	DBMGR_MERGE,			// Datensaetze in Felder
85 	DBMGR_INSERT,			// Datensaetze in Text
86 	DBMGR_MERGE_MAILMERGE,	// Serienbriefe drucken
87 	DBMGR_MERGE_MAILING,	// Serienbriefe als email versenden
88     DBMGR_MERGE_MAILFILES,  // Serienbriefe als Datei(en) speichern
89     DBMGR_MERGE_DOCUMENTS,   // Print merged documents
90     DBMGR_MERGE_SINGLE_FILE  // save merge as single file
91 };
92 
93 // -----------------------------------------------------------------------
94 
95 /*--------------------------------------------------------------------
96 	 Beschreibung: (neue) Logische Datenbanken verwalten
97  --------------------------------------------------------------------*/
98 #define SW_DB_SELECT_UNKNOWN 	0
99 #define SW_DB_SELECT_TABLE		1
100 #define SW_DB_SELECT_QUERY      2
101 
102 struct SwDSParam : public SwDBData
103 {
104 	com::sun::star::util::Date 	aNullDate;
105 
106 	::com::sun::star::uno::Reference<com::sun::star::util::XNumberFormatter> 	xFormatter;
107 	::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection> 		xConnection;
108 	::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XStatement>		xStatement;
109 	::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XResultSet>   	xResultSet;
110 	::com::sun::star::uno::Sequence<  ::com::sun::star::uno::Any > 				aSelection;
111 	sal_Bool bScrollable;
112 	sal_Bool bEndOfDB;
113 	sal_Bool bAfterSelection;
114 	long nSelectionIndex;
115 
SwDSParamSwDSParam116     SwDSParam(const SwDBData& rData) :
117         SwDBData(rData),
118         bScrollable(sal_False),
119 		bEndOfDB(sal_False),
120 		bAfterSelection(sal_False),
121 		nSelectionIndex(0)
122 		{}
123 
SwDSParamSwDSParam124     SwDSParam(const SwDBData& rData,
125 		const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XResultSet>&   	xResSet,
126 		const ::com::sun::star::uno::Sequence<  ::com::sun::star::uno::Any >& 	rSelection) :
127         SwDBData(rData),
128 		xResultSet(xResSet),
129 		aSelection(rSelection),
130 		bScrollable(sal_True),
131 		bEndOfDB(sal_False),
132 		bAfterSelection(sal_False),
133 		nSelectionIndex(0)
134 		{}
135 
CheckEndOfDBSwDSParam136 		void CheckEndOfDB()
137 		{
138 			if(bEndOfDB)
139 				bAfterSelection = sal_True;
140 		}
141 };
142 typedef SwDSParam* SwDSParamPtr;
143 SV_DECL_PTRARR_DEL(SwDSParamArr, SwDSParamPtr, 0, 5)
144 
145 struct SwMergeDescriptor
146 {
147     sal_uInt16                                              nMergeType;
148     SwWrtShell&                                         rSh;
149     const ::svx::ODataAccessDescriptor&                 rDescriptor;
150     String                                              sSaveToFilter; //export filter to save resulting files
151     String                                              sSaveToFilterOptions; //
152     com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue > aSaveToFilterData;
153 
154     String                                              sSubject;
155     String                                              sAddressFromColumn;
156     String                                              sMailBody;
157     String                                              sAttachmentName;
158     ::com::sun::star::uno::Sequence< ::rtl::OUString >  aCopiesTo;
159     ::com::sun::star::uno::Sequence< ::rtl::OUString >  aBlindCopiesTo;
160 
161     ::com::sun::star::uno::Reference< com::sun::star::mail::XSmtpService > xSmtpServer;
162 
163     sal_Bool                                            bSendAsHTML;
164     sal_Bool                                            bSendAsAttachment;
165 
166     sal_Bool                                            bPrintAsync;
167     sal_Bool                                            bCreateSingleFile;
168 
169     SwMailMergeConfigItem*                              pMailMergeConfigItem;
170 
171     ::com::sun::star::uno::Sequence<  ::com::sun::star::beans::PropertyValue >  aPrintOptions;
172 
SwMergeDescriptorSwMergeDescriptor173     SwMergeDescriptor( sal_uInt16 nType, SwWrtShell& rShell, ::svx::ODataAccessDescriptor& rDesc ) :
174         nMergeType(nType),
175         rSh(rShell),
176         rDescriptor(rDesc),
177         bSendAsHTML( sal_True ),
178         bSendAsAttachment( sal_False ),
179         bPrintAsync( sal_False ),
180         bCreateSingleFile( sal_False ),
181         pMailMergeConfigItem(0)
182         {}
183 
184 };
185 
186 struct SwNewDBMgr_Impl;
187 class SwConnectionDisposedListener_Impl;
188 class AbstractMailMergeDlg;
189 
190 class SW_DLLPUBLIC SwNewDBMgr
191 {
192 friend class SwConnectionDisposedListener_Impl;
193 
194     static SwDbtoolsClient* pDbtoolsClient;
195 
196     String              sEMailAddrFld;  // Mailing: Spaltenname der E-Mail Adresse
197 	String				sSubject;		// Mailing: Subject
198 	String				sAttached;		// Mailing: Attachte Files
199 	sal_uInt16				nMergeType;
200 	sal_Bool				bInitDBFields : 1;	// sal_True: Datenbank an Feldern beim Mergen
201     sal_Bool                bSingleJobs : 1;    // Einzelne Druckjobs bei Aufruf aus Basic
202 	sal_Bool				bCancel : 1;		// Serienbrief-Save abgebrochen
203 
204 	sal_Bool 				bInMerge	: 1;	//merge process active
205     sal_Bool                bMergeSilent : 1;   // suppress display of dialogs/boxes (used when called over API)
206     sal_Bool                bMergeLock : 1;     // prevent update of database fields while document is
207                                             // actually printed at the ViewShell
208 	SwDSParamArr		aDataSourceParams;
209     SwNewDBMgr_Impl*    pImpl;
210     const SwXMailMerge* pMergeEvtSrc;   // != 0 if mail merge events are to be send
211 
212     SW_DLLPRIVATE SwDSParam*          FindDSData(const SwDBData& rData, sal_Bool bCreate);
213     SW_DLLPRIVATE SwDSParam*          FindDSConnection(const ::rtl::OUString& rSource, sal_Bool bCreate);
214 
215 
216 	SW_DLLPRIVATE DECL_LINK( PrtCancelHdl, Button * );
217 
218 	// Datensaetze als Text ins Dokument einfuegen
219 	SW_DLLPRIVATE void ImportFromConnection( SwWrtShell* pSh);
220 
221 	// Einzelnen Datensatz als Text ins Dokument einfuegen
222 	SW_DLLPRIVATE void ImportDBEntry(SwWrtShell* pSh);
223 
224     // merge to file _and_ merge to e-Mail
225     SW_DLLPRIVATE sal_Bool          MergeMailFiles(SwWrtShell* pSh,
226                                         const SwMergeDescriptor& rMergeDescriptor );
227     SW_DLLPRIVATE sal_Bool          ToNextRecord(SwDSParam* pParam);
228 
229 public:
230 	SwNewDBMgr();
231 	~SwNewDBMgr();
232 
233 	// Art des aktellen Mergens. Siehe DBMgrOptions-enum
GetMergeType() const234 	inline sal_uInt16	GetMergeType() const			{ return nMergeType; }
SetMergeType(sal_uInt16 nTyp)235 	inline void 	SetMergeType( sal_uInt16 nTyp ) 	{ nMergeType = nTyp; }
236 
237     // MailMergeEvent source
GetMailMergeEvtSrc() const238     const SwXMailMerge *    GetMailMergeEvtSrc() const  { return pMergeEvtSrc; }
SetMailMergeEvtSrc(const SwXMailMerge * pSrc)239     void SetMailMergeEvtSrc( const SwXMailMerge *pSrc ) { pMergeEvtSrc = pSrc; }
240 
IsMergeSilent() const241     inline sal_Bool     IsMergeSilent() const           { return bMergeSilent != 0; }
SetMergeSilent(sal_Bool bVal)242     inline void     SetMergeSilent( sal_Bool bVal )     { bMergeSilent = bVal; }
243 
244 	// Mischen von Datensaetzen in Felder
245     sal_Bool            MergeNew( const SwMergeDescriptor& rMergeDesc );
246     sal_Bool            Merge(SwWrtShell* pSh);
247 
248     // Datenbankfelder mit fehlendem Datenbankname initialisieren
IsInitDBFields() const249 	inline sal_Bool 	IsInitDBFields() const 	{ return bInitDBFields;	}
SetInitDBFields(sal_Bool b)250 	inline void 	SetInitDBFields(sal_Bool b)	{ bInitDBFields = b;	}
251 
252 	// Serienbriefe einzelnd oder alle zusammen drucken/speichern
IsSingleJobs() const253     inline sal_Bool     IsSingleJobs() const    { return bSingleJobs;   }
SetSingleJobs(sal_Bool b)254     inline void     SetSingleJobs(sal_Bool b)   { bSingleJobs = b;  }
255 
256 	// Mailing
257 	// email-Daten setzen
SetEMailColumn(const String & sColName)258 	inline void		SetEMailColumn(const String& sColName) { sEMailAddrFld = sColName; }
SetSubject(const String & sSbj)259 	inline void		SetSubject(const String& sSbj) { sSubject = sSbj; }
SetAttachment(const String & sAtt)260 	inline void		SetAttachment(const String& sAtt) { sAttached = sAtt; }
261 
262 
263 	// Listbox mit allen Tabellennamen einer Datenbank fuellen
264 	sal_Bool			GetTableNames(ListBox* pListBox, const String& rDBName );
265 
266 	// Listbox mit allen Spaltennamen einer Datenbanktabelle fuellen
267 	sal_Bool			GetColumnNames(ListBox* pListBox,
268 						const String& rDBName, const String& rTableName, sal_Bool bAppend = sal_False);
269     sal_Bool            GetColumnNames(ListBox* pListBox,
270                         ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection> xConnection,
271                         const String& rTableName, sal_Bool bAppend = sal_False);
272 
273     sal_uLong GetColumnFmt( ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XDataSource> xSource,
274                         ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection> xConnection,
275                         ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet> xColumn,
276 						SvNumberFormatter* pNFmtr,
277 						long nLanguage );
278 
279     sal_uLong GetColumnFmt( const String& rDBName,
280 						const String& rTableName,
281 						const String& rColNm,
282 						SvNumberFormatter* pNFmtr,
283 						long nLanguage );
284 	sal_Int32 GetColumnType( const String& rDBName,
285 						  const String& rTableName,
286 						  const String& rColNm );
287 
IsInMerge() const288 	inline sal_Bool		IsInMerge() const	{ return bInMerge; }
289 	void			EndMerge();
290 
291 	void 			ExecuteFormLetter(SwWrtShell& rSh,
292                         const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue>& rProperties,
293                         sal_Bool bWithDataSourceBrowser = sal_False);
294 
295 	void 			InsertText(SwWrtShell& rSh,
296 						const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue>& rProperties);
297 
298     // check if a data source is open
299     sal_Bool            IsDataSourceOpen(const String& rDataSource,
300                                     const String& rTableOrQuery, sal_Bool bMergeOnly);
301 
302 	// open the source while fields are updated - for the calculator only!
303     sal_Bool            OpenDataSource(const String& rDataSource, const String& rTableOrQuery,
304                         sal_Int32 nCommandType = -1, bool bCreate = false);
305     sal_uInt32      GetSelectedRecordId(const String& rDataSource, const String& rTableOrQuery, sal_Int32 nCommandType = -1);
306 	sal_Bool			GetColumnCnt(const String& rSourceName, const String& rTableName,
307 							const String& rColumnName, sal_uInt32 nAbsRecordId, long nLanguage,
308 							String& rResult, double* pNumber);
309     //create and store or find an already stored connection to a data source for use
310     //in SwFldMgr and SwDBTreeList
311     ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection>
312                     RegisterConnection(::rtl::OUString& rSource);
313 
CreateDSData(const SwDBData & rData)314     const SwDSParam* CreateDSData(const SwDBData& rData)
315                         {return FindDSData(rData, sal_True);}
GetDSParamArray() const316 	const SwDSParamArr& GetDSParamArray() const {return aDataSourceParams;}
317 
318 
319 	//close all data sources - after fields were updated
320 	void			CloseAll(sal_Bool bIncludingMerge = sal_True);
321 
322 	sal_Bool			GetMergeColumnCnt(const String& rColumnName, sal_uInt16 nLanguage,
323 								String &rResult, double *pNumber, sal_uInt32 *pFormat);
324 	sal_Bool			ToNextMergeRecord();
325     sal_Bool            ToNextRecord(const String& rDataSource, const String& rTableOrQuery, sal_Int32 nCommandType = -1);
326 
327 	sal_Bool			ExistsNextRecord()const;
328 	sal_uInt32 		GetSelectedRecordId();
329 	sal_Bool 		ToRecordId(sal_Int32 nSet);
330 
331 	const SwDBData&	GetAddressDBName();
332 
333 	static String GetDBField(
334 					::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet > xColumnProp,
335 					const SwDBFormatData& rDBFormatData,
336 					double *pNumber = NULL);
337 
338 	static ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection>
339 			GetConnection(const String& rDataSource,
340 				::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XDataSource>& rxSource);
341 
342 	static ::com::sun::star::uno::Reference< ::com::sun::star::sdbcx::XColumnsSupplier>
343 			GetColumnSupplier(::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection>,
344 									const String& rTableOrQuery,
345 									sal_uInt8	eTableOrQuery = SW_DB_SELECT_UNKNOWN);
346 
347 	static ::com::sun::star::uno::Sequence<rtl::OUString> GetExistingDatabaseNames();
348 
349     /**
350      Loads a data source from file and registers it. Returns the registered name.
351      */
352     static String               LoadAndRegisterDataSource();
353 
354     static SwDbtoolsClient&    GetDbtoolsClient();
355     // has to be called from _FinitUI()
356     static void                RemoveDbtoolsClient();
357 
358 	/** try to get the data source from the given connection through the XChild interface.
359 		If this is not possible, the data source will be created through its name.
360 		@param _xConnection
361 			The connection which should support the XChild interface. (not a must)
362 		@param _sDataSourceName
363 			The data source name will be used to create the data source when the connection can not be used for it.
364 		@return
365 			The data source.
366 	*/
367 	static ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XDataSource>
368 			getDataSourceAsParent(const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection>& _xConnection,const ::rtl::OUString& _sDataSourceName);
369 
370 	/** creates a RowSet, which must be disposed after use.
371 		@param	_sDataSourceName
372 			The data source name
373 		@param	_sCommand
374 			The command.
375 		@param	_nCommandType
376 			The type of the command.
377 		@param	_xConnection
378 			The active connection which may be <NULL/>.
379 		@return
380 			The new created RowSet.
381 
382 	*/
383 	static ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XResultSet>
384 			createCursor(	const ::rtl::OUString& _sDataSourceName,
385 							const ::rtl::OUString& _sCommand,
386 							sal_Int32 _nCommandType,
387 							const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection>& _xConnection
388 							);
389     //merge into one document - returns the number of merged docs
390     sal_Int32 MergeDocuments( SwMailMergeConfigItem& rMMConfig, SwView& rSourceView );
391 };
392 
393 #endif
394 
395