Hi Carlos,
I had some time this weekend and looked at what you have done so far.
I downloaded your Translator.rar file. I was not familiar with .rar files, so it took me awhile to figure out how to extract your files from it. But I figured it out.
First off, your Translator.java program needs the methods described in chapter 5 of the AwareIM Programmers Reference. I took the liberty of updating your code with these methods. I put comments by everything I added/changed. I did not touch most of your original code. Below is the updated code. I did some testing of the program and it appears to work, but I don’t guarantee it. Also, I am calling your function TRANSLATE.
package javaapplication5;
// Added by Bob, needed for AwareIM
import java.util.Collection;
import com.bas.shared.functions.IFunctionLibrary;
import com.bas.shared.data.FunctionDescription;
import com.bas.shared.ruleparser.INodeHelper;
import com.bas.shared.ruleparser.ISQLBuilderHelper;
// implements clause added by Bob, needed for AwareIM
public class Translator implements IFunctionLibrary
{
private String Unidad[] = {"Cero","Uno","Dos","Tres","Cuatro","Cinco","Seis","Siete","Ocho","Nueve","Diez","Once","Doce","Trece","Catorce","Quince","Dieciseis","Diecisiete","Dieciocho","Diecinueve","Veinte"};
private String Decena[] = {"Veinti", "Treinta", "Cuarenta", "Cincuenta", "Sesenta", "Setenta", "Ochenta", "Noventa"};
private String Centena[] ={"Cien","Doscientos","Trescientos","Cuatrocientos","Quinientos","Seiscientos","Setecientos","Ochosientos","Novecientos","Mil","Un Millón","Millones","Un Billón","Billones"};
// START of code added by Bob, needed for AwareIM
// This method is required by AwareIM. It just returns the library (class) name.
public String getName () {
return "Translator";
}
// This method is required by AwareIM. It checks the input against the functions in this library.
// If there is a match, it returns true, otherwise it returns false.
public boolean hasFunction (String functionName, int paramNmb) {
if (paramNmb == 1 && functionName.equalsIgnoreCase("TRANSLATE")) {
return true;
}
return false;
}
// This method is required by AwareIM. This returns all the functions in this library along with
// a short description
public FunctionDescription [] getAvailableFunctions () {
FunctionDescription [] funct = new FunctionDescription [1];
funct [0] = new FunctionDescription ("TRANSLATE", "Takes in a number and converts it to text");
return funct;
}
// This method is required by AwareIM. This is where you code your actual function.
// This calls your original program and returns the text
public Object calculate (String functionName, Object[] params, INodeHelper helper) {
String resultString;
if (params.length == 1 && functionName.equalsIgnoreCase("TRANSLATE")) {
String theNumber=(String) params[0];
resultString=toLetras(theNumber);
return resultString;
}
return null;
}
// This method is required by AwareIM. This method must return the SQL representation of the function
// if it supports one. The SQL representation is essential if a function is to be used in queries.
// Since this function does not have a SQL representation, this method does not do anything.
public String toSQL (String functionName, Collection parameters, ISQLBuilderHelper sqlHelper)
throws Exception {
return null;
}
// This method is required by AwareIM.
// It returns the class type of the return values of your functions.
public Class getTypeClass (String functionName, Collection params) {
if (functionName.equalsIgnoreCase("TRANSLATE")) {
return String.class;
} else {
return null;
}
}
// END of code added by Bob, needed for AwareIM
private String getUnidad(long numero)
{String aux="";
for(int p=0;p<=20;p++)
{if(numero== p)
{ aux = Unidad[p] + " ";
return aux;
}
}
return " ";
}
private String getDecena(long numero)
{String aux="";
long pf=numero%10;
long pi=(numero/10);
int p=0;
boolean sal=false;
//pf: parte final del numero,pi: parte inicial del numero
while(p<=8 & sal==false)
{if(pi== p+2)
{ aux = Decena[p];
sal=true;
}
p++;
}
if (pf==0)
return aux+" ";
if(numero>20 & numero<30)
return(aux+getUnidad(pf)+" ");
return aux+" y "+getUnidad(pf)+" ";
}
private String getCentena(long numero)
{String aux="",aux2="";
long pf=numero%100;
long pi=(numero/100);
int p=0;
boolean sal=false;
while(p<=10 & sal==false)
{if(pi== p+1)
{ aux = Centena[p];
sal=true;
}
p++;
}
if (pf==0)
return aux;
if (pf<21)
aux2=getUnidad(pf);
else
aux2=getDecena(pf);
if (numero<200)
return(aux+"to "+aux2+" ");
else
return(aux+" "+aux2+" ");
}
private String getMil(long numero)
{ String aux="",aux2="";
long pf=numero%1000;
long pi=(numero/1000);
long p=0;
if(numero==1000) return "Mil";
if (numero>1000 & numero < 1999)
aux=Centena[9]+" ";
else
aux=resolverIntervalo(pi)+Centena[9]+" ";
if (pf!=0)
return(aux+resolverIntervalo(pf)+" ");
return aux;
}
private String getMillon(long numero)
{ String aux="",aux2="";
if (numero==1000000)
{
return aux= "Un Millón de";
}
long pf=numero%1000000;
long pi=(numero/1000000);
long p=0;
if (numero>1000000 & numero < 1999999)
aux=Centena[10]+" ";
else
aux=resolverIntervalo(pi)+Centena[11]+" ";
if (pf!=0)
return(aux+resolverIntervalo(pf)+" ");
return aux;
}
private String getBillon(long numero)
{ String aux="",aux2="";
long pf=numero%1000000000;
long pi=(numero/1000000000);
long p=0;
if (numero>1000000000 & numero <1999999999)
aux=Centena[12]+" ";
else
aux=resolverIntervalo(pi)+Centena[13]+" ";
if (pf!=0)
return(aux+resolverIntervalo(pf)+" ");
return aux;
}
private String resolverIntervalo(long numero)
{ if(numero>=0 & numero<=20)
return getUnidad(numero);
if(numero>=21 & numero<=99)
return getDecena(numero);
if(numero>=100 & numero<=999)
return getCentena(numero);
if(numero>=1000 & numero<=999999)
return getMil(numero);
if(numero>=1000000 & numero<=999999999)
return getMillon(numero);
if(numero>=1000000000 & numero<=2000000000)
return getBillon(numero);
return "<<El numero esta fuera del rango>>";
}
public String toLetras(String cantidad)
{
boolean isCent=false;
long numerosEnteros=0;
int numerosDecimales=0;
if (cantidad.startsWith("."))
{ isCent=true;
String enteros=cantidad.substring(1);
System.out.println(enteros);
try
{
numerosEnteros=Long.parseLong(enteros);
}
catch (NumberFormatException e){return "FORMATO INVALIDO";}
}
else
{
if (cantidad.indexOf(".")!=-1)
{
String enteros=cantidad.substring(0,cantidad.indexOf("."));
String decimales=cantidad.substring(cantidad.indexOf(".")+1);
try{
numerosEnteros=Long.parseLong(enteros);
numerosDecimales=Integer.parseInt(decimales);
}
catch (NumberFormatException e){return "FORMATO INVALIDO";}
}
else
{
try
{
numerosEnteros=Long.parseLong(cantidad);
}
catch (NumberFormatException e){return "FORMATO INVALIDO";}
}
}
if(numerosEnteros>1|numerosEnteros==0)
{
if (isCent)
{
String letras="Cero Pesos y "+resolverIntervalo(numerosEnteros)+"/100 M.N";
letras=letras.replaceAll(" "," ");
letras=letras.replaceAll(" "," ");
return letras.toUpperCase() ;
}
else
{
String letras=resolverIntervalo(numerosEnteros)+" Pesos "+numerosDecimales + "/100 M.N.";
letras=letras.replaceAll(" "," ");
letras=letras.replaceAll(" "," ");
return letras.toUpperCase() ;
}
}
if (numerosEnteros==1)
{
return ("UN PESO "+numerosDecimales +"/100 M.N.");
}
return null;
}
}
Second, your jar file would not have worked even if the program was correct. If you open a command prompt and cd to where you jar file is and enter the following command (which just displays what is in the jar file):
C:\Program Files\Java\jdk1.6.0_24\bin\jar.exe tvf Translator.jar
You will see that the path to the Translator.class file is thru C:\AwareIM\JDK\bin and it should be javaapplications5\Translator.class. The javaapplications5 comes from the package name which is the first line of your program.
Here are the steps I would take to compile your program and create a jar file.
1) Create a folder called javaapplications5 off of the c: drive.
2) Copy the above code into a file called Translator.java and put it in the javaapplications5 folder.
3) From a C: prompt compile the code with the following command:
c:\AwareIM\JDK\bin\javac -classpath C:\AwareIM\Tomcat\shared\lib\awareim.jar javaapplications5\Translator.java
NOTE: if you made changes to your AwareIM set up, you might have to change the path to both the AwareIM javac and awareim.jar file.
4) From a C: prompt create the jar file with the following code
C:\Program Files\Java\jdk1.6.0_24\bin\jar.exe cvf Translator.jar javaapplications5\*.class
This will create the Translator.jar file on the c: drive.
5) Move the jar file to c:\AwareIM\Tomcat\shared\lib
6) Add
%AWARE_HOME%\Translator.jar;
to setenv.bat and setenvClient.bat files in the AwareIM\bin folder.
7) Add
FunctionLibraryTranslator=javaapplications5.Translator
to the BASServer.props file in the AwareIM\bin folder.
User your new function something like: BO.TextFiled=TRANSLATE(BO.NumberField)
If you want the function name (TRANSLATE) changed to something else, just change TRANSLATE to whatever you want everywhere in the program.
Also, it looks like your original program was expecting the incoming number to be a String (Plain Text in AwareIM terms). If this is incorrect, let me know and I can change it.
Let me know if you have any questions or can not get this to work.
Good luck,
Bob