velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Tom Bednarz" <l...@bednarz.ch>
Subject [VelStruts] FieldMethodizer() question
Date Mon, 11 Mar 2002 16:11:10 GMT
Hi,

I have the following java class which I need to access from velocity:

public class NTUserAccountTrxStatistics extends Object implements Cloneable
{
    // TRX time
    public final static int TODAY       = 0;
    public final static int YESTERDAY   = 1;
    public final static int PAST7DAYS   = 2;
    public final static int PAST30DAYS  = 3;
    public final static int PAST360DAYS = 4;

    // TRX types
    public final static int CREATES     = 0;
    public final static int MODIFIES    = 1;
    public final static int DELETES     = 2;

    // TRX states
    public final static int ALL         = 0;
    public final static int SUCCESSFUL  = 3;
    public final static int PENDING     = 6;
    public final static int FAILED      = 9;
    public final static int UNCONFIRMED = 12;


    private final static int rows = 15;
    private final static int columns = 5;
    public int[][] mData = new int[rows][columns]; //table with 15 rows and
5 columns

    public NTUserAccountTrxStatistics()
    {
        for (int i = 0; i < rows; i++)
        {
            for (int y = 0; y < columns; y++)
            {
                mData[i][y] = 0;
            }
        }
    }

    public int getValue(int time, int type, int state)
    {
        int value = 0;
        synchronized(this)
        {
            value = mData[type+state][time];
        }
        return value;
    }

    public void setValue(int time, int state, int[] ValueArray)
    {
        int len = ValueArray.length;
        synchronized(this)
        {
            for (int i = 0; i < len; i++)
            {
                mData[state+i][time] = ValueArray[i];
            }
        }
    }

    public Object clone()
    {
        NTUserAccountTrxStatistics s = null;
        synchronized(this)
        {
            s = new NTUserAccountTrxStatistics();
            for (int i = 0; i < rows; i++)
            {
                for (int y = 0; y < columns; y++)
                {
                    s.mData[i][y] = mData[i][y];
                }
            }
        }
        return s;
    }

}


This class defines some constants which need to be passed to the
getValue(int, int, int) method.

In my action class (Struts) I do:

....
        NTUserAccountTrxStatistics stat = ei.getNTUsrAcctStatistics();
        request.setAttribute("TrxStatistics", stat);
        request.setAttribute("TrxStatConst", new FieldMethodizer(stat));
....


And in my Velocity template I access cache table with

....
<TR>
    <TD CLASS=datalightAlignLeft>Account Creations</TD>
    <TD CLASS=datalight>$TrxStatistics.getValue($TrxStatConst.TODAY,
$TrxStatConst.CREATES, $TrxStatConst.ALL)</TD>
    <TD CLASS=datalight>$TrxStatistics.getValue($TrxStatConst.YESTERDAY,
$TrxStatConst.CREATES, $TrxStatConst.ALL)</TD>
    <TD CLASS=datalight>$TrxStatistics.getValue($TrxStatConst.PAST7DAYS,
$TrxStatConst.CREATES, $TrxStatConst.ALL)</TD>
    <TD CLASS=datalight>$TrxStatistics.getValue($TrxStatConst.PAST30DAYS,
$TrxStatConst.CREATES, $TrxStatConst.ALL)</TD>
    <TD CLASS=datalight>$TrxStatistics.getValue($TrxStatConst.PAST360DAYS,
$TrxStatConst.CREATES, $TrxStatConst.ALL)</TD>
</TR>
....

What I DISLIKE:

I have to put two times the same object in the context: once the object
itself and once passed into the contstructor of FieldMethodizer().
Is there no way to avoid this unnecessary overhead?

The FieldMethodizer introspects the object and finds all public static
fields. Why this cannot be done automatically by velocity when introspecting
the public methods? Constants are different from non-constant public fields
(which should not exist in proper OO design). But constants are made for a
better code readability (instead of lots of magic numbers) and easier code
maintenance. It is good programming style to use constants and therefore I
personally think they should be supported in Velocity directly (without the
FieldMethodizer() tool.)


Thomas


--
To unsubscribe, e-mail:   <mailto:velocity-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:velocity-user-help@jakarta.apache.org>


Mime
View raw message