对函数的调用不对应于的任何重载

时间:2019-03-25 11:07:25

标签: java crystal-reports

我正在开发一个Java库来管理Crystal Report中的各种条形码。 Crystal Report为Eclipse提供了一个sdk,在一个新项目中,有一个库用于开发ERP和CR之间的集成。这个库中的一个管理条形码,但是库很差,因为我只能管理一种条形码。我找到了实施UFL的文档。该文档提供了一个简单的示例,并感谢我编写了一个类来管理三种条形码,但是有一些带有两个参数的Crystal Report函数,并且该示例未显示如何正确实现。我试图开发新的类来管理此类函数,但始终收到以下错误消息:“对函数的调用与'函数名'的任何重载都不对应。”

对于此功能,我尝试仅传递一个参数,并且应用程序运行良好。我对方法进行了重载,传递了两个参数,但总是收到相同的错误消息。

这是我的代码的一部分

public final FormulaValue evaluate(FormulaValueReference[] arguments)
     throws FormulaFunctionCallException
  {

       StringValue dataStringArg = (StringValue)arguments[0].getFormulaValue();
       StringValue opzArg =(StringValue)arguments[1].getFormulaValue();

     String dataString = dataStringArg.getString();
     String opz=opzArg.getString();



    String returnVal = doTranslation(dataString,opz);

    return StringValue.fromString(returnVal);
   }

  protected abstract String translate(String inputData,String opz)
   throws InvalidBarcodeDataException;


public class CodeEAN13
  extends CodeEAN13Base
{


   public String getIdentifier()
   {
    return "barcodeean13";
  }

protected String translate(String inputData, String opz) throws InvalidBarcodeDataException {
    Integer checkDigitSubtotal;
.....
}

除了显示带有这种条形码的报告外,我。 CodeEAN13类应运行一种算法来计算条形码字体的字符串。

这是程序运行时从库中显示的代码,并显示在堆栈跟踪中。对于stracktrace的每一行,我都输入了相应的代码。

//com.crystaldecisions.reports.formulas.FormulaService.compile(SourceFile:347)

public void compile(FormulaDefinitionBase paramFormulaDefinitionBase, FormulaContext paramFormulaContext)
    throws FormulaException
  {
    if (paramFormulaDefinitionBase == null) {
      throw new NullPointerException("A formula must be given.");
    }
    paramFormulaDefinitionBase.getFormulaInfo().a(paramFormulaContext);
  }

//com.crystaldecisions.reports.formulas.FormulaInfo.a(SourceFile:570)

void a(FormulaContext paramFormulaContext)
    throws FormulaException
  {
    if (this.jdField_try)
    {
      this.o = true;

      throw new FormulaException("JRC00000433", "", FormulaResources.a(), "RecursiveCompileDetected", this);
    }
    this.o = false;
    if (paramFormulaContext == null) {
      throw new NullPointerException("A formula context must be given.");
    }
    try
    {
      this.jdField_try = true;
      jdMethod_if(paramFormulaContext);
      this.t = this.jdField_long.getFormulaValueType();
      this.jdField_do.a();//(SourceFile:570)
    }
    catch (FormulaException localFormulaException)
    {
      if ((localFormulaException.getResourceKey().equals("RecursiveCompileDetected")) && (this.o)) {
        throw new FormulaException("JRC00000434", "", FormulaResources.a(), "RecursionNotAllowed", null, localFormulaException);
      }
      throw localFormulaException;
    }
    finally
    {
      this.jdField_try = false;
      jdMethod_if();
    }
  }

//com.crystaldecisions.reports.formulas.r.a(SourceFile:90)
public void a()
    throws FormulaException
  {
    FormulaInfo localFormulaInfo = this.a.getFormulaInfo();
    FormulaDefinitionBase localFormulaDefinitionBase = this.a.getFormula();
    String str1 = "Formula";
    String str2 = "";
    if ((localFormulaDefinitionBase instanceof CustomFunctionDefinition))
    {
      str1 = "Custom function";
      str2 = "'";
    }
    if (if.isDebugEnabled()) {
      if.debug("Compiling " + StringUtil.toLowerCaseEng(str1) + " " + str2 + localFormulaDefinitionBase.getFormulaForm() + str2 + "...");
    }
    localFormulaInfo.invalidate();
    try
    {
      ExpressionNode localExpressionNode = o.a(this.a).a();
      localExpressionNode = this.jdField_do.jdMethod_void(localExpressionNode);//(SourceFile:90)
      localExpressionNode = this.jdField_for.c(localExpressionNode);
      localFormulaInfo.a(localExpressionNode);

      localFormulaDefinitionBase.formulaInfoUpdated();
      if (if.isInfoEnabled()) {
        if.info(str1 + " " + str2 + localFormulaDefinitionBase.getFormulaForm() + str2 + " compiled.");
      }
    }
    catch (FormulaException localFormulaException)
    {
      if (if.isInfoEnabled()) {
        if.info(str1 + " " + str2 + localFormulaDefinitionBase.getFormulaForm() + str2 + " did not compile: " + localFormulaException);
      }
      localFormulaInfo.invalidate();

      throw localFormulaException;
    }
  }
}
//com.crystaldecisions.reports.formulas.c.void(SourceFile:70)
 public ExpressionNode jdMethod_void(ExpressionNode paramExpressionNode)
    throws FormulaException
  {
    ExpressionNode localExpressionNode = paramExpressionNode;

    this.jdField_int = 0;
    this.jdField_for = 0;

    FormulaDefinitionBase localFormulaDefinitionBase = this.a.getFormula();
    FormulaInfo localFormulaInfo = this.a.getFormulaInfo();

    localExpressionNode = jdMethod_do(localExpressionNode);//(SourceFile:70)

    FormulaValueType localFormulaValueType = a(localFormulaDefinitionBase, localFormulaInfo, localExpressionNode);
    if ((localExpressionNode != null) && (!localFormulaValueType.equals(localExpressionNode.getFormulaValueType())))
    {
      localExpressionNode = a(localExpressionNode, localFormulaValueType);
      if ((!new) && (!localExpressionNode.getFormulaValueType().equals(localFormulaValueType))) {
        throw new AssertionError("Coercion failed!!! This should never happen.");
      }
    }
    localFormulaInfo.a(localFormulaValueType);

    return localExpressionNode;
  }

  //com.crystaldecisions.reports.formulas.j.do(SourceFile:74)
  final ExpressionNode jdMethod_do(ExpressionNode paramExpressionNode)
    throws FormulaException
  {
    try
    {
      return a(paramExpressionNode);//(SourceFile:74)
    }
    catch (StackOverflowError localStackOverflowError)
    {
      throw new FormulaException("JRC00000461", "", FormulaResources.a(), "StackOverflow", this.a);
    }
  }

  //com.crystaldecisions.reports.formulas.j.a(SourceFile:250)
  ExpressionNode a(ExpressionNode paramExpressionNode)
    throws FormulaException
  {
    if (paramExpressionNode == null) {
      return null;
    }
    switch (paramExpressionNode.jdField_for.value())
    {
        ......
        case 30: 
      return jdMethod_for((MultiOperatorNode)paramExpressionNode);


//com.crystaldecisions.reports.formulas.c.for(SourceFile:1225)
 ExpressionNode jdMethod_for(MultiOperatorNode paramMultiOperatorNode)
    throws FormulaException
  {
    int i = paramMultiOperatorNode.size();
    if (i == 0) {
      return null;
    }
    for (int j = 0; j < i; j++)
    {
      ExpressionNode localExpressionNode = a(paramMultiOperatorNode.getOperand(j));//(SourceFile:1225)
      if (localExpressionNode == null) {
        return null;
      }
      paramMultiOperatorNode.setOperand(j, localExpressionNode);
    }
    paramMultiOperatorNode.jdField_try = paramMultiOperatorNode.getOperand(i - 1).jdField_try;

    return paramMultiOperatorNode;
  } 

  //com.crystaldecisions.reports.formulas.j.a(SourceFile:295)
  case 8: 
      return a((FunctionNode)paramExpressionNode);


//com.crystaldecisions.reports.formulas.j.a(SourceFile:3148)
 ExpressionNode a(FunctionNode paramFunctionNode)
    throws FormulaException
  {
    int i = paramFunctionNode.size();
    ExpressionNode[] arrayOfExpressionNode = ArrayPool.a(i, this.a);
    try
    {
      for (int j = 0; j < i; j++)
      {
        ExpressionNode localExpressionNode2 = a(paramFunctionNode.get(j), arrayOfExpressionNode);
        if (localExpressionNode2 == null) {
          return null;
        }
        arrayOfExpressionNode[j] = localExpressionNode2;
      }
      return a(paramFunctionNode, arrayOfExpressionNode);//(SourceFile:3148)
    }
    finally
    {
      ArrayPool.a(arrayOfExpressionNode, this.a);
    }
  }

 //com.crystaldecisions.reports.formulas.c.a(SourceFile:1809)



//com.crystaldecisions.reports.formulas.j.a(SourceFile:3507)
 FormulaException a(String paramString1, String paramString2, String paramString3, String[] paramArrayOfString, ExpressionNode paramExpressionNode)
  {
    return a(paramString1, paramString2, paramString3, paramArrayOfString, null, paramExpressionNode);
  }

  //com.crystaldecisions.reports.formulas.j.a(SourceFile:3528)
  FormulaException a(String paramString1, String paramString2, String paramString3, String[] paramArrayOfString, Throwable paramThrowable, ExpressionNode paramExpressionNode)
  {
    return new FormulaException(paramString1, paramString2,FormulaResources.a(), paramString3, paramArrayOfString, paramThrowable, this.a, paramExpressionNode);//(SourceFile:3528)
  }


   public FormulaException(String paramString1, String paramString2, CrystalResourcesFactory paramCrystalResourcesFactory, String paramString3, String[] paramArrayOfString, Throwable paramThrowable, FormulaEnvironment paramFormulaEnvironment, ExpressionNode paramExpressionNode)
  {
    super(paramString1, paramString2, paramCrystalResourcesFactory, paramString3, paramArrayOfString, paramThrowable);

    a(paramFormulaEnvironment, paramExpressionNode);
  }

4 个答案:

答案 0 :(得分:0)

这是完整的代码

import com.crystaldecisions.reports.formulas.FormulaFunction;
import com.crystaldecisions.reports.formulas.FormulaFunctionLibrary;



 public class BarcodeLibrary
   implements FormulaFunctionLibrary
 {
   private final FormulaFunction[] functionArray = {new Code39(), new Code39Ascii(), new CodeI2of5(), new Code2of5()
                                                    ,new CodeC128SetA(),new CodeEAN13()};

   public FormulaFunction getFunction(int functionNumber)
   {
     return this.functionArray[functionNumber];
   }

   public int size()
   {
    return this.functionArray.length;
  }
 }

*************************************************************************
import com.crystaldecisions.reports.common.CrystalResourcesFactory;
import com.crystaldecisions.reports.common.value.FormulaValue;
import com.crystaldecisions.reports.common.value.FormulaValueType;
import com.crystaldecisions.reports.common.value.StringValue;
import com.crystaldecisions.reports.formulas.FormulaFunction;
import com.crystaldecisions.reports.formulas.FormulaFunctionArgumentDefinition;
import com.crystaldecisions.reports.formulas.FormulaFunctionCallException;
import com.crystaldecisions.reports.formulas.FormulaValueReference;
import com.crystaldecisions.reports.formulas.SimpleFormulaFunctionArgumentDefinition;

public abstract class CodeEAN13Base
   implements FormulaFunction
 {
   FormulaFunctionArgumentDefinition[] myArguments = { SimpleFormulaFunctionArgumentDefinition.string };

protected CrystalResourcesFactory resCrystal;



  public CodeEAN13Base()
   {
     this.resCrystal = new CrystalResourcesFactory("Messages");

   }


  protected final String doTranslation(String inputData,String opz)

   {
    String returnVal = "";

     returnVal = returnVal + translateCharWrapper(inputData,opz);



    return returnVal;
   }


   @Override
   public final FormulaValue evaluate(FormulaValueReference[] arguments)
     throws FormulaFunctionCallException
  {

       StringValue dataStringArg = (StringValue)arguments[0].getFormulaValue();
       StringValue opzArg =(StringValue)arguments[1].getFormulaValue();

     String dataString = dataStringArg.getString();
     String opz=opzArg.getString();



    String returnVal = doTranslation(dataString,opz);

    return StringValue.fromString(returnVal);
   }

   @Override
   public final FormulaFunctionArgumentDefinition[] getArguments()
  {
     return this.myArguments;
  }

 @Override  
public final FormulaValueType getReturnType()
  {
     return FormulaValueType.string;
   }


   protected abstract String translate(String inputData,String opz)
   throws InvalidBarcodeDataException;

//protected abstract String translateChar(String inputData)
//throws InvalidBarcodeDataException;

 // private String translateCharWrapper(String inputData)
   private String translateCharWrapper(String inputData,String opz)
  {
     String returnString;

     try
    {
        // returnString = translateChar(inputData);
      returnString = translate(inputData,opz);
     } catch (InvalidBarcodeDataException e) {
      returnString = "";
     }

    return returnString;
  }

   @Override
   public void validateArgumentValues(FormulaValueReference[] arguments)
     throws FormulaFunctionCallException
   {}
 }

*************************************************************************
public class CodeEAN13
  extends CodeEAN13Base
{

    protected String opz;



   @Override
   public String getIdentifier()
   {
    return "barcodeean13";
  }



  private static String oddBar(String code) {

        return String.valueOf((char)(65+Integer.parseInt(code)));

    }

  private static String evenBar(String code) {

        return String.valueOf((char)(75+Integer.parseInt(code)));

    }





@Override
protected String translate(String inputData, String opz) throws InvalidBarcodeDataException {
    // TODO Auto-generated method stub
    Integer checkDigitSubtotal;
  String checkDigit;
      String temp = null;
      String code=inputData;
      String azalea_EAN13 = null;
      Object opzM=opz;

      if(!opzM.equals(null)) {
       code=code+opzM;
      }

      //Calculate the EAN-13 check digit.

      checkDigitSubtotal=3*(Integer.parseInt(code.substring(1, 2))+Integer.parseInt(code.substring(3, 4))+Integer.parseInt(code.substring(5, 6))
                           +Integer.parseInt(code.substring(7, 8))+Integer.parseInt(code.substring(9, 10))
                           +Integer.parseInt(code.substring(code.length()-1)));


  checkDigitSubtotal=checkDigitSubtotal+(Integer.parseInt(code.substring(0,1))+Integer.parseInt(code.substring(2, 3))
                                       +Integer.parseInt(code.substring(4, 5))+Integer.parseInt(code.substring(6, 7))
                                       +Integer.parseInt(code.substring(8, 9))+Integer.parseInt(code.substring(10, 11)));

  checkDigit=String.valueOf(300-checkDigitSubtotal);
  checkDigit=checkDigit.substring(checkDigit.length()-1);



 // Begin building the output string with the 1st character's human-readable, L guard bars & odd parity of 1st digit
   temp=(char)(194+Integer.parseInt(code.substring(0,1)))+"x"+oddBar(code.substring(1, 2));  

  // Build the remainder of left half of symbol's parity is based on 1st digit
   switch (code.substring(0,1)) {
   case "0":
      {
         temp=temp+oddBar(code.substring(2, 3));
         temp=temp+oddBar(code.substring(3, 4));
         temp=temp+oddBar(code.substring(4, 5));
         temp=temp+oddBar(code.substring(5, 6));
         temp=temp+oddBar(code.substring(6, 7));         
      }
    break;
   case "1":
      {
          temp=temp+oddBar(code.substring(2, 3)); 
          temp=temp+evenBar(code.substring(3, 4));
          temp=temp+oddBar(code.substring(4, 5));
          temp=temp+evenBar(code.substring(5, 6));
          temp=temp+evenBar(code.substring(6, 7));
      }
      break;
   case "2":
   {
       temp=temp+oddBar(code.substring(2, 3));
       temp=temp+evenBar(code.substring(3, 4));
       temp=temp+evenBar(code.substring(4, 5));
       temp=temp+oddBar(code.substring(5, 6));
       temp=temp+evenBar(code.substring(6, 7));
   }
   break;
   case "3":
   {
       temp=temp+oddBar(code.substring(2, 3));
       temp=temp+evenBar(code.substring(3, 4));
       temp=temp+evenBar(code.substring(4, 5));
       temp=temp+evenBar(code.substring(5, 6));
       temp=temp+oddBar(code.substring(6, 7));
   }
    break;
   case "4":
   {
       temp=temp+evenBar(code.substring(2, 3));
       temp=temp+oddBar(code.substring(3, 4));
       temp=temp+oddBar(code.substring(4, 5));
       temp=temp+evenBar(code.substring(5, 6));
       temp=temp+evenBar(code.substring(6, 7));

   }
   break;
   case "5":
   {
       temp=temp+evenBar(code.substring(2, 3));
       temp=temp+evenBar(code.substring(3, 4));
       temp=temp+oddBar(code.substring(4, 5));
       temp=temp+oddBar(code.substring(5, 6));
       temp=temp+evenBar(code.substring(6, 7));

   }
   break;
   case "6":
   {
       temp=temp+evenBar(code.substring(2, 3));
       temp=temp+evenBar(code.substring(3, 4));
       temp=temp+evenBar(code.substring(4, 5));
       temp=temp+oddBar(code.substring(5, 6));
       temp=temp+oddBar(code.substring(6, 7));

   }
   break;
   case "7":
   {
       temp=temp+evenBar(code.substring(2, 3));
       temp=temp+oddBar(code.substring(3, 4));
       temp=temp+evenBar(code.substring(4, 5));
       temp=temp+oddBar(code.substring(5, 6));
       temp=temp+evenBar(code.substring(6, 7));



   }
   break;
   case "8":
   {
       temp=temp+evenBar(code.substring(2, 3));
       temp=temp+oddBar(code.substring(3, 4));
       temp=temp+evenBar(code.substring(4, 5));
       temp=temp+evenBar(code.substring(5, 6));
       temp=temp+oddBar(code.substring(6, 7));

   }
   break;
   case "9":
   {
       temp=temp+evenBar(code.substring(2, 3));
       temp=temp+evenBar(code.substring(3, 4));
       temp=temp+oddBar(code.substring(4, 5));
       temp=temp+evenBar(code.substring(5, 6));
       temp=temp+oddBar(code.substring(6, 7));

   }
   break;

    default:
   }

////Add the center guard bars & the build right half of symbol using set C (0-9)

    return azalea_EAN13;
   }


 }


答案 1 :(得分:0)

部分日志:

1922 [TSLVReader #1] DEBUG com.crystaldecisions.reports.formulas.Compiler  - Compiling formula {@ean13}...
1922 [TSLVReader #1] INFO com.crystaldecisions.reports.formulas.Compiler  - Formula {@ean13} did not compile: com.crystaldecisions.reports.formulas.FormulaException: Exception in the formulas '{@ean13}' a 'BarcodeEAN13('412345678901','567')':
the call to a function does not correspond to any overload of barcodeean13.
1922 [TSLVReader #1] WARN com.crystaldecisions.reports.reportdefinition.ReportDefinition  - com.crystaldecisions.reports.formulas.FormulaException: Exception in the formulas  '{@ean13}' a 'BarcodeEAN13('412345678901','567')':
the call to a function does not correspond to any overload of barcodeean13.
1923 [TSLVReader #1] ERROR com.crystaldecisions.reports.dataengine  - Formula error: recompile formulas failed
com.crystaldecisions.reports.formulas.FormulaException: Exception in the formulas  '{@ean13}' a 'BarcodeEAN13('412345678901','567')':
the call to a function does not correspond to any overload of barcodeean13.
    at com.crystaldecisions.reports.formulas.j.a(SourceFile:3528)
    at com.crystaldecisions.reports.formulas.j.a(SourceFile:3507)
    at com.crystaldecisions.reports.formulas.c.a(SourceFile:1809)
    at com.crystaldecisions.reports.formulas.j.a(SourceFile:3148)
    at com.crystaldecisions.reports.formulas.j.a(SourceFile:295)
    at com.crystaldecisions.reports.formulas.c.for(SourceFile:1225)
    at com.crystaldecisions.reports.formulas.j.a(SourceFile:250)
    at com.crystaldecisions.reports.formulas.j.do(SourceFile:74)
    at com.crystaldecisions.reports.formulas.c.void(SourceFile:70)
    at com.crystaldecisions.reports.formulas.r.a(SourceFile:90)
    at com.crystaldecisions.reports.formulas.FormulaInfo.a(SourceFile:570)
    at com.crystaldecisions.reports.formulas.FormulaService.compile(SourceFile:347)

答案 2 :(得分:0)

1922 [TSLVReader #1] DEBUG com.crystaldecisions.reports.formulas.Compiler  - Compiling formula {@ean13}...
1922 [TSLVReader #1] INFO com.crystaldecisions.reports.formulas.Compiler  - Formula {@ean13} did not compile: com.crystaldecisions.reports.formulas.FormulaException: Exception in the formulas '{@ean13}' a 'BarcodeEAN13('412345678901','567')':
the call to a function does not correspond to any overload of barcodeean13.
1922 [TSLVReader #1] WARN com.crystaldecisions.reports.reportdefinition.ReportDefinition  - com.crystaldecisions.reports.formulas.FormulaException: Exception in the formulas  '{@ean13}' a 'BarcodeEAN13('412345678901','567')':
the call to a function does not correspond to any overload of barcodeean13.
1923 [TSLVReader #1] ERROR com.crystaldecisions.reports.dataengine  - Formula error: recompile formulas failed
com.crystaldecisions.reports.formulas.FormulaException: Exception in the formulas  '{@ean13}' a 'BarcodeEAN13('412345678901','567')':
the call to a function does not correspond to any overload of barcodeean13.
    at com.crystaldecisions.reports.formulas.j.a(SourceFile:3528)
    at com.crystaldecisions.reports.formulas.j.a(SourceFile:3507)
    at com.crystaldecisions.reports.formulas.c.a(SourceFile:1809)
    at com.crystaldecisions.reports.formulas.j.a(SourceFile:3148)
    at com.crystaldecisions.reports.formulas.j.a(SourceFile:295)
    at com.crystaldecisions.reports.formulas.c.for(SourceFile:1225)
    at com.crystaldecisions.reports.formulas.j.a(SourceFile:250)
    at com.crystaldecisions.reports.formulas.j.do(SourceFile:74)
    at com.crystaldecisions.reports.formulas.c.void(SourceFile:70)
    at com.crystaldecisions.reports.formulas.r.a(SourceFile:90)
    at com.crystaldecisions.reports.formulas.FormulaInfo.a(SourceFile:570)
    at com.crystaldecisions.reports.formulas.FormulaService.compile(SourceFile:347)

stacktrace会告诉您您需要了解的内容,但是我无法提供更具体的信息,因为Crystal Reports似乎是收费的,并且它们不提供文档(至少是免费的),因此我无法查看文档的来源。罐子。
无论如何,此片段将告诉您大部分您需要了解的内容:

1923 [TSLVReader #1] ERROR com.crystaldecisions.reports.dataengine  - Formula error: recompile formulas failed
com.crystaldecisions.reports.formulas.FormulaException: Exception in the formulas  '{@ean13}' a 'BarcodeEAN13('412345678901','567')':
the call to a function does not correspond to any overload of barcodeean13.
    at com.crystaldecisions.reports.formulas.j.a(SourceFile:3528)

此行:

com.crystaldecisions.reports.formulas.FormulaException  

告诉您抛出的异常来自Crystal Reports源代码,而不是Java特定的错误。因此, 如果可以的话,您应该查阅文档(如果您可以访问)或检查源代码以查看导致该错误的条件 。如果不确定如何执行此操作,可以在问题中添加一些此类信息,我或其他人可以尝试提供帮助。

at com.crystaldecisions.reports.formulas.j.a(SourceFile:3528)

括号中的部分告诉您在哪里可以找到异常源SourceFile,可能但不一定在3528行。此行号在实践中可能会有所不同,特别是如果您要查看的代码是反编译的,因此,如果事情没有按预期进行,您可能需要四处寻找确切的路线。

不幸的是,如果没有文档或源代码,将很难为您提供进一步的帮助。

答案 3 :(得分:0)

问题出在以下陈述上:

FormulaFunctionArgumentDefinition[] myArguments = {SimpleFormulaFunctionArgumentDefinition.string };

参数的正确配置是:

FormulaFunctionArgumentDefinition[] myArguments = { SimpleFormulaFunctionArgumentDefinition.string,SimpleFormulaFunctionArgumentDefinition.string };

在我的Crystal Report公式中,取决于公式,通常有两种String类型或一种String类型。 下面是解决带两个参数的条形码功能的UFL的完整代码。这段代码设置了Crystal Report的UFL库。

import com.crystaldecisions.reports.common.CrystalResourcesFactory;
import com.crystaldecisions.reports.common.value.FormulaValue;
import com.crystaldecisions.reports.common.value.FormulaValueType;

import com.crystaldecisions.reports.common.value.StringValue;
import com.crystaldecisions.reports.formulas.FormulaFunction;
import com.crystaldecisions.reports.formulas.FormulaFunctionArgumentDefinition;
import com.crystaldecisions.reports.formulas.FormulaFunctionCallException;
import com.crystaldecisions.reports.formulas.FormulaValueReference;
import com.crystaldecisions.reports.formulas.SimpleFormulaFunctionArgumentDefinition;

public abstract class CodeEAN13Base
   implements FormulaFunction
 {
   FormulaFunctionArgumentDefinition[] myArguments = { SimpleFormulaFunctionArgumentDefinition.string,SimpleFormulaFunctionArgumentDefinition.string };

protected CrystalResourcesFactory resCrystal;



  public CodeEAN13Base()
   {
     this.resCrystal = new CrystalResourcesFactory("Messages");

   }


  protected final String doTranslation(String inputData,String opz)
 // protected final String doTranslation(String inputData)
   {
    String returnVal = "";

   //  returnVal = returnVal + translateCharWrapper(inputData);
     returnVal = returnVal + translateCharWrapper(inputData,opz);


    return returnVal;
   }



   @Override
   public final FormulaValue evaluate(FormulaValueReference[] arguments)
     throws FormulaFunctionCallException
  {

       StringValue dataStringArg = (StringValue)arguments[0].getFormulaValue();
       StringValue opzArg =(StringValue)arguments[1].getFormulaValue();

     String dataString = dataStringArg.getString();
     String opz=opzArg.getString();





    String returnVal = doTranslation(dataString,opz);

    return StringValue.fromString(returnVal);
   }

   @Override
   public final FormulaFunctionArgumentDefinition[] getArguments()
  {
     return this.myArguments;
  }

 @Override  
public final FormulaValueType getReturnType()
  {
     return FormulaValueType.string;
   }


   protected abstract String translate(String inputData,String opz)
   throws InvalidBarcodeDataException;

//protected abstract String translate(String inputData)
//throws InvalidBarcodeDataException;

 // private String translateCharWrapper(String inputData)
   private String translateCharWrapper(String inputData,String opz)
  {
     String returnString;

     try
    {
        // returnString = translate(inputData);
      returnString = translate(inputData,opz);
     } catch (InvalidBarcodeDataException e) {
      returnString = "";
     }

    return returnString;
  }



   @Override
   public void validateArgumentValues(FormulaValueReference[] arguments)
     throws FormulaFunctionCallException
   {}
 }

public class CodeEAN13
  extends CodeEAN13Base
{

    //protected String opz;



   @Override
   public String getIdentifier()
   {
    return "barcodeean13";
  }



  private static String oddBar(String code) {

        return String.valueOf((char)(65+Integer.parseInt(code)));

    }

  private static String evenBar(String code) {

        return String.valueOf((char)(75+Integer.parseInt(code)));

    }


@Override

    protected String translate(String inputData, String opz) throws InvalidBarcodeDataException {   

     Integer checkDigitSubtotal;
     String checkDigit;
      String temp = null;
      String code=inputData;
      String azalea_EAN13 = null;
      String opzM=opz;


//    if(!opzM.equals(null)) {
//     code=code+opzM;
//    }

      //Calculate the EAN-13 check digit.

      checkDigitSubtotal=3*(Integer.parseInt(code.substring(1, 2))+Integer.parseInt(code.substring(3, 4))+Integer.parseInt(code.substring(5, 6))
                           +Integer.parseInt(code.substring(7, 8))+Integer.parseInt(code.substring(9, 10))
                           +Integer.parseInt(code.substring(code.length()-1)));


      checkDigitSubtotal=checkDigitSubtotal+(Integer.parseInt(code.substring(0,1))+Integer.parseInt(code.substring(2, 3))
                                       +Integer.parseInt(code.substring(4, 5))+Integer.parseInt(code.substring(6, 7))
                                       +Integer.parseInt(code.substring(8, 9))+Integer.parseInt(code.substring(10, 11)));

  checkDigit=String.valueOf(300-checkDigitSubtotal);
  checkDigit=checkDigit.substring(checkDigit.length()-1);



 // Begin building the output string with the 1st character's human-readable, L guard bars & odd parity of 1st digit
   temp=(char)(194+Integer.parseInt(code.substring(0,1)))+"x"+oddBar(code.substring(1, 2));  

  // Build the remainder of left half of symbol's parity is based on 1st digit
   switch (code.substring(0,1)) {
   case "0":
      {
         //temp="U|x"; 
         temp=temp+oddBar(code.substring(2, 3));
         temp=temp+oddBar(code.substring(3, 4));
         temp=temp+oddBar(code.substring(4, 5));
         temp=temp+oddBar(code.substring(5, 6));
         temp=temp+oddBar(code.substring(6, 7));         
      }
    break;
   case "1":
      {
          //temp="[|x";
          temp=temp+oddBar(code.substring(2, 3)); 
          temp=temp+evenBar(code.substring(3, 4));
          temp=temp+oddBar(code.substring(4, 5));
          temp=temp+evenBar(code.substring(5, 6));
          temp=temp+evenBar(code.substring(6, 7));
      }
      break;
   case "2":
   {
       //temp="V|x";
       temp=temp+oddBar(code.substring(2, 3));
       temp=temp+evenBar(code.substring(3, 4));
       temp=temp+evenBar(code.substring(4, 5));
       temp=temp+oddBar(code.substring(5, 6));
       temp=temp+evenBar(code.substring(6, 7));
   }
   break;
   case "3":
   {
       //temp="W|x";
       temp=temp+oddBar(code.substring(2, 3));
       temp=temp+evenBar(code.substring(3, 4));
       temp=temp+evenBar(code.substring(4, 5));
       temp=temp+evenBar(code.substring(5, 6));
       temp=temp+oddBar(code.substring(6, 7));
   }
    break;
   case "4":
   {
       //temp="X|x";
       temp=temp+evenBar(code.substring(2, 3));
       temp=temp+oddBar(code.substring(3, 4));
       temp=temp+oddBar(code.substring(4, 5));
       temp=temp+evenBar(code.substring(5, 6));
       temp=temp+evenBar(code.substring(6, 7));

   }
   break;
   case "5":
   {
       //temp="Y|x";
       temp=temp+evenBar(code.substring(2, 3));
       temp=temp+evenBar(code.substring(3, 4));
       temp=temp+oddBar(code.substring(4, 5));
       temp=temp+oddBar(code.substring(5, 6));
       temp=temp+evenBar(code.substring(6, 7));

   }
   break;
   case "6":
   {
       //temp="Z|x";
       temp=temp+evenBar(code.substring(2, 3));
       temp=temp+evenBar(code.substring(3, 4));
       temp=temp+evenBar(code.substring(4, 5));
       temp=temp+oddBar(code.substring(5, 6));
       temp=temp+oddBar(code.substring(6, 7));

   }
   break;
   case "7":
   {
       //temp="u|x";
       temp=temp+evenBar(code.substring(2, 3));
       temp=temp+oddBar(code.substring(3, 4));
       temp=temp+evenBar(code.substring(4, 5));
       temp=temp+oddBar(code.substring(5, 6));
       temp=temp+evenBar(code.substring(6, 7));



   }
   break;
   case "8":
   {
       //temp="\\|x";
       temp=temp+evenBar(code.substring(2, 3));
       temp=temp+oddBar(code.substring(3, 4));
       temp=temp+evenBar(code.substring(4, 5));
       temp=temp+evenBar(code.substring(5, 6));
       temp=temp+oddBar(code.substring(6, 7));

   }
   break;
   case "9":
   {
       //temp="]|x";
       temp=temp+evenBar(code.substring(2, 3));
       temp=temp+evenBar(code.substring(3, 4));
       temp=temp+oddBar(code.substring(4, 5));
       temp=temp+evenBar(code.substring(5, 6));
       temp=temp+oddBar(code.substring(6, 7));

   }
   break;

    default:
   }

////Add the center guard bars & the build right half of symbol using set C (0-9)


    return azalea_EAN13+opzM;
   }




 }