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 package com.sun.star.report.pentaho;
24 
25 import com.sun.star.lib.uno.helper.PropertySetMixin;
26 import com.sun.star.lib.uno.helper.WeakBase;
27 import com.sun.star.report.meta.XFunctionCategory;
28 import com.sun.star.sheet.FunctionArgument;
29 import com.sun.star.uno.Type;
30 import com.sun.star.uno.XComponentContext;
31 
32 import java.util.Locale;
33 import java.util.MissingResourceException;
34 
35 import org.pentaho.reporting.libraries.formula.DefaultFormulaContext;
36 import org.pentaho.reporting.libraries.formula.function.FunctionDescription;
37 
38 public final class StarFunctionDescription extends WeakBase
39         implements com.sun.star.report.meta.XFunctionDescription
40 {
41 
42     private final XComponentContext m_xContext;
43     private final PropertySetMixin m_prophlp;
44     // attributes
45 //     final private com.sun.star.report.meta.XFunctionCategory m_Category;
46     private final FunctionDescription functionDescription;
47     private final XFunctionCategory category;
48     private final Locale defaultLocale;
49 
StarFunctionDescription(final DefaultFormulaContext defaultContext, final XComponentContext context, final XFunctionCategory category, final FunctionDescription functionDescription)50     public StarFunctionDescription(final DefaultFormulaContext defaultContext, final XComponentContext context, final XFunctionCategory category, final FunctionDescription functionDescription)
51     {
52         m_xContext = context;
53         this.category = category;
54         Locale locale;
55         try
56         {
57             functionDescription.getDisplayName(defaultContext.getLocalizationContext().getLocale());
58             locale = defaultContext.getLocalizationContext().getLocale();
59         }
60         catch (MissingResourceException e)
61         {
62             locale = Locale.ENGLISH;
63         }
64         this.defaultLocale = locale;
65 
66         this.functionDescription = functionDescription;
67         // use the last parameter of the PropertySetMixin constructor
68         // for your optional attributes if necessary. See the documentation
69         // of the PropertySetMixin helper for further information.
70         // Ensure that your attributes are initialized correctly!
71         m_prophlp = new PropertySetMixin(m_xContext, this,
72                 new Type(com.sun.star.report.meta.XFunctionDescription.class), null);
73     }
74 
75     // com.sun.star.beans.XPropertySet:
getPropertySetInfo()76     public com.sun.star.beans.XPropertySetInfo getPropertySetInfo()
77     {
78         return m_prophlp.getPropertySetInfo();
79     }
80 
setPropertyValue(String aPropertyName, Object aValue)81     public void setPropertyValue(String aPropertyName, Object aValue) throws com.sun.star.beans.UnknownPropertyException, com.sun.star.beans.PropertyVetoException, com.sun.star.lang.IllegalArgumentException, com.sun.star.lang.WrappedTargetException
82     {
83         m_prophlp.setPropertyValue(aPropertyName, aValue);
84     }
85 
getPropertyValue(String aPropertyName)86     public Object getPropertyValue(String aPropertyName) throws com.sun.star.beans.UnknownPropertyException, com.sun.star.lang.WrappedTargetException
87     {
88         return m_prophlp.getPropertyValue(aPropertyName);
89     }
90 
addPropertyChangeListener(String aPropertyName, com.sun.star.beans.XPropertyChangeListener xListener)91     public void addPropertyChangeListener(String aPropertyName, com.sun.star.beans.XPropertyChangeListener xListener) throws com.sun.star.beans.UnknownPropertyException, com.sun.star.lang.WrappedTargetException
92     {
93         m_prophlp.addPropertyChangeListener(aPropertyName, xListener);
94     }
95 
removePropertyChangeListener(String aPropertyName, com.sun.star.beans.XPropertyChangeListener xListener)96     public void removePropertyChangeListener(String aPropertyName, com.sun.star.beans.XPropertyChangeListener xListener) throws com.sun.star.beans.UnknownPropertyException, com.sun.star.lang.WrappedTargetException
97     {
98         m_prophlp.removePropertyChangeListener(aPropertyName, xListener);
99     }
100 
addVetoableChangeListener(String aPropertyName, com.sun.star.beans.XVetoableChangeListener xListener)101     public void addVetoableChangeListener(String aPropertyName, com.sun.star.beans.XVetoableChangeListener xListener) throws com.sun.star.beans.UnknownPropertyException, com.sun.star.lang.WrappedTargetException
102     {
103         m_prophlp.addVetoableChangeListener(aPropertyName, xListener);
104     }
105 
removeVetoableChangeListener(String aPropertyName, com.sun.star.beans.XVetoableChangeListener xListener)106     public void removeVetoableChangeListener(String aPropertyName, com.sun.star.beans.XVetoableChangeListener xListener) throws com.sun.star.beans.UnknownPropertyException, com.sun.star.lang.WrappedTargetException
107     {
108         m_prophlp.removeVetoableChangeListener(aPropertyName, xListener);
109     }
110 
111     // com.sun.star.report.meta.XFunctionDescription:
getCategory()112     public com.sun.star.report.meta.XFunctionCategory getCategory()
113     {
114         return category;
115     }
116 
getName()117     public String getName()
118     {
119         try
120         {
121             return functionDescription.getDisplayName(defaultLocale);
122         }
123         catch (Exception ex)
124         {
125         }
126         return "Missing function name for " + this.getClass().getName();
127     }
128 
getDescription()129     public String getDescription()
130     {
131         try
132         {
133             return functionDescription.getDescription(defaultLocale);
134         }
135         catch (Exception ex)
136         {
137         }
138         return "Missing function description for " + this.getClass().getName();
139     }
140 
getSignature()141     public String getSignature()
142     {
143         final int count = functionDescription.getParameterCount();
144         final StringBuffer signature = new StringBuffer(getName());
145         signature.append('(');
146         for (int i = 0; i < count; i++)
147         {
148             signature.append(functionDescription.getParameterDisplayName(i, defaultLocale));
149             if (i != (count - 1))
150             {
151                 signature.append(';');
152             }
153         }
154         signature.append(')');
155         return signature.toString();
156     }
157 
getArguments()158     public com.sun.star.sheet.FunctionArgument[] getArguments()
159     {
160         int count = functionDescription.getParameterCount();
161         final boolean infinite = functionDescription.isInfiniteParameterCount();
162         if (infinite)
163         {
164             count = 30;
165         }
166         final FunctionArgument[] args = new FunctionArgument[count];
167         for (int i = 0; i < args.length; i++)
168         {
169             final int pos = infinite ? 0 : i;
170             args[i] = new FunctionArgument();
171             args[i].Description = functionDescription.getParameterDescription(pos, defaultLocale);
172             args[i].Name = functionDescription.getParameterDisplayName(pos, defaultLocale);
173             args[i].IsOptional = !functionDescription.isParameterMandatory(pos);
174         }
175         return args;
176     }
177 
createFormula(String[] arguments)178     public String createFormula(String[] arguments) throws com.sun.star.lang.DisposedException, com.sun.star.lang.IllegalArgumentException, com.sun.star.uno.Exception
179     {
180         final boolean infinite = functionDescription.isInfiniteParameterCount();
181         final int count = functionDescription.getParameterCount();
182         if (!infinite && arguments.length > count)
183         {
184             throw new com.sun.star.lang.IllegalArgumentException();
185         }
186 
187         final StringBuffer formula = new StringBuffer(getName());
188         formula.append('(');
189         for (int i = 0; i < arguments.length; ++i)
190         {
191             if (arguments[i].length() == 0)
192             {
193                 break;
194             }
195             formula.append(arguments[i]);
196             if (i < (arguments.length - 1) && arguments[i + 1].length() != 0)
197             {
198                 formula.append(';');
199             }
200         }
201         formula.append(')');
202         return formula.toString();
203     }
204 }
205