Search in sources :

Example 1 with TaskUtils

use of org.apache.syncope.core.persistence.api.entity.task.TaskUtils in project syncope by apache.

the class TaskDataBinderImpl method getTaskTO.

@Override
public <T extends TaskTO> T getTaskTO(final Task task, final TaskUtils taskUtils, final boolean details) {
    T taskTO = taskUtils.newTaskTO();
    BeanUtils.copyProperties(task, taskTO, IGNORE_TASK_PROPERTIES);
    TaskExec latestExec = taskExecDAO.findLatestStarted(task);
    if (latestExec == null) {
        taskTO.setLatestExecStatus(StringUtils.EMPTY);
    } else {
        taskTO.setLatestExecStatus(latestExec.getStatus());
        taskTO.setStart(latestExec.getStart());
        taskTO.setEnd(latestExec.getEnd());
    }
    if (details) {
        task.getExecs().stream().filter(execution -> execution != null).forEachOrdered(execution -> taskTO.getExecutions().add(getExecTO(execution)));
    }
    switch(taskUtils.getType()) {
        case PROPAGATION:
            PropagationTask propagationTask = (PropagationTask) task;
            PropagationTaskTO propagationTaskTO = (PropagationTaskTO) taskTO;
            propagationTaskTO.setAnyTypeKind(propagationTask.getAnyTypeKind());
            propagationTaskTO.setEntityKey(propagationTask.getEntityKey());
            propagationTaskTO.setResource(propagationTask.getResource().getKey());
            propagationTaskTO.setAttributes(propagationTask.getSerializedAttributes());
            break;
        case SCHEDULED:
            SchedTask schedTask = (SchedTask) task;
            SchedTaskTO schedTaskTO = (SchedTaskTO) taskTO;
            setExecTime(schedTaskTO, task);
            if (schedTask.getJobDelegate() != null) {
                schedTaskTO.setJobDelegate(schedTask.getJobDelegate().getKey());
            }
            break;
        case PULL:
            PullTask pullTask = (PullTask) task;
            PullTaskTO pullTaskTO = (PullTaskTO) taskTO;
            setExecTime(pullTaskTO, task);
            pullTaskTO.setDestinationRealm(pullTask.getDestinatioRealm().getFullPath());
            pullTaskTO.setResource(pullTask.getResource().getKey());
            pullTaskTO.setMatchingRule(pullTask.getMatchingRule() == null ? MatchingRule.UPDATE : pullTask.getMatchingRule());
            pullTaskTO.setUnmatchingRule(pullTask.getUnmatchingRule() == null ? UnmatchingRule.PROVISION : pullTask.getUnmatchingRule());
            if (pullTask.getReconFilterBuilder() != null) {
                pullTaskTO.setReconFilterBuilder(pullTask.getReconFilterBuilder().getKey());
            }
            pullTaskTO.getActions().addAll(pullTask.getActions().stream().map(Entity::getKey).collect(Collectors.toList()));
            pullTask.getTemplates().forEach(template -> {
                pullTaskTO.getTemplates().put(template.getAnyType().getKey(), template.get());
            });
            pullTaskTO.setRemediation(pullTask.isRemediation());
            break;
        case PUSH:
            PushTask pushTask = (PushTask) task;
            PushTaskTO pushTaskTO = (PushTaskTO) taskTO;
            setExecTime(pushTaskTO, task);
            pushTaskTO.setSourceRealm(pushTask.getSourceRealm().getFullPath());
            pushTaskTO.setResource(pushTask.getResource().getKey());
            pushTaskTO.setMatchingRule(pushTask.getMatchingRule() == null ? MatchingRule.LINK : pushTask.getMatchingRule());
            pushTaskTO.setUnmatchingRule(pushTask.getUnmatchingRule() == null ? UnmatchingRule.ASSIGN : pushTask.getUnmatchingRule());
            pushTaskTO.getActions().addAll(pushTask.getActions().stream().map(Entity::getKey).collect(Collectors.toList()));
            pushTask.getFilters().forEach(filter -> {
                pushTaskTO.getFilters().put(filter.getAnyType().getKey(), filter.getFIQLCond());
            });
            break;
        case NOTIFICATION:
            NotificationTask notificationTask = (NotificationTask) task;
            NotificationTaskTO notificationTaskTO = (NotificationTaskTO) taskTO;
            notificationTaskTO.setNotification(notificationTask.getNotification().getKey());
            notificationTaskTO.setAnyTypeKind(notificationTask.getAnyTypeKind());
            notificationTaskTO.setEntityKey(notificationTask.getEntityKey());
            if (notificationTask.isExecuted() && StringUtils.isBlank(taskTO.getLatestExecStatus())) {
                taskTO.setLatestExecStatus("[EXECUTED]");
            }
            break;
        default:
    }
    return taskTO;
}
Also used : SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) TaskTO(org.apache.syncope.common.lib.to.TaskTO) LoggerFactory(org.slf4j.LoggerFactory) AnyType(org.apache.syncope.core.persistence.api.entity.AnyType) Autowired(org.springframework.beans.factory.annotation.Autowired) NotificationTask(org.apache.syncope.core.persistence.api.entity.task.NotificationTask) Entity(org.apache.syncope.core.persistence.api.entity.Entity) StringUtils(org.apache.commons.lang3.StringUtils) PropagationTask(org.apache.syncope.core.persistence.api.entity.task.PropagationTask) Scheduler(org.quartz.Scheduler) PropagationTaskTO(org.apache.syncope.common.lib.to.PropagationTaskTO) SchedTaskTO(org.apache.syncope.common.lib.to.SchedTaskTO) ProvisioningTaskTO(org.apache.syncope.common.lib.to.ProvisioningTaskTO) TaskExecDAO(org.apache.syncope.core.persistence.api.dao.TaskExecDAO) SchedulerFactoryBean(org.springframework.scheduling.quartz.SchedulerFactoryBean) ImplementationDAO(org.apache.syncope.core.persistence.api.dao.ImplementationDAO) NotificationTaskTO(org.apache.syncope.common.lib.to.NotificationTaskTO) PullJobDelegate(org.apache.syncope.core.provisioning.java.pushpull.PullJobDelegate) ExecTO(org.apache.syncope.common.lib.to.ExecTO) Task(org.apache.syncope.core.persistence.api.entity.task.Task) JobNamer(org.apache.syncope.core.provisioning.api.job.JobNamer) Implementation(org.apache.syncope.core.persistence.api.entity.Implementation) Collectors(java.util.stream.Collectors) UnmatchingRule(org.apache.syncope.common.lib.types.UnmatchingRule) NotFoundException(org.apache.syncope.core.persistence.api.dao.NotFoundException) AnyTypeDAO(org.apache.syncope.core.persistence.api.dao.AnyTypeDAO) ImplementationEngine(org.apache.syncope.common.lib.types.ImplementationEngine) EntityFactory(org.apache.syncope.core.persistence.api.entity.EntityFactory) AnyTemplatePullTask(org.apache.syncope.core.persistence.api.entity.task.AnyTemplatePullTask) TaskUtilsFactory(org.apache.syncope.core.persistence.api.entity.task.TaskUtilsFactory) ProvisioningTask(org.apache.syncope.core.persistence.api.entity.task.ProvisioningTask) TriggerKey(org.quartz.TriggerKey) ExternalResourceDAO(org.apache.syncope.core.persistence.api.dao.ExternalResourceDAO) TaskDataBinder(org.apache.syncope.core.provisioning.api.data.TaskDataBinder) BeanUtils(org.apache.syncope.core.spring.BeanUtils) PullTask(org.apache.syncope.core.persistence.api.entity.task.PullTask) SchedulerException(org.quartz.SchedulerException) Trigger(org.quartz.Trigger) ImplementationType(org.apache.syncope.common.lib.types.ImplementationType) TaskExec(org.apache.syncope.core.persistence.api.entity.task.TaskExec) ClientExceptionType(org.apache.syncope.common.lib.types.ClientExceptionType) SchedTask(org.apache.syncope.core.persistence.api.entity.task.SchedTask) RealmDAO(org.apache.syncope.core.persistence.api.dao.RealmDAO) TemplateUtils(org.apache.syncope.core.provisioning.java.utils.TemplateUtils) PushJobDelegate(org.apache.syncope.core.provisioning.java.pushpull.PushJobDelegate) PushTaskTO(org.apache.syncope.common.lib.to.PushTaskTO) Logger(org.slf4j.Logger) PushTaskAnyFilter(org.apache.syncope.core.persistence.api.entity.task.PushTaskAnyFilter) TaskUtils(org.apache.syncope.core.persistence.api.entity.task.TaskUtils) ExternalResource(org.apache.syncope.core.persistence.api.entity.resource.ExternalResource) PushTask(org.apache.syncope.core.persistence.api.entity.task.PushTask) Component(org.springframework.stereotype.Component) PullTaskTO(org.apache.syncope.common.lib.to.PullTaskTO) JobType(org.apache.syncope.common.lib.types.JobType) MatchingRule(org.apache.syncope.common.lib.types.MatchingRule) TaskType(org.apache.syncope.common.lib.types.TaskType) Entity(org.apache.syncope.core.persistence.api.entity.Entity) NotificationTask(org.apache.syncope.core.persistence.api.entity.task.NotificationTask) PropagationTask(org.apache.syncope.core.persistence.api.entity.task.PropagationTask) PropagationTaskTO(org.apache.syncope.common.lib.to.PropagationTaskTO) PushTask(org.apache.syncope.core.persistence.api.entity.task.PushTask) SchedTask(org.apache.syncope.core.persistence.api.entity.task.SchedTask) SchedTaskTO(org.apache.syncope.common.lib.to.SchedTaskTO) AnyTemplatePullTask(org.apache.syncope.core.persistence.api.entity.task.AnyTemplatePullTask) PullTask(org.apache.syncope.core.persistence.api.entity.task.PullTask) TaskExec(org.apache.syncope.core.persistence.api.entity.task.TaskExec) PushTaskTO(org.apache.syncope.common.lib.to.PushTaskTO) PullTaskTO(org.apache.syncope.common.lib.to.PullTaskTO) NotificationTaskTO(org.apache.syncope.common.lib.to.NotificationTaskTO)

Example 2 with TaskUtils

use of org.apache.syncope.core.persistence.api.entity.task.TaskUtils in project syncope by apache.

the class TaskLogic method read.

@PreAuthorize("hasRole('" + StandardEntitlement.TASK_READ + "')")
@Transactional(readOnly = true)
public <T extends TaskTO> T read(final TaskType type, final String key, final boolean details) {
    Task task = taskDAO.find(key);
    if (task == null) {
        throw new NotFoundException("Task " + key);
    }
    TaskUtils taskUtils = taskUtilsFactory.getInstance(task);
    if (type != null && taskUtils.getType() != type) {
        SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.InvalidRequest);
        sce.getElements().add("Found " + type + ", expected " + taskUtils.getType());
        throw sce;
    }
    return binder.getTaskTO(task, taskUtilsFactory.getInstance(task), details);
}
Also used : TaskUtils(org.apache.syncope.core.persistence.api.entity.task.TaskUtils) NotificationTask(org.apache.syncope.core.persistence.api.entity.task.NotificationTask) SchedTask(org.apache.syncope.core.persistence.api.entity.task.SchedTask) Task(org.apache.syncope.core.persistence.api.entity.task.Task) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) NotFoundException(org.apache.syncope.core.persistence.api.dao.NotFoundException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with TaskUtils

use of org.apache.syncope.core.persistence.api.entity.task.TaskUtils in project syncope by apache.

the class TaskLogic method updateSchedTask.

@PreAuthorize("hasRole('" + StandardEntitlement.TASK_UPDATE + "')")
public <T extends SchedTaskTO> T updateSchedTask(final TaskType type, final SchedTaskTO taskTO) {
    SchedTask task = taskDAO.find(taskTO.getKey());
    if (task == null) {
        throw new NotFoundException("Task " + taskTO.getKey());
    }
    TaskUtils taskUtils = taskUtilsFactory.getInstance(task);
    if (taskUtils.getType() != type) {
        SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.InvalidRequest);
        sce.getElements().add("Found " + type + ", expected " + taskUtils.getType());
        throw sce;
    }
    binder.updateSchedTask(task, taskTO, taskUtils);
    task = taskDAO.save(task);
    try {
        jobManager.register(task, task.getStartAt(), confDAO.find("tasks.interruptMaxRetries", 1L));
    } catch (Exception e) {
        LOG.error("While registering quartz job for task " + task.getKey(), e);
        SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.Scheduling);
        sce.getElements().add(e.getMessage());
        throw sce;
    }
    return binder.getTaskTO(task, taskUtils, false);
}
Also used : TaskUtils(org.apache.syncope.core.persistence.api.entity.task.TaskUtils) SchedTask(org.apache.syncope.core.persistence.api.entity.task.SchedTask) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) NotFoundException(org.apache.syncope.core.persistence.api.dao.NotFoundException) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) SchedulerException(org.quartz.SchedulerException) NotFoundException(org.apache.syncope.core.persistence.api.dao.NotFoundException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 4 with TaskUtils

use of org.apache.syncope.core.persistence.api.entity.task.TaskUtils in project syncope by apache.

the class TaskLogic method delete.

@PreAuthorize("hasRole('" + StandardEntitlement.TASK_DELETE + "')")
public <T extends TaskTO> T delete(final TaskType type, final String key) {
    Task task = taskDAO.find(key);
    if (task == null) {
        throw new NotFoundException("Task " + key);
    }
    TaskUtils taskUtils = taskUtilsFactory.getInstance(task);
    if (type != null && taskUtils.getType() != type) {
        SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.InvalidRequest);
        sce.getElements().add("Found " + type + ", expected " + taskUtils.getType());
        throw sce;
    }
    T taskToDelete = binder.getTaskTO(task, taskUtils, true);
    if (TaskType.SCHEDULED == taskUtils.getType() || TaskType.PULL == taskUtils.getType() || TaskType.PUSH == taskUtils.getType()) {
        jobManager.unregister(task);
    }
    taskDAO.delete(task);
    return taskToDelete;
}
Also used : TaskUtils(org.apache.syncope.core.persistence.api.entity.task.TaskUtils) NotificationTask(org.apache.syncope.core.persistence.api.entity.task.NotificationTask) SchedTask(org.apache.syncope.core.persistence.api.entity.task.SchedTask) Task(org.apache.syncope.core.persistence.api.entity.task.Task) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) NotFoundException(org.apache.syncope.core.persistence.api.dao.NotFoundException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 5 with TaskUtils

use of org.apache.syncope.core.persistence.api.entity.task.TaskUtils in project syncope by apache.

the class TaskLogic method execute.

@PreAuthorize("hasRole('" + StandardEntitlement.TASK_EXECUTE + "')")
@Override
public ExecTO execute(final String key, final Date startAt, final boolean dryRun) {
    Task task = taskDAO.find(key);
    if (task == null) {
        throw new NotFoundException("Task " + key);
    }
    if (startAt != null && startAt.before(new Date())) {
        SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.Scheduling);
        sce.getElements().add("Cannot schedule in the past");
        throw sce;
    }
    TaskUtils taskUtil = taskUtilsFactory.getInstance(task);
    ExecTO result = null;
    switch(taskUtil.getType()) {
        case PROPAGATION:
            TaskExec propExec = taskExecutor.execute((PropagationTaskTO) binder.getTaskTO(task, taskUtil, false));
            result = binder.getExecTO(propExec);
            break;
        case NOTIFICATION:
            TaskExec notExec = notificationJobDelegate.executeSingle((NotificationTask) task);
            result = binder.getExecTO(notExec);
            break;
        case SCHEDULED:
        case PULL:
        case PUSH:
            if (!((SchedTask) task).isActive()) {
                SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.Scheduling);
                sce.getElements().add("Task " + key + " is not active");
                throw sce;
            }
            try {
                Map<String, Object> jobDataMap = jobManager.register((SchedTask) task, startAt, confDAO.find("tasks.interruptMaxRetries", 1L));
                jobDataMap.put(TaskJob.DRY_RUN_JOBDETAIL_KEY, dryRun);
                if (startAt == null) {
                    scheduler.getScheduler().triggerJob(JobNamer.getJobKey(task), new JobDataMap(jobDataMap));
                }
            } catch (Exception e) {
                LOG.error("While executing task {}", task, e);
                SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.Scheduling);
                sce.getElements().add(e.getMessage());
                throw sce;
            }
            result = new ExecTO();
            result.setJobType(JobType.TASK);
            result.setRefKey(task.getKey());
            result.setRefDesc(binder.buildRefDesc(task));
            result.setStart(new Date());
            result.setStatus("JOB_FIRED");
            result.setMessage("Job fired; waiting for results...");
            break;
        default:
    }
    return result;
}
Also used : TaskUtils(org.apache.syncope.core.persistence.api.entity.task.TaskUtils) NotificationTask(org.apache.syncope.core.persistence.api.entity.task.NotificationTask) SchedTask(org.apache.syncope.core.persistence.api.entity.task.SchedTask) Task(org.apache.syncope.core.persistence.api.entity.task.Task) JobDataMap(org.quartz.JobDataMap) ExecTO(org.apache.syncope.common.lib.to.ExecTO) TaskExec(org.apache.syncope.core.persistence.api.entity.task.TaskExec) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) NotFoundException(org.apache.syncope.core.persistence.api.dao.NotFoundException) Date(java.util.Date) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) SchedulerException(org.quartz.SchedulerException) NotFoundException(org.apache.syncope.core.persistence.api.dao.NotFoundException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Aggregations

SyncopeClientException (org.apache.syncope.common.lib.SyncopeClientException)6 NotFoundException (org.apache.syncope.core.persistence.api.dao.NotFoundException)6 SchedTask (org.apache.syncope.core.persistence.api.entity.task.SchedTask)6 TaskUtils (org.apache.syncope.core.persistence.api.entity.task.TaskUtils)6 NotificationTask (org.apache.syncope.core.persistence.api.entity.task.NotificationTask)4 Task (org.apache.syncope.core.persistence.api.entity.task.Task)4 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)4 SchedulerException (org.quartz.SchedulerException)3 ExecTO (org.apache.syncope.common.lib.to.ExecTO)2 TaskExec (org.apache.syncope.core.persistence.api.entity.task.TaskExec)2 Date (java.util.Date)1 Collectors (java.util.stream.Collectors)1 StringUtils (org.apache.commons.lang3.StringUtils)1 NotificationTaskTO (org.apache.syncope.common.lib.to.NotificationTaskTO)1 PropagationTaskTO (org.apache.syncope.common.lib.to.PropagationTaskTO)1 ProvisioningTaskTO (org.apache.syncope.common.lib.to.ProvisioningTaskTO)1 PullTaskTO (org.apache.syncope.common.lib.to.PullTaskTO)1 PushTaskTO (org.apache.syncope.common.lib.to.PushTaskTO)1 SchedTaskTO (org.apache.syncope.common.lib.to.SchedTaskTO)1 TaskTO (org.apache.syncope.common.lib.to.TaskTO)1