1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 #include "conditionupdater.hxx" 29 #include "reportformula.hxx" 30 31 /** === begin UNO includes === **/ 32 #include <com/sun/star/report/XFormatCondition.hpp> 33 /** === end UNO includes === **/ 34 35 #include <tools/diagnose_ex.h> 36 37 //........................................................................ 38 namespace rptui 39 { 40 //........................................................................ 41 42 /** === begin UNO using === **/ 43 using ::com::sun::star::beans::PropertyChangeEvent; 44 using ::com::sun::star::uno::Reference; 45 using ::com::sun::star::report::XReportControlModel; 46 using ::com::sun::star::uno::UNO_QUERY; 47 using ::com::sun::star::report::XFormatCondition; 48 using ::com::sun::star::uno::UNO_QUERY_THROW; 49 using ::com::sun::star::uno::Exception; 50 /** === end UNO using === **/ 51 52 //==================================================================== 53 //= ConditionUpdater 54 //==================================================================== 55 //-------------------------------------------------------------------- 56 ConditionUpdater::ConditionUpdater() 57 { 58 } 59 60 //-------------------------------------------------------------------- 61 ConditionUpdater::~ConditionUpdater() 62 { 63 } 64 65 //-------------------------------------------------------------------- 66 void ConditionUpdater::notifyPropertyChange( const PropertyChangeEvent& _rEvent ) 67 { 68 if ( !impl_lateInit_nothrow() ) 69 return; 70 71 Reference< XReportControlModel > xRptControlModel( _rEvent.Source, UNO_QUERY ); 72 if ( xRptControlModel.is() && _rEvent.PropertyName.equalsAscii( "DataField" ) ) 73 { 74 ::rtl::OUString sOldDataSource, sNewDataSource; 75 OSL_VERIFY( _rEvent.OldValue >>= sOldDataSource ); 76 OSL_VERIFY( _rEvent.NewValue >>= sNewDataSource ); 77 impl_adjustFormatConditions_nothrow( xRptControlModel, sOldDataSource, sNewDataSource ); 78 } 79 } 80 81 //-------------------------------------------------------------------- 82 bool ConditionUpdater::impl_lateInit_nothrow() 83 { 84 if ( !m_aConditionalExpressions.empty() ) 85 return true; 86 87 ConditionalExpressionFactory::getKnownConditionalExpressions( m_aConditionalExpressions ); 88 return true; 89 } 90 91 //-------------------------------------------------------------------- 92 void ConditionUpdater::impl_adjustFormatConditions_nothrow( const Reference< XReportControlModel >& _rxRptControlModel, 93 const ::rtl::OUString& _rOldDataSource, const ::rtl::OUString& _rNewDataSource ) 94 { 95 try 96 { 97 ReportFormula aOldContentFormula( _rOldDataSource ); 98 ::rtl::OUString sOldUnprefixed( aOldContentFormula.getBracketedFieldOrExpression() ); 99 ReportFormula aNewContentFormula( _rNewDataSource ); 100 ::rtl::OUString sNewUnprefixed( aNewContentFormula.getBracketedFieldOrExpression() ); 101 102 sal_Int32 nCount( _rxRptControlModel->getCount() ); 103 Reference< XFormatCondition > xFormatCondition; 104 ::rtl::OUString sFormulaExpression, sLHS, sRHS; 105 for ( sal_Int32 i=0; i<nCount; ++i ) 106 { 107 xFormatCondition.set( _rxRptControlModel->getByIndex( i ), UNO_QUERY_THROW ); 108 ReportFormula aFormula( xFormatCondition->getFormula() ); 109 sFormulaExpression = aFormula.getExpression(); 110 111 for ( ConditionalExpressions::const_iterator loop = m_aConditionalExpressions.begin(); 112 loop != m_aConditionalExpressions.end(); 113 ++loop 114 ) 115 { 116 if ( !loop->second->matchExpression( sFormulaExpression, sOldUnprefixed, sLHS, sRHS ) ) 117 continue; 118 119 // the expression matches -> translate it to the new data source of the report control model 120 sFormulaExpression = loop->second->assembleExpression( sNewUnprefixed, sLHS, sRHS ); 121 aFormula = ReportFormula( ReportFormula::Expression, sFormulaExpression ); 122 xFormatCondition->setFormula( aFormula.getCompleteFormula() ); 123 break; 124 } 125 } 126 } 127 catch( const Exception& ) 128 { 129 DBG_UNHANDLED_EXCEPTION(); 130 } 131 } 132 133 //........................................................................ 134 } // namespace rptui 135 //........................................................................ 136