Example usage for javax.xml.datatype Duration getSign

List of usage examples for javax.xml.datatype Duration getSign

Introduction

In this page you can find the example usage for javax.xml.datatype Duration getSign.

Prototype

public abstract int getSign();

Source Link

Document

Returns the sign of this duration in -1,0, or 1.

Usage

From source file:com.evolveum.midpoint.task.quartzimpl.TaskManagerQuartzImpl.java

@Override
public void cleanupTasks(CleanupPolicyType policy, Task executionTask, OperationResult parentResult)
        throws SchemaException {
    OperationResult result = parentResult.createSubresult(CLEANUP_TASKS);

    if (policy.getMaxAge() == null) {
        return;/*  w  ww  .j a  v a2 s . com*/
    }

    Duration duration = policy.getMaxAge();
    if (duration.getSign() > 0) {
        duration = duration.negate();
    }
    Date deleteTasksClosedUpTo = new Date();
    duration.addTo(deleteTasksClosedUpTo);

    LOGGER.info("Starting cleanup for closed tasks deleting up to {} (duration '{}').",
            new Object[] { deleteTasksClosedUpTo, duration });

    XMLGregorianCalendar timeXml = XmlTypeConverter.createXMLGregorianCalendar(deleteTasksClosedUpTo.getTime());

    List<PrismObject<TaskType>> obsoleteTasks;
    try {
        ObjectQuery obsoleteTasksQuery = ObjectQuery.createObjectQuery(AndFilter.createAnd(
                LessFilter.createLess(TaskType.F_COMPLETION_TIMESTAMP, TaskType.class, getPrismContext(),
                        timeXml, true),
                EqualFilter.createEqual(TaskType.F_PARENT, TaskType.class, getPrismContext(), null)));

        obsoleteTasks = repositoryService.searchObjects(TaskType.class, obsoleteTasksQuery, null, result);
    } catch (SchemaException e) {
        throw new SchemaException("Couldn't get the list of obsolete tasks: " + e.getMessage(), e);
    }

    LOGGER.debug("Found {} task tree(s) to be cleaned up", obsoleteTasks.size());

    boolean interrupted = false;
    int deleted = 0;
    int problems = 0;
    int bigProblems = 0;
    for (PrismObject<TaskType> rootTaskPrism : obsoleteTasks) {

        if (!executionTask.canRun()) {
            result.recordPartialError("Interrupted");
            LOGGER.warn("Task cleanup was interrupted.");
            interrupted = true;
            break;
        }

        // get whole tree
        Task rootTask = createTaskInstance(rootTaskPrism, result);
        List<Task> taskTreeMembers = rootTask.listSubtasksDeeply(result);
        taskTreeMembers.add(rootTask);

        LOGGER.trace("Removing task {} along with its {} children.", rootTask, taskTreeMembers.size() - 1);

        boolean problem = false;
        for (Task task : taskTreeMembers) {
            try {
                deleteTask(task.getOid(), result);
            } catch (SchemaException e) {
                LoggingUtils.logException(LOGGER, "Couldn't delete obsolete task {} due to schema exception", e,
                        task);
                problem = true;
            } catch (ObjectNotFoundException e) {
                LoggingUtils.logException(LOGGER,
                        "Couldn't delete obsolete task {} due to object not found exception", e, task);
                problem = true;
            } catch (RuntimeException e) {
                LoggingUtils.logException(LOGGER, "Couldn't delete obsolete task {} due to a runtime exception",
                        e, task);
                problem = true;
            }

            if (problem) {
                problems++;
                if (!task.getTaskIdentifier().equals(rootTask.getTaskIdentifier())) {
                    bigProblems++;
                }
            } else {
                deleted++;
            }
        }
    }
    result.computeStatusIfUnknown();

    LOGGER.info(
            "Task cleanup procedure " + (interrupted ? "was interrupted" : "finished")
                    + ". Successfully deleted {} tasks; there were problems with deleting {} tasks.",
            deleted, problems);
    if (bigProblems > 0) {
        LOGGER.error(
                "{} subtask(s) couldn't be deleted. Inspect that manually, otherwise they might reside in repo forever.",
                bigProblems);
    }
    String suffix = interrupted ? " Interrupted." : "";
    if (problems == 0) {
        parentResult.createSubresult(CLEANUP_TASKS + ".statistics").recordStatus(OperationResultStatus.SUCCESS,
                "Successfully deleted " + deleted + " task(s)." + suffix);
    } else {
        parentResult.createSubresult(CLEANUP_TASKS + ".statistics")
                .recordPartialError("Successfully deleted " + deleted + " task(s), "
                        + "there was problems with deleting " + problems + " tasks." + suffix
                        + (bigProblems > 0
                                ? (" " + bigProblems + " subtask(s) couldn't be deleted, please see the log.")
                                : ""));
    }

}