Search in sources :

Example 1 with DeleteNotAllowedException

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);
}
Also used : DeleteNotAllowedException(org.hisp.dhis.common.DeleteNotAllowedException) ProxyObject(javassist.util.proxy.ProxyObject) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) DeleteNotAllowedException(org.hisp.dhis.common.DeleteNotAllowedException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with DeleteNotAllowedException

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;
}
Also used : DeleteNotAllowedException(org.hisp.dhis.common.DeleteNotAllowedException) I18nLocale(org.hisp.dhis.i18n.locale.I18nLocale)

Example 3 with DeleteNotAllowedException

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;
}
Also used : DeleteNotAllowedException(org.hisp.dhis.common.DeleteNotAllowedException) PageRange(org.hisp.dhis.commons.util.PageRange) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with DeleteNotAllowedException

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);
        }
    }
}
Also used : DeleteNotAllowedException(org.hisp.dhis.common.DeleteNotAllowedException) Period(org.hisp.dhis.period.Period)

Example 5 with DeleteNotAllowedException

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;
}
Also used : Program(org.hisp.dhis.program.Program) DeleteNotAllowedException(org.hisp.dhis.common.DeleteNotAllowedException)

Aggregations

DeleteNotAllowedException (org.hisp.dhis.common.DeleteNotAllowedException)6 Program (org.hisp.dhis.program.Program)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 ProxyObject (javassist.util.proxy.ProxyObject)1 PageRange (org.hisp.dhis.commons.util.PageRange)1 I18nLocale (org.hisp.dhis.i18n.locale.I18nLocale)1 Period (org.hisp.dhis.period.Period)1 ProgramStage (org.hisp.dhis.program.ProgramStage)1 Transactional (org.springframework.transaction.annotation.Transactional)1