Transient atribute only show one decimal in browse screen

Hi all, in an entity I created a “transient” read-only attribute that is of type Bigdecimal, is a field calculated on two other attributes of the same entity. The problem is that when I show it on a browse screen only shows me a decimal, how could I configure it to show two?
Greetings and thanks.

Hi,
As I understood, you need to calculate percents by values of “resultado” and “ventas” in the same row. This might be done with a custom aggregation class.
I have shared the project on GitHub. The approach is a little bit hacky but it works. And you can do something similar to it.

  1. The problem is that we could not get access to current aggregated values of “resultado” and “ventas” from the porcentajeSventas column. So I created a new not persistent entity “AccuEntry” and put “resultado”, “ventas” and “porcentajeSventas” to it.
    As “porcentajeSventas” is added to namePattern UI will display it.
@NamePattern("%s|percentajeSventas")
@MetaClass(name = "acumulados$AccuEntry")
public class AccuEntry extends AbstractNotPersistentEntity {
    private static final long serialVersionUID = 496066028217252883L;

    public AccuEntry(BigDecimal resultado, BigDecimal ventas){
        this.resultado = resultado;
        this.ventas = ventas;

    }

    @MetaProperty
    protected BigDecimal resultado;

    @MetaProperty
    protected BigDecimal ventas;

    @MetaProperty
    protected BigDecimal percentajeSventas;
...
    public void setPercentajeSventas(BigDecimal percentajeSventas) {
        this.percentajeSventas = percentajeSventas;
    }

    public BigDecimal getPercentajeSventas(){
        return getResultado().divide(getVentas(), 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100));
    }

  1. In the “Accumulados” entity a new field was added.
    @MetaProperty
    public AccuEntry getAccuEntry() {
        return new AccuEntry(getResultado(), getVentas());
    }
  1. Then the groupTable was adjusted to display the field and a custom aggregator specified for the column.
 <column id="accuEntry">
                    <aggregation                        strategyClass="com.company.acumulados.web.acumulados.MyAggregator"/>
                </column>
  1. In the custom aggreationStrategy class the following logic was implemented (override the “aggregate” method).
public class MyAggregator implements AggregationStrategy{

    @Override
    public Object aggregate(Collection propertyValues) {
        String str = "Cannot calculate";     
        try {
            Object o = propertyValues.toArray()&#91;0&#93;;
                AccuEntry ae = (AccuEntry) o;
            str =  ae.getResultado().divide(ae.getVentas(), 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100)).toString();           
        }
        catch (Exception e){}
        return str;
    }

    @Override
    public Class getResultClass() {
        return String.class;
    }
}

See also Timesheets sample. The “TimeEntry” entity was created there for similar purposes.

Thanks Rostislav, sorry but I was away for a few days and I could not answer. I tried your project but I do not get it to work, I tried to replicate your tips in mine but I have the following problem:
The percentage calculation is correct in the last grouping (Level4) but not in the upper groups (Level1, Level2, Level3). At these levels it does not summarize the accumulations of the lower levels. Could you help me ?, I attach the project and an explanatory image.
Thanks for everything and greetings.

Acumulados.zip (32.1K)

percentages

Hi,

Could you provide a test project demonstrating the issue?

Hello Konstantin, I attach a project in which I reflect the doubts I have:
1.- Adding the Spanish language when I have a field Transient (type BigDecimal) that makes a division between two fields BigDecimal does not show me any decimal, if I leave the English language if it behaves correctly. The Transient field is “porcentajeSventas”.
2.- I have two Transient (BigDecimal) fields “resultado” and “porcentajeSventas” that are calculated on two other BigDecimal fields “compras” and “ventas”, the partial accumulations per group are correct in the fields “compras” and “ventas” But in the fields “resultado” and “porcentajeSventas” does not show me any information. How would you calculate these fields accumulated by each group?.
Attach project and image.
Greetings and thanks.

Acumulados.zip (30.7K)

Image1

image2

Sorry but forget one more thing, when exporting to excel, does not export any partial accumulation per group, only exports the total accumulated.
Greetings.

Hi,
Thank you for your sample project. I have looked at it.
1, It seems that everything works well. On my machine, the correct value (with the decimal part) is displayed not depending on the language selection. It seems that the “scale” option was incorrect at the moment you have observed the issue. Entity classes are located in the global module and are not hot-deployed. So the application restart is required for changes to take effect. Try to redeploy the application - this could solve the problem.
2. Partial sums are not presented for the generated columns by the reason aggregation is not adjusted for them.
Add the property and partial sums should appear.



 <groupTable id="acumuladosesTable"
                    aggregatable="true"
                    multiselect="true"
                    responsive="true"
                    width="100%">
           ...
            <columns>
                ...
                <column id="compras">
                    <aggregation type="SUM"/>
                </column>
                <column id="ventas">
                    <aggregation type="SUM"/>
                </column>

                <column id="resultado" >
                    <aggregation type="SUM"/>
                </column>

                <column id="porcentajeSventas"/>
            </columns>
            <rows datasource="acumuladosPruebaDs"/>
          ...
        </groupTable>

Regarding the Excel action, you are right. See the YouTrack issue linked to your topic.

Thanks Rostislav, the result of the tests is:
1.- You are indeed right and upon restarting the server the decimals were displayed correctly.
2.- Explain to me wrong with aggregation, my problem is in the column “porcentaje sventas” because this column has a calculation on another two columns to show a percentage “(resultado / ventas) * 100”, this calculation is shown row to row but I want that in the groups also is shown, to show it in the groups the calculation must be done on the summations of the rows that each group contains to know the percentage of the group and to show it in the column “porcentaje sventas” of the group, ( In the image that I have marked in red). How can I make this calculation in the field “porcentaje sventas” of the groups?
Greetings and thanks.

sumatorios

Hi,
I am sure you have already found the bug in the calculation logic.
It should be something like this:

@Override
    public Object aggregate(Collection propertyValues) {
        String str = "Sin Calculo";
        try {

            BigDecimal ventas = new BigDecimal(0);
            BigDecimal resultado = new BigDecimal(0);
            for (Object o : propertyValues){

                CalculoPorcentaje porcentage = (CalculoPorcentaje)o;

                ventas = ventas.add(porcentage.getVentas());
                resultado = resultado.add(porcentage.getResultado());

            }

            str =  resultado.divide(ventas, 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100)).setScale(2).toString();
        }
        catch (Exception e){}

        return str;
    }

Hi Rostislav, I did find the problem and I solved it, anyway thank you for answering.
Greetings.