1*1a37d047SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*1a37d047SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*1a37d047SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*1a37d047SAndrew Rist * distributed with this work for additional information 6*1a37d047SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*1a37d047SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*1a37d047SAndrew Rist * "License"); you may not use this file except in compliance 9*1a37d047SAndrew Rist * with the License. You may obtain a copy of the License at 10*1a37d047SAndrew Rist * 11*1a37d047SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*1a37d047SAndrew Rist * 13*1a37d047SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*1a37d047SAndrew Rist * software distributed under the License is distributed on an 15*1a37d047SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*1a37d047SAndrew Rist * KIND, either express or implied. See the License for the 17*1a37d047SAndrew Rist * specific language governing permissions and limitations 18*1a37d047SAndrew Rist * under the License. 19*1a37d047SAndrew Rist * 20*1a37d047SAndrew Rist *************************************************************/ 21*1a37d047SAndrew Rist 22*1a37d047SAndrew Rist 23cdf0e10cSrcweir package com.sun.star.report.pentaho; 24cdf0e10cSrcweir 25cdf0e10cSrcweir import com.sun.star.lib.uno.helper.PropertySetMixin; 26cdf0e10cSrcweir import com.sun.star.lib.uno.helper.WeakBase; 27cdf0e10cSrcweir import com.sun.star.report.meta.XFunctionCategory; 28cdf0e10cSrcweir import com.sun.star.sheet.FunctionArgument; 29cdf0e10cSrcweir import com.sun.star.uno.Type; 30cdf0e10cSrcweir import com.sun.star.uno.XComponentContext; 31cdf0e10cSrcweir 32cdf0e10cSrcweir import java.util.Locale; 33cdf0e10cSrcweir import java.util.MissingResourceException; 34cdf0e10cSrcweir 35cdf0e10cSrcweir import org.pentaho.reporting.libraries.formula.DefaultFormulaContext; 36cdf0e10cSrcweir import org.pentaho.reporting.libraries.formula.function.FunctionDescription; 37cdf0e10cSrcweir 38cdf0e10cSrcweir public final class StarFunctionDescription extends WeakBase 39cdf0e10cSrcweir implements com.sun.star.report.meta.XFunctionDescription 40cdf0e10cSrcweir { 41cdf0e10cSrcweir 42cdf0e10cSrcweir private final XComponentContext m_xContext; 43cdf0e10cSrcweir private final PropertySetMixin m_prophlp; 44cdf0e10cSrcweir // attributes 45cdf0e10cSrcweir // final private com.sun.star.report.meta.XFunctionCategory m_Category; 46cdf0e10cSrcweir private final FunctionDescription functionDescription; 47cdf0e10cSrcweir private final XFunctionCategory category; 48cdf0e10cSrcweir private final Locale defaultLocale; 49cdf0e10cSrcweir StarFunctionDescription(final DefaultFormulaContext defaultContext, final XComponentContext context, final XFunctionCategory category, final FunctionDescription functionDescription)50cdf0e10cSrcweir public StarFunctionDescription(final DefaultFormulaContext defaultContext, final XComponentContext context, final XFunctionCategory category, final FunctionDescription functionDescription) 51cdf0e10cSrcweir { 52cdf0e10cSrcweir m_xContext = context; 53cdf0e10cSrcweir this.category = category; 54cdf0e10cSrcweir Locale locale; 55cdf0e10cSrcweir try 56cdf0e10cSrcweir { 57cdf0e10cSrcweir functionDescription.getDisplayName(defaultContext.getLocalizationContext().getLocale()); 58cdf0e10cSrcweir locale = defaultContext.getLocalizationContext().getLocale(); 59cdf0e10cSrcweir } 60cdf0e10cSrcweir catch (MissingResourceException e) 61cdf0e10cSrcweir { 62cdf0e10cSrcweir locale = Locale.ENGLISH; 63cdf0e10cSrcweir } 64cdf0e10cSrcweir this.defaultLocale = locale; 65cdf0e10cSrcweir 66cdf0e10cSrcweir this.functionDescription = functionDescription; 67cdf0e10cSrcweir // use the last parameter of the PropertySetMixin constructor 68cdf0e10cSrcweir // for your optional attributes if necessary. See the documentation 69cdf0e10cSrcweir // of the PropertySetMixin helper for further information. 70cdf0e10cSrcweir // Ensure that your attributes are initialized correctly! 71cdf0e10cSrcweir m_prophlp = new PropertySetMixin(m_xContext, this, 72cdf0e10cSrcweir new Type(com.sun.star.report.meta.XFunctionDescription.class), null); 73cdf0e10cSrcweir } 74cdf0e10cSrcweir 75cdf0e10cSrcweir // com.sun.star.beans.XPropertySet: getPropertySetInfo()76cdf0e10cSrcweir public com.sun.star.beans.XPropertySetInfo getPropertySetInfo() 77cdf0e10cSrcweir { 78cdf0e10cSrcweir return m_prophlp.getPropertySetInfo(); 79cdf0e10cSrcweir } 80cdf0e10cSrcweir setPropertyValue(String aPropertyName, Object aValue)81cdf0e10cSrcweir 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 82cdf0e10cSrcweir { 83cdf0e10cSrcweir m_prophlp.setPropertyValue(aPropertyName, aValue); 84cdf0e10cSrcweir } 85cdf0e10cSrcweir getPropertyValue(String aPropertyName)86cdf0e10cSrcweir public Object getPropertyValue(String aPropertyName) throws com.sun.star.beans.UnknownPropertyException, com.sun.star.lang.WrappedTargetException 87cdf0e10cSrcweir { 88cdf0e10cSrcweir return m_prophlp.getPropertyValue(aPropertyName); 89cdf0e10cSrcweir } 90cdf0e10cSrcweir addPropertyChangeListener(String aPropertyName, com.sun.star.beans.XPropertyChangeListener xListener)91cdf0e10cSrcweir public void addPropertyChangeListener(String aPropertyName, com.sun.star.beans.XPropertyChangeListener xListener) throws com.sun.star.beans.UnknownPropertyException, com.sun.star.lang.WrappedTargetException 92cdf0e10cSrcweir { 93cdf0e10cSrcweir m_prophlp.addPropertyChangeListener(aPropertyName, xListener); 94cdf0e10cSrcweir } 95cdf0e10cSrcweir removePropertyChangeListener(String aPropertyName, com.sun.star.beans.XPropertyChangeListener xListener)96cdf0e10cSrcweir public void removePropertyChangeListener(String aPropertyName, com.sun.star.beans.XPropertyChangeListener xListener) throws com.sun.star.beans.UnknownPropertyException, com.sun.star.lang.WrappedTargetException 97cdf0e10cSrcweir { 98cdf0e10cSrcweir m_prophlp.removePropertyChangeListener(aPropertyName, xListener); 99cdf0e10cSrcweir } 100cdf0e10cSrcweir addVetoableChangeListener(String aPropertyName, com.sun.star.beans.XVetoableChangeListener xListener)101cdf0e10cSrcweir public void addVetoableChangeListener(String aPropertyName, com.sun.star.beans.XVetoableChangeListener xListener) throws com.sun.star.beans.UnknownPropertyException, com.sun.star.lang.WrappedTargetException 102cdf0e10cSrcweir { 103cdf0e10cSrcweir m_prophlp.addVetoableChangeListener(aPropertyName, xListener); 104cdf0e10cSrcweir } 105cdf0e10cSrcweir removeVetoableChangeListener(String aPropertyName, com.sun.star.beans.XVetoableChangeListener xListener)106cdf0e10cSrcweir public void removeVetoableChangeListener(String aPropertyName, com.sun.star.beans.XVetoableChangeListener xListener) throws com.sun.star.beans.UnknownPropertyException, com.sun.star.lang.WrappedTargetException 107cdf0e10cSrcweir { 108cdf0e10cSrcweir m_prophlp.removeVetoableChangeListener(aPropertyName, xListener); 109cdf0e10cSrcweir } 110cdf0e10cSrcweir 111cdf0e10cSrcweir // com.sun.star.report.meta.XFunctionDescription: getCategory()112cdf0e10cSrcweir public com.sun.star.report.meta.XFunctionCategory getCategory() 113cdf0e10cSrcweir { 114cdf0e10cSrcweir return category; 115cdf0e10cSrcweir } 116cdf0e10cSrcweir getName()117cdf0e10cSrcweir public String getName() 118cdf0e10cSrcweir { 1199320a50fSMichael Stahl try 1209320a50fSMichael Stahl { 1219320a50fSMichael Stahl return functionDescription.getDisplayName(defaultLocale); 1229320a50fSMichael Stahl } 1239320a50fSMichael Stahl catch (Exception ex) 1249320a50fSMichael Stahl { 1259320a50fSMichael Stahl } 1269320a50fSMichael Stahl return "Missing function name for " + this.getClass().getName(); 127cdf0e10cSrcweir } 128cdf0e10cSrcweir getDescription()129cdf0e10cSrcweir public String getDescription() 130cdf0e10cSrcweir { 1319320a50fSMichael Stahl try 1329320a50fSMichael Stahl { 1339320a50fSMichael Stahl return functionDescription.getDescription(defaultLocale); 1349320a50fSMichael Stahl } 1359320a50fSMichael Stahl catch (Exception ex) 1369320a50fSMichael Stahl { 1379320a50fSMichael Stahl } 1389320a50fSMichael Stahl return "Missing function description for " + this.getClass().getName(); 139cdf0e10cSrcweir } 140cdf0e10cSrcweir getSignature()141cdf0e10cSrcweir public String getSignature() 142cdf0e10cSrcweir { 143cdf0e10cSrcweir final int count = functionDescription.getParameterCount(); 144cdf0e10cSrcweir final StringBuffer signature = new StringBuffer(getName()); 145cdf0e10cSrcweir signature.append('('); 146cdf0e10cSrcweir for (int i = 0; i < count; i++) 147cdf0e10cSrcweir { 148cdf0e10cSrcweir signature.append(functionDescription.getParameterDisplayName(i, defaultLocale)); 149cdf0e10cSrcweir if (i != (count - 1)) 150cdf0e10cSrcweir { 151cdf0e10cSrcweir signature.append(';'); 152cdf0e10cSrcweir } 153cdf0e10cSrcweir } 154cdf0e10cSrcweir signature.append(')'); 155cdf0e10cSrcweir return signature.toString(); 156cdf0e10cSrcweir } 157cdf0e10cSrcweir getArguments()158cdf0e10cSrcweir public com.sun.star.sheet.FunctionArgument[] getArguments() 159cdf0e10cSrcweir { 160cdf0e10cSrcweir int count = functionDescription.getParameterCount(); 161cdf0e10cSrcweir final boolean infinite = functionDescription.isInfiniteParameterCount(); 162cdf0e10cSrcweir if (infinite) 163cdf0e10cSrcweir { 164cdf0e10cSrcweir count = 30; 165cdf0e10cSrcweir } 166cdf0e10cSrcweir final FunctionArgument[] args = new FunctionArgument[count]; 167cdf0e10cSrcweir for (int i = 0; i < args.length; i++) 168cdf0e10cSrcweir { 169cdf0e10cSrcweir final int pos = infinite ? 0 : i; 170cdf0e10cSrcweir args[i] = new FunctionArgument(); 171cdf0e10cSrcweir args[i].Description = functionDescription.getParameterDescription(pos, defaultLocale); 172cdf0e10cSrcweir args[i].Name = functionDescription.getParameterDisplayName(pos, defaultLocale); 173cdf0e10cSrcweir args[i].IsOptional = !functionDescription.isParameterMandatory(pos); 174cdf0e10cSrcweir } 175cdf0e10cSrcweir return args; 176cdf0e10cSrcweir } 177cdf0e10cSrcweir createFormula(String[] arguments)178cdf0e10cSrcweir public String createFormula(String[] arguments) throws com.sun.star.lang.DisposedException, com.sun.star.lang.IllegalArgumentException, com.sun.star.uno.Exception 179cdf0e10cSrcweir { 180cdf0e10cSrcweir final boolean infinite = functionDescription.isInfiniteParameterCount(); 181cdf0e10cSrcweir final int count = functionDescription.getParameterCount(); 182cdf0e10cSrcweir if (!infinite && arguments.length > count) 183cdf0e10cSrcweir { 184cdf0e10cSrcweir throw new com.sun.star.lang.IllegalArgumentException(); 185cdf0e10cSrcweir } 186cdf0e10cSrcweir 187cdf0e10cSrcweir final StringBuffer formula = new StringBuffer(getName()); 188cdf0e10cSrcweir formula.append('('); 189cdf0e10cSrcweir for (int i = 0; i < arguments.length; ++i) 190cdf0e10cSrcweir { 191cdf0e10cSrcweir if (arguments[i].length() == 0) 192cdf0e10cSrcweir { 193cdf0e10cSrcweir break; 194cdf0e10cSrcweir } 195cdf0e10cSrcweir formula.append(arguments[i]); 196cdf0e10cSrcweir if (i < (arguments.length - 1) && arguments[i + 1].length() != 0) 197cdf0e10cSrcweir { 198cdf0e10cSrcweir formula.append(';'); 199cdf0e10cSrcweir } 200cdf0e10cSrcweir } 201cdf0e10cSrcweir formula.append(')'); 202cdf0e10cSrcweir return formula.toString(); 203cdf0e10cSrcweir } 204cdf0e10cSrcweir } 205