use of org.hisp.dhis.common.DeleteNotAllowedException in project dhis2-core by dhis2.
the class DefaultDeletionManager method execute.
// -------------------------------------------------------------------------
// DeletionManager implementation
// -------------------------------------------------------------------------
@Override
public void execute(Object object) {
if (deletionHandlers == null || deletionHandlers.isEmpty()) {
log.info("No deletion handlers registered, aborting deletion handling");
return;
}
log.debug("Deletion handlers detected: " + deletionHandlers.size());
Class<?> clazz = getClazz(object);
String className = clazz.getSimpleName();
// ---------------------------------------------------------------------
// Verify that object is allowed to be deleted
// ---------------------------------------------------------------------
String allowMethodName = ALLOW_METHOD_PREFIX + className;
String currentHandler = null;
try {
Method allowMethod = DeletionHandler.class.getMethod(allowMethodName, new Class[] { clazz });
for (DeletionHandler handler : deletionHandlers) {
currentHandler = handler.getClass().getSimpleName();
log.debug("Check if allowed using " + currentHandler + " for class " + className);
Object allow = allowMethod.invoke(handler, object);
if (allow != null) {
String hint = String.valueOf(allow);
String message = "Could not delete due to association with another object: " + handler.getClassName() + (hint.isEmpty() ? hint : (" (" + hint + ")"));
log.info("Delete was not allowed by " + currentHandler + ": " + message);
throw new DeleteNotAllowedException(DeleteNotAllowedException.ERROR_ASSOCIATED_BY_OTHER_OBJECTS, message);
}
}
} catch (NoSuchMethodException e) {
log.error("Method '" + allowMethodName + "' does not exist on class '" + clazz + "'", e);
return;
} catch (IllegalAccessException ex) {
log.error("Method '" + allowMethodName + "' can not be invoked on DeletionHandler '" + currentHandler + "'", ex);
return;
} catch (InvocationTargetException ex) {
log.error("Method '" + allowMethodName + "' threw exception on DeletionHandler '" + currentHandler + "'", ex);
return;
}
// ---------------------------------------------------------------------
// Delete associated objects
// ---------------------------------------------------------------------
String deleteMethodName = DELETE_METHOD_PREFIX + className;
try {
Method deleteMethod = DeletionHandler.class.getMethod(deleteMethodName, new Class[] { clazz });
for (DeletionHandler handler : deletionHandlers) {
currentHandler = handler.getClass().getSimpleName();
log.debug("Deleting object using " + currentHandler + " for class " + className);
deleteMethod.invoke(handler, object);
}
} catch (Exception ex) {
log.error("Failed to invoke method " + deleteMethodName + " on DeletionHandler '" + currentHandler + "'", ex);
return;
}
log.info("Deleted objects associated with object of type " + className);
}
use of org.hisp.dhis.common.DeleteNotAllowedException in project dhis2-core by dhis2.
the class RemoveLocaleAction method execute.
// -------------------------------------------------------------------------
// Action implementation
// -------------------------------------------------------------------------
@Override
public String execute() throws Exception {
try {
I18nLocale i18nLocale = localeService.getI18nLocale(id);
localeService.deleteI18nLocale(i18nLocale);
} catch (DeleteNotAllowedException ex) {
if (ex.getErrorCode().equals(DeleteNotAllowedException.ERROR_ASSOCIATED_BY_OTHER_OBJECTS)) {
message = i18n.getString("object_not_deleted_associated_by_objects") + " " + ex.getMessage();
return ERROR;
}
}
return SUCCESS;
}
use of org.hisp.dhis.common.DeleteNotAllowedException in project dhis2-core by dhis2.
the class DefaultMaintenanceService method removeExpiredInvitations.
@Override
@Transactional
public int removeExpiredInvitations() {
UserQueryParams params = new UserQueryParams();
params.setInvitationStatus(UserInvitationStatus.EXPIRED);
int userCount = userService.getUserCount(params);
int removeCount = 0;
PageRange range = new PageRange(userCount).setPageSize(200);
List<int[]> pages = range.getPages();
// Iterate from end since users are deleted
Collections.reverse(pages);
log.debug("Pages: " + pages);
for (int[] page : pages) {
params.setFirst(page[0]);
params.setMax(range.getPageSize());
List<User> users = userService.getUsers(params);
for (User user : users) {
try {
userService.deleteUser(user);
removeCount++;
} catch (DeleteNotAllowedException ex) {
log.warn("Could not delete user " + user.getUsername());
}
}
}
log.info("Removed expired invitations: " + removeCount);
return removeCount;
}
use of org.hisp.dhis.common.DeleteNotAllowedException in project dhis2-core by dhis2.
the class DefaultMaintenanceService method prunePeriods.
@Override
public void prunePeriods() {
for (Period period : periodService.getAllPeriods()) {
int periodId = period.getId();
try {
periodService.deletePeriod(period);
log.info("Deleted period with id: " + periodId);
} catch (DeleteNotAllowedException ex) {
log.debug("Period has associated objects and could not be deleted: " + periodId);
}
}
}
use of org.hisp.dhis.common.DeleteNotAllowedException in project dhis2-core by dhis2.
the class RemoveProgramAction method execute.
// -------------------------------------------------------------------------
// Action implementation
// -------------------------------------------------------------------------
@Override
public String execute() throws Exception {
try {
Program program = programService.getProgram(id);
programService.deleteProgram(program);
} catch (DeleteNotAllowedException ex) {
if (ex.getErrorCode().equals(DeleteNotAllowedException.ERROR_ASSOCIATED_BY_OTHER_OBJECTS)) {
message = i18n.getString("object_not_deleted_associated_by_objects") + " " + ex.getMessage();
return ERROR;
}
}
return SUCCESS;
}
Aggregations