FUNCTION TO CONVERT (NUMBER TO TEXT) / (CURRENCY TO WORDS)
DONT FORGET TO SHARE YOUR AwareIM stuff!!!
Follow the steps. Really easy to create
1.- Create a folder in "C:\" named: javaapplication7
2.- Open a text editor like "Notepad ++" and create file named: TranslatorEn.java
Copy the next code and save the file in C:\javaapplication7
Note: Watch for upper and lower case.
package javaapplication7;
// 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 TranslatorEn implements IFunctionLibrary
{
private String Unidad[] = {"Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen","Twenty"};
private String Decena[] = {"Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
private String Centena[] ={"One Hundred","Two Hundred","Three Hundred","Four Hundred","Five Hundred","Six Hundred","Seven Hundred","Eight Hundred","Nine Hundred","Thousand","One Million","Million","One Billion","Billions"};
// 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 "TranslatorEn";
}
// 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("TRANSLATEEN")) {
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 ("TRANSLATEEN", "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("TRANSLATEEN")) {
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("TRANSLATEEN")) {
return String.class;
} else {
return null;
}
}
////////////end
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++;
}
return aux+" "+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);
return(aux+" "+aux2+" ");
}
private String getMil(long numero)
{ String aux="";
long pf=numero%1000;
long pi=(numero/1000);
long p=0;
aux=resolverIntervalo(pi)+" "+Centena[9]+" ";
if (pf!=0)
return(aux+resolverIntervalo(pf)+" ");
return aux;
}
private String getMillon(long numero)
{ String aux="";
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="";
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;
String decimales="0";
if (cantidad.startsWith("."))
{ isCent=true;
decimales=cantidad.substring(1);
if (decimales.length()==1)
{
decimales+=0;
}
}
else
{
if (cantidad.indexOf(".")!=-1)
{
String enteros=cantidad.substring(0,cantidad.indexOf("."));
decimales=cantidad.substring(cantidad.indexOf(".")+1);
if (decimales.length()==1)
{
decimales+=0;
}
try{
numerosEnteros=Long.parseLong(enteros);
}
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="Zero dollars and "+decimales+"/100 Cents";
letras=letras.replaceAll(" "," ");
letras=letras.replaceAll(" "," ");
return letras.toUpperCase() ;
}
else
{
String letras=resolverIntervalo(numerosEnteros)+"US Dollars and "+decimales + "/100 Cents";
letras=letras.replaceAll(" "," ");
letras=letras.replaceAll(" "," ");
return letras.toUpperCase() ;
}
}
if (numerosEnteros==1)
{
return ("One Dollar and "+decimales +"/100 Cents").toUpperCase();
}
return null;
}
}
3.- From a C: prompt compile the code with the following command:
c:\AwareIM\JDK\bin\javac -classpath C:\AwareIM\Tomcat\lib\awareim.jar javaapplication7\TranslatorEn.java
4.-From a C:\ prompt create the jar file with the following code:
(Note: Here you might need Netbeans from the Java site.)
C:\"Program Files"\Java\jdk1.6.0_24\bin\jar.exe cvf TranslatorEn.jar javaapplication7\*.class
Note: jdk1.6.0_24 might change depending on your netbeans version.
5.- The file TranslatorEn.jar will be created in C:[/b]
6.- Copy the file TranslatorEn.jar to C:\AwareIM\Tomcat\lib
7.- Edit the files: setenv.bat and setenvClient.bat in C:\AwareIM\bin folder adding: %AWARE_HOME%\TranslatorEn.jar;
%AWARE_HOME%\TranslatorEn.jar;
at the end of the line
8.- Edit file BASServer.props and add this around line 10
FunctionLibraryTranslatorEN=javaapplication7.TranslatorEn
9.- Use your new code like this:
BO.TextFiled=TRANSLATEEN(AS_STRING(BO.NumberField))
Or
INVOICE.CurrencyText=TRANSLATEEN(AS_STRING(INVOICE.Total))
Note: Remember, you have to restart your AwareIM Server in order to get the function working.
DONT FORGET TO SHARE YOUR AwareIM stuff!!!
I want to Thank Bobk for his help and Support, and Robert (independet java developer).