[Bug #988] Check calendar is not being referenced by other entities
FEA: ItEr74S04BugFixing
This commit is contained in:
parent
0a0a80b9a1
commit
6808910c07
5 changed files with 127 additions and 17 deletions
|
|
@ -29,8 +29,14 @@ import org.apache.commons.lang.StringUtils;
|
|||
import org.hibernate.Criteria;
|
||||
import org.hibernate.criterion.Restrictions;
|
||||
import org.navalplanner.business.calendars.entities.BaseCalendar;
|
||||
import org.navalplanner.business.calendars.entities.CalendarData;
|
||||
import org.navalplanner.business.calendars.entities.ResourceCalendar;
|
||||
import org.navalplanner.business.common.daos.IntegrationEntityDAO;
|
||||
import org.navalplanner.business.common.exceptions.ValidationException;
|
||||
import org.navalplanner.business.orders.entities.Order;
|
||||
import org.navalplanner.business.planner.entities.TaskElement;
|
||||
import org.navalplanner.business.resources.entities.Resource;
|
||||
import org.navalplanner.business.templates.entities.OrderTemplate;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
|
@ -42,6 +48,7 @@ import org.springframework.transaction.annotation.Transactional;
|
|||
*
|
||||
* @author Manuel Rego Casasnovas <mrego@igalia.com>
|
||||
* @author Fernando Bellas Permuy <fbellas@udc.es>
|
||||
* @author Diego Pino García <dpino@igalia.com>
|
||||
*/
|
||||
@Repository
|
||||
@Scope(BeanDefinition.SCOPE_SINGLETON)
|
||||
|
|
@ -124,4 +131,62 @@ public class BaseCalendarDAO extends IntegrationEntityDAO<BaseCalendar>
|
|||
return !one.getId().equals(other.getId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void checkIsReferencedByOtherEntities(BaseCalendar calendar) {
|
||||
checkHasResources(calendar);
|
||||
checkHasOrders(calendar);
|
||||
checkHasTasks(calendar);
|
||||
checkHasTemplates(calendar);
|
||||
}
|
||||
|
||||
/**
|
||||
* A {@link BaseCalendar} is being used by a {@link Resource} if there is
|
||||
* {@link CalendarData} that has as a parent the parameter calendar
|
||||
*
|
||||
* @param calendar
|
||||
*/
|
||||
private void checkHasResources(BaseCalendar calendar) {
|
||||
List calendarData = getSession().createCriteria(CalendarData.class)
|
||||
.add(Restrictions.eq("parent", calendar)).list();
|
||||
if (!calendarData.isEmpty()) {
|
||||
throw ValidationException
|
||||
.invalidValue(
|
||||
"Cannot delete calendar. It is being used at this moment by some resources.",
|
||||
calendar);
|
||||
}
|
||||
}
|
||||
|
||||
private void checkHasOrders(BaseCalendar calendar) {
|
||||
List orders = getSession().createCriteria(Order.class)
|
||||
.add(Restrictions.eq("calendar", calendar)).list();
|
||||
if (!orders.isEmpty()) {
|
||||
throw ValidationException
|
||||
.invalidValue(
|
||||
"Cannot delete calendar. It is being used at this moment by some orders.",
|
||||
calendar);
|
||||
}
|
||||
}
|
||||
|
||||
private void checkHasTasks(BaseCalendar calendar) {
|
||||
List tasks = getSession().createCriteria(TaskElement.class)
|
||||
.add(Restrictions.eq("calendar", calendar)).list();
|
||||
if (!tasks.isEmpty()) {
|
||||
throw ValidationException
|
||||
.invalidValue(
|
||||
"Cannot delete calendar. It is being used at this moment by some tasks.",
|
||||
calendar);
|
||||
}
|
||||
}
|
||||
|
||||
private void checkHasTemplates(BaseCalendar calendar) {
|
||||
List templates = getSession().createCriteria(OrderTemplate.class)
|
||||
.add(Restrictions.eq("calendar", calendar)).list();
|
||||
if (!templates.isEmpty()) {
|
||||
throw ValidationException
|
||||
.invalidValue(
|
||||
"Cannot delete calendar. It is being used at this moment by some templates.",
|
||||
calendar);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,11 +25,13 @@ import java.util.List;
|
|||
|
||||
import org.navalplanner.business.calendars.entities.BaseCalendar;
|
||||
import org.navalplanner.business.common.daos.IIntegrationEntityDAO;
|
||||
import org.navalplanner.business.common.exceptions.ValidationException;
|
||||
|
||||
/**
|
||||
* Contract for {@link BaseCalendarDAO}
|
||||
*
|
||||
* @author Manuel Rego Casasnovas <mrego@igalia.com>
|
||||
* @author Diego Pino García <dpino@igalia.com>
|
||||
*/
|
||||
public interface IBaseCalendarDAO extends
|
||||
IIntegrationEntityDAO<BaseCalendar> {
|
||||
|
|
@ -44,4 +46,6 @@ public interface IBaseCalendarDAO extends
|
|||
|
||||
boolean thereIsOtherWithSameName(BaseCalendar baseCalendar);
|
||||
|
||||
void checkIsReferencedByOtherEntities(BaseCalendar calendar) throws ValidationException;
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ package org.navalplanner.web.calendars;
|
|||
|
||||
import static org.navalplanner.web.I18nHelper._;
|
||||
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.joda.time.LocalDate;
|
||||
import org.navalplanner.business.calendars.entities.BaseCalendar;
|
||||
import org.navalplanner.business.common.exceptions.ValidationException;
|
||||
|
|
@ -55,6 +56,8 @@ import org.zkoss.zul.api.Window;
|
|||
*/
|
||||
public class BaseCalendarCRUDController extends GenericForwardComposer {
|
||||
|
||||
private static final org.apache.commons.logging.Log LOG = LogFactory.getLog(BaseCalendarCRUDController.class);
|
||||
|
||||
private IBaseCalendarModel baseCalendarModel;
|
||||
|
||||
private Window listWindow;
|
||||
|
|
@ -393,27 +396,56 @@ public class BaseCalendarCRUDController extends GenericForwardComposer {
|
|||
+ "Please, change the default calendar in the Configuration window before."));
|
||||
return;
|
||||
}
|
||||
removeCalendar(calendar);
|
||||
}
|
||||
|
||||
try {
|
||||
int status = Messagebox
|
||||
.show(_("Confirm deleting {0}. Are you sure?",
|
||||
calendar.getName()), _("Delete"), Messagebox.OK
|
||||
| Messagebox.CANCEL, Messagebox.QUESTION);
|
||||
if (Messagebox.OK == status) {
|
||||
remove(calendar);
|
||||
private void removeCalendar(BaseCalendar calendar) {
|
||||
if (!isReferencedByOtherEntities(calendar)) {
|
||||
int result = showConfirmDeleteCalendar(calendar);
|
||||
if (result == Messagebox.OK) {
|
||||
final String calendarName = calendar.getName();
|
||||
baseCalendarModel.confirmRemove(calendar);
|
||||
messagesForUser.showMessage(Level.INFO,
|
||||
_("Removed calendar \"{0}\"", calendarName));
|
||||
Util.reloadBindings(listWindow);
|
||||
}
|
||||
|
||||
} catch (InterruptedException e) {
|
||||
messagesForUser.showMessage(Level.ERROR, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private void remove(BaseCalendar calendar) {
|
||||
final String name = calendar.getName();
|
||||
baseCalendarModel.confirmRemove(calendar);
|
||||
messagesForUser.showMessage(Level.INFO,
|
||||
_("Removed calendar \"{0}\"", name));
|
||||
Util.reloadBindings(listWindow);
|
||||
private int showConfirmDeleteCalendar(BaseCalendar calendar) {
|
||||
try {
|
||||
return Messagebox
|
||||
.show(_("Confirm deleting {0}. Are you sure?",
|
||||
calendar.getName()), _("Delete"), Messagebox.OK
|
||||
| Messagebox.CANCEL, Messagebox.QUESTION);
|
||||
} catch (InterruptedException e) {
|
||||
LOG.error(
|
||||
_("Error on showing removing element: ", calendar.getId()),
|
||||
e);
|
||||
}
|
||||
return Messagebox.CANCEL;
|
||||
}
|
||||
|
||||
private boolean isReferencedByOtherEntities(BaseCalendar calendar) {
|
||||
try {
|
||||
baseCalendarModel.checkIsReferencedByOtherEntities(calendar);
|
||||
return false;
|
||||
} catch (ValidationException e) {
|
||||
showCannotDeleteCalendarDialog(e.getInvalidValue().getMessage(),
|
||||
calendar);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private void showCannotDeleteCalendarDialog(String message, BaseCalendar calendar) {
|
||||
try {
|
||||
Messagebox.show(_(message), _("Warning"), Messagebox.OK,
|
||||
Messagebox.EXCLAMATION);
|
||||
} catch (InterruptedException e) {
|
||||
LOG.error(
|
||||
_("Error on showing warning message removing calendar: ",
|
||||
calendar.getId()), e);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isDefault(BaseCalendar calendar) {
|
||||
|
|
|
|||
|
|
@ -759,4 +759,11 @@ public class BaseCalendarModel extends IntegrationEntityModel implements
|
|||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public void checkIsReferencedByOtherEntities(BaseCalendar calendar) throws ValidationException {
|
||||
baseCalendarDAO.checkIsReferencedByOtherEntities(calendar);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -210,4 +210,6 @@ public interface IBaseCalendarModel extends IIntegrationEntityModel {
|
|||
|
||||
boolean isOwnException(CalendarException exception);
|
||||
|
||||
}
|
||||
void checkIsReferencedByOtherEntities(BaseCalendar calendar) throws ValidationException;
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue