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 
25 #include "precompiled_reportdesign.hxx"
26 #include "FunctionHelper.hxx"
27 #include <tools/debug.hxx>
28 
29 // =============================================================================
30 namespace rptui
31 {
32 // =============================================================================
33     using namespace ::com::sun::star;
34 
FunctionManager(const uno::Reference<report::meta::XFunctionManager> & _xMgr)35 FunctionManager::FunctionManager(const uno::Reference< report::meta::XFunctionManager>& _xMgr)
36 : m_xMgr(_xMgr)
37 {
38 }
~FunctionManager()39 FunctionManager::~FunctionManager()
40 {
41 }
getSingleToken(const formula::IFunctionManager::EToken _eToken) const42 sal_Unicode FunctionManager::getSingleToken(const formula::IFunctionManager::EToken _eToken) const
43 {
44     switch(_eToken)
45     {
46         case eOk:
47             return sal_Unicode('(');
48         case eClose:
49             return sal_Unicode(')');
50         case eSep:
51             return sal_Unicode(';');
52         case eArrayOpen:
53             return sal_Unicode('{');
54         case eArrayClose:
55             return sal_Unicode('}');
56     } // switch(_eToken)
57     return 0;
58 }
59 // -----------------------------------------------------------------------------
getCount() const60 sal_uInt32 FunctionManager::getCount() const
61 {
62     return m_xMgr->getCount();
63 }
64 // -----------------------------------------------------------------------------
getCategory(sal_uInt32 _nPos) const65 const formula::IFunctionCategory* FunctionManager::getCategory(sal_uInt32 _nPos) const
66 {
67     if ( _nPos >= m_aCategoryIndex.size() )
68     {
69         uno::Reference< report::meta::XFunctionCategory> xCategory = m_xMgr->getCategory(_nPos);
70         ::boost::shared_ptr< FunctionCategory > pCategory(new FunctionCategory(this,_nPos + 1,xCategory));
71         m_aCategoryIndex.push_back( m_aCategories.insert(TCategoriesMap::value_type(xCategory->getName(),pCategory)).first );
72     }
73     return m_aCategoryIndex[_nPos]->second.get();
74 }
75 // -----------------------------------------------------------------------------
getFunctionByName(const::rtl::OUString & _sFunctionName) const76 const formula::IFunctionDescription* FunctionManager::getFunctionByName(const ::rtl::OUString& _sFunctionName) const
77 {
78     const formula::IFunctionDescription* pDesc = NULL;
79     try
80     {
81         pDesc = get(m_xMgr->getFunctionByName(_sFunctionName)).get();
82     }
83     catch(uno::Exception&)
84     {
85     }
86     return pDesc;
87 }
88 // -----------------------------------------------------------------------------
fillLastRecentlyUsedFunctions(::std::vector<const formula::IFunctionDescription * > &) const89 void FunctionManager::fillLastRecentlyUsedFunctions(::std::vector< const formula::IFunctionDescription*>& /*_rLastRUFunctions*/) const
90 {
91     //const sal_uInt32 nCount = getCount();
92     //for(sal_uInt32 i = 0 ; i < nCount ; ++i)
93     //{
94     //    const formula::IFunctionCategory* pCategory = getCategory(
95     //}
96 }
97 // -----------------------------------------------------------------------------
get(const uno::Reference<report::meta::XFunctionDescription> & _xFunctionDescription) const98 ::boost::shared_ptr< FunctionDescription > FunctionManager::get(const uno::Reference< report::meta::XFunctionDescription>& _xFunctionDescription) const
99 {
100     ::boost::shared_ptr< FunctionDescription > pDesc;
101     if ( _xFunctionDescription.is() )
102     {
103         const ::rtl::OUString sFunctionName = _xFunctionDescription->getName();
104         TFunctionsMap::const_iterator aFunctionFind = m_aFunctions.find(sFunctionName);
105         if ( aFunctionFind == m_aFunctions.end() )
106         {
107             const uno::Reference< report::meta::XFunctionCategory> xCategory = _xFunctionDescription->getCategory();
108             const ::rtl::OUString sCategoryName = xCategory->getName();
109             TCategoriesMap::iterator aCategoryFind = m_aCategories.find(sCategoryName);
110             if ( aCategoryFind == m_aCategories.end() )
111             {
112                 aCategoryFind = m_aCategories.insert(TCategoriesMap::value_type(sCategoryName,::boost::shared_ptr< FunctionCategory > (new FunctionCategory(this,xCategory->getNumber() + 1,xCategory)))).first;
113                 m_aCategoryIndex.push_back( aCategoryFind );
114             }
115             aFunctionFind = m_aFunctions.insert(TFunctionsMap::value_type(sFunctionName,::boost::shared_ptr<FunctionDescription>(new FunctionDescription(aCategoryFind->second.get(),_xFunctionDescription)))).first;
116         } // if ( aFind == m_aFunctions.end() )
117         pDesc = aFunctionFind->second;
118     } // if ( _xFunctionDescription.is() )
119     return pDesc;
120 }
121 // -----------------------------------------------------------------------------
FunctionCategory(const FunctionManager * _pFMgr,sal_uInt32 _nPos,const uno::Reference<report::meta::XFunctionCategory> & _xCategory)122 FunctionCategory::FunctionCategory(const FunctionManager* _pFMgr,sal_uInt32 _nPos,const uno::Reference< report::meta::XFunctionCategory>& _xCategory)
123 : m_xCategory(_xCategory)
124 ,m_nFunctionCount(_xCategory->getCount())
125 , m_nNumber(_nPos)
126 ,m_pFunctionManager(_pFMgr)
127 {
128 }
129 // -----------------------------------------------------------------------------
getCount() const130 sal_uInt32 FunctionCategory::getCount() const
131 {
132     return m_nFunctionCount;
133 }
134 // -----------------------------------------------------------------------------
getFunction(sal_uInt32 _nPos) const135 const formula::IFunctionDescription* FunctionCategory::getFunction(sal_uInt32 _nPos) const
136 {
137     if ( _nPos >= m_aFunctions.size() && _nPos < m_nFunctionCount )
138     {
139         uno::Reference< report::meta::XFunctionDescription> xFunctionDescription = m_xCategory->getFunction(_nPos);
140         ::boost::shared_ptr< FunctionDescription > pFunction = m_pFunctionManager->get(xFunctionDescription);
141         m_aFunctions.push_back( pFunction );
142     }
143     return m_aFunctions[_nPos].get();
144 }
145 // -----------------------------------------------------------------------------
getNumber() const146 sal_uInt32 FunctionCategory::getNumber() const
147 {
148     return m_nNumber;
149 }
150 // -----------------------------------------------------------------------------
getFunctionManager() const151 const formula::IFunctionManager* FunctionCategory::getFunctionManager() const
152 {
153     return m_pFunctionManager;
154 }
155 // -----------------------------------------------------------------------------
getName() const156 ::rtl::OUString FunctionCategory::getName() const
157 {
158     return m_xCategory->getName();
159 }
160 // -----------------------------------------------------------------------------
FunctionDescription(const formula::IFunctionCategory * _pFunctionCategory,const uno::Reference<report::meta::XFunctionDescription> & _xFunctionDescription)161 FunctionDescription::FunctionDescription(const formula::IFunctionCategory* _pFunctionCategory,const uno::Reference< report::meta::XFunctionDescription>& _xFunctionDescription)
162 : m_xFunctionDescription(_xFunctionDescription)
163 , m_pFunctionCategory(_pFunctionCategory)
164 {
165     m_aParameter = m_xFunctionDescription->getArguments();
166 }
getFunctionName() const167 ::rtl::OUString FunctionDescription::getFunctionName() const
168 {
169     return m_xFunctionDescription->getName();
170 }
171 // -----------------------------------------------------------------------------
getCategory() const172 const formula::IFunctionCategory* FunctionDescription::getCategory() const
173 {
174     return m_pFunctionCategory;
175 }
176 // -----------------------------------------------------------------------------
getDescription() const177 ::rtl::OUString FunctionDescription::getDescription() const
178 {
179     return m_xFunctionDescription->getDescription();
180 }
181 // -----------------------------------------------------------------------------
getSuppressedArgumentCount() const182 xub_StrLen FunctionDescription::getSuppressedArgumentCount() const
183 {
184     return static_cast<xub_StrLen>(m_aParameter.getLength());
185 }
186 // -----------------------------------------------------------------------------
getFormula(const::std::vector<::rtl::OUString> & _aArguments) const187 ::rtl::OUString FunctionDescription::getFormula(const ::std::vector< ::rtl::OUString >& _aArguments) const
188 {
189     ::rtl::OUString sFormula;
190     try
191     {
192         const ::rtl::OUString *pArguments = _aArguments.empty() ? 0 : &_aArguments[0];
193         sFormula = m_xFunctionDescription->createFormula(uno::Sequence< ::rtl::OUString >(pArguments, _aArguments.size()));
194     }
195     catch(const uno::Exception&)
196     {
197         DBG_ERROR("Exception caught!");
198     }
199     return sFormula;
200 }
201 // -----------------------------------------------------------------------------
fillVisibleArgumentMapping(::std::vector<sal_uInt16> & _rArguments) const202 void FunctionDescription::fillVisibleArgumentMapping(::std::vector<sal_uInt16>& _rArguments) const
203 {
204     const sal_Int32 nCount = m_aParameter.getLength();
205     for(sal_uInt16 i = 0;i < nCount; ++i)
206     {
207         _rArguments.push_back(i);
208     }
209 }
210 // -----------------------------------------------------------------------------
initArgumentInfo() const211 void FunctionDescription::initArgumentInfo()  const
212 {
213 }
214 // -----------------------------------------------------------------------------
getSignature() const215 ::rtl::OUString FunctionDescription::getSignature() const
216 {
217     return m_xFunctionDescription->getSignature();
218 }
219 // -----------------------------------------------------------------------------
getHelpId() const220 rtl::OString FunctionDescription::getHelpId() const
221 {
222     return rtl::OString();
223 }
224 // -----------------------------------------------------------------------------
getParameterCount() const225 sal_uInt32 FunctionDescription::getParameterCount() const
226 {
227     return m_aParameter.getLength();
228 }
229 // -----------------------------------------------------------------------------
getParameterName(sal_uInt32 _nPos) const230 ::rtl::OUString FunctionDescription::getParameterName(sal_uInt32 _nPos) const
231 {
232     if ( _nPos < static_cast<sal_uInt32>(m_aParameter.getLength()) )
233         return m_aParameter[_nPos].Name;
234     return ::rtl::OUString();
235 }
236 // -----------------------------------------------------------------------------
getParameterDescription(sal_uInt32 _nPos) const237 ::rtl::OUString FunctionDescription::getParameterDescription(sal_uInt32 _nPos) const
238 {
239     if ( _nPos < static_cast<sal_uInt32>(m_aParameter.getLength()) )
240         return m_aParameter[_nPos].Description;
241     return ::rtl::OUString();
242 }
243 // -----------------------------------------------------------------------------
isParameterOptional(sal_uInt32 _nPos) const244 bool FunctionDescription::isParameterOptional(sal_uInt32 _nPos) const
245 {
246     if ( _nPos < static_cast<sal_uInt32>(m_aParameter.getLength()) )
247         return m_aParameter[_nPos].IsOptional;
248     return false;
249 }
250 // -----------------------------------------------------------------------------
251 // =============================================================================
252 } // rptui
253 // =============================================================================
254