Refactoring ExpenseSheetCRUDController to extend BaseCRUDController
FEA: ItEr76S22ExpenseTrackingSystem
This commit is contained in:
parent
6e2e51652d
commit
c0dc6dede3
5 changed files with 84 additions and 166 deletions
|
|
@ -30,6 +30,7 @@ import org.hibernate.validator.NotEmpty;
|
|||
import org.hibernate.validator.NotNull;
|
||||
import org.hibernate.validator.Valid;
|
||||
import org.joda.time.LocalDate;
|
||||
import org.libreplan.business.common.IHumanIdentifiable;
|
||||
import org.libreplan.business.common.IntegrationEntity;
|
||||
import org.libreplan.business.common.Registry;
|
||||
import org.libreplan.business.common.entities.EntitySequence;
|
||||
|
|
@ -40,7 +41,7 @@ import org.libreplan.business.expensesheet.daos.IExpenseSheetDAO;
|
|||
*
|
||||
* @author Susana Montes Pedreira <smontes@wirelessgalicia.com>
|
||||
*/
|
||||
public class ExpenseSheet extends IntegrationEntity {
|
||||
public class ExpenseSheet extends IntegrationEntity implements IHumanIdentifiable {
|
||||
|
||||
private LocalDate firstExpense;
|
||||
|
||||
|
|
@ -62,7 +63,7 @@ public class ExpenseSheet extends IntegrationEntity {
|
|||
protected ExpenseSheet() {
|
||||
}
|
||||
|
||||
protected ExpenseSheet(LocalDate firstExpense, LocalDate lastExpense, BigDecimal total) {
|
||||
private ExpenseSheet(LocalDate firstExpense, LocalDate lastExpense, BigDecimal total) {
|
||||
this.setFirstExpense(firstExpense);
|
||||
this.setLastExpense(lastExpense);
|
||||
this.setTotal(total);
|
||||
|
|
@ -192,4 +193,9 @@ public class ExpenseSheet extends IntegrationEntity {
|
|||
this.updateCalculatedProperties();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHumanId() {
|
||||
return getCode() + (description != null ? description : "");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -29,16 +29,15 @@ import java.util.SortedSet;
|
|||
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.joda.time.LocalDate;
|
||||
import org.libreplan.business.common.exceptions.InstanceNotFoundException;
|
||||
import org.libreplan.business.common.exceptions.ValidationException;
|
||||
import org.libreplan.business.expensesheet.entities.ExpenseSheet;
|
||||
import org.libreplan.business.expensesheet.entities.ExpenseSheetLine;
|
||||
import org.libreplan.business.orders.entities.Order;
|
||||
import org.libreplan.business.orders.entities.OrderElement;
|
||||
import org.libreplan.business.resources.entities.Resource;
|
||||
import org.libreplan.web.common.ConstraintChecker;
|
||||
import org.libreplan.web.common.IMessagesForUser;
|
||||
import org.libreplan.web.common.BaseCRUDController;
|
||||
import org.libreplan.web.common.Level;
|
||||
import org.libreplan.web.common.MessagesForUser;
|
||||
import org.libreplan.web.common.OnlyOneVisible;
|
||||
import org.libreplan.web.common.Util;
|
||||
import org.libreplan.web.common.components.bandboxsearch.BandboxSearch;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
|
@ -48,9 +47,7 @@ import org.zkoss.zk.ui.event.CheckEvent;
|
|||
import org.zkoss.zk.ui.event.Event;
|
||||
import org.zkoss.zk.ui.event.EventListener;
|
||||
import org.zkoss.zk.ui.event.Events;
|
||||
import org.zkoss.zk.ui.util.GenericForwardComposer;
|
||||
import org.zkoss.zul.Button;
|
||||
import org.zkoss.zul.Checkbox;
|
||||
import org.zkoss.zul.Constraint;
|
||||
import org.zkoss.zul.Datebox;
|
||||
import org.zkoss.zul.Decimalbox;
|
||||
|
|
@ -60,31 +57,20 @@ import org.zkoss.zul.Messagebox;
|
|||
import org.zkoss.zul.Row;
|
||||
import org.zkoss.zul.RowRenderer;
|
||||
import org.zkoss.zul.Textbox;
|
||||
import org.zkoss.zul.api.Window;
|
||||
|
||||
/**
|
||||
* Controller for CRUD actions over a {@link ExpenseSheet}
|
||||
*
|
||||
* @author Susana Montes Pedreira <smontes@wirelessgalicia.com>
|
||||
*/
|
||||
public class ExpenseSheetCRUDController extends GenericForwardComposer {
|
||||
public class ExpenseSheetCRUDController extends BaseCRUDController<ExpenseSheet> {
|
||||
|
||||
private static final org.apache.commons.logging.Log LOG = LogFactory
|
||||
.getLog(ExpenseSheetCRUDController.class);
|
||||
|
||||
private Window editWindow;
|
||||
|
||||
private Window listWindow;
|
||||
|
||||
@Autowired
|
||||
private IExpenseSheetModel expenseSheetModel;
|
||||
|
||||
private OnlyOneVisible visibility;
|
||||
|
||||
private IMessagesForUser messagesForUser;
|
||||
|
||||
private Component messagesContainer;
|
||||
|
||||
private static final String ITEM = "item";
|
||||
|
||||
/*
|
||||
|
|
@ -97,10 +83,6 @@ public class ExpenseSheetCRUDController extends GenericForwardComposer {
|
|||
* components editWindow
|
||||
*/
|
||||
|
||||
private Textbox txtExpenseCode;
|
||||
|
||||
private Checkbox generateCode;
|
||||
|
||||
private BandboxSearch bandboxSelectOrder;
|
||||
|
||||
private Datebox dateboxExpenseDate;
|
||||
|
|
@ -143,87 +125,26 @@ public class ExpenseSheetCRUDController extends GenericForwardComposer {
|
|||
};
|
||||
|
||||
@Override
|
||||
public void doAfterCompose(Component comp) throws Exception {
|
||||
super.doAfterCompose(comp);
|
||||
messagesForUser = new MessagesForUser(messagesContainer);
|
||||
comp.setVariable("controller", this, true);
|
||||
goToList();
|
||||
public void save() throws ValidationException {
|
||||
expenseSheetModel.confirmSave();
|
||||
}
|
||||
|
||||
private OnlyOneVisible getVisibility() {
|
||||
return (visibility == null) ? new OnlyOneVisible(editWindow, listWindow) : visibility;
|
||||
}
|
||||
|
||||
public void saveAndExit() {
|
||||
if (save()) {
|
||||
goToList();
|
||||
}
|
||||
}
|
||||
|
||||
public void saveAndContinue() {
|
||||
if (save()) {
|
||||
goToEditForm(getExpenseSheet());
|
||||
}
|
||||
}
|
||||
|
||||
public boolean save() {
|
||||
ConstraintChecker.isValid(editWindow);
|
||||
@Override
|
||||
protected void beforeSaving() throws ValidationException {
|
||||
super.beforeSaving();
|
||||
expenseSheetModel.generateExpenseSheetLineCodesIfIsNecessary();
|
||||
|
||||
if (this.getExpenseSheet() != null
|
||||
&& (getExpenseSheet().getExpenseSheetLines() == null || getExpenseSheet()
|
||||
.getExpenseSheetLines().isEmpty())) {
|
||||
messagesForUser.showMessage(Level.ERROR,
|
||||
"The expense sheet line collection cannot be empty.");
|
||||
LOG.error(_("Error on saving element: ", getExpenseSheet().getId()));
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
expenseSheetModel.confirmSave();
|
||||
messagesForUser.showMessage(Level.INFO, _("Expense sheet saved"));
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void cancel() {
|
||||
goToList();
|
||||
}
|
||||
|
||||
public void goToList() {
|
||||
expenseSheetModel.prepareToList();
|
||||
goToListWindow();
|
||||
}
|
||||
|
||||
private void goToListWindow() {
|
||||
getVisibility().showOnly(listWindow);
|
||||
Util.reloadBindings(listWindow);
|
||||
loadComponentsListWindow();
|
||||
}
|
||||
|
||||
private void loadComponentsListWindow() {
|
||||
listing = (Grid) this.listWindow.getFellowIfAny("listing");
|
||||
}
|
||||
|
||||
public void goToCreateForm() {
|
||||
expenseSheetModel.initCreate();
|
||||
goToEditWindow();
|
||||
}
|
||||
|
||||
public void goToEditForm(ExpenseSheet expenseSheet) {
|
||||
expenseSheetModel.prepareToEdit(expenseSheet);
|
||||
goToEditWindow();
|
||||
}
|
||||
|
||||
private void goToEditWindow() {
|
||||
getVisibility().showOnly(editWindow);
|
||||
Util.reloadBindings(editWindow);
|
||||
showEditWindow();
|
||||
loadComponentsEditWindow();
|
||||
}
|
||||
|
||||
private void loadComponentsEditWindow() {
|
||||
private void loadComponentsEditWindow() {
|
||||
tbConcept = (Textbox) editWindow.getFellowIfAny("tbConcept");
|
||||
dateboxExpenseDate = (Datebox) editWindow.getFellowIfAny("dateboxExpenseDate");
|
||||
dboxValue = (Decimalbox) editWindow.getFellowIfAny("dboxValue");
|
||||
|
|
@ -258,44 +179,10 @@ public class ExpenseSheetCRUDController extends GenericForwardComposer {
|
|||
* Operations in the list window
|
||||
*/
|
||||
|
||||
public void onCreateNewExpenseSheet() {
|
||||
goToCreateForm();
|
||||
}
|
||||
|
||||
public List<ExpenseSheet> getExpenseSheets() {
|
||||
return expenseSheetModel.getExpenseSheets();
|
||||
}
|
||||
|
||||
public void confirmRemove(ExpenseSheet expenseSheet) {
|
||||
try {
|
||||
int status = Messagebox.show(
|
||||
_("Confirm deleting {0}. Are you sure?", getExpenseSheetName(expenseSheet)),
|
||||
_("Delete"), Messagebox.OK | Messagebox.CANCEL, Messagebox.QUESTION);
|
||||
if (Messagebox.OK == status) {
|
||||
removeExpenseSheet(expenseSheet);
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
messagesForUser.showMessage(Level.ERROR, e.getMessage());
|
||||
LOG.error(_("Error on showing removing element: ", expenseSheet.getId()), e);
|
||||
}
|
||||
}
|
||||
|
||||
private void removeExpenseSheet(ExpenseSheet expenseSheet) {
|
||||
expenseSheetModel.removeExpenseSheet(expenseSheet);
|
||||
reloadExpenseSheetList();
|
||||
}
|
||||
|
||||
private String getExpenseSheetName(ExpenseSheet expenseSheet) {
|
||||
if (expenseSheet != null) {
|
||||
String code = expenseSheet.getCode();
|
||||
String description = expenseSheet.getDescription();
|
||||
if (code != null && description != null) {
|
||||
return _("expense sheet ") + code + " - " + description;
|
||||
}
|
||||
}
|
||||
return ITEM;
|
||||
}
|
||||
|
||||
/*
|
||||
* Operations in the create window
|
||||
*/
|
||||
|
|
@ -308,10 +195,6 @@ public class ExpenseSheetCRUDController extends GenericForwardComposer {
|
|||
return expenseSheetModel.getExpenseSheetLines();
|
||||
}
|
||||
|
||||
private void reloadExpenseSheetList() {
|
||||
Util.reloadBindings(this.listing);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a new {@link ExpenseSheetLine} to the list of rows
|
||||
*
|
||||
|
|
@ -659,17 +542,14 @@ public class ExpenseSheetCRUDController extends GenericForwardComposer {
|
|||
public void validate(Component comp, Object value) throws WrongValueException {
|
||||
if (!getExpenseSheet().isCodeAutogenerated()) {
|
||||
String code = (String) value;
|
||||
if (code == null || code.isEmpty()){
|
||||
throw new WrongValueException(comp,
|
||||
_("The code cannot be empty."));
|
||||
}else{
|
||||
if (code == null || code.isEmpty()) {
|
||||
throw new WrongValueException(comp, _("The code cannot be empty."));
|
||||
} else {
|
||||
String oldCode = line.getCode();
|
||||
line.setCode(code);
|
||||
if(!getExpenseSheet()
|
||||
.checkConstraintNonRepeatedExpenseSheetLinesCodes()) {
|
||||
line.setCode(oldCode);
|
||||
throw new WrongValueException(comp,
|
||||
_("The code must be unique."));
|
||||
if (!getExpenseSheet().checkConstraintNonRepeatedExpenseSheetLinesCodes()) {
|
||||
line.setCode(oldCode);
|
||||
throw new WrongValueException(comp, _("The code must be unique."));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -706,8 +586,44 @@ public class ExpenseSheetCRUDController extends GenericForwardComposer {
|
|||
|
||||
public void setExpenseSheetLineDate(Date date) {
|
||||
if (expenseSheetModel.getExpenseSheetLineDTO() != null) {
|
||||
expenseSheetModel.getExpenseSheetLineDTO().setDate(LocalDate.fromDateFields(date));
|
||||
LocalDate localDate = null;
|
||||
if (date != null) {
|
||||
localDate = LocalDate.fromDateFields(date);
|
||||
}
|
||||
expenseSheetModel.getExpenseSheetLineDTO().setDate(localDate);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initCreate() {
|
||||
expenseSheetModel.initCreate();
|
||||
goToEditWindow();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initEdit(ExpenseSheet expenseSheet) {
|
||||
expenseSheetModel.prepareToEdit(expenseSheet);
|
||||
goToEditWindow();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ExpenseSheet getEntityBeingEdited() {
|
||||
return expenseSheetModel.getExpenseSheet();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void delete(ExpenseSheet expenseSheet) throws InstanceNotFoundException {
|
||||
expenseSheetModel.removeExpenseSheet(expenseSheet);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getEntityType() {
|
||||
return _("Expense sheet");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getPluralEntityType() {
|
||||
return _("Expense sheets");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -93,15 +93,17 @@ public class ExpenseSheetModel extends IntegrationEntityModel implements IExpens
|
|||
|
||||
@Override
|
||||
@Transactional
|
||||
public boolean confirmSave() {
|
||||
if (!hasExpenseSheetLines()) {
|
||||
return false;
|
||||
public void confirmSave() {
|
||||
updateCalculatedFields(getExpenseSheet());
|
||||
expenseSheetDAO.save(getExpenseSheet());
|
||||
dontPoseAsTransientAndChildrenObjects(getExpenseSheet());
|
||||
}
|
||||
|
||||
private void dontPoseAsTransientAndChildrenObjects(ExpenseSheet expenseSheet) {
|
||||
expenseSheet.dontPoseAsTransientObjectAnymore();
|
||||
for (ExpenseSheetLine expenseSheetLine : expenseSheet.getExpenseSheetLines()) {
|
||||
expenseSheetLine.dontPoseAsTransientObjectAnymore();
|
||||
}
|
||||
if(getExpenseSheet() != null){
|
||||
updateCalculatedFields(getExpenseSheet());
|
||||
expenseSheetDAO.save(getExpenseSheet());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -239,8 +241,7 @@ public class ExpenseSheetModel extends IntegrationEntityModel implements IExpens
|
|||
@Override
|
||||
public Set<IntegrationEntity> getChildren() {
|
||||
return (Set<IntegrationEntity>) (getExpenseSheet() != null ? getExpenseSheet()
|
||||
.getExpenseSheetLines()
|
||||
: new HashSet<IntegrationEntity>());
|
||||
.getExpenseSheetLines() : new HashSet<IntegrationEntity>());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -248,10 +249,6 @@ public class ExpenseSheetModel extends IntegrationEntityModel implements IExpens
|
|||
return this.expenseSheet;
|
||||
}
|
||||
|
||||
public boolean hasExpenseSheetLines() {
|
||||
return (!getExpenseSheet().getExpenseSheetLines().isEmpty());
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public List<Order> getOrders() {
|
||||
|
|
|
|||
|
|
@ -35,7 +35,7 @@ import org.libreplan.web.common.IIntegrationEntityModel;
|
|||
*/
|
||||
public interface IExpenseSheetModel extends IIntegrationEntityModel {
|
||||
|
||||
boolean confirmSave();
|
||||
void confirmSave();
|
||||
|
||||
void prepareToList();
|
||||
|
||||
|
|
|
|||
|
|
@ -33,7 +33,7 @@
|
|||
<hbox align="center">
|
||||
<!-- Create new Expense Sheet -->
|
||||
<button label="${i18n:_('New expense sheet')}"
|
||||
onClick="controller.onCreateNewExpenseSheet()"/>
|
||||
onClick="controller.goToCreateForm()"/>
|
||||
</hbox>
|
||||
</div>
|
||||
|
||||
|
|
@ -58,7 +58,7 @@
|
|||
sortDirection="ascending"
|
||||
width="260px"/>
|
||||
<column label="${i18n:_('Description')}" width="286px"/>
|
||||
<column label="${i18n:_('Actions')}" width="80px"/>
|
||||
<column label="${i18n:_('Actions')}" width="80px" align="center"/>
|
||||
</columns>
|
||||
<rows>
|
||||
<row self="@{each='expensesheet'}" value="@{expensesheet}" onClick="controller.goToEditForm(self.value);">
|
||||
|
|
@ -77,7 +77,7 @@
|
|||
<button sclass="icono" image="/common/img/ico_borrar1.png"
|
||||
hoverImage="/common/img/ico_borrar.png"
|
||||
tooltiptext="${i18n:_('Delete')}"
|
||||
onClick="controller.confirmRemove(self.parent.parent.value);">
|
||||
onClick="controller.confirmDelete(self.parent.parent.value);">
|
||||
</button>
|
||||
</hbox>
|
||||
</row>
|
||||
|
|
@ -86,6 +86,7 @@
|
|||
</window>
|
||||
|
||||
<window id="editWindow">
|
||||
<caption id="caption" sclass="caption-title" />
|
||||
<groupbox closable="false" width="600px">
|
||||
<caption label="${i18n:_('Identification')}" />
|
||||
<separator bar="false" spacing="5px" orient="horizontal"/>
|
||||
|
|
@ -122,8 +123,7 @@
|
|||
<hbox>
|
||||
<label value="${i18n:_('Expense date')}" />
|
||||
<datebox id="dateboxExpenseDate" width="150px"
|
||||
value="@{controller.expenseSheetLineDate}"
|
||||
constraint="no empty:${i18n:_('cannot be null or empty')}"/>
|
||||
value="@{controller.expenseSheetLineDate}"/>
|
||||
</hbox>
|
||||
</vbox>
|
||||
</groupbox>
|
||||
|
|
@ -166,16 +166,15 @@
|
|||
</hbox>
|
||||
<separator bar="false" spacing="10px" orient="horizontal"/>
|
||||
<grid id="gridExpenseLines" mold="paging" pageSize="10"
|
||||
model="@{controller.expenseSheetLines}"
|
||||
fixedLayout="true" width="100%"
|
||||
model="@{controller.expenseSheetLines}" width="100%"
|
||||
rowRenderer="@{controller.expenseSheetLineRenderer}">
|
||||
<columns>
|
||||
<column label="${i18n:_('Task')}" width="275px"/>
|
||||
<column label="${i18n:_('Value')}" width="100px"/>
|
||||
<column label="${i18n:_('Concept')}" width="176px"/>
|
||||
<column label="${i18n:_('Date')}" width="120px"/>
|
||||
<column label="${i18n:_('Concept')}" width="172px"/>
|
||||
<column label="${i18n:_('Date')}" width="118px"/>
|
||||
<column label="${i18n:_('Resource')}" width="275px"/>
|
||||
<column label="${i18n:_('Code')}" width="187px"/>
|
||||
<column label="${i18n:_('Code')}" width="182px"/>
|
||||
<column label="${i18n:_('Operation')}" align="center"/>
|
||||
</columns>
|
||||
</grid>
|
||||
|
|
@ -185,7 +184,7 @@
|
|||
label="${i18n:_('Save')}" sclass="save-button global-action"/>
|
||||
<button onClick="controller.saveAndContinue();"
|
||||
label="${i18n:_('Save & Continue')}" sclass="saveandcontinue-button global-action"/>
|
||||
<button onClick="controller.cancel();"
|
||||
<button onClick="controller.cancelForm();"
|
||||
label="${i18n:_('Cancel')}" sclass="cancel-button global-action"/>
|
||||
</window>
|
||||
</window>
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue