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_WIZ_TYPESELECT_HXX
27 #include "WTypeSelect.hxx"
28 #endif
29 #ifndef DBAUI_WIZARD_PAGES_HRC
30 #include "WizardPages.hrc"
31 #endif
32 #ifndef DBACCESS_SHARED_DBUSTRINGS_HRC
33 #include "dbustrings.hrc"
34 #endif
35 #ifndef _TOOLS_DEBUG_HXX
36 #include <tools/debug.hxx>
37 #endif
38 #ifndef TOOLS_DIAGNOSE_EX_H
39 #include <tools/diagnose_ex.h>
40 #endif
41 #ifndef DBAUI_FIELDDESCRIPTIONS_HXX
42 #include "FieldDescriptions.hxx"
43 #endif
44 #ifndef DBAUI_WIZ_COPYTABLEDIALOG_HXX
45 #include "WCopyTable.hxx"
46 #endif
47 #ifndef _DBA_DBACCESS_HELPID_HRC_
48 #include "dbaccess_helpid.hrc"
49 #endif
50 #ifndef _DBU_MISC_HRC_
51 #include "dbu_misc.hrc"
52 #endif
53 #ifndef _STREAM_HXX
54 #include <tools/stream.hxx>
55 #endif
56 #ifndef _SVPARSER_HXX
57 #include <svtools/svparser.hxx>
58 #endif
59 #ifndef DBAUI_TOOLS_HXX
60 #include "UITools.hxx"
61 #endif
62 #ifndef _DBAUI_SQLMESSAGE_HXX_
63 #include "sqlmessage.hxx"
64 #endif
65 #ifndef DBAUI_FIELDCONTROLS_HXX
66 #include "FieldControls.hxx"
67 #endif
68 
69 #include "dbaccess_slotid.hrc"
70 
71 using namespace ::dbaui;
72 using namespace ::com::sun::star::uno;
73 using namespace ::com::sun::star::beans;
74 using namespace ::com::sun::star::container;
75 using namespace ::com::sun::star::util;
76 using namespace ::com::sun::star::sdbc;
77 //	using namespace ::com::sun::star::sdbcx;
78 
79 //========================================================================
80 // OWizTypeSelectControl
DBG_NAME(OWizTypeSelectControl)81 DBG_NAME(OWizTypeSelectControl)
82 //========================================================================
83 OWizTypeSelectControl::OWizTypeSelectControl(Window* pParent, const ResId& rResId,OTableDesignHelpBar* pHelpBar)
84 	: OFieldDescControl(pParent,rResId,pHelpBar)
85 {
86     DBG_CTOR(OWizTypeSelectControl,NULL);
87 
88 }
89 // -----------------------------------------------------------------------------
~OWizTypeSelectControl()90 OWizTypeSelectControl::~OWizTypeSelectControl()
91 {
92 
93     DBG_DTOR(OWizTypeSelectControl,NULL);
94 }
95 // -----------------------------------------------------------------------
ActivateAggregate(EControlType eType)96 void OWizTypeSelectControl::ActivateAggregate( EControlType eType )
97 {
98 	switch(eType )
99 	{
100 		case tpFormat:
101 		case tpDefault:
102 		case tpAutoIncrement:
103 		case tpAutoIncrementValue:
104 			break;
105 		default:
106 			OFieldDescControl::ActivateAggregate( eType );
107 	}
108 }
109 // -----------------------------------------------------------------------
DeactivateAggregate(EControlType eType)110 void OWizTypeSelectControl::DeactivateAggregate( EControlType eType )
111 {
112 	switch(eType )
113 	{
114 		case tpFormat:
115 		case tpDefault:
116 		case tpAutoIncrement:
117 		case tpAutoIncrementValue:
118 			break;
119 		default:
120 			OFieldDescControl::DeactivateAggregate( eType );
121 	}
122 }
123 // -----------------------------------------------------------------------
CellModified(long nRow,sal_uInt16 nColId)124 void OWizTypeSelectControl::CellModified(long nRow, sal_uInt16 nColId )
125 {
126 	DBG_ASSERT(nRow == -1,"nRow muss -1 sein!");
127     (void)nRow;
128 
129 	MultiListBox &aListBox = ((OWizTypeSelect*)GetParent())->m_lbColumnNames;
130 
131     OFieldDescription* pCurFieldDescr = getCurrentFieldDescData();
132 
133 	sal_uInt16 nPos = aListBox.GetEntryPos( String( pCurFieldDescr->GetName() ) );
134 	pCurFieldDescr = static_cast< OFieldDescription* >( aListBox.GetEntryData( nPos ) );
135     OSL_ENSURE( pCurFieldDescr, "OWizTypeSelectControl::CellModified: Columnname/type not found in the listbox!" );
136 	if ( !pCurFieldDescr )
137 		return;
138     setCurrentFieldDescData( pCurFieldDescr );
139 
140 	::rtl::OUString sName = pCurFieldDescr->GetName();
141 	::rtl::OUString sNewName;
142 	const OPropColumnEditCtrl* pColumnName = getColumnCtrl();
143 	if ( pColumnName )
144 		sNewName = pColumnName->GetText();
145 
146 	switch(nColId)
147 	{
148 		case FIELD_PRPOERTY_COLUMNNAME:
149 			{
150 				OCopyTableWizard* pWiz = static_cast<OCopyTableWizard*>(GetParent()->GetParent());
151 				// first we have to check if this name already exists
152 				sal_Bool bDoubleName = sal_False;
153 				sal_Bool bCase = sal_True;
154 				if ( getMetaData().is() && !getMetaData()->supportsMixedCaseQuotedIdentifiers() )
155 				{
156 					bCase = sal_False;
157 					sal_uInt16 nCount = aListBox.GetEntryCount();
158 					for (sal_uInt16 i=0 ; !bDoubleName && i < nCount ; ++i)
159 					{
160 						::rtl::OUString sEntry(aListBox.GetEntry(i));
161 						bDoubleName = sNewName.equalsIgnoreAsciiCase(sEntry);
162 					}
163 					if ( !bDoubleName && pWiz->shouldCreatePrimaryKey() )
164 						bDoubleName = sNewName.equalsIgnoreAsciiCase(pWiz->getPrimaryKeyName());
165 
166 				}
167 				else
168 					bDoubleName =  ((aListBox.GetEntryPos(String(sNewName)) != LISTBOX_ENTRY_NOTFOUND)
169 									|| ( pWiz->shouldCreatePrimaryKey()
170 										&&	pWiz->getPrimaryKeyName() == sNewName) );
171 
172 				if ( bDoubleName )
173 				{
174 					String strMessage = String(ModuleRes(STR_TABLEDESIGN_DUPLICATE_NAME));
175 					strMessage.SearchAndReplaceAscii("$column$", sNewName);
176                     pWiz->showError(strMessage);
177 					pCurFieldDescr->SetName(sName);
178 					DisplayData(pCurFieldDescr);
179 					static_cast<OWizTypeSelect*>(GetParent())->setDuplicateName(sal_True);
180 					return;
181 				}
182 
183 				::rtl::OUString sOldName = pCurFieldDescr->GetName();
184 				pCurFieldDescr->SetName(sNewName);
185 				static_cast<OWizTypeSelect*>(GetParent())->setDuplicateName(sal_False);
186 
187 				// now we change the name
188 				OCopyTableWizard::TNameMapping::iterator aIter = pWiz->m_mNameMapping.begin();
189 				OCopyTableWizard::TNameMapping::iterator aEnd  = pWiz->m_mNameMapping.end();
190 
191 				::comphelper::UStringMixEqual aCase(bCase);
192 				for(;aIter != aEnd;++aIter)
193 				{
194 					if ( aCase(aIter->second,sName) )
195 					{
196 						aIter->second = sNewName;
197 						break;
198 					}
199 				}
200 
201 				aListBox.RemoveEntry(nPos);
202 				aListBox.InsertEntry(pCurFieldDescr->GetName(),nPos);
203 				aListBox.SetEntryData(nPos,pCurFieldDescr);
204 
205 				pWiz->replaceColumn(nPos,pCurFieldDescr,sOldName);
206 			}
207 			break;
208 	}
209 	saveCurrentFieldDescData();
210 }
211 // -----------------------------------------------------------------------------
GetLocale() const212 ::com::sun::star::lang::Locale	OWizTypeSelectControl::GetLocale() const
213 {
214 	return static_cast<OWizTypeSelect*>(GetParent())->m_pParent->GetLocale();
215 }
216 // -----------------------------------------------------------------------------
GetFormatter() const217 Reference< XNumberFormatter > OWizTypeSelectControl::GetFormatter() const
218 {
219 	return static_cast<OWizTypeSelect*>(GetParent())->m_pParent->GetFormatter();
220 }
221 // -----------------------------------------------------------------------------
getTypeInfo(sal_Int32 _nPos)222 TOTypeInfoSP	OWizTypeSelectControl::getTypeInfo(sal_Int32 _nPos)
223 {
224 	return static_cast<OWizTypeSelect*>(GetParent())->m_pParent->getDestTypeInfo(_nPos);
225 }
226 // -----------------------------------------------------------------------------
getTypeInfo() const227 const OTypeInfoMap*	OWizTypeSelectControl::getTypeInfo() const
228 {
229 	return static_cast<OWizTypeSelect*>(GetParent())->m_pParent->getDestTypeInfo();
230 }
231 // -----------------------------------------------------------------------------
getMetaData()232 ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XDatabaseMetaData> OWizTypeSelectControl::getMetaData()
233 {
234 	return ((OWizTypeSelect*)GetParent())->m_pParent->m_xDestConnection->getMetaData();
235 }
236 // -----------------------------------------------------------------------------
getConnection()237 ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection> OWizTypeSelectControl::getConnection()
238 {
239 	return ((OWizTypeSelect*)GetParent())->m_pParent->m_xDestConnection;
240 }
241 // -----------------------------------------------------------------------------
isAutoIncrementValueEnabled() const242 sal_Bool OWizTypeSelectControl::isAutoIncrementValueEnabled() const
243 {
244 	return ((OWizTypeSelect*)GetParent())->m_bAutoIncrementEnabled;
245 }
246 // -----------------------------------------------------------------------------
getAutoIncrementValue() const247 ::rtl::OUString OWizTypeSelectControl::getAutoIncrementValue() const
248 {
249 	return ((OWizTypeSelect*)GetParent())->m_sAutoIncrementValue;
250 }
251 // -----------------------------------------------------------------------------
252 
253 //========================================================================
254 DBG_NAME(OWizTypeSelect);
255 #define IMG_PRIMARY_KEY 1
256 //========================================================================
OWizTypeSelect(Window * pParent,SvStream * _pStream)257 OWizTypeSelect::OWizTypeSelect( Window* pParent, SvStream* _pStream )
258 			   :OWizardPage( pParent, ModuleRes( TAB_WIZ_TYPE_SELECT ))
259 			   ,m_lbColumnNames( this, ModuleRes( LB_NEW_COLUMN_NAMES ) )
260                ,m_flColumns( this, ModuleRes( FL_COLUMN_NAME ) )
261 			   ,m_aTypeControl( this, ModuleRes( CONTROL_CONTAINER ))
262                ,m_flAutoType( this, ModuleRes( FL_AUTO_TYPE ) )
263 			   ,m_ftAuto( this, ModuleRes( FT_AUTO ) )
264 			   ,m_etAuto( this, ModuleRes( ET_AUTO ) )
265 			   ,m_pbAuto( this, ModuleRes( PB_AUTO ) )
266 			   ,m_pParserStream( _pStream )
267 			   ,m_nDisplayRow(0)
268 			   ,m_bAutoIncrementEnabled(sal_False)
269 			   ,m_bDuplicateName(sal_False)
270 {
271 	DBG_CTOR(OWizTypeSelect,NULL);
272 	m_lbColumnNames.SetSelectHdl(LINK(this,OWizTypeSelect,ColumnSelectHdl));
273 
274     ModuleRes aModuleRes(isHiContrast(&m_lbColumnNames) ? IMG_JOINS_H : IMG_JOINS);
275 	ImageList aImageList(aModuleRes);
276 	m_imgPKey = aImageList.GetImage(IMG_PRIMARY_KEY);
277 
278 	m_aTypeControl.Show();
279 	m_aTypeControl.Init();
280 	m_etAuto.SetText(String::CreateFromAscii("10"));
281 	m_etAuto.SetDecimalDigits(0);
282 	m_pbAuto.SetClickHdl(LINK(this,OWizTypeSelect,ButtonClickHdl));
283 	m_lbColumnNames.EnableMultiSelection(sal_True);
284 
285 	try
286 	{
287 		m_lbColumnNames.SetPKey( m_pParent->supportsPrimaryKey() );
288 		::dbaui::fillAutoIncrementValue( m_pParent->m_xDestConnection, m_bAutoIncrementEnabled, m_sAutoIncrementValue );
289 	}
290 	catch(const Exception&)
291 	{
292         DBG_UNHANDLED_EXCEPTION();
293 	}
294 
295 	FreeResource();
296 }
297 // -----------------------------------------------------------------------
~OWizTypeSelect()298 OWizTypeSelect::~OWizTypeSelect()
299 {
300 	DBG_DTOR(OWizTypeSelect,NULL);
301 }
302 // -----------------------------------------------------------------------------
GetTitle() const303 String OWizTypeSelect::GetTitle() const
304 {
305 	DBG_CHKTHIS(OWizTypeSelect,NULL);
306 	return String(ModuleRes(STR_WIZ_TYPE_SELECT_TITEL));
307 }
308 // -----------------------------------------------------------------------
Resize()309 void OWizTypeSelect::Resize()
310 {
311 	DBG_CHKTHIS(OWizTypeSelect,NULL);
312 }
313 // -----------------------------------------------------------------------
314 IMPL_LINK( OWizTypeSelect, ColumnSelectHdl, MultiListBox *, /*pListBox*/ )
315 {
316 	String aColumnName( m_lbColumnNames.GetSelectEntry() );
317 
318 	OFieldDescription* pField = static_cast<OFieldDescription*>(m_lbColumnNames.GetEntryData(m_lbColumnNames.GetEntryPos(aColumnName)));
319 	if(pField)
320 		m_aTypeControl.DisplayData(pField);
321 
322     m_aTypeControl.Enable(m_lbColumnNames.GetSelectEntryCount() == 1 );
323 	return 0;
324 }
325 // -----------------------------------------------------------------------
Reset()326 void OWizTypeSelect::Reset()
327 {
328 	// urspr"unglichen zustand wiederherstellen
329 	DBG_CHKTHIS(OWizTypeSelect,NULL);
330 
331 	while(m_lbColumnNames.GetEntryCount())
332 		m_lbColumnNames.RemoveEntry(0);
333 	m_lbColumnNames.Clear();
334 	sal_Int32 nBreakPos;
335 	m_pParent->CheckColumns(nBreakPos);
336 
337 	const ODatabaseExport::TColumnVector* pDestColumns = m_pParent->getDestVector();
338 	ODatabaseExport::TColumnVector::const_iterator aIter = pDestColumns->begin();
339     ODatabaseExport::TColumnVector::const_iterator aEnd = pDestColumns->end();
340 	for(;aIter != aEnd;++aIter)
341 	{
342 		sal_uInt16 nPos;
343 		if((*aIter)->second->IsPrimaryKey())
344 			nPos = m_lbColumnNames.InsertEntry((*aIter)->first, m_imgPKey );
345 		else
346 			nPos = m_lbColumnNames.InsertEntry((*aIter)->first);
347 		m_lbColumnNames.SetEntryData(nPos,(*aIter)->second);
348 	}
349 	m_bFirstTime = sal_False;
350 }
351 // -----------------------------------------------------------------------
ActivatePage()352 void OWizTypeSelect::ActivatePage( )
353 {
354 	DBG_CHKTHIS(OWizTypeSelect,NULL);
355 	sal_Bool bOldFirstTime = m_bFirstTime;
356 	Reset();
357 	m_bFirstTime = bOldFirstTime;
358 
359 	m_lbColumnNames.SelectEntryPos(static_cast<sal_uInt16>(m_nDisplayRow));
360 	m_nDisplayRow = 0;
361 	m_lbColumnNames.GetSelectHdl().Call(&m_lbColumnNames);
362 }
363 // -----------------------------------------------------------------------
LeavePage()364 sal_Bool OWizTypeSelect::LeavePage()
365 {
366 	DBG_CHKTHIS(OWizTypeSelect,NULL);
367 	String aColumnName( m_lbColumnNames.GetSelectEntry() );
368 
369 	sal_Bool bDuplicateName = sal_False;
370 	OFieldDescription* pField = static_cast<OFieldDescription*>(m_lbColumnNames.GetEntryData(m_lbColumnNames.GetEntryPos(aColumnName)));
371 	if ( pField )
372 	{
373 		m_aTypeControl.SaveData(pField);
374 		bDuplicateName = m_bDuplicateName;
375 	}
376 	return !bDuplicateName;
377 }
378 //------------------------------------------------------------------------------
EnableAuto(sal_Bool bEnable)379 void OWizTypeSelect::EnableAuto(sal_Bool bEnable)
380 {
381 	DBG_CHKTHIS(OWizTypeSelect,NULL);
382 	m_ftAuto.Show(bEnable);
383 	m_etAuto.Show(bEnable);
384 	m_pbAuto.Show(bEnable);
385     m_flAutoType.Show(bEnable);
386 }
387 //------------------------------------------------------------------------------
388 IMPL_LINK( OWizTypeSelect, ButtonClickHdl, Button *, /*pButton*/ )
389 {
390 	DBG_CHKTHIS(OWizTypeSelect,NULL);
391 	sal_Int32 nBreakPos;
392 	m_pParent->CheckColumns(nBreakPos);
393 	fillColumnList(m_etAuto.GetText().ToInt32());
394 
395 	ActivatePage();
396 
397 	return 0;
398 }
399 //------------------------------------------------------------------------
IsPrimaryKeyAllowed() const400 sal_Bool OWizTypeSelectList::IsPrimaryKeyAllowed() const
401 {
402 	DBG_CHKTHIS(OWizTypeSelect,NULL);
403 	sal_uInt16 nCount = GetSelectEntryCount();
404 	sal_uInt16 j;
405 
406 	for( j = 0; m_bPKey && j < nCount; ++j )
407 	{
408 		OFieldDescription* pField = static_cast<OFieldDescription*>(GetEntryData(GetSelectEntryPos(j)));
409 		if(!pField || pField->getTypeInfo()->nSearchType == ColumnSearch::NONE)
410 			break;
411 	}
412 	return j == nCount;
413 }
414 // -----------------------------------------------------------------------------
setPrimaryKey(OFieldDescription * _pFieldDescr,sal_uInt16 _nPos,sal_Bool _bSet)415 void OWizTypeSelectList::setPrimaryKey(OFieldDescription* _pFieldDescr,sal_uInt16 _nPos,sal_Bool _bSet)
416 {
417 	String sColumnName = GetEntry(_nPos);
418 	RemoveEntry(_nPos);
419 	_pFieldDescr->SetPrimaryKey(_bSet);
420 	if( _bSet )
421 		InsertEntry(sColumnName,((OWizTypeSelect*)GetParent())->m_imgPKey,_nPos);
422 	else if( _pFieldDescr->getTypeInfo()->bNullable )
423 	{
424 		_pFieldDescr->SetControlDefault(Any());
425 		InsertEntry(sColumnName,_nPos);
426 	}
427 	SetEntryData(_nPos,_pFieldDescr);
428 }
429 //------------------------------------------------------------------------
PreNotify(NotifyEvent & rEvt)430 long OWizTypeSelectList::PreNotify( NotifyEvent& rEvt )
431 {
432 	long nDone = 0;
433 	switch( rEvt.GetType() )
434 	{
435 		case EVENT_MOUSEBUTTONDOWN:
436 		{
437 			const MouseEvent* pMEvt = rEvt.GetMouseEvent();
438 			if(pMEvt->IsRight() && !pMEvt->GetModifier())
439 				nDone = 1;
440 		}
441 			break;
442 		case EVENT_COMMAND:
443 		{
444 			if(!IsPrimaryKeyAllowed())
445 				break;
446 
447 			const CommandEvent* pComEvt = rEvt.GetCommandEvent();
448 			if(pComEvt->GetCommand() != COMMAND_CONTEXTMENU)
449 				break;
450 			// die Stelle, an der geklickt wurde
451 			Point ptWhere(0,0);
452 			if (pComEvt->IsMouseEvent())
453 				ptWhere = pComEvt->GetMousePosPixel();
454 
455 			PopupMenu aContextMenu(ModuleRes(RID_SBA_RTF_PKEYPOPUP));
456 			switch( aContextMenu.Execute( this, ptWhere ) )
457 			{
458 				case SID_TABLEDESIGN_TABED_PRIMARYKEY:
459 				{
460 					String sColumnName;
461 					sal_uInt16 nCount = GetEntryCount();
462 					for(sal_uInt16 j = 0 ; j < nCount ; ++j)
463 					{
464 						OFieldDescription* pFieldDescr = static_cast<OFieldDescription*>(GetEntryData(j));
465 						if( pFieldDescr )
466 						{
467 							if(pFieldDescr->IsPrimaryKey() && !IsEntryPosSelected(j))
468 								setPrimaryKey(pFieldDescr,j);
469 							else if(IsEntryPosSelected(j))
470 							{
471 								setPrimaryKey(pFieldDescr,j,!pFieldDescr->IsPrimaryKey());
472 								SelectEntryPos(j);
473 							}
474 						}
475 					}
476 					GetSelectHdl().Call(this);
477 				}
478 				break;
479 			}
480 			nDone = 1;
481 		}
482 		break;
483 	}
484 	return nDone ? nDone : MultiListBox::PreNotify(rEvt);
485 }
486 // -----------------------------------------------------------------------------
fillColumnList(sal_uInt32 nRows)487 void OWizTypeSelect::fillColumnList(sal_uInt32 nRows)
488 {
489 	DBG_CHKTHIS(OWizTypeSelect,NULL);
490 	if(m_pParserStream)
491 	{
492 		sal_uInt32 nTell = m_pParserStream->Tell(); // ver�ndert vielleicht die Position des Streams
493 
494 		SvParser *pReader = createReader(nRows);
495 		if(pReader)
496 		{
497 			pReader->AddRef();
498 			pReader->CallParser();
499 			pReader->ReleaseRef();
500 		}
501 		m_pParserStream->Seek(nTell);
502 	}
503 }
504 // -----------------------------------------------------------------------------
505 
506 
507