implements an empty method called "beforeSaving" in the GenericRESTService
and overrides it in the ExpenseSheetServiceRest in order to save the sum of expenses. FEA: ItEr76S22ExpenseTrackingSystem
This commit is contained in:
parent
99d7a69885
commit
9080e7ecb4
2 changed files with 13 additions and 52 deletions
|
|
@ -141,6 +141,7 @@ public abstract class GenericRESTService<E extends IntegrationEntity,
|
||||||
* Validate and save (insert or update) the entity.
|
* Validate and save (insert or update) the entity.
|
||||||
*/
|
*/
|
||||||
entity.validate();
|
entity.validate();
|
||||||
|
beforeSaving(entity);
|
||||||
entityDAO.saveWithoutValidating(entity);
|
entityDAO.saveWithoutValidating(entity);
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
|
|
@ -153,6 +154,13 @@ public abstract class GenericRESTService<E extends IntegrationEntity,
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* it adds operations that must be done before saving.
|
||||||
|
*/
|
||||||
|
protected void beforeSaving(E entity) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* It creates an entity from a DTO.
|
* It creates an entity from a DTO.
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -27,9 +27,7 @@ import javax.ws.rs.PathParam;
|
||||||
import javax.ws.rs.Produces;
|
import javax.ws.rs.Produces;
|
||||||
import javax.ws.rs.core.Response;
|
import javax.ws.rs.core.Response;
|
||||||
|
|
||||||
import org.libreplan.business.common.IOnTransaction;
|
|
||||||
import org.libreplan.business.common.daos.IIntegrationEntityDAO;
|
import org.libreplan.business.common.daos.IIntegrationEntityDAO;
|
||||||
import org.libreplan.business.common.exceptions.InstanceNotFoundException;
|
|
||||||
import org.libreplan.business.common.exceptions.ValidationException;
|
import org.libreplan.business.common.exceptions.ValidationException;
|
||||||
import org.libreplan.business.expensesheet.daos.IExpenseSheetDAO;
|
import org.libreplan.business.expensesheet.daos.IExpenseSheetDAO;
|
||||||
import org.libreplan.business.expensesheet.entities.ExpenseSheet;
|
import org.libreplan.business.expensesheet.entities.ExpenseSheet;
|
||||||
|
|
@ -62,56 +60,11 @@ public class ExpenseSheetServiceREST extends
|
||||||
@Autowired
|
@Autowired
|
||||||
private IExpenseSheetDAO expenseSheetDAO;
|
private IExpenseSheetDAO expenseSheetDAO;
|
||||||
|
|
||||||
/**
|
@Override
|
||||||
* It saves (inserts or updates) an entity DTO by using a new transaction.
|
protected void beforeSaving(ExpenseSheet entity) {
|
||||||
*
|
sumExpensesDAO.updateRelatedSumExpensesWithExpenseSheetLineSet(entity
|
||||||
* @throws ValidationException if validations are not passed
|
.getExpenseSheetLines());
|
||||||
* @throws RecoverableErrorException if a recoverable error occurs
|
entity.updateCalculatedProperties();
|
||||||
* @author Susana Montes Pedreira <smontes@wirelessgalicia.com>
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
protected void insertOrUpdate(final ExpenseSheetDTO entityDTO)
|
|
||||||
throws ValidationException, RecoverableErrorException {
|
|
||||||
/*
|
|
||||||
* NOTE: ValidationException and RecoverableErrorException are runtime
|
|
||||||
* exceptions. In consequence, if any of them occurs, transaction is
|
|
||||||
* automatically rolled back.
|
|
||||||
*/
|
|
||||||
|
|
||||||
IOnTransaction<Void> save = new IOnTransaction<Void>() {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Void execute() {
|
|
||||||
|
|
||||||
ExpenseSheet entity = null;
|
|
||||||
IIntegrationEntityDAO<ExpenseSheet> entityDAO = getIntegrationEntityDAO();
|
|
||||||
|
|
||||||
/* Insert or update? */
|
|
||||||
try {
|
|
||||||
entity = entityDAO.findByCode(entityDTO.code);
|
|
||||||
updateEntity(entity, entityDTO);
|
|
||||||
} catch (InstanceNotFoundException e) {
|
|
||||||
entity = toEntity(entityDTO);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Validate and save (insert or update) the entity.
|
|
||||||
*/
|
|
||||||
entity.validate();
|
|
||||||
sumExpensesDAO
|
|
||||||
.updateRelatedSumExpensesWithExpenseSheetLineSet(entity
|
|
||||||
.getExpenseSheetLines());
|
|
||||||
entity.updateCalculatedProperties();
|
|
||||||
|
|
||||||
entityDAO.saveWithoutValidating(entity);
|
|
||||||
|
|
||||||
return null;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
transactionService.runOnAnotherTransaction(save);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue