Bump for 3.6-28
[LibreOffice.git] / extensions / source / bibliography / datman.cxx
blobb036674ff92da4fb4523acb8d0b9f43e938f00d1
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include <osl/mutex.hxx>
30 #include <tools/diagnose_ex.h>
31 #include <comphelper/processfactory.hxx>
32 #include <com/sun/star/io/XPersistObject.hpp>
33 #include <com/sun/star/sdbc/ResultSetType.hpp>
34 #include <com/sun/star/sdbc/ResultSetConcurrency.hpp>
35 #include <com/sun/star/sdbc/XResultSetUpdate.hpp>
36 #include <com/sun/star/sdbcx/XRowLocate.hpp>
37 #include <com/sun/star/sdbc/DataType.hpp>
38 #include <com/sun/star/sdb/XSingleSelectQueryComposer.hpp>
39 #include <com/sun/star/sdbc/XDatabaseMetaData.hpp>
40 #include <com/sun/star/sdb/XDatabaseEnvironment.hpp>
41 #include <com/sun/star/uno/XNamingService.hpp>
42 #include <com/sun/star/sdbc/XDataSource.hpp>
43 #include <com/sun/star/sdb/CommandType.hpp>
44 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
45 #include <com/sun/star/sdbc/XConnection.hpp>
46 #include <com/sun/star/sdb/XCompletedConnection.hpp>
47 #include <com/sun/star/task/XInteractionHandler.hpp>
48 #include <com/sun/star/form/XLoadable.hpp>
49 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
50 #include <com/sun/star/form/XGridColumnFactory.hpp>
51 #include <com/sun/star/io/XDataInputStream.hpp>
52 #include <com/sun/star/container/XNameContainer.hpp>
53 #include <com/sun/star/ucb/XContentProvider.hpp>
54 #include <com/sun/star/ucb/XContentAccess.hpp>
55 #include <ucbhelper/contentbroker.hxx>
56 #include <ucbhelper/content.hxx>
57 #include <ucbhelper/contentidentifier.hxx>
58 #include <comphelper/container.hxx>
59 #include <svl/urihelper.hxx>
60 #include <svtools/svtabbx.hxx>
61 #include <svtools/headbar.hxx>
62 #include <vcl/dialog.hxx>
63 #include <vcl/button.hxx>
64 #include <vcl/fixed.hxx>
65 #include <vcl/group.hxx>
66 #include <vcl/lstbox.hxx>
67 #include <vcl/edit.hxx>
68 #include <vcl/msgbox.hxx>
69 #include <tools/debug.hxx>
70 #include "datman.hxx"
71 #include "bibresid.hxx"
72 #include "bibmod.hxx"
73 #include "bibview.hxx"
74 // #100312# ---------
75 #include "bibprop.hrc"
76 #include "toolbar.hxx"
77 #include "toolbar.hrc"
78 #include "bibconfig.hxx"
79 #include "bibbeam.hxx"
80 #include "bib.hrc"
81 #include "datman.hrc"
82 #include "bibliography.hrc"
83 #include <connectivity/dbtools.hxx>
85 using namespace ::com::sun::star;
86 using namespace ::com::sun::star::beans;
87 using namespace ::com::sun::star::container;
88 using namespace ::com::sun::star::uno;
89 using namespace ::com::sun::star::sdb;
90 using namespace ::com::sun::star::sdbc;
91 using namespace ::com::sun::star::sdbcx;
92 using namespace ::com::sun::star::form;
93 using namespace ::com::sun::star::frame;
94 using namespace ::com::sun::star::ucb;
95 using namespace ::com::sun::star::lang;
96 using namespace ::ucb;
98 #define C2U(cChar) ::rtl::OUString::createFromAscii(cChar)
99 #define C2S(cChar) String::CreateFromAscii(cChar)
100 #define MAP_TOKEN ';'
101 #define PAIR_TOKEN ':'
103 Reference< XConnection > getConnection(const ::rtl::OUString& _rURL)
105 // first get the sdb::DataSource corresponding to the url
106 Reference< XDataSource > xDataSource;
107 // is it a favorite title ?
108 Reference< XMultiServiceFactory > xMgr = comphelper::getProcessServiceFactory();
109 Reference<XInterface> xNamingContextIfc = xMgr->createInstance(C2U("com.sun.star.sdb.DatabaseContext"));
110 Reference< XNameAccess > xNamingContext(xNamingContextIfc, UNO_QUERY);
111 if (xNamingContext.is() && xNamingContext->hasByName(_rURL))
113 DBG_ASSERT(Reference< XNamingService > (xNamingContext, UNO_QUERY).is(), "::getDataSource : no NamingService interface on the sdb::DatabaseAccessContext !");
116 xDataSource = Reference< XDataSource > (Reference< XNamingService > (xNamingContext, UNO_QUERY)->getRegisteredObject(_rURL), UNO_QUERY);
118 catch (const Exception&)
120 OSL_FAIL("Exception caught in ODatabaseContext::getRegisteredObject()");
123 // build the connection from the data source
124 Reference< XConnection > xConn;
125 if (xDataSource.is())
127 // need user/pwd for this
128 Reference< XPropertySet > xDataSourceProps(xDataSource, UNO_QUERY);
129 Reference< XCompletedConnection > xComplConn(xDataSource, UNO_QUERY);
133 Reference<XInterface> xHdl = xMgr->createInstance(C2U("com.sun.star.task.InteractionHandler"));
134 Reference<task::XInteractionHandler> xIHdl(xHdl, UNO_QUERY);
135 xConn = xComplConn->connectWithCompletion(xIHdl);
137 catch (const SQLException&)
139 // TODO : a real error handling
141 catch (const Exception&)
145 return xConn;
148 Reference< XConnection > getConnection(const Reference< XInterface > & xRowSet)
150 Reference< XConnection > xConn;
153 Reference< XPropertySet > xFormProps(xRowSet, UNO_QUERY);
154 if (!xFormProps.is())
155 return xConn;
157 xConn = Reference< XConnection > (*(Reference< XInterface > *)xFormProps->getPropertyValue(C2U("ActiveConnection")).getValue(), UNO_QUERY);
158 if (!xConn.is())
160 DBG_WARNING("no active connection");
163 catch (const Exception&)
165 OSL_FAIL("exception in getConnection");
168 return xConn;
171 Reference< XNameAccess > getColumns(const Reference< XForm > & _rxForm)
173 Reference< XNameAccess > xReturn;
174 // check if the form is alive
175 Reference< XColumnsSupplier > xSupplyCols( _rxForm, UNO_QUERY );
176 if (xSupplyCols.is())
177 xReturn = xSupplyCols->getColumns();
179 if (!xReturn.is() || (xReturn->getElementNames().getLength() == 0))
180 { // no ....
181 xReturn = NULL;
182 // -> get the table the form is bound to and ask it for their columns
183 Reference< XTablesSupplier > xSupplyTables( getConnection( _rxForm ), UNO_QUERY );
184 Reference< XPropertySet > xFormProps( _rxForm, UNO_QUERY );
185 if (xFormProps.is() && xSupplyTables.is())
189 DBG_ASSERT((*(sal_Int32*)xFormProps->getPropertyValue(C2U("CommandType")).getValue()) == CommandType::TABLE,
190 "::getColumns : invalid form (has no table as data source) !");
191 ::rtl::OUString sTable;
192 xFormProps->getPropertyValue(C2U("Command")) >>= sTable;
193 Reference< XNameAccess > xTables = xSupplyTables->getTables();
194 if (xTables.is() && xTables->hasByName(sTable))
195 xSupplyCols = Reference< XColumnsSupplier > (
196 *(Reference< XInterface > *)xTables->getByName(sTable).getValue(), UNO_QUERY);
197 if (xSupplyCols.is())
198 xReturn = xSupplyCols->getColumns();
200 catch (const Exception& e)
202 #ifdef DBG_UTIL
203 String sMsg(String::CreateFromAscii("::getColumns : catched an exception ("));
204 sMsg += String(e.Message);
205 sMsg.AppendAscii(") ...");
206 OSL_FAIL(rtl::OUStringToOString(sMsg, RTL_TEXTENCODING_ASCII_US ).getStr());
207 #else
208 (void)e;
209 #endif
214 return xReturn;
217 class MappingDialog_Impl : public ModalDialog
219 BibDataManager* pDatMan;
220 OKButton aOKBT;
221 CancelButton aCancelBT;
222 HelpButton aHelpBT;
223 FixedLine aMapGB;
224 FixedText aIdentifierFT;
225 ListBox aIdentifierLB;
226 FixedText aAuthorityTypeFT;
227 ListBox aAuthorityTypeLB;
228 FixedText aAuthorFT;
229 ListBox aAuthorLB;
230 FixedText aTitleFT;
231 ListBox aTitleLB;
232 FixedText aMonthFT;
233 ListBox aMonthLB;
234 FixedText aYearFT;
235 ListBox aYearLB;
236 FixedText aISBNFT;
237 ListBox aISBNLB;
238 FixedText aBooktitleFT;
239 ListBox aBooktitleLB;
240 FixedText aChapterFT;
241 ListBox aChapterLB;
242 FixedText aEditionFT;
243 ListBox aEditionLB;
244 FixedText aEditorFT;
245 ListBox aEditorLB;
246 FixedText aHowpublishedFT;
247 ListBox aHowpublishedLB;
248 FixedText aInstitutionFT;
249 ListBox aInstitutionLB;
250 FixedText aJournalFT;
251 ListBox aJournalLB;
252 FixedText aNoteFT;
253 ListBox aNoteLB;
254 FixedText aAnnoteFT;
255 ListBox aAnnoteLB;
256 FixedText aNumberFT;
257 ListBox aNumberLB;
258 FixedText aOrganizationsFT;
259 ListBox aOrganizationsLB;
260 FixedText aPagesFT;
261 ListBox aPagesLB;
262 FixedText aPublisherFT;
263 ListBox aPublisherLB;
264 FixedText aAddressFT;
265 ListBox aAddressLB;
266 FixedText aSchoolFT;
267 ListBox aSchoolLB;
268 FixedText aSeriesFT;
269 ListBox aSeriesLB;
270 FixedText aReportTypeFT;
271 ListBox aReportTypeLB;
272 FixedText aVolumeFT;
273 ListBox aVolumeLB;
274 FixedText aURLFT;
275 ListBox aURLLB;
276 FixedText aCustom1FT;
277 ListBox aCustom1LB;
278 FixedText aCustom2FT;
279 ListBox aCustom2LB;
280 FixedText aCustom3FT;
281 ListBox aCustom3LB;
282 FixedText aCustom4FT;
283 ListBox aCustom4LB;
284 FixedText aCustom5FT;
285 ListBox aCustom5LB;
286 ListBox* aListBoxes[COLUMN_COUNT];
287 String sNone;
289 sal_Bool bModified;
293 DECL_LINK(OkHdl, void *);
294 DECL_LINK(ListBoxSelectHdl, ListBox*);
296 public:
297 MappingDialog_Impl(Window* pParent, BibDataManager* pDatMan);
298 ~MappingDialog_Impl();
300 void SetModified() {bModified = sal_True;}
304 sal_uInt16 lcl_FindLogicalName(BibConfig* pConfig ,
305 const ::rtl::OUString& rLogicalColumnName)
307 for(sal_uInt16 i = 0; i < COLUMN_COUNT; i++)
309 if(rLogicalColumnName == pConfig->GetDefColumnName(i))
310 return i;
312 return USHRT_MAX;
314 //-----------------------------------------------------------------------------
315 MappingDialog_Impl::MappingDialog_Impl(Window* pParent, BibDataManager* pMan) :
316 ModalDialog(pParent, BibResId(RID_DLG_MAPPING) ),
317 pDatMan(pMan),
318 aOKBT(this, BibResId( BT_OK )),
319 aCancelBT(this, BibResId( BT_CANCEL )),
320 aHelpBT(this, BibResId( BT_HELP )),
322 aMapGB(this, BibResId( GB_MAPPING )),
324 aIdentifierFT(this, BibResId( FT_IDENTIFIER )),
325 aIdentifierLB(this, BibResId( LB_IDENTIFIER )),
326 aAuthorityTypeFT(this, BibResId( FT_AUTHORITYTYPE )),
327 aAuthorityTypeLB(this, BibResId( LB_AUTHORITYTYPE )),
328 aAuthorFT(this, BibResId( FT_AUTHOR )),
329 aAuthorLB(this, BibResId( LB_AUTHOR )),
330 aTitleFT(this, BibResId( FT_TITLE )),
331 aTitleLB(this, BibResId( LB_TITLE )),
332 aMonthFT(this, BibResId( FT_MONTH )),
333 aMonthLB(this, BibResId( LB_MONTH )),
334 aYearFT(this, BibResId( FT_YEAR )),
335 aYearLB(this, BibResId( LB_YEAR )),
336 aISBNFT(this, BibResId( FT_ISBN )),
337 aISBNLB(this, BibResId( LB_ISBN )),
338 aBooktitleFT(this, BibResId( FT_BOOKTITLE )),
339 aBooktitleLB(this, BibResId( LB_BOOKTITLE )),
340 aChapterFT(this, BibResId( FT_CHAPTER )),
341 aChapterLB(this, BibResId( LB_CHAPTER )),
342 aEditionFT(this, BibResId( FT_EDITION )),
343 aEditionLB(this, BibResId( LB_EDITION )),
344 aEditorFT(this, BibResId( FT_EDITOR )),
345 aEditorLB(this, BibResId( LB_EDITOR )),
346 aHowpublishedFT(this, BibResId( FT_HOWPUBLISHED )),
347 aHowpublishedLB(this, BibResId( LB_HOWPUBLISHED )),
348 aInstitutionFT(this, BibResId( FT_INSTITUTION )),
349 aInstitutionLB(this, BibResId( LB_INSTITUTION )),
350 aJournalFT(this, BibResId( FT_JOURNAL )),
351 aJournalLB(this, BibResId( LB_JOURNAL )),
352 aNoteFT(this, BibResId( FT_NOTE )),
353 aNoteLB(this, BibResId( LB_NOTE )),
354 aAnnoteFT(this, BibResId( FT_ANNOTE )),
355 aAnnoteLB(this, BibResId( LB_ANNOTE )),
356 aNumberFT(this, BibResId( FT_NUMBER )),
357 aNumberLB(this, BibResId( LB_NUMBER )),
358 aOrganizationsFT(this, BibResId( FT_ORGANIZATIONS )),
359 aOrganizationsLB(this, BibResId( LB_ORGANIZATIONS )),
360 aPagesFT(this, BibResId( FT_PAGES )),
361 aPagesLB(this, BibResId( LB_PAGES )),
362 aPublisherFT(this, BibResId( FT_PUBLISHER )),
363 aPublisherLB(this, BibResId( LB_PUBLISHER )),
364 aAddressFT(this, BibResId( FT_ADDRESS )),
365 aAddressLB(this, BibResId( LB_ADDRESS )),
366 aSchoolFT(this, BibResId( FT_SCHOOL )),
367 aSchoolLB(this, BibResId( LB_SCHOOL )),
368 aSeriesFT(this, BibResId( FT_SERIES )),
369 aSeriesLB(this, BibResId( LB_SERIES )),
370 aReportTypeFT(this, BibResId( FT_REPORTTYPE )),
371 aReportTypeLB(this, BibResId( LB_REPORTTYPE )),
372 aVolumeFT(this, BibResId( FT_VOLUME )),
373 aVolumeLB(this, BibResId( LB_VOLUME )),
374 aURLFT(this, BibResId( FT_URL )),
375 aURLLB(this, BibResId( LB_URL )),
376 aCustom1FT(this, BibResId( FT_CUSTOM1 )),
377 aCustom1LB(this, BibResId( LB_CUSTOM1 )),
378 aCustom2FT(this, BibResId( FT_CUSTOM2 )),
379 aCustom2LB(this, BibResId( LB_CUSTOM2 )),
380 aCustom3FT(this, BibResId( FT_CUSTOM3 )),
381 aCustom3LB(this, BibResId( LB_CUSTOM3 )),
382 aCustom4FT(this, BibResId( FT_CUSTOM4 )),
383 aCustom4LB(this, BibResId( LB_CUSTOM4 )),
384 aCustom5FT(this, BibResId( FT_CUSTOM5 )),
385 aCustom5LB(this, BibResId( LB_CUSTOM5 )),
386 sNone(BibResId(ST_NONE)),
387 bModified(sal_False)
389 FreeResource();
391 aIdentifierFT.SetText(String( BibResId( ST_IDENTIFIER )));
392 aAuthorityTypeFT.SetText(String( BibResId( ST_AUTHTYPE )));
393 aAuthorFT.SetText(String( BibResId( ST_AUTHOR )));
394 aTitleFT.SetText(String( BibResId( ST_TITLE )));
395 aMonthFT.SetText(String( BibResId( ST_MONTH )));
396 aYearFT.SetText(String( BibResId( ST_YEAR )));
397 aISBNFT.SetText(String( BibResId( ST_ISBN )));
398 aBooktitleFT.SetText(String( BibResId( ST_BOOKTITLE )));
399 aChapterFT.SetText(String( BibResId( ST_CHAPTER )));
400 aEditionFT.SetText(String( BibResId( ST_EDITION )));
401 aEditorFT.SetText(String( BibResId( ST_EDITOR )));
402 aHowpublishedFT.SetText(String( BibResId( ST_HOWPUBLISHED )));
403 aInstitutionFT.SetText(String( BibResId( ST_INSTITUTION )));
404 aJournalFT.SetText(String( BibResId( ST_JOURNAL )));
405 aNoteFT.SetText(String( BibResId( ST_NOTE )));
406 aAnnoteFT.SetText(String( BibResId( ST_ANNOTE )));
407 aNumberFT.SetText(String( BibResId( ST_NUMBER )));
408 aOrganizationsFT.SetText(String( BibResId( ST_ORGANIZATION )));
409 aPagesFT.SetText(String( BibResId( ST_PAGE )));
410 aPublisherFT.SetText(String( BibResId( ST_PUBLISHER )));
411 aAddressFT.SetText(String( BibResId( ST_ADDRESS )));
412 aSchoolFT.SetText(String( BibResId( ST_SCHOOL )));
413 aSeriesFT.SetText(String( BibResId( ST_SERIES )));
414 aReportTypeFT.SetText(String( BibResId( ST_REPORT )));
415 aVolumeFT.SetText(String( BibResId( ST_VOLUME )));
416 aURLFT.SetText(String( BibResId( ST_URL )));
417 aCustom1FT.SetText(String( BibResId( ST_CUSTOM1 )));
418 aCustom2FT.SetText(String( BibResId( ST_CUSTOM2 )));
419 aCustom3FT.SetText(String( BibResId( ST_CUSTOM3 )));
420 aCustom4FT.SetText(String( BibResId( ST_CUSTOM4 )));
421 aCustom5FT.SetText(String( BibResId( ST_CUSTOM5 )));
423 aOKBT.SetClickHdl(LINK(this, MappingDialog_Impl, OkHdl));
424 String sTitle = GetText();
425 sTitle.SearchAndReplace(C2S("%1"), pDatMan->getActiveDataTable(), 0);
426 SetText(sTitle);
428 aListBoxes[0] = &aIdentifierLB;
429 aListBoxes[1] = &aAuthorityTypeLB;
430 aListBoxes[2] = &aAuthorLB;
431 aListBoxes[3] = &aTitleLB;
432 aListBoxes[4] = &aYearLB;
433 aListBoxes[5] = &aISBNLB;
434 aListBoxes[6] = &aBooktitleLB;
435 aListBoxes[7] = &aChapterLB;
436 aListBoxes[8] = &aEditionLB;
437 aListBoxes[9] = &aEditorLB;
438 aListBoxes[10] = &aHowpublishedLB;
439 aListBoxes[11] = &aInstitutionLB;
440 aListBoxes[12] = &aJournalLB;
441 aListBoxes[13] = &aMonthLB;
442 aListBoxes[14] = &aNoteLB;
443 aListBoxes[15] = &aAnnoteLB;
444 aListBoxes[16] = &aNumberLB;
445 aListBoxes[17] = &aOrganizationsLB;
446 aListBoxes[18] = &aPagesLB;
447 aListBoxes[19] = &aPublisherLB;
448 aListBoxes[20] = &aAddressLB;
449 aListBoxes[21] = &aSchoolLB;
450 aListBoxes[22] = &aSeriesLB;
451 aListBoxes[23] = &aReportTypeLB;
452 aListBoxes[24] = &aVolumeLB;
453 aListBoxes[25] = &aURLLB;
454 aListBoxes[26] = &aCustom1LB;
455 aListBoxes[27] = &aCustom2LB;
456 aListBoxes[28] = &aCustom3LB;
457 aListBoxes[29] = &aCustom4LB;
458 aListBoxes[30] = &aCustom5LB;
460 aListBoxes[0]->InsertEntry(sNone);
461 Reference< XNameAccess > xFields = getColumns( pDatMan->getForm() );
462 DBG_ASSERT(xFields.is(), "MappingDialog_Impl::MappingDialog_Impl : gave me an invalid form !");
463 if(xFields.is())
465 Sequence< ::rtl::OUString > aNames = xFields->getElementNames();
466 sal_Int32 nFieldsCount = aNames.getLength();
467 const ::rtl::OUString* pNames = aNames.getConstArray();
469 for(sal_Int32 nField = 0; nField < nFieldsCount; nField++)
470 aListBoxes[0]->InsertEntry(pNames[nField]);
473 Link aLnk = LINK(this, MappingDialog_Impl, ListBoxSelectHdl);
475 aListBoxes[0]->SelectEntryPos(0);
476 aListBoxes[0]->SetSelectHdl(aLnk);
477 for(sal_uInt16 i = 1; i < COLUMN_COUNT; i++)
479 for(sal_uInt16 j = 0; j < aListBoxes[0]->GetEntryCount();j++)
480 aListBoxes[i]->InsertEntry(aListBoxes[0]->GetEntry(j));
481 aListBoxes[i]->SelectEntryPos(0);
482 aListBoxes[i]->SetSelectHdl(aLnk);
484 BibConfig* pConfig = BibModul::GetConfig();
485 BibDBDescriptor aDesc;
486 aDesc.sDataSource = pDatMan->getActiveDataSource();
487 aDesc.sTableOrQuery = pDatMan->getActiveDataTable();
488 aDesc.nCommandType = CommandType::TABLE;
489 const Mapping* pMapping = pConfig->GetMapping(aDesc);
490 if(pMapping)
492 for(sal_uInt16 nEntry = 0; nEntry < COLUMN_COUNT; nEntry++)
494 sal_uInt16 nListBoxIndex = lcl_FindLogicalName( pConfig, pMapping->aColumnPairs[nEntry].sLogicalColumnName);
495 if(nListBoxIndex < COLUMN_COUNT)
497 aListBoxes[nListBoxIndex]->SelectEntry(pMapping->aColumnPairs[nEntry].sRealColumnName);
503 MappingDialog_Impl::~MappingDialog_Impl()
506 IMPL_LINK(MappingDialog_Impl, ListBoxSelectHdl, ListBox*, pListBox)
508 sal_uInt16 nEntryPos = pListBox->GetSelectEntryPos();
509 if(0 < nEntryPos)
511 for(sal_uInt16 i = 0; i < COLUMN_COUNT; i++)
513 if(pListBox != aListBoxes[i] && aListBoxes[i]->GetSelectEntryPos() == nEntryPos)
514 aListBoxes[i]->SelectEntryPos(0);
517 SetModified();
518 return 0;
521 IMPL_LINK_NOARG(MappingDialog_Impl, OkHdl)
523 if(bModified)
525 Mapping aNew;
526 aNew.sTableName = String(pDatMan->getActiveDataTable());
527 aNew.sURL = String(pDatMan->getActiveDataSource());
529 sal_uInt16 nWriteIndex = 0;
530 BibConfig* pConfig = BibModul::GetConfig();
531 for(sal_uInt16 nEntry = 0; nEntry < COLUMN_COUNT; nEntry++)
533 String sSel = aListBoxes[nEntry]->GetSelectEntry();
534 if(sSel != sNone)
536 aNew.aColumnPairs[nWriteIndex].sRealColumnName = sSel;
537 aNew.aColumnPairs[nWriteIndex].sLogicalColumnName = pConfig->GetDefColumnName(nEntry);
538 nWriteIndex++;
541 BibDBDescriptor aDesc;
542 aDesc.sDataSource = pDatMan->getActiveDataSource();
543 aDesc.sTableOrQuery = pDatMan->getActiveDataTable();
544 aDesc.nCommandType = CommandType::TABLE;
545 pDatMan->ResetIdentifierMapping();
546 pConfig->SetMapping(aDesc, &aNew);
548 EndDialog(bModified ? RET_OK : RET_CANCEL);
549 return 0;
552 class DBChangeDialog_Impl : public ModalDialog
554 OKButton aOKBT;
555 CancelButton aCancelBT;
556 HelpButton aHelpBT;
557 FixedLine aSelectionGB;
558 SvTabListBox aSelectionLB;
559 HeaderBar aSelectionHB;
560 DBChangeDialogConfig_Impl aConfig;
561 String aEntryST;
562 String aURLST;
564 BibDataManager* pDatMan;
566 DECL_LINK(DoubleClickHdl, SvTabListBox*);
567 public:
568 DBChangeDialog_Impl(Window* pParent, BibDataManager* pMan );
569 ~DBChangeDialog_Impl();
571 String GetCurrentURL()const;
574 DBChangeDialog_Impl::DBChangeDialog_Impl(Window* pParent, BibDataManager* pMan ) :
575 ModalDialog(pParent, BibResId(RID_DLG_DBCHANGE) ),
576 aOKBT(this, BibResId( BT_OK )),
577 aCancelBT(this, BibResId( BT_CANCEL )),
578 aHelpBT(this, BibResId( BT_HELP )),
579 aSelectionGB(this, BibResId( GB_SELECTION )),
580 aSelectionLB(this, BibResId( LB_SELECTION )),
581 aSelectionHB(this, BibResId( HB_SELECTION )),
582 aEntryST(BibResId(ST_ENTRY)),
583 aURLST( BibResId(ST_URL)),
584 pDatMan(pMan)
586 FreeResource();
587 aSelectionLB.SetDoubleClickHdl( LINK(this, DBChangeDialog_Impl, DoubleClickHdl));
590 Reference< XMultiServiceFactory > xMgr = comphelper::getProcessServiceFactory();
592 ::Size aSize = aSelectionHB.GetSizePixel();
593 long nTabs[2];
594 nTabs[0] = 1;// Number of Tabs
595 nTabs[1] = aSize.Width() / 4;
597 aSelectionHB.SetStyle(aSelectionHB.GetStyle()|WB_STDHEADERBAR);
598 aSelectionHB.InsertItem( 1, aEntryST, aSize.Width());
599 aSelectionHB.SetSizePixel(aSelectionHB.CalcWindowSizePixel());
600 aSelectionHB.Show();
602 aSelectionLB.SetTabs( &nTabs[0], MAP_PIXEL );
603 aSelectionLB.SetStyle(aSelectionLB.GetStyle()|WB_CLIPCHILDREN|WB_SORT);
604 aSelectionLB.GetModel()->SetSortMode(SortAscending);
606 ::rtl::OUString sActiveSource = pDatMan->getActiveDataSource();
607 const Sequence< ::rtl::OUString >& rSources = aConfig.GetDataSourceNames();
608 const ::rtl::OUString* pSourceNames = rSources.getConstArray();
609 for(int i = 0; i < rSources.getLength(); i++)
611 SvLBoxEntry* pEntry = aSelectionLB.InsertEntry(pSourceNames[i]);
612 if(pSourceNames[i] == sActiveSource)
614 aSelectionLB.Select(pEntry);
617 aSelectionLB.GetModel()->Resort();
619 catch (const Exception&)
621 OSL_FAIL("Exception in BibDataManager::DBChangeDialog_Impl::DBChangeDialog_Impl");
625 IMPL_LINK(DBChangeDialog_Impl, DoubleClickHdl, SvTabListBox*, /*pLB*/)
627 EndDialog(RET_OK);
628 return 0;
631 DBChangeDialog_Impl::~DBChangeDialog_Impl()
635 String DBChangeDialog_Impl::GetCurrentURL()const
637 String sRet;
638 SvLBoxEntry* pEntry = aSelectionLB.FirstSelected();
639 if(pEntry)
641 sRet = aSelectionLB.GetEntryText(pEntry, 0);
643 return sRet;
646 // XDispatchProvider
647 BibInterceptorHelper::BibInterceptorHelper( ::bib::BibBeamer* pBibBeamer, ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatch > xDispatch)
649 if( pBibBeamer )
651 xInterception = pBibBeamer->getDispatchProviderInterception();
652 if( xInterception.is() )
653 xInterception->registerDispatchProviderInterceptor( this );
655 if( xDispatch.is() )
656 xFormDispatch = xDispatch;
659 BibInterceptorHelper::~BibInterceptorHelper( )
663 void BibInterceptorHelper::ReleaseInterceptor()
665 if ( xInterception.is() )
666 xInterception->releaseDispatchProviderInterceptor( this );
667 xInterception.clear();
670 ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatch > SAL_CALL
671 BibInterceptorHelper::queryDispatch( const ::com::sun::star::util::URL& aURL, const ::rtl::OUString& aTargetFrameName, sal_Int32 nSearchFlags ) throw (::com::sun::star::uno::RuntimeException)
673 Reference< XDispatch > xReturn;
675 String aCommand( aURL.Path );
676 if ( aCommand.EqualsAscii("FormSlots/ConfirmDeletion") )
677 xReturn = xFormDispatch;
678 else
679 if ( xSlaveDispatchProvider.is() )
680 xReturn = xSlaveDispatchProvider->queryDispatch( aURL, aTargetFrameName, nSearchFlags);
682 return xReturn;
685 ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatch > > SAL_CALL
686 BibInterceptorHelper::queryDispatches( const ::com::sun::star::uno::Sequence< ::com::sun::star::frame::DispatchDescriptor >& aDescripts ) throw (::com::sun::star::uno::RuntimeException)
688 Sequence< Reference< XDispatch> > aReturn( aDescripts.getLength() );
689 Reference< XDispatch >* pReturn = aReturn.getArray();
690 const DispatchDescriptor* pDescripts = aDescripts.getConstArray();
691 for ( sal_Int16 i=0; i<aDescripts.getLength(); ++i, ++pReturn, ++pDescripts )
693 *pReturn = queryDispatch( pDescripts->FeatureURL, pDescripts->FrameName, pDescripts->SearchFlags );
695 return aReturn;
698 // XDispatchProviderInterceptor
699 ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatchProvider > SAL_CALL
700 BibInterceptorHelper::getSlaveDispatchProvider( ) throw (::com::sun::star::uno::RuntimeException)
702 return xSlaveDispatchProvider;
705 void SAL_CALL BibInterceptorHelper::setSlaveDispatchProvider( const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatchProvider >& xNewSlaveDispatchProvider ) throw (::com::sun::star::uno::RuntimeException)
707 xSlaveDispatchProvider = xNewSlaveDispatchProvider;
710 ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatchProvider > SAL_CALL
711 BibInterceptorHelper::getMasterDispatchProvider( ) throw (::com::sun::star::uno::RuntimeException)
713 return xMasterDispatchProvider;
716 void SAL_CALL BibInterceptorHelper::setMasterDispatchProvider( const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatchProvider >& xNewMasterDispatchProvider ) throw (::com::sun::star::uno::RuntimeException)
718 xMasterDispatchProvider = xNewMasterDispatchProvider;
721 //-----------------------------------------------------------------------------
722 #define STR_UID "uid"
723 ::rtl::OUString gGridName(C2U("theGrid"));
724 ::rtl::OUString gViewName(C2U("theView"));
725 ::rtl::OUString gGlobalName(C2U("theGlobals"));
726 ::rtl::OUString gBeamerSize(C2U("theBeamerSize"));
727 ::rtl::OUString gViewSize(C2U("theViewSize"));
729 BibDataManager::BibDataManager()
730 :BibDataManager_Base( GetMutex() )
731 ,m_pInterceptorHelper( NULL )
732 ,m_aLoadListeners(m_aMutex)
733 ,pBibView( NULL )
734 ,pToolbar(0)
739 BibDataManager::~BibDataManager()
741 Reference< XLoadable > xLoad( m_xForm, UNO_QUERY );
742 Reference< XPropertySet > xPrSet( m_xForm, UNO_QUERY );
743 Reference< XComponent > xComp( m_xForm, UNO_QUERY );
744 if ( m_xForm.is() )
746 Reference< XComponent > xConnection;
747 xPrSet->getPropertyValue(C2U("ActiveConnection")) >>= xConnection;
748 RemoveMeAsUidListener();
749 if (xLoad.is())
750 xLoad->unload();
751 if (xComp.is())
752 xComp->dispose();
753 if(xConnection.is())
754 xConnection->dispose();
755 m_xForm = NULL;
757 if( m_pInterceptorHelper )
759 m_pInterceptorHelper->ReleaseInterceptor();
760 m_pInterceptorHelper->release();
761 m_pInterceptorHelper = NULL;
764 //------------------------------------------------------------------------
765 void BibDataManager::InsertFields(const Reference< XFormComponent > & _rxGrid)
767 if ( !_rxGrid.is() )
768 return;
772 Reference< XNameContainer > xColContainer( _rxGrid, UNO_QUERY );
773 // remove the old fields
774 if ( xColContainer->hasElements() )
776 Sequence< ::rtl::OUString > aNames = xColContainer->getElementNames();
777 const ::rtl::OUString* pNames = aNames.getConstArray();
778 const ::rtl::OUString* pNamesEnd = pNames + aNames.getLength();
779 for ( ; pNames != pNamesEnd; ++pNames )
780 xColContainer->removeByName( *pNames );
783 Reference< XNameAccess > xFields = getColumns( m_xForm );
784 if (!xFields.is())
785 return;
787 Reference< XGridColumnFactory > xColFactory( _rxGrid, UNO_QUERY );
789 Reference< XPropertySet > xField;
791 Sequence< ::rtl::OUString > aFields( xFields->getElementNames() );
792 const ::rtl::OUString* pFields = aFields.getConstArray();
793 const ::rtl::OUString* pFieldsEnd = pFields + aFields.getLength();
795 for ( ; pFields != pFieldsEnd; ++pFields )
797 xFields->getByName( *pFields ) >>= xField;
799 ::rtl::OUString sCurrentModelType;
800 const ::rtl::OUString sType(C2U("Type"));
801 sal_Int32 nType = 0;
802 sal_Bool bIsFormatted = sal_False;
803 sal_Bool bFormattedIsNumeric = sal_True;
804 xField->getPropertyValue(sType) >>= nType;
805 switch(nType)
807 case DataType::BIT:
808 case DataType::BOOLEAN:
809 sCurrentModelType = C2U("CheckBox");
810 break;
812 case DataType::BINARY:
813 case DataType::VARBINARY:
814 case DataType::LONGVARBINARY:
815 case DataType::BLOB:
816 sCurrentModelType = C2U("TextField");
817 break;
819 case DataType::VARCHAR:
820 case DataType::LONGVARCHAR:
821 case DataType::CHAR:
822 case DataType::CLOB:
823 bFormattedIsNumeric = sal_False;
824 // _NO_ break !
825 default:
826 sCurrentModelType = C2U("FormattedField");
827 bIsFormatted = sal_True;
828 break;
831 Reference< XPropertySet > xCurrentCol = xColFactory->createColumn(sCurrentModelType);
832 if (bIsFormatted)
834 ::rtl::OUString sFormatKey(C2U("FormatKey"));
835 xCurrentCol->setPropertyValue(sFormatKey, xField->getPropertyValue(sFormatKey));
836 Any aFormatted(&bFormattedIsNumeric, ::getBooleanCppuType());
837 xCurrentCol->setPropertyValue(C2U("TreatAsNumber"), aFormatted);
839 Any aColName = makeAny( *pFields );
840 xCurrentCol->setPropertyValue(FM_PROP_CONTROLSOURCE, aColName);
841 xCurrentCol->setPropertyValue(FM_PROP_LABEL, aColName);
843 xColContainer->insertByName( *pFields, makeAny( xCurrentCol ) );
846 catch (const Exception&)
848 OSL_FAIL("Exception in BibDataManager::InsertFields");
852 Reference< awt::XControlModel > BibDataManager::updateGridModel()
854 return updateGridModel( m_xForm );
857 Reference< awt::XControlModel > BibDataManager::updateGridModel(const Reference< XForm > & xDbForm)
861 Reference< XPropertySet > aFormPropSet( xDbForm, UNO_QUERY );
862 ::rtl::OUString sName;
863 aFormPropSet->getPropertyValue(C2U("Command")) >>= sName;
865 if ( !m_xGridModel.is() )
867 m_xGridModel = createGridModel( gGridName );
869 Reference< XNameContainer > xNameCont(xDbForm, UNO_QUERY);
870 xNameCont->insertByName( sName, makeAny( m_xGridModel ) );
873 // insert the fields
874 Reference< XFormComponent > xFormComp( m_xGridModel, UNO_QUERY );
875 InsertFields( xFormComp );
877 catch (const Exception&)
879 OSL_FAIL("::updateGridModel: something went wrong !");
882 return m_xGridModel;
885 Reference< XForm > BibDataManager::createDatabaseForm(BibDBDescriptor& rDesc)
887 Reference< XForm > xResult;
890 Reference< XMultiServiceFactory > xMgr = comphelper::getProcessServiceFactory();
891 m_xForm = Reference< XForm > ( xMgr->createInstance( C2U("com.sun.star.form.component.Form") ), UNO_QUERY );
893 Reference< XPropertySet > aPropertySet( m_xForm, UNO_QUERY );
895 aDataSourceURL = rDesc.sDataSource;
896 if(aPropertySet.is())
898 Any aVal;
899 aVal <<= (sal_Int32)ResultSetType::SCROLL_INSENSITIVE;
900 aPropertySet->setPropertyValue(C2U("ResultSetType"),aVal );
901 aVal <<= (sal_Int32)ResultSetConcurrency::READ_ONLY;
902 aPropertySet->setPropertyValue(C2U("ResultSetConcurrency"), aVal);
904 //Caching for Performance
905 aVal <<= (sal_Int32)50;
906 aPropertySet->setPropertyValue(C2U("FetchSize"), aVal);
908 Reference< XConnection > xConnection = getConnection(rDesc.sDataSource);
909 aVal <<= xConnection;
910 aPropertySet->setPropertyValue(C2U("ActiveConnection"), aVal);
912 Reference< XTablesSupplier > xSupplyTables(xConnection, UNO_QUERY);
913 Reference< XNameAccess > xTables = xSupplyTables.is() ?
914 xSupplyTables->getTables() : Reference< XNameAccess > ();
916 Sequence< ::rtl::OUString > aTableNameSeq;
917 if (xTables.is())
918 aTableNameSeq = xTables->getElementNames();
920 if(aTableNameSeq.getLength() > 0)
922 const ::rtl::OUString* pTableNames = aTableNameSeq.getConstArray();
923 if(!rDesc.sTableOrQuery.isEmpty())
924 aActiveDataTable = rDesc.sTableOrQuery;
925 else
927 rDesc.sTableOrQuery = aActiveDataTable = pTableNames[0];
928 rDesc.nCommandType = CommandType::TABLE;
931 aVal <<= aActiveDataTable;
932 aPropertySet->setPropertyValue(C2U("Command"), aVal);
933 aVal <<= rDesc.nCommandType;
934 aPropertySet->setPropertyValue(C2U("CommandType"), aVal);
937 Reference< XDatabaseMetaData > xMetaData = xConnection->getMetaData();
938 aQuoteChar = xMetaData->getIdentifierQuoteString();
940 Reference< XMultiServiceFactory > xFactory(xConnection, UNO_QUERY);
941 if ( xFactory.is() )
942 m_xParser.set( xFactory->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.sdb.SingleSelectQueryComposer" ) ) ), UNO_QUERY );
944 ::rtl::OUString aString(C2U("SELECT * FROM "));
946 ::rtl::OUString sCatalog, sSchema, sName;
947 ::dbtools::qualifiedNameComponents( xMetaData, aActiveDataTable, sCatalog, sSchema, sName, ::dbtools::eInDataManipulation );
948 aString += ::dbtools::composeTableNameForSelect( xConnection, sCatalog, sSchema, sName );
950 m_xParser->setElementaryQuery(aString);
951 BibConfig* pConfig = BibModul::GetConfig();
952 pConfig->setQueryField(getQueryField());
953 startQueryWith(pConfig->getQueryText());
955 xResult = m_xForm;
959 catch (const Exception&)
961 OSL_FAIL("::createDatabaseForm: something went wrong !");
964 return xResult;
966 //------------------------------------------------------------------------
967 Sequence< ::rtl::OUString > BibDataManager::getDataSources()
969 Sequence< ::rtl::OUString > aTableNameSeq;
973 Reference< XTablesSupplier > xSupplyTables( getConnection( m_xForm ), UNO_QUERY );
974 Reference< XNameAccess > xTables;
975 if (xSupplyTables.is())
976 xTables = xSupplyTables->getTables();
977 if (xTables.is())
978 aTableNameSeq = xTables->getElementNames();
980 catch (const Exception&)
982 OSL_FAIL("::getDataSources: something went wrong !");
985 return aTableNameSeq;
987 //------------------------------------------------------------------------
988 ::rtl::OUString BibDataManager::getActiveDataTable()
990 return aActiveDataTable;
992 //------------------------------------------------------------------------
993 void BibDataManager::setFilter(const ::rtl::OUString& rQuery)
995 if(!m_xParser.is())
996 return;
999 m_xParser->setFilter( rQuery );
1000 ::rtl::OUString aQuery = m_xParser->getFilter();
1001 Reference< XPropertySet > xFormProps( m_xForm, UNO_QUERY_THROW );
1002 xFormProps->setPropertyValue( C2U( "Filter" ), makeAny( aQuery ) );
1003 xFormProps->setPropertyValue( C2U( "ApplyFilter" ), makeAny( sal_True ) );
1004 reload();
1006 catch (const Exception&)
1008 DBG_UNHANDLED_EXCEPTION();
1013 //------------------------------------------------------------------------
1014 ::rtl::OUString BibDataManager::getFilter()
1017 ::rtl::OUString aQueryString;
1020 Reference< XPropertySet > xFormProps( m_xForm, UNO_QUERY_THROW );
1021 OSL_VERIFY( xFormProps->getPropertyValue( C2U( "Filter" ) ) >>= aQueryString );
1023 catch (const Exception&)
1025 DBG_UNHANDLED_EXCEPTION();
1029 return aQueryString;
1032 //------------------------------------------------------------------------
1033 Sequence< ::rtl::OUString > BibDataManager::getQueryFields()
1035 Sequence< ::rtl::OUString > aFieldSeq;
1036 Reference< XNameAccess > xFields = getColumns( m_xForm );
1037 if (xFields.is())
1038 aFieldSeq = xFields->getElementNames();
1039 return aFieldSeq;
1041 //------------------------------------------------------------------------
1042 ::rtl::OUString BibDataManager::getQueryField()
1044 BibConfig* pConfig = BibModul::GetConfig();
1045 ::rtl::OUString aFieldString = pConfig->getQueryField();
1046 if(aFieldString.isEmpty())
1048 Sequence< ::rtl::OUString > aSeq = getQueryFields();
1049 const ::rtl::OUString* pFields = aSeq.getConstArray();
1050 if(aSeq.getLength()>0)
1052 aFieldString=pFields[0];
1055 return aFieldString;
1057 //------------------------------------------------------------------------
1058 void BibDataManager::startQueryWith(const ::rtl::OUString& rQuery)
1060 BibConfig* pConfig = BibModul::GetConfig();
1061 pConfig->setQueryText( rQuery );
1063 ::rtl::OUString aQueryString;
1064 if(!rQuery.isEmpty())
1066 aQueryString=aQuoteChar;
1067 aQueryString+=getQueryField();
1068 aQueryString+=aQuoteChar;
1069 aQueryString+=C2U(" like '");
1070 String sQuery(rQuery);
1071 sQuery.SearchAndReplaceAll('?','_');
1072 sQuery.SearchAndReplaceAll('*','%');
1073 aQueryString += sQuery;
1074 aQueryString+=C2U("%'");
1076 setFilter(aQueryString);
1079 void BibDataManager::setActiveDataSource(const ::rtl::OUString& rURL)
1081 ::rtl::OUString sTmp(aDataSourceURL);
1082 aDataSourceURL = rURL;
1084 Reference< XPropertySet > aPropertySet( m_xForm, UNO_QUERY );
1085 if(aPropertySet.is())
1087 unload();
1089 Reference< XComponent > xOldConnection;
1090 aPropertySet->getPropertyValue(C2U("ActiveConnection")) >>= xOldConnection;
1092 Reference< XConnection > xConnection = getConnection(rURL);
1093 if(!xConnection.is())
1095 aDataSourceURL = sTmp;
1096 return;
1098 Any aVal; aVal <<= xConnection;
1099 aPropertySet->setPropertyValue(C2U("ActiveConnection"), aVal);
1100 Reference< XMultiServiceFactory > xFactory(xConnection, UNO_QUERY);
1101 if ( xFactory.is() )
1102 m_xParser.set( xFactory->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.sdb.SingleSelectQueryComposer" ) ) ), UNO_QUERY );
1104 if(xOldConnection.is())
1105 xOldConnection->dispose();
1107 Sequence< ::rtl::OUString > aTableNameSeq;
1108 Reference< XTablesSupplier > xSupplyTables(xConnection, UNO_QUERY);
1109 if(xSupplyTables.is())
1111 Reference< XNameAccess > xAccess = xSupplyTables->getTables();
1112 aTableNameSeq = xAccess->getElementNames();
1114 if(aTableNameSeq.getLength() > 0)
1116 const ::rtl::OUString* pTableNames = aTableNameSeq.getConstArray();
1117 aActiveDataTable = pTableNames[0];
1118 aVal <<= aActiveDataTable;
1119 aPropertySet->setPropertyValue(C2U("Command"), aVal);
1120 aPropertySet->setPropertyValue(C2U("CommandType"), makeAny(CommandType::TABLE));
1121 //Caching for Performance
1122 aVal <<= (sal_Int32)50;
1123 aPropertySet->setPropertyValue(C2U("FetchSize"), aVal);
1124 ::rtl::OUString aString(C2U("SELECT * FROM "));
1125 // quote the table name which may contain catalog.schema.table
1126 Reference<XDatabaseMetaData> xMetaData(xConnection->getMetaData(),UNO_QUERY);
1127 aQuoteChar = xMetaData->getIdentifierQuoteString();
1129 ::rtl::OUString sCatalog, sSchema, sName;
1130 ::dbtools::qualifiedNameComponents( xMetaData, aActiveDataTable, sCatalog, sSchema, sName, ::dbtools::eInDataManipulation );
1131 aString += ::dbtools::composeTableNameForSelect( xConnection, sCatalog, sSchema, sName );
1133 m_xParser->setElementaryQuery(aString);
1134 BibConfig* pConfig = BibModul::GetConfig();
1135 pConfig->setQueryField(getQueryField());
1136 startQueryWith(pConfig->getQueryText());
1137 setActiveDataTable(aActiveDataTable);
1139 FeatureStateEvent aEvent;
1140 util::URL aURL;
1141 aEvent.IsEnabled = sal_True;
1142 aEvent.Requery = sal_False;
1143 aEvent.FeatureDescriptor = getActiveDataTable();
1145 aEvent.State = makeAny( getDataSources() );
1147 if(pToolbar)
1149 aURL.Complete =C2U(".uno:Bib/source");
1150 aEvent.FeatureURL = aURL;
1151 pToolbar->statusChanged( aEvent );
1154 updateGridModel();
1155 load();
1160 void BibDataManager::setActiveDataTable(const ::rtl::OUString& rTable)
1162 ResetIdentifierMapping();
1165 Reference< XPropertySet > aPropertySet( m_xForm, UNO_QUERY );
1167 if(aPropertySet.is())
1169 Reference< XConnection > xConnection = getConnection( m_xForm );
1170 Reference< XTablesSupplier > xSupplyTables(xConnection, UNO_QUERY);
1171 Reference< XNameAccess > xAccess = xSupplyTables->getTables();
1172 Sequence< ::rtl::OUString > aTableNameSeq = xAccess->getElementNames();
1173 sal_uInt32 nCount = aTableNameSeq.getLength();
1175 const ::rtl::OUString* pTableNames = aTableNameSeq.getConstArray();
1176 const ::rtl::OUString* pTableNamesEnd = pTableNames + nCount;
1178 for ( ; pTableNames != pTableNamesEnd; ++pTableNames )
1180 if ( rTable == *pTableNames )
1182 aActiveDataTable = rTable;
1183 Any aVal; aVal <<= rTable;
1184 aPropertySet->setPropertyValue( C2U("Command"), aVal );
1185 break;
1188 if (pTableNames != pTableNamesEnd)
1190 Reference< XDatabaseMetaData > xMetaData = xConnection->getMetaData();
1191 aQuoteChar = xMetaData->getIdentifierQuoteString();
1193 Reference< XMultiServiceFactory > xFactory(xConnection, UNO_QUERY);
1194 if ( xFactory.is() )
1195 m_xParser.set( xFactory->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.sdb.SingleSelectQueryComposer" ) ) ), UNO_QUERY );
1197 ::rtl::OUString aString(C2U("SELECT * FROM "));
1199 ::rtl::OUString sCatalog, sSchema, sName;
1200 ::dbtools::qualifiedNameComponents( xMetaData, aActiveDataTable, sCatalog, sSchema, sName, ::dbtools::eInDataManipulation );
1201 aString += ::dbtools::composeTableNameForSelect( xConnection, sCatalog, sSchema, sName );
1203 m_xParser->setElementaryQuery(aString);
1205 BibConfig* pConfig = BibModul::GetConfig();
1206 pConfig->setQueryField(getQueryField());
1207 startQueryWith(pConfig->getQueryText());
1209 BibDBDescriptor aDesc;
1210 aDesc.sDataSource = aDataSourceURL;
1211 aDesc.sTableOrQuery = aActiveDataTable;
1212 aDesc.nCommandType = CommandType::TABLE;
1213 BibModul::GetConfig()->SetBibliographyURL(aDesc);
1217 catch (const Exception&)
1219 OSL_FAIL("::setActiveDataTable: something went wrong !");
1223 //------------------------------------------------------------------------
1224 void SAL_CALL BibDataManager::load( ) throw (RuntimeException)
1226 if ( isLoaded() )
1227 // nothing to do
1228 return;
1230 Reference< XLoadable > xFormAsLoadable( m_xForm, UNO_QUERY );
1231 DBG_ASSERT( xFormAsLoadable.is() || !m_xForm.is(), "BibDataManager::load: invalid form!");
1232 if ( xFormAsLoadable.is() )
1234 xFormAsLoadable->load();
1235 SetMeAsUidListener();
1237 EventObject aEvt( static_cast< XWeak* >( this ) );
1238 m_aLoadListeners.notifyEach( &XLoadListener::loaded, aEvt );
1242 //------------------------------------------------------------------------
1243 void SAL_CALL BibDataManager::unload( ) throw (RuntimeException)
1245 if ( !isLoaded() )
1246 // nothing to do
1247 return;
1249 Reference< XLoadable >xFormAsLoadable( m_xForm, UNO_QUERY );
1250 DBG_ASSERT( xFormAsLoadable.is() || !m_xForm.is(), "BibDataManager::unload: invalid form!");
1251 if ( xFormAsLoadable.is() )
1253 EventObject aEvt( static_cast< XWeak* >( this ) );
1256 m_aLoadListeners.notifyEach( &XLoadListener::unloading, aEvt );
1259 RemoveMeAsUidListener();
1260 xFormAsLoadable->unload();
1263 m_aLoadListeners.notifyEach( &XLoadListener::unloaded, aEvt );
1268 //------------------------------------------------------------------------
1269 void SAL_CALL BibDataManager::reload( ) throw (RuntimeException)
1271 if ( !isLoaded() )
1272 // nothing to do
1273 return;
1275 Reference< XLoadable >xFormAsLoadable( m_xForm, UNO_QUERY );
1276 DBG_ASSERT( xFormAsLoadable.is() || !m_xForm.is(), "BibDataManager::unload: invalid form!");
1277 if ( xFormAsLoadable.is() )
1279 EventObject aEvt( static_cast< XWeak* >( this ) );
1282 m_aLoadListeners.notifyEach( &XLoadListener::reloading, aEvt );
1285 xFormAsLoadable->reload();
1288 m_aLoadListeners.notifyEach( &XLoadListener::reloaded, aEvt );
1293 //------------------------------------------------------------------------
1294 sal_Bool SAL_CALL BibDataManager::isLoaded( ) throw (RuntimeException)
1296 Reference< XLoadable >xFormAsLoadable( m_xForm, UNO_QUERY );
1297 DBG_ASSERT( xFormAsLoadable.is() || !m_xForm.is(), "BibDataManager::isLoaded: invalid form!");
1299 sal_Bool bLoaded = sal_False;
1300 if ( xFormAsLoadable.is() )
1301 bLoaded = xFormAsLoadable->isLoaded();
1302 return bLoaded;
1305 //------------------------------------------------------------------------
1306 void SAL_CALL BibDataManager::addLoadListener( const Reference< XLoadListener >& aListener ) throw (RuntimeException)
1308 m_aLoadListeners.addInterface( aListener );
1311 //------------------------------------------------------------------------
1312 void SAL_CALL BibDataManager::removeLoadListener( const Reference< XLoadListener >& aListener ) throw (RuntimeException)
1314 m_aLoadListeners.removeInterface( aListener );
1317 //------------------------------------------------------------------------
1318 Reference< awt::XControlModel > BibDataManager::createGridModel(const ::rtl::OUString& rName)
1320 Reference< awt::XControlModel > xModel;
1324 // create the control model
1325 Reference< XMultiServiceFactory > xMgr = ::comphelper::getProcessServiceFactory();
1326 Reference< XInterface > xObject = xMgr->createInstance(C2U("com.sun.star.form.component.GridControl"));
1327 xModel=Reference< awt::XControlModel > ( xObject, UNO_QUERY );
1329 // set the
1330 Reference< XPropertySet > xPropSet( xModel, UNO_QUERY );
1331 xPropSet->setPropertyValue( C2U("Name"), makeAny( rName ) );
1333 // set the name of the to-be-created control
1334 ::rtl::OUString aControlName(C2U("com.sun.star.form.control.InteractionGridControl"));
1335 Any aAny; aAny <<= aControlName;
1336 xPropSet->setPropertyValue( C2U("DefaultControl"),aAny );
1338 // the the helpURL
1339 ::rtl::OUString uProp(C2U("HelpURL"));
1340 Reference< XPropertySetInfo > xPropInfo = xPropSet->getPropertySetInfo();
1341 if (xPropInfo->hasPropertyByName(uProp))
1343 ::rtl::OUString sId(RTL_CONSTASCII_USTRINGPARAM( INET_HID_SCHEME ));
1344 sId += ::rtl::OUString::createFromAscii( HID_BIB_DB_GRIDCTRL );
1345 xPropSet->setPropertyValue( uProp, makeAny( sId ) );
1348 catch (const Exception&)
1350 OSL_FAIL("::createGridModel: something went wrong !");
1353 return xModel;
1355 //------------------------------------------------------------------------
1356 ::rtl::OUString BibDataManager::getControlName(sal_Int32 nFormatKey )
1358 ::rtl::OUString aResStr;
1359 switch (nFormatKey)
1361 case DataType::BIT:
1362 case DataType::BOOLEAN:
1363 aResStr=C2U("CheckBox");
1364 break;
1365 case DataType::TINYINT:
1366 case DataType::SMALLINT:
1367 case DataType::INTEGER:
1368 aResStr=C2U("NumericField"); ;
1369 break;
1370 case DataType::REAL:
1371 case DataType::DOUBLE:
1372 case DataType::NUMERIC:
1373 case DataType::DECIMAL:
1374 aResStr=C2U("FormattedField");
1375 break;
1376 case DataType::TIMESTAMP:
1377 aResStr=C2U("FormattedField");
1378 break;
1379 case DataType::DATE:
1380 aResStr=C2U("DateField");
1381 break;
1382 case DataType::TIME:
1383 aResStr=C2U("TimeField");
1384 break;
1385 case DataType::CHAR:
1386 case DataType::VARCHAR:
1387 case DataType::LONGVARCHAR:
1388 default:
1389 aResStr=C2U("TextField");
1390 break;
1392 return aResStr;
1394 //------------------------------------------------------------------------
1395 Reference< awt::XControlModel > BibDataManager::loadControlModel(
1396 const ::rtl::OUString& rName, sal_Bool bForceListBox)
1398 Reference< awt::XControlModel > xModel;
1399 ::rtl::OUString aName(C2U("View_"));
1400 aName += rName;
1404 Reference< XNameAccess > xFields = getColumns( m_xForm );
1405 if (!xFields.is())
1406 return xModel;
1407 Reference< XPropertySet > xField;
1409 Any aElement;
1411 if(xFields->hasByName(rName))
1413 aElement = xFields->getByName(rName);
1414 aElement >>= xField;
1415 Reference< XPropertySetInfo > xInfo = xField.is() ? xField->getPropertySetInfo() : Reference< XPropertySetInfo > ();
1417 ::rtl::OUString sCurrentModelType;
1418 const ::rtl::OUString sType(C2U("Type"));
1419 sal_Int32 nFormatKey = 0;
1420 xField->getPropertyValue(sType) >>= nFormatKey;
1422 ::rtl::OUString aInstanceName(C2U("com.sun.star.form.component."));
1424 if (bForceListBox)
1425 aInstanceName += C2U("ListBox");
1426 else
1427 aInstanceName += getControlName(nFormatKey);
1429 Reference< XMultiServiceFactory > xMgr = comphelper::getProcessServiceFactory();
1430 Reference< XInterface > xObject = xMgr->createInstance(aInstanceName);
1431 xModel=Reference< awt::XControlModel > ( xObject, UNO_QUERY );
1432 Reference< XPropertySet > xPropSet( xModel, UNO_QUERY );
1433 Any aFieldName; aFieldName <<= aName;
1435 xPropSet->setPropertyValue( FM_PROP_NAME,aFieldName);
1436 xPropSet->setPropertyValue( FM_PROP_CONTROLSOURCE, makeAny( rName ) );
1437 xPropSet->setPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "NativeWidgetLook" ) ), makeAny( (sal_Bool)sal_True ) );
1439 Reference< XFormComponent > aFormComp(xModel,UNO_QUERY );
1441 Reference< XNameContainer > xNameCont( m_xForm, UNO_QUERY );
1442 xNameCont->insertByName(aName, makeAny( aFormComp ) );
1444 // now if the form where we inserted the new model is already loaded, notify the model of this
1445 // Note that this implementation below is a HACK as it relies on the fact that the model adds itself
1446 // as load listener to it's parent, which is an implementation detail of the model.
1448 // the better solution would be the following:
1449 // in the current scenario, we insert a control model into a form. This results in the control model
1450 // adding itself as load listener to the form. Now, the form should realize that it's already loaded
1451 // and notify the model (which it knows as XLoadListener only) immediately. This seems to make sense.
1452 // (as an anologon to the XStatusListener semantics).
1454 // But this would be way too risky for this last-day fix here.
1455 Reference< XLoadable > xLoad( m_xForm, UNO_QUERY );
1456 if ( xLoad.is() && xLoad->isLoaded() )
1458 Reference< XLoadListener > xListener( aFormComp, UNO_QUERY );
1459 if ( xListener.is() )
1461 EventObject aLoadSource;
1462 aLoadSource.Source = xLoad;
1463 xListener->loaded( aLoadSource );
1468 catch (const Exception&)
1470 OSL_FAIL("::loadControlModel: something went wrong !");
1472 return xModel;
1474 //------------------------------------------------------------------------
1475 void SAL_CALL BibDataManager::disposing()
1477 BibDataManager_Base::WeakComponentImplHelperBase::disposing();
1480 //------------------------------------------------------------------------
1481 void BibDataManager::disposing( const EventObject& /*Source*/ ) throw( ::com::sun::star::uno::RuntimeException )
1483 // not interested in
1486 //------------------------------------------------------------------------
1487 void BibDataManager::propertyChange(const beans::PropertyChangeEvent& evt) throw( RuntimeException )
1491 if(evt.PropertyName == FM_PROP_VALUE)
1493 if( evt.NewValue.getValueType() == ::getCppuType((Reference<io::XInputStream>*)0) )
1495 Reference< io::XDataInputStream > xStream(
1496 *(const Reference< io::XInputStream > *)evt.NewValue.getValue(), UNO_QUERY );
1497 aUID <<= xStream->readUTF();
1499 else
1500 aUID = evt.NewValue;
1502 Reference< XRowLocate > xLocate(xBibCursor, UNO_QUERY);
1503 DBG_ASSERT(xLocate.is(), "BibDataManager::propertyChange : invalid cursor !");
1504 xLocate->moveToBookmark(aUID);
1507 catch (const Exception&)
1509 OSL_FAIL("::propertyChange: something went wrong !");
1513 //------------------------------------------------------------------------
1514 void BibDataManager::SetMeAsUidListener()
1518 Reference< XNameAccess > xFields = getColumns( m_xForm );
1519 if (!xFields.is())
1520 return;
1522 Sequence< ::rtl::OUString > aFields(xFields->getElementNames());
1523 const ::rtl::OUString* pFields = aFields.getConstArray();
1524 sal_Int32 nCount=aFields.getLength();
1525 String StrUID(C2S(STR_UID));
1526 ::rtl::OUString theFieldName;
1527 for( sal_Int32 i=0; i<nCount; i++ )
1529 String aName= pFields[i];
1531 if(aName.EqualsIgnoreCaseAscii(StrUID))
1533 theFieldName=pFields[i];
1534 break;
1538 if(!theFieldName.isEmpty())
1540 Reference< XPropertySet > xPropSet;
1541 Any aElement;
1543 aElement = xFields->getByName(theFieldName);
1544 xPropSet = *(Reference< XPropertySet > *)aElement.getValue();
1546 xPropSet->addPropertyChangeListener(FM_PROP_VALUE, this);
1550 catch (const Exception&)
1552 OSL_FAIL("Exception in BibDataManager::SetMeAsUidListener");
1556 //------------------------------------------------------------------------
1557 void BibDataManager::RemoveMeAsUidListener()
1561 Reference< XNameAccess > xFields = getColumns( m_xForm );
1562 if (!xFields.is())
1563 return;
1566 Sequence< ::rtl::OUString > aFields(xFields->getElementNames());
1567 const ::rtl::OUString* pFields = aFields.getConstArray();
1568 sal_Int32 nCount=aFields.getLength();
1569 String StrUID(C2S(STR_UID));
1570 ::rtl::OUString theFieldName;
1571 for( sal_Int32 i=0; i<nCount; i++ )
1573 String aName= pFields[i];
1575 if(aName.EqualsIgnoreCaseAscii(StrUID))
1577 theFieldName=pFields[i];
1578 break;
1582 if(!theFieldName.isEmpty())
1584 Reference< XPropertySet > xPropSet;
1585 Any aElement;
1587 aElement = xFields->getByName(theFieldName);
1588 xPropSet = *(Reference< XPropertySet > *)aElement.getValue();
1590 xPropSet->removePropertyChangeListener(FM_PROP_VALUE, this);
1594 catch (const Exception&)
1596 OSL_FAIL("Exception in BibDataManager::RemoveMeAsUidListener");
1600 void BibDataManager::CreateMappingDialog(Window* pParent)
1602 MappingDialog_Impl* pDlg = new MappingDialog_Impl(pParent, this);
1603 if(RET_OK == pDlg->Execute() && pBibView)
1605 reload();
1607 delete pDlg;
1610 ::rtl::OUString BibDataManager::CreateDBChangeDialog(Window* pParent)
1612 ::rtl::OUString uRet;
1613 DBChangeDialog_Impl * pDlg = new DBChangeDialog_Impl(pParent, this );
1614 if(RET_OK == pDlg->Execute())
1616 String sNewURL = pDlg->GetCurrentURL();
1617 if(sNewURL != String(getActiveDataSource()))
1619 uRet = sNewURL;
1622 delete pDlg;
1623 return uRet;
1626 void BibDataManager::DispatchDBChangeDialog()
1628 if(pToolbar)
1629 pToolbar->SendDispatch(TBC_BT_CHANGESOURCE, Sequence< PropertyValue >());
1632 const ::rtl::OUString& BibDataManager::GetIdentifierMapping()
1634 if(sIdentifierMapping.isEmpty())
1636 BibConfig* pConfig = BibModul::GetConfig();
1637 BibDBDescriptor aDesc;
1638 aDesc.sDataSource = getActiveDataSource();
1639 aDesc.sTableOrQuery = getActiveDataTable();
1640 aDesc.nCommandType = CommandType::TABLE;
1641 const Mapping* pMapping = pConfig->GetMapping(aDesc);
1642 sIdentifierMapping = pConfig->GetDefColumnName(IDENTIFIER_POS);
1643 if(pMapping)
1645 for(sal_uInt16 nEntry = 0; nEntry < COLUMN_COUNT; nEntry++)
1647 if(pMapping->aColumnPairs[nEntry].sLogicalColumnName == sIdentifierMapping)
1649 sIdentifierMapping = pMapping->aColumnPairs[nEntry].sRealColumnName;
1650 break;
1655 return sIdentifierMapping;
1658 void BibDataManager::SetToolbar(BibToolBar* pSet)
1660 pToolbar = pSet;
1661 if(pToolbar)
1662 pToolbar->SetDatMan(*this);
1665 uno::Reference< form::runtime::XFormController > BibDataManager::GetFormController()
1667 if(!m_xFormCtrl.is())
1669 Reference< lang::XMultiServiceFactory > xMgr = comphelper::getProcessServiceFactory();
1670 m_xFormCtrl = uno::Reference< form::runtime::XFormController > (
1671 xMgr->createInstance(C2U("com.sun.star.form.runtime.FormController")), UNO_QUERY);
1672 m_xFormCtrl->setModel(uno::Reference< awt::XTabControllerModel > (getForm(), UNO_QUERY));
1673 m_xFormDispatch = uno::Reference< frame::XDispatch > ( m_xFormCtrl, UNO_QUERY);
1675 return m_xFormCtrl;
1678 void BibDataManager::RegisterInterceptor( ::bib::BibBeamer* pBibBeamer)
1680 DBG_ASSERT( !m_pInterceptorHelper, "BibDataManager::RegisterInterceptor: called twice!" );
1682 if( pBibBeamer )
1683 m_pInterceptorHelper = new BibInterceptorHelper( pBibBeamer, m_xFormDispatch);
1684 if( m_pInterceptorHelper )
1685 m_pInterceptorHelper->acquire();
1689 sal_Bool BibDataManager::HasActiveConnection()const
1691 sal_Bool bRet = sal_False;
1692 Reference< XPropertySet > xPrSet( m_xForm, UNO_QUERY );
1693 if( xPrSet.is() )
1695 Reference< XComponent > xConnection;
1696 xPrSet->getPropertyValue(C2U("ActiveConnection")) >>= xConnection;
1697 bRet = xConnection.is();
1699 return bRet;
1702 sal_Bool BibDataManager::HasActiveConnection()
1704 return getConnection( m_xForm ).is();
1707 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */