1*9e0e4191SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*9e0e4191SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*9e0e4191SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*9e0e4191SAndrew Rist  * distributed with this work for additional information
6*9e0e4191SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*9e0e4191SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*9e0e4191SAndrew Rist  * "License"); you may not use this file except in compliance
9*9e0e4191SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*9e0e4191SAndrew Rist  *
11*9e0e4191SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*9e0e4191SAndrew Rist  *
13*9e0e4191SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*9e0e4191SAndrew Rist  * software distributed under the License is distributed on an
15*9e0e4191SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*9e0e4191SAndrew Rist  * KIND, either express or implied.  See the License for the
17*9e0e4191SAndrew Rist  * specific language governing permissions and limitations
18*9e0e4191SAndrew Rist  * under the License.
19*9e0e4191SAndrew Rist  *
20*9e0e4191SAndrew Rist  *************************************************************/
21*9e0e4191SAndrew Rist 
22*9e0e4191SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir 
25cdf0e10cSrcweir #include "precompiled_reportdesign.hxx"
26cdf0e10cSrcweir #include "FunctionHelper.hxx"
27cdf0e10cSrcweir #include <tools/debug.hxx>
28cdf0e10cSrcweir 
29cdf0e10cSrcweir // =============================================================================
30cdf0e10cSrcweir namespace rptui
31cdf0e10cSrcweir {
32cdf0e10cSrcweir // =============================================================================
33cdf0e10cSrcweir     using namespace ::com::sun::star;
34cdf0e10cSrcweir 
FunctionManager(const uno::Reference<report::meta::XFunctionManager> & _xMgr)35cdf0e10cSrcweir FunctionManager::FunctionManager(const uno::Reference< report::meta::XFunctionManager>& _xMgr)
36cdf0e10cSrcweir : m_xMgr(_xMgr)
37cdf0e10cSrcweir {
38cdf0e10cSrcweir }
~FunctionManager()39cdf0e10cSrcweir FunctionManager::~FunctionManager()
40cdf0e10cSrcweir {
41cdf0e10cSrcweir }
getSingleToken(const formula::IFunctionManager::EToken _eToken) const42cdf0e10cSrcweir sal_Unicode FunctionManager::getSingleToken(const formula::IFunctionManager::EToken _eToken) const
43cdf0e10cSrcweir {
44cdf0e10cSrcweir     switch(_eToken)
45cdf0e10cSrcweir     {
46cdf0e10cSrcweir         case eOk:
47cdf0e10cSrcweir             return sal_Unicode('(');
48cdf0e10cSrcweir         case eClose:
49cdf0e10cSrcweir             return sal_Unicode(')');
50cdf0e10cSrcweir         case eSep:
51cdf0e10cSrcweir             return sal_Unicode(';');
52cdf0e10cSrcweir         case eArrayOpen:
53cdf0e10cSrcweir             return sal_Unicode('{');
54cdf0e10cSrcweir         case eArrayClose:
55cdf0e10cSrcweir             return sal_Unicode('}');
56cdf0e10cSrcweir     } // switch(_eToken)
57cdf0e10cSrcweir     return 0;
58cdf0e10cSrcweir }
59cdf0e10cSrcweir // -----------------------------------------------------------------------------
getCount() const60cdf0e10cSrcweir sal_uInt32 FunctionManager::getCount() const
61cdf0e10cSrcweir {
62cdf0e10cSrcweir     return m_xMgr->getCount();
63cdf0e10cSrcweir }
64cdf0e10cSrcweir // -----------------------------------------------------------------------------
getCategory(sal_uInt32 _nPos) const65cdf0e10cSrcweir const formula::IFunctionCategory* FunctionManager::getCategory(sal_uInt32 _nPos) const
66cdf0e10cSrcweir {
67cdf0e10cSrcweir     if ( _nPos >= m_aCategoryIndex.size() )
68cdf0e10cSrcweir     {
69cdf0e10cSrcweir         uno::Reference< report::meta::XFunctionCategory> xCategory = m_xMgr->getCategory(_nPos);
70cdf0e10cSrcweir         ::boost::shared_ptr< FunctionCategory > pCategory(new FunctionCategory(this,_nPos + 1,xCategory));
71cdf0e10cSrcweir         m_aCategoryIndex.push_back( m_aCategories.insert(TCategoriesMap::value_type(xCategory->getName(),pCategory)).first );
72cdf0e10cSrcweir     }
73cdf0e10cSrcweir     return m_aCategoryIndex[_nPos]->second.get();
74cdf0e10cSrcweir }
75cdf0e10cSrcweir // -----------------------------------------------------------------------------
getFunctionByName(const::rtl::OUString & _sFunctionName) const76cdf0e10cSrcweir const formula::IFunctionDescription* FunctionManager::getFunctionByName(const ::rtl::OUString& _sFunctionName) const
77cdf0e10cSrcweir {
78cdf0e10cSrcweir     const formula::IFunctionDescription* pDesc = NULL;
79cdf0e10cSrcweir     try
80cdf0e10cSrcweir     {
81cdf0e10cSrcweir         pDesc = get(m_xMgr->getFunctionByName(_sFunctionName)).get();
82cdf0e10cSrcweir     }
83cdf0e10cSrcweir     catch(uno::Exception&)
84cdf0e10cSrcweir     {
85cdf0e10cSrcweir     }
86cdf0e10cSrcweir     return pDesc;
87cdf0e10cSrcweir }
88cdf0e10cSrcweir // -----------------------------------------------------------------------------
fillLastRecentlyUsedFunctions(::std::vector<const formula::IFunctionDescription * > &) const89cdf0e10cSrcweir void FunctionManager::fillLastRecentlyUsedFunctions(::std::vector< const formula::IFunctionDescription*>& /*_rLastRUFunctions*/) const
90cdf0e10cSrcweir {
91cdf0e10cSrcweir     //const sal_uInt32 nCount = getCount();
92cdf0e10cSrcweir     //for(sal_uInt32 i = 0 ; i < nCount ; ++i)
93cdf0e10cSrcweir     //{
94cdf0e10cSrcweir     //    const formula::IFunctionCategory* pCategory = getCategory(
95cdf0e10cSrcweir     //}
96cdf0e10cSrcweir }
97cdf0e10cSrcweir // -----------------------------------------------------------------------------
get(const uno::Reference<report::meta::XFunctionDescription> & _xFunctionDescription) const98cdf0e10cSrcweir ::boost::shared_ptr< FunctionDescription > FunctionManager::get(const uno::Reference< report::meta::XFunctionDescription>& _xFunctionDescription) const
99cdf0e10cSrcweir {
100cdf0e10cSrcweir     ::boost::shared_ptr< FunctionDescription > pDesc;
101cdf0e10cSrcweir     if ( _xFunctionDescription.is() )
102cdf0e10cSrcweir     {
103cdf0e10cSrcweir         const ::rtl::OUString sFunctionName = _xFunctionDescription->getName();
104cdf0e10cSrcweir         TFunctionsMap::const_iterator aFunctionFind = m_aFunctions.find(sFunctionName);
105cdf0e10cSrcweir         if ( aFunctionFind == m_aFunctions.end() )
106cdf0e10cSrcweir         {
107cdf0e10cSrcweir             const uno::Reference< report::meta::XFunctionCategory> xCategory = _xFunctionDescription->getCategory();
108cdf0e10cSrcweir             const ::rtl::OUString sCategoryName = xCategory->getName();
109cdf0e10cSrcweir             TCategoriesMap::iterator aCategoryFind = m_aCategories.find(sCategoryName);
110cdf0e10cSrcweir             if ( aCategoryFind == m_aCategories.end() )
111cdf0e10cSrcweir             {
112cdf0e10cSrcweir                 aCategoryFind = m_aCategories.insert(TCategoriesMap::value_type(sCategoryName,::boost::shared_ptr< FunctionCategory > (new FunctionCategory(this,xCategory->getNumber() + 1,xCategory)))).first;
113cdf0e10cSrcweir                 m_aCategoryIndex.push_back( aCategoryFind );
114cdf0e10cSrcweir             }
115cdf0e10cSrcweir             aFunctionFind = m_aFunctions.insert(TFunctionsMap::value_type(sFunctionName,::boost::shared_ptr<FunctionDescription>(new FunctionDescription(aCategoryFind->second.get(),_xFunctionDescription)))).first;
116cdf0e10cSrcweir         } // if ( aFind == m_aFunctions.end() )
117cdf0e10cSrcweir         pDesc = aFunctionFind->second;
118cdf0e10cSrcweir     } // if ( _xFunctionDescription.is() )
119cdf0e10cSrcweir     return pDesc;
120cdf0e10cSrcweir }
121cdf0e10cSrcweir // -----------------------------------------------------------------------------
FunctionCategory(const FunctionManager * _pFMgr,sal_uInt32 _nPos,const uno::Reference<report::meta::XFunctionCategory> & _xCategory)122cdf0e10cSrcweir FunctionCategory::FunctionCategory(const FunctionManager* _pFMgr,sal_uInt32 _nPos,const uno::Reference< report::meta::XFunctionCategory>& _xCategory)
123cdf0e10cSrcweir : m_xCategory(_xCategory)
124cdf0e10cSrcweir ,m_nFunctionCount(_xCategory->getCount())
125cdf0e10cSrcweir , m_nNumber(_nPos)
126cdf0e10cSrcweir ,m_pFunctionManager(_pFMgr)
127cdf0e10cSrcweir {
128cdf0e10cSrcweir }
129cdf0e10cSrcweir // -----------------------------------------------------------------------------
getCount() const130cdf0e10cSrcweir sal_uInt32 FunctionCategory::getCount() const
131cdf0e10cSrcweir {
132cdf0e10cSrcweir     return m_nFunctionCount;
133cdf0e10cSrcweir }
134cdf0e10cSrcweir // -----------------------------------------------------------------------------
getFunction(sal_uInt32 _nPos) const135cdf0e10cSrcweir const formula::IFunctionDescription* FunctionCategory::getFunction(sal_uInt32 _nPos) const
136cdf0e10cSrcweir {
137cdf0e10cSrcweir     if ( _nPos >= m_aFunctions.size() && _nPos < m_nFunctionCount )
138cdf0e10cSrcweir     {
139cdf0e10cSrcweir         uno::Reference< report::meta::XFunctionDescription> xFunctionDescription = m_xCategory->getFunction(_nPos);
140cdf0e10cSrcweir         ::boost::shared_ptr< FunctionDescription > pFunction = m_pFunctionManager->get(xFunctionDescription);
141cdf0e10cSrcweir         m_aFunctions.push_back( pFunction );
142cdf0e10cSrcweir     }
143cdf0e10cSrcweir     return m_aFunctions[_nPos].get();
144cdf0e10cSrcweir }
145cdf0e10cSrcweir // -----------------------------------------------------------------------------
getNumber() const146cdf0e10cSrcweir sal_uInt32 FunctionCategory::getNumber() const
147cdf0e10cSrcweir {
148cdf0e10cSrcweir     return m_nNumber;
149cdf0e10cSrcweir }
150cdf0e10cSrcweir // -----------------------------------------------------------------------------
getFunctionManager() const151cdf0e10cSrcweir const formula::IFunctionManager* FunctionCategory::getFunctionManager() const
152cdf0e10cSrcweir {
153cdf0e10cSrcweir     return m_pFunctionManager;
154cdf0e10cSrcweir }
155cdf0e10cSrcweir // -----------------------------------------------------------------------------
getName() const156cdf0e10cSrcweir ::rtl::OUString FunctionCategory::getName() const
157cdf0e10cSrcweir {
158cdf0e10cSrcweir     return m_xCategory->getName();
159cdf0e10cSrcweir }
160cdf0e10cSrcweir // -----------------------------------------------------------------------------
FunctionDescription(const formula::IFunctionCategory * _pFunctionCategory,const uno::Reference<report::meta::XFunctionDescription> & _xFunctionDescription)161cdf0e10cSrcweir FunctionDescription::FunctionDescription(const formula::IFunctionCategory* _pFunctionCategory,const uno::Reference< report::meta::XFunctionDescription>& _xFunctionDescription)
162cdf0e10cSrcweir : m_xFunctionDescription(_xFunctionDescription)
163cdf0e10cSrcweir , m_pFunctionCategory(_pFunctionCategory)
164cdf0e10cSrcweir {
165cdf0e10cSrcweir     m_aParameter = m_xFunctionDescription->getArguments();
166cdf0e10cSrcweir }
getFunctionName() const167cdf0e10cSrcweir ::rtl::OUString FunctionDescription::getFunctionName() const
168cdf0e10cSrcweir {
169cdf0e10cSrcweir     return m_xFunctionDescription->getName();
170cdf0e10cSrcweir }
171cdf0e10cSrcweir // -----------------------------------------------------------------------------
getCategory() const172cdf0e10cSrcweir const formula::IFunctionCategory* FunctionDescription::getCategory() const
173cdf0e10cSrcweir {
174cdf0e10cSrcweir     return m_pFunctionCategory;
175cdf0e10cSrcweir }
176cdf0e10cSrcweir // -----------------------------------------------------------------------------
getDescription() const177cdf0e10cSrcweir ::rtl::OUString FunctionDescription::getDescription() const
178cdf0e10cSrcweir {
179cdf0e10cSrcweir     return m_xFunctionDescription->getDescription();
180cdf0e10cSrcweir }
181cdf0e10cSrcweir // -----------------------------------------------------------------------------
getSuppressedArgumentCount() const182cdf0e10cSrcweir xub_StrLen FunctionDescription::getSuppressedArgumentCount() const
183cdf0e10cSrcweir {
184cdf0e10cSrcweir     return static_cast<xub_StrLen>(m_aParameter.getLength());
185cdf0e10cSrcweir }
186cdf0e10cSrcweir // -----------------------------------------------------------------------------
getFormula(const::std::vector<::rtl::OUString> & _aArguments) const187cdf0e10cSrcweir ::rtl::OUString FunctionDescription::getFormula(const ::std::vector< ::rtl::OUString >& _aArguments) const
188cdf0e10cSrcweir {
189cdf0e10cSrcweir     ::rtl::OUString sFormula;
190cdf0e10cSrcweir     try
191cdf0e10cSrcweir     {
192cdf0e10cSrcweir         const ::rtl::OUString *pArguments = _aArguments.empty() ? 0 : &_aArguments[0];
193cdf0e10cSrcweir         sFormula = m_xFunctionDescription->createFormula(uno::Sequence< ::rtl::OUString >(pArguments, _aArguments.size()));
194cdf0e10cSrcweir     }
195cdf0e10cSrcweir     catch(const uno::Exception&)
196cdf0e10cSrcweir     {
197cdf0e10cSrcweir         DBG_ERROR("Exception caught!");
198cdf0e10cSrcweir     }
199cdf0e10cSrcweir     return sFormula;
200cdf0e10cSrcweir }
201cdf0e10cSrcweir // -----------------------------------------------------------------------------
fillVisibleArgumentMapping(::std::vector<sal_uInt16> & _rArguments) const202cdf0e10cSrcweir void FunctionDescription::fillVisibleArgumentMapping(::std::vector<sal_uInt16>& _rArguments) const
203cdf0e10cSrcweir {
204cdf0e10cSrcweir     const sal_Int32 nCount = m_aParameter.getLength();
205cdf0e10cSrcweir     for(sal_uInt16 i = 0;i < nCount; ++i)
206cdf0e10cSrcweir     {
207cdf0e10cSrcweir         _rArguments.push_back(i);
208cdf0e10cSrcweir     }
209cdf0e10cSrcweir }
210cdf0e10cSrcweir // -----------------------------------------------------------------------------
initArgumentInfo() const211cdf0e10cSrcweir void FunctionDescription::initArgumentInfo()  const
212cdf0e10cSrcweir {
213cdf0e10cSrcweir }
214cdf0e10cSrcweir // -----------------------------------------------------------------------------
getSignature() const215cdf0e10cSrcweir ::rtl::OUString FunctionDescription::getSignature() const
216cdf0e10cSrcweir {
217cdf0e10cSrcweir     return m_xFunctionDescription->getSignature();
218cdf0e10cSrcweir }
219cdf0e10cSrcweir // -----------------------------------------------------------------------------
getHelpId() const220cdf0e10cSrcweir rtl::OString FunctionDescription::getHelpId() const
221cdf0e10cSrcweir {
222cdf0e10cSrcweir     return rtl::OString();
223cdf0e10cSrcweir }
224cdf0e10cSrcweir // -----------------------------------------------------------------------------
getParameterCount() const225cdf0e10cSrcweir sal_uInt32 FunctionDescription::getParameterCount() const
226cdf0e10cSrcweir {
227cdf0e10cSrcweir     return m_aParameter.getLength();
228cdf0e10cSrcweir }
229cdf0e10cSrcweir // -----------------------------------------------------------------------------
getParameterName(sal_uInt32 _nPos) const230cdf0e10cSrcweir ::rtl::OUString FunctionDescription::getParameterName(sal_uInt32 _nPos) const
231cdf0e10cSrcweir {
232cdf0e10cSrcweir     if ( _nPos < static_cast<sal_uInt32>(m_aParameter.getLength()) )
233cdf0e10cSrcweir         return m_aParameter[_nPos].Name;
234cdf0e10cSrcweir     return ::rtl::OUString();
235cdf0e10cSrcweir }
236cdf0e10cSrcweir // -----------------------------------------------------------------------------
getParameterDescription(sal_uInt32 _nPos) const237cdf0e10cSrcweir ::rtl::OUString FunctionDescription::getParameterDescription(sal_uInt32 _nPos) const
238cdf0e10cSrcweir {
239cdf0e10cSrcweir     if ( _nPos < static_cast<sal_uInt32>(m_aParameter.getLength()) )
240cdf0e10cSrcweir         return m_aParameter[_nPos].Description;
241cdf0e10cSrcweir     return ::rtl::OUString();
242cdf0e10cSrcweir }
243cdf0e10cSrcweir // -----------------------------------------------------------------------------
isParameterOptional(sal_uInt32 _nPos) const244cdf0e10cSrcweir bool FunctionDescription::isParameterOptional(sal_uInt32 _nPos) const
245cdf0e10cSrcweir {
246cdf0e10cSrcweir     if ( _nPos < static_cast<sal_uInt32>(m_aParameter.getLength()) )
247cdf0e10cSrcweir         return m_aParameter[_nPos].IsOptional;
248cdf0e10cSrcweir     return false;
249cdf0e10cSrcweir }
250cdf0e10cSrcweir // -----------------------------------------------------------------------------
251cdf0e10cSrcweir // =============================================================================
252cdf0e10cSrcweir } // rptui
253cdf0e10cSrcweir // =============================================================================
254