Calculate amount from different entity

Hi,

I have an entity named as employee_project. The attributes as per below:
image

I have another entity named as Employee and the attributes as per below:
image

I would like to calculate the amount of Allocation_cost (from employee_project entity) where the calculation should be:

Allocation_cost= Allocation_percentage(from employee_project entity) x CTC (from employee entity)

This is my code:

package com.company.bizopv7.web.employee_project;

import com.company.bizopv7.entity.Employee;
import com.company.bizopv7.entity.Employee_project;
import com.haulmont.chile.core.datatypes.impl.BigDecimalDatatype;
import com.haulmont.cuba.gui.components.AbstractLookup;
import com.haulmont.cuba.gui.data.CollectionDatasource;
import com.haulmont.cuba.gui.data.GroupDatasource;

import javax.inject.Inject;
import java.math.BigDecimal;
import java.util.Map;
import java.util.UUID;

public class Employee_projectBrowse extends AbstractLookup {


    @Inject
    private CollectionDatasource<Employee_project, UUID> employee_projectsDs;

    @Inject
    private CollectionDatasource<Employee, UUID>employeesDs;

    @Override
    public void init(Map<String, Object> params) {
        employee_projectsDs.addCollectionChangeListener(e ->calculateAmount());
    }

    private void calculateAmount() {
        BigDecimal amount = BigDecimal.ZERO;

        for(Employee_project:employee_projectsDs.getItems())
            for(Employee:employeesDs.getItems())

                amount=amount.multiply(employee_project.getAllocation_percentage().employee.getCtc());
        getItems().setAllocation_cost(amount);
    }
}

Can anyone help to solve the code? Because my code seems wrong.

Hello @ameera_aynie

You want to display calculated allocation cost in the corresponding column on Employee Project browse screen, am I right?

Hi Tsarev,

Yes, correct.

In this case you should use generated columns mechanism: documentation.

First of all set an id for a table that contains “Employee_Project” entity:

<table id="emloyeeProjectTable" ...>
  ...
</table>

Then inject the table in screen controller:

public class Employee_projectBrowse extends AbstractLookup {

    @Inject
    private Table<Employee_project> emloyeeProjectTable;

    // a bean that is used to create UI components that will be used later
    @Inject
    private ComponentsFactory componentsFactory;

    ...

}

And add generated column inside of init method:

@Override
public void init(Map<String, Object> params) {
    // first parameter should be equal to the corresponding column id
    emloyeeProjectTable.addGeneratedColumn("allocationCost", new Table.ColumnGenerator() {
        @Override
        public Component generateCell(Employee_project employeeProject) {
            BigDecimal employeeCtc = employeeProject.getEmployee().getCtc();
            BigDecimal allocationCost = employeeProject.getAllocation_percentage().multiply(employeeCtc);

            Label allocationCostLabel = componentsFactory.createComponent(Label.class);
            allocationCostLabel.setValue(String.valueOf(allocationCost));
            
            return allocationCostLabel;
        }
    });
}

I would recommend you to use camel case in your code:

  • instead of Employee_project use EmployeeProject
  • instead of getAllocation_percentage() use getAllocationPercentage()
  • instead of employee_projectsDs use employeeProjectsDs

Regards,
Daniil.

Hi Daniil,

image

I have tried to follow your steps, but as you can see from the picture above, I still got some error and I wonder why that happen. Did you know why the code still appear in red?

Thanks in advance.

Helllo @ameera_aynie

Check that you have all required imports (Component, BigDecimal, Label, etc) and entity getter (getEmployee) has the same name as it used in the column generator.

Hi Daniil,

image

I have already import bigdecimal but it appears grey in colour which means it is not been used. Can you help?

Thank you!

It looks like your project is not correctly imported into Intellij IDEA.

Try the following steps:

  1. Close the project
  2. In project directory execute in console: gradlew cleanIdea cleanIW to remove all IDEA project files
  3. Execute gradlew idea to generate new project files
  4. Open *.ipr file located in root project directory

Hi Daniil,

Where can I find the project directory console?

Thank you.

I apologize, for some reasons I forgot that the same thing can be easily done with CUBA Studio. Use the following actions to re-create IDEA project files:

%D0%B8%D0%B7%D0%BE%D0%B1%D1%80%D0%B0%D0%B6%D0%B5%D0%BD%D0%B8%D0%B5