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 #include "conditionupdater.hxx"
25 #include "reportformula.hxx"
26 
27 /** === begin UNO includes === **/
28 #include <com/sun/star/report/XFormatCondition.hpp>
29 /** === end UNO includes === **/
30 
31 #include <tools/diagnose_ex.h>
32 
33 //........................................................................
34 namespace rptui
35 {
36 //........................................................................
37 
38 	/** === begin UNO using === **/
39     using ::com::sun::star::beans::PropertyChangeEvent;
40     using ::com::sun::star::uno::Reference;
41     using ::com::sun::star::report::XReportControlModel;
42     using ::com::sun::star::uno::UNO_QUERY;
43     using ::com::sun::star::report::XFormatCondition;
44     using ::com::sun::star::uno::UNO_QUERY_THROW;
45     using ::com::sun::star::uno::Exception;
46 	/** === end UNO using === **/
47 
48 	//====================================================================
49 	//= ConditionUpdater
50 	//====================================================================
51 	//--------------------------------------------------------------------
ConditionUpdater()52     ConditionUpdater::ConditionUpdater()
53     {
54     }
55 
56 	//--------------------------------------------------------------------
~ConditionUpdater()57     ConditionUpdater::~ConditionUpdater()
58     {
59     }
60 
61 	//--------------------------------------------------------------------
notifyPropertyChange(const PropertyChangeEvent & _rEvent)62     void ConditionUpdater::notifyPropertyChange( const PropertyChangeEvent& _rEvent )
63     {
64         if ( !impl_lateInit_nothrow() )
65             return;
66 
67         Reference< XReportControlModel > xRptControlModel( _rEvent.Source, UNO_QUERY );
68         if ( xRptControlModel.is() && _rEvent.PropertyName.equalsAscii( "DataField" ) )
69         {
70             ::rtl::OUString sOldDataSource, sNewDataSource;
71             OSL_VERIFY( _rEvent.OldValue >>= sOldDataSource );
72             OSL_VERIFY( _rEvent.NewValue >>= sNewDataSource );
73             impl_adjustFormatConditions_nothrow( xRptControlModel, sOldDataSource, sNewDataSource );
74         }
75     }
76 
77     //--------------------------------------------------------------------
impl_lateInit_nothrow()78     bool ConditionUpdater::impl_lateInit_nothrow()
79     {
80         if ( !m_aConditionalExpressions.empty() )
81             return true;
82 
83         ConditionalExpressionFactory::getKnownConditionalExpressions( m_aConditionalExpressions );
84         return true;
85     }
86 
87     //--------------------------------------------------------------------
impl_adjustFormatConditions_nothrow(const Reference<XReportControlModel> & _rxRptControlModel,const::rtl::OUString & _rOldDataSource,const::rtl::OUString & _rNewDataSource)88     void ConditionUpdater::impl_adjustFormatConditions_nothrow( const Reference< XReportControlModel >& _rxRptControlModel,
89         const ::rtl::OUString& _rOldDataSource, const ::rtl::OUString& _rNewDataSource )
90     {
91         try
92         {
93             ReportFormula aOldContentFormula( _rOldDataSource );
94             ::rtl::OUString sOldUnprefixed( aOldContentFormula.getBracketedFieldOrExpression() );
95             ReportFormula aNewContentFormula( _rNewDataSource );
96             ::rtl::OUString sNewUnprefixed( aNewContentFormula.getBracketedFieldOrExpression() );
97 
98             sal_Int32 nCount( _rxRptControlModel->getCount() );
99             Reference< XFormatCondition > xFormatCondition;
100             ::rtl::OUString sFormulaExpression, sLHS, sRHS;
101             for ( sal_Int32 i=0; i<nCount; ++i )
102             {
103                 xFormatCondition.set( _rxRptControlModel->getByIndex( i ), UNO_QUERY_THROW );
104                 ReportFormula aFormula( xFormatCondition->getFormula() );
105                 sFormulaExpression = aFormula.getExpression();
106 
107                 for (   ConditionalExpressions::const_iterator loop = m_aConditionalExpressions.begin();
108                         loop != m_aConditionalExpressions.end();
109                         ++loop
110                     )
111                 {
112                     if ( !loop->second->matchExpression( sFormulaExpression, sOldUnprefixed, sLHS, sRHS ) )
113                         continue;
114 
115                     // the expression matches -> translate it to the new data source of the report control model
116                     sFormulaExpression = loop->second->assembleExpression( sNewUnprefixed, sLHS, sRHS );
117                     aFormula = ReportFormula( ReportFormula::Expression, sFormulaExpression );
118                     xFormatCondition->setFormula( aFormula.getCompleteFormula() );
119                     break;
120                 }
121             }
122         }
123         catch( const Exception& )
124         {
125         	DBG_UNHANDLED_EXCEPTION();
126         }
127     }
128 
129 //........................................................................
130 } // namespace rptui
131 //........................................................................
132