import com.hof.mi.interfaces.AnalyticalFunction; import com.hof.mi.interfaces.UserInputParameters; import com.hof.mi.interfaces.UserInputParameters.Parameter; import com.hof.util.Const; import com.hof.util.UtilString; import java.math.BigDecimal; public class OperatorMathAnalyticalFunction extends AnalyticalFunction { private static final String cvsId = "$Id: DivideByColumnAnalyticalFunction.java,v 1.2 2013/05/21 01:19:40 peterd Exp $"; private static Number Zero = new BigDecimal("0"); public String getName() { return "Column Math"; } public String getDescription() { return "Performs the selected operation on two columns."; } public String getColumnHeading(String paramString) { return paramString; } public String getCategory() { return UtilString.getResourceString("mi.text.analysis"); } public int getReturnType() { return 1; } public boolean acceptsNativeType(int paramInt) { return paramInt == 1; } protected void setupParameters() { UserInputParameters.Parameter p = new UserInputParameters.Parameter(); p.setUniqueKey("FIELD"); p.setDisplayName(UtilString.getResourceString("mi.text.analytic.function.dividebycolumn.parameter.column.name")); p.setDescription("Left of Operator"); p.setDataType(100); p.setAcceptsFieldType(1, true); p.setDisplayType(6); addParameter(p); p = new UserInputParameters.Parameter(); p.setUniqueKey("OPERATOR"); p.setDisplayName("Operator"); p.setDescription("Select which Operator to apply"); p.setDataType(2); p.setDisplayType(6); p.addOption("Add"); p.addOption("Subtract"); p.addOption("Divide"); p.addOption("Multiply"); addParameter(p); p = new UserInputParameters.Parameter(); p.setUniqueKey("FIELD2"); p.setDisplayName(UtilString.getResourceString("mi.text.analytic.function.dividebycolumn.parameter.column.name")); p.setDescription("Right of Operator"); p.setDataType(100); p.setAcceptsFieldType(1, true); p.setDisplayType(6); addParameter(p); } public Object applyAnalyticFunction(int paramInt, Object paramObject) { Object[] col1 = (Object[])getParameterValue("FIELD"); Object[] col2 = (Object[])getParameterValue("FIELD2"); Number value1 = (Number)col1[paramInt]; Number value2 = (Number)col2[paramInt]; if ((value1 == null) || (value2 == null)) { return Zero; } BigDecimal val = null; if ((value2 instanceof BigDecimal)) { val = (BigDecimal)value2; } else { val = new BigDecimal(value2.toString()); } int scale = val.scale(); if (scale < 0) { scale = 0; } BigDecimal val2 = null; if ((value1 instanceof BigDecimal)) { val2 = (BigDecimal)value1; } else { val2 = new BigDecimal(value1.toString()); } String operator = (String)getParameterValue("OPERATOR"); if ((val == null) || (val2 == null)) { return null; } if (operator.equals("Divide")){ if (val2.compareTo(Const.BDZERO) == 0) { return null; } val = val2.divide(val, 50, 4); } if (operator.equals("Multiply")){ val = val2.multiply(val); } if (operator.equals("Add")){ val = val2.add(val); } if (operator.equals("Subract")){ val = val2.subtract(val); } val = val.stripTrailingZeros(); if (val.scale() < scale) { val = val.setScale(scale); } return val; } }