I haven't contributed code before but thought I'd share what I did with
Interceptors. (sorry, no javadoc comments yet) (the web just begs for
strings)..if you have helpful feedback, feel free to comment. I made String
based calculators..and stuck them in this Interceptor which I then extend so
that I can use the calculator in other interceptors. A full blown workflow
or rules engine might come next (we'll see). I'm still learning a lot about
java web based development. TIA for any helpful feedback.
I was hoping that someday, I could make calculator attributes for the
dbforms. I am also curious how to get started contributing... I have about 4
applications that I've done with dbforms now. I'm also hoping to hack the
Event classes to do EJBs.
<<<<<FILE: BasicTestInterceptor.java>>>>>>>>>>>>>>
package com.yourorg.calc;
import javax.servlet.http.*;
import javax.servlet.*;
import org.dbforms.event.*;
import org.dbforms.*;
import java.util.*;
import java.text.*;
import java.sql.*;
import java.math.*;
public class BasicTestInterceptor extends DbEventInterceptorSupport
{
public static String round(String num,int digits)
{
double dig=Math.pow(10,digits);
BigDecimal bd = new BigDecimal(num);
bd=bd.divide(new BigDecimal(dig),0,BigDecimal.ROUND_HALF_UP);
bd=bd.multiply(new BigDecimal(dig));
return (""+bd);
}
//Arithmetic Rational number calculator
public String calcDiff(String a, String b)
{
BigDecimal ba = new BigDecimal(a);
BigDecimal bb = new BigDecimal(b);
ba.setScale(1,BigDecimal.ROUND_HALF_UP);
bb.setScale(1,BigDecimal.ROUND_HALF_UP);
return ba.subtract(bb).toString();
}//calcDiff(String,String)
public String calcAdd(String a, String b)
{
BigDecimal ba = new BigDecimal(a);
BigDecimal bb = new BigDecimal(b);
ba.setScale(1,BigDecimal.ROUND_HALF_UP);
bb.setScale(1,BigDecimal.ROUND_HALF_UP);
return ba.add(bb).toString();
}
public String calcProd(String a, String b)
{
BigDecimal ba = new BigDecimal(a);
BigDecimal bb = new BigDecimal(b);
ba.setScale(1,BigDecimal.ROUND_HALF_UP);
bb.setScale(1,BigDecimal.ROUND_HALF_UP);
return ba.multiply(bb).toString();
}
public String calcDiv(String a, String b)
throws ValidationException
{
try
{
BigDecimal ba = new BigDecimal(a);
BigDecimal bb = new BigDecimal(b);
if(bb.doubleValue()==0) return "0.0";
ba.setScale(1,BigDecimal.ROUND_HALF_UP);
bb.setScale(1,BigDecimal.ROUND_HALF_UP);
return new String( "" +
(ba.divide(bb,1,BigDecimal.ROUND_HALF_UP).doubleValue()) );
}
catch(ArithmeticException ae)
{
throw new ValidationException("You probably left something blank or
divided by zero on the percentage");
}
catch(Exception e)
{
throw new ValidationException("Something else went wrong with the
calculation, check your input");
}
}//calcPct(String,String)
public String calcPct(String a, String b)
throws ValidationException
{
try
{
BigDecimal ba = new BigDecimal(a);
BigDecimal bb = new BigDecimal(b);
if(bb.doubleValue()==0) return "0.0";
ba.setScale(3,BigDecimal.ROUND_HALF_UP);
bb.setScale(3,BigDecimal.ROUND_HALF_UP);
return new String( "" +
(ba.divide(bb,3,BigDecimal.ROUND_HALF_UP).multiply(new
BigDecimal(100.0)).doubleValue()) );
}
catch(ArithmeticException ae)
{
throw new ValidationException("You probably left something blank or
divided by zero on the percentage");
}
catch(Exception e)
{
throw new ValidationException("Something else went wrong with the
calculation, check your input");
}
}//calcPct(String,String)
public String calcAve(Vector v)
throws ValidationException
{
try
{
BigDecimal sum = new BigDecimal(0);
int size = v.size();
int numpoints = size;
for(int i=0;i<size;i++)
{
double d = Double.parseDouble( (String)v.elementAt(i) );
//System.out.println("averaging"+d);
if(d==0)
numpoints--;
BigDecimal u = new BigDecimal( d );
sum = sum.add(u);
}
if(numpoints==0)
return "0.0";
String rv = new String(""+sum.divide(new BigDecimal( new
Double(numpoints+"").doubleValue() ),1,BigDecimal.ROUND_HALF_UP));
//System.out.println("Average:"+rv);
return rv;
}
catch(ArithmeticException ae)
{
throw new ValidationException("You probably left something blank or
divided by zero during the average operation. Please enter data before
updating values.");
}
catch(Exception e)
{
throw new ValidationException("Something else went wrong with the
calculation, check your input");
}
}//calcAve(Vector)
//////////////////////////////////DYNAMIC PRECISION
VERSION//////////////////////////////////////////
//Arithmetic Rational number calculator
public String calcDiff(String a, String b, String prec)
{
BigDecimal ba = new BigDecimal(a);
BigDecimal bb = new BigDecimal(b);
ba.setScale(Integer.parseInt(prec),BigDecimal.ROUND_HALF_UP);
bb.setScale(Integer.parseInt(prec),BigDecimal.ROUND_HALF_UP);
return ba.subtract(bb).toString();
}//calcDiff(String,String)
public String calcAdd(String a, String b, String prec)
{
BigDecimal ba = new BigDecimal(a);
BigDecimal bb = new BigDecimal(b);
ba.setScale(Integer.parseInt(prec),BigDecimal.ROUND_HALF_UP);
bb.setScale(Integer.parseInt(prec),BigDecimal.ROUND_HALF_UP);
return ba.add(bb).toString();
}
public String calcProd(String a, String b, String prec)
{
BigDecimal ba = new BigDecimal(a);
BigDecimal bb = new BigDecimal(b);
ba.setScale(Integer.parseInt(prec),BigDecimal.ROUND_HALF_UP);
bb.setScale(Integer.parseInt(prec),BigDecimal.ROUND_HALF_UP);
return ba.multiply(bb).toString();
}
public String calcDiv(String a, String b, String prec)
throws ValidationException
{
try
{
BigDecimal ba = new BigDecimal(a);
BigDecimal bb = new BigDecimal(b);
if(bb.doubleValue()==0) return "0.0";
ba.setScale(Integer.parseInt(prec),BigDecimal.ROUND_HALF_UP);
bb.setScale(Integer.parseInt(prec),BigDecimal.ROUND_HALF_UP);
return new String( "" +
(ba.divide(bb,Integer.parseInt(prec),BigDecimal.ROUND_HALF_UP).doubleValue()
) );
}
catch(ArithmeticException ae)
{
throw new ValidationException("You probably left something blank or
divided by zero on the division of "+a+" over "+b+": Root
Cause:\n"+ae.getMessage());
}
catch(Exception e)
{
throw new ValidationException("Something else went wrong with the
calculation, check your input");
}
}//calcPct(String,String)
public String calcPct(String a, String b, String prec)
throws ValidationException
{
try
{
BigDecimal ba = new BigDecimal(a);
BigDecimal bb = new BigDecimal(b);
if(bb.doubleValue()==0) return "0.0";
//ba.setScale(3,BigDecimal.ROUND_HALF_UP);
//bb.setScale(3,BigDecimal.ROUND_HALF_UP);
return (ba.divide(bb,10,BigDecimal.ROUND_HALF_UP).multiply(new
BigDecimal(100.0)).setScale(Integer.parseInt(prec),BigDecimal.ROUND_HALF_UP
).toString());
}
catch(ArithmeticException ae)
{
throw new ValidationException("You probably left something blank or
divided by zero on the percentage in calculating percentage of 100x"+a+"
over "+b);
}
catch(Exception e)
{
throw new ValidationException("Something else went wrong with the
calculation, check your input");
}
}//calcPct(String,String)
public String calcAve(Vector v, String prec)
throws ValidationException
{
try
{
String rv = "0.0";
BigDecimal sum = new BigDecimal(0);
int size = v.size();
int numpoints = size;
for(int i=0;i<size;i++)
{
double d = Double.parseDouble( (String)v.elementAt(i) );
//System.out.println("averaging"+d);
if(d==0)
numpoints--;
BigDecimal u = new BigDecimal( d );
sum = sum.add(u);
}
if(numpoints==0)
{
//System.out.println("WARNING:Basic Interceptor:
calculation of average
attempted on an empty set. 0.0 returned.");
return rv;
}
try
{
rv = new String(""+sum.divide(new BigDecimal( new
Double(numpoints+"").doubleValue() ),Integer.parseInt(prec),BigDecimal.ROUND
_HALF_UP));
}
catch(Exception e)
{
throw new ValidationException("You are averaging these
numbers: "+v+"
which sum is divided by "+numpoints);
}
//System.out.println("Average:"+rv);
return rv;
}
catch(ArithmeticException ae)
{
throw new ValidationException("You probably left something blank or
divided by zero during the average operation. Please enter data before
updating values.");
}
catch(Exception e)
{
throw new ValidationException("Something else went wrong with the
calculation, check your input");
}
}//calcAve(Vector)
//////////////rounding up//////////////////////////
//Arithmetic Rational number calculator
public String calcDiff(String a, String b, int prec)
{
BigDecimal ba = new BigDecimal(a);
BigDecimal bb = new BigDecimal(b);
ba.setScale(0,BigDecimal.ROUND_HALF_UP);
bb.setScale(0,BigDecimal.ROUND_HALF_UP);
return ba.subtract(bb).toString();
}//calcDiff(String,String)
public String calcAdd(String a, String b, int prec)
{
String rv = "0";
BigDecimal ba = new BigDecimal(a);
BigDecimal bb = new BigDecimal(b);
ba.setScale(0,BigDecimal.ROUND_HALF_UP);
bb.setScale(0,BigDecimal.ROUND_HALF_UP);
rv = ba.add(bb).toString();
rv = round(rv,prec);
return rv;
}
public String calcProd(String a, String b, int prec)
{
String rv = "0";
BigDecimal ba = new BigDecimal(a);
BigDecimal bb = new BigDecimal(b);
ba.setScale(0,BigDecimal.ROUND_HALF_UP);
bb.setScale(0,BigDecimal.ROUND_HALF_UP);
rv = ba.multiply(bb).toString();
rv = round(rv,prec);
return rv;
}
public String calcDiv(String a, String b, int prec)
throws ValidationException
{
String rv = "0";
try
{
BigDecimal ba = new BigDecimal(a);
BigDecimal bb = new BigDecimal(b);
if(bb.doubleValue()==0) return "0.0";
ba.setScale(0,BigDecimal.ROUND_HALF_UP);
bb.setScale(0,BigDecimal.ROUND_HALF_UP);
rv = new String( "" +
(ba.divide(bb,0,BigDecimal.ROUND_HALF_UP).doubleValue()) );
rv = round(rv,prec);
}
catch(ArithmeticException ae)
{
throw new ValidationException("You probably left something blank or
divided by zero on the division of "+a+" over "+b+": Root
Cause:\n"+ae.getMessage());
}
catch(Exception e)
{
throw new ValidationException("Something else went wrong with the
calculation, check your input");
}
return rv;
}//calcPct(String,String)
public String calcPct(String a, String b, int prec)
throws ValidationException
{
String rv = "0";
try
{
BigDecimal ba = new BigDecimal(a);
BigDecimal bb = new BigDecimal(b);
if(bb.doubleValue()==0) return "0.0";
ba.setScale(0,BigDecimal.ROUND_HALF_UP);
bb.setScale(0,BigDecimal.ROUND_HALF_UP);
rv = new String( "" +
(ba.divide(bb,0,BigDecimal.ROUND_HALF_UP).multiply(new
BigDecimal(100.0)).doubleValue()) );
rv = round(rv,prec);
}
catch(ArithmeticException ae)
{
throw new ValidationException("You probably left something blank or
divided by zero on the percentage in calculating percentage of 100x"+a+"
over "+b);
}
catch(Exception e)
{
throw new ValidationException("Something else went wrong with the
calculation, check your input");
}
return rv;
}//calcPct(String,String)
public String calcAve(Vector v, int prec)
throws ValidationException
{
String rv = "0";
try
{
BigDecimal sum = new BigDecimal(0);
int size = v.size();
int numpoints = size;
for(int i=0;i<size;i++)
{
double d = Double.parseDouble( (String)v.elementAt(i) );
//System.out.println("averaging"+d);
if(d==0)
numpoints--;
BigDecimal u = new BigDecimal( d );
sum = sum.add(u);
}
if(numpoints==0)
{
//System.out.println("WARNING:Basic Interceptor:
calculation of average
attempted on an empty set. 0.0 returned.");
return rv;
}
try
{
rv = new String(""+sum.divide(new BigDecimal( new
Double(numpoints+"").doubleValue() ),0,BigDecimal.ROUND_HALF_UP));
rv = round(rv,prec);
}
catch(Exception e)
{
throw new ValidationException("You are averaging these
numbers: "+v+"
which sum is divided by "+numpoints);
}
//System.out.println("Average:"+rv);
}
catch(ArithmeticException ae)
{
throw new ValidationException("You probably left something blank or
divided by zero during the average operation. Please enter data before
updating values.");
}
catch(Exception e)
{
throw new ValidationException("Something else went wrong with the
calculation, check your input");
}
return rv;
}//calcAve(Vector)
//Default all updates to do what an insert would do.
public int preUpdate(HttpServletRequest request, Hashtable fieldValues,
DbFormsConfig config, Connection conn)
throws ValidationException
{
return preInsert(request,fieldValues,config,conn);
}//preUpdate(..)
public static void main(String[] args) throws Exception
{
}//_main(..)
}//BasicTestInterceptor
_______________________________________________________________
Don't miss the 2002 Sprint PCS Application Developer's Conference
August 25-28 in Las Vegas -- http://devcon.sprintpcs.com/adp/index.cfm
_______________________________________________
DbForms Mailing List
http://www.wap-force.net/dbforms