Search in sources :

Example 1 with NotificationTask

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

the class NotificationManagerImpl method countExecutionsWithStatus.

@Override
public long countExecutionsWithStatus(final String taskKey, final String status) {
    NotificationTask task = taskDAO.find(taskKey);
    long count = 0;
    for (TaskExec taskExec : task.getExecs()) {
        if (status == null) {
            if (taskExec.getStatus() == null) {
                count++;
            }
        } else if (status.equals(taskExec.getStatus())) {
            count++;
        }
    }
    return count;
}
Also used : NotificationTask(org.apache.syncope.core.persistence.api.entity.task.NotificationTask) TaskExec(org.apache.syncope.core.persistence.api.entity.task.TaskExec)

Example 2 with NotificationTask

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

the class NotificationManagerImpl method storeExec.

@Override
public TaskExec storeExec(final TaskExec execution) {
    NotificationTask task = taskDAO.find(execution.getTask().getKey());
    task.add(execution);
    task.setExecuted(true);
    taskDAO.save(task);
    // this flush call is needed to generate a value for the execution key
    taskDAO.flush();
    return execution;
}
Also used : NotificationTask(org.apache.syncope.core.persistence.api.entity.task.NotificationTask)

Example 3 with NotificationTask

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

the class NotificationManagerImpl method getNotificationTask.

/**
 * Create a notification task.
 *
 * @param notification notification to take as model
 * @param any the any object this task is about
 * @param jexlVars JEXL variables
 * @return notification task, fully populated
 */
private NotificationTask getNotificationTask(final Notification notification, final Any<?> any, final Map<String, Object> jexlVars) {
    if (any != null) {
        virAttrHander.getValues(any);
    }
    List<User> recipients = new ArrayList<>();
    if (notification.getRecipientsFIQL() != null) {
        recipients.addAll(searchDAO.<User>search(SearchCondConverter.convert(notification.getRecipientsFIQL()), Collections.<OrderByClause>emptyList(), AnyTypeKind.USER));
    }
    if (notification.isSelfAsRecipient() && any instanceof User) {
        recipients.add((User) any);
    }
    Set<String> recipientEmails = new HashSet<>();
    List<UserTO> recipientTOs = new ArrayList<>(recipients.size());
    recipients.forEach(recipient -> {
        virAttrHander.getValues(recipient);
        String email = getRecipientEmail(notification.getRecipientAttrName(), recipient);
        if (email == null) {
            LOG.warn("{} cannot be notified: {} not found", recipient, notification.getRecipientAttrName());
        } else {
            recipientEmails.add(email);
            recipientTOs.add(userDataBinder.getUserTO(recipient, true));
        }
    });
    if (notification.getStaticRecipients() != null) {
        recipientEmails.addAll(notification.getStaticRecipients());
    }
    if (notification.getRecipientsProvider() != null) {
        try {
            RecipientsProvider recipientsProvider = ImplementationManager.build(notification.getRecipientsProvider());
            recipientEmails.addAll(recipientsProvider.provideRecipients(notification));
        } catch (Exception e) {
            LOG.error("While building {}", notification.getRecipientsProvider(), e);
        }
    }
    jexlVars.put("recipients", recipientTOs);
    jexlVars.put("syncopeConf", this.findAllSyncopeConfs());
    jexlVars.put("events", notification.getEvents());
    NotificationTask task = entityFactory.newEntity(NotificationTask.class);
    task.setNotification(notification);
    if (any != null) {
        task.setEntityKey(any.getKey());
        task.setAnyTypeKind(any.getType().getKind());
    }
    task.setTraceLevel(notification.getTraceLevel());
    task.getRecipients().addAll(recipientEmails);
    task.setSender(notification.getSender());
    task.setSubject(notification.getSubject());
    if (StringUtils.isNotBlank(notification.getTemplate().getTextTemplate())) {
        task.setTextBody(evaluate(notification.getTemplate().getTextTemplate(), jexlVars));
    }
    if (StringUtils.isNotBlank(notification.getTemplate().getHTMLTemplate())) {
        task.setHtmlBody(evaluate(notification.getTemplate().getHTMLTemplate(), jexlVars));
    }
    return task;
}
Also used : RecipientsProvider(org.apache.syncope.core.provisioning.api.notification.RecipientsProvider) NotificationTask(org.apache.syncope.core.persistence.api.entity.task.NotificationTask) User(org.apache.syncope.core.persistence.api.entity.user.User) OrderByClause(org.apache.syncope.core.persistence.api.dao.search.OrderByClause) UserTO(org.apache.syncope.common.lib.to.UserTO) ArrayList(java.util.ArrayList) ParseException(java.text.ParseException) HashSet(java.util.HashSet)

Example 4 with NotificationTask

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

the class NotificationManagerImpl method createTasks.

@Override
public List<NotificationTask> createTasks(final AuditElements.EventCategoryType type, final String category, final String subcategory, final String event, final Result condition, final Object before, final Object output, final Object... input) {
    Any<?> any = null;
    if (before instanceof UserTO) {
        any = userDAO.find(((UserTO) before).getKey());
    } else if (output instanceof UserTO) {
        any = userDAO.find(((UserTO) output).getKey());
    } else if (output instanceof Pair && ((Pair) output).getRight() instanceof UserTO) {
        any = userDAO.find(((UserTO) ((Pair) output).getRight()).getKey());
    } else if (output instanceof ProvisioningResult && ((ProvisioningResult) output).getEntity() instanceof UserTO) {
        any = userDAO.find(((ProvisioningResult) output).getEntity().getKey());
    } else if (before instanceof AnyObjectTO) {
        any = anyObjectDAO.find(((AnyObjectTO) before).getKey());
    } else if (output instanceof AnyObjectTO) {
        any = anyObjectDAO.find(((AnyObjectTO) output).getKey());
    } else if (output instanceof ProvisioningResult && ((ProvisioningResult) output).getEntity() instanceof AnyObjectTO) {
        any = anyObjectDAO.find(((ProvisioningResult) output).getEntity().getKey());
    } else if (before instanceof GroupTO) {
        any = groupDAO.find(((GroupTO) before).getKey());
    } else if (output instanceof GroupTO) {
        any = groupDAO.find(((GroupTO) output).getKey());
    } else if (output instanceof ProvisioningResult && ((ProvisioningResult) output).getEntity() instanceof GroupTO) {
        any = groupDAO.find(((ProvisioningResult) output).getEntity().getKey());
    }
    AnyType anyType = any == null ? null : any.getType();
    LOG.debug("Search notification for [{}]{}", anyType, any);
    List<NotificationTask> notifications = new ArrayList<>();
    for (Notification notification : notificationDAO.findAll()) {
        if (LOG.isDebugEnabled()) {
            notification.getAbouts().forEach(about -> {
                LOG.debug("Notification about {} defined: {}", about.getAnyType(), about.get());
            });
        }
        if (notification.isActive()) {
            String currentEvent = AuditLoggerName.buildEvent(type, category, subcategory, event, condition);
            if (!notification.getEvents().contains(currentEvent)) {
                LOG.debug("No events found about {}", any);
            } else if (anyType == null || any == null || !notification.getAbout(anyType).isPresent() || searchDAO.matches(any, SearchCondConverter.convert(notification.getAbout(anyType).get().get()))) {
                LOG.debug("Creating notification task for event {} about {}", currentEvent, any);
                final Map<String, Object> model = new HashMap<>();
                model.put("type", type);
                model.put("category", category);
                model.put("subcategory", subcategory);
                model.put("event", event);
                model.put("condition", condition);
                model.put("before", before);
                model.put("output", output);
                model.put("input", input);
                if (any instanceof User) {
                    model.put("user", userDataBinder.getUserTO((User) any, true));
                } else if (any instanceof Group) {
                    model.put("group", groupDataBinder.getGroupTO((Group) any, true));
                } else if (any instanceof AnyObject) {
                    model.put("group", anyObjectDataBinder.getAnyObjectTO((AnyObject) any, true));
                }
                NotificationTask notificationTask = getNotificationTask(notification, any, model);
                notificationTask = taskDAO.save(notificationTask);
                notifications.add(notificationTask);
            }
        } else {
            LOG.debug("Notification {} is not active, task will not be created", notification.getKey());
        }
    }
    return notifications;
}
Also used : Group(org.apache.syncope.core.persistence.api.entity.group.Group) NotificationTask(org.apache.syncope.core.persistence.api.entity.task.NotificationTask) User(org.apache.syncope.core.persistence.api.entity.user.User) ProvisioningResult(org.apache.syncope.common.lib.to.ProvisioningResult) ArrayList(java.util.ArrayList) Notification(org.apache.syncope.core.persistence.api.entity.Notification) GroupTO(org.apache.syncope.common.lib.to.GroupTO) AnyObjectTO(org.apache.syncope.common.lib.to.AnyObjectTO) AnyObject(org.apache.syncope.core.persistence.api.entity.anyobject.AnyObject) UserTO(org.apache.syncope.common.lib.to.UserTO) AnyType(org.apache.syncope.core.persistence.api.entity.AnyType) Map(java.util.Map) HashMap(java.util.HashMap) Pair(org.apache.commons.lang3.tuple.Pair)

Example 5 with NotificationTask

use of org.apache.syncope.core.persistence.api.entity.task.NotificationTask 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)

Aggregations

NotificationTask (org.apache.syncope.core.persistence.api.entity.task.NotificationTask)7 TaskExec (org.apache.syncope.core.persistence.api.entity.task.TaskExec)3 ArrayList (java.util.ArrayList)2 SyncopeClientException (org.apache.syncope.common.lib.SyncopeClientException)2 ExecTO (org.apache.syncope.common.lib.to.ExecTO)2 UserTO (org.apache.syncope.common.lib.to.UserTO)2 NotFoundException (org.apache.syncope.core.persistence.api.dao.NotFoundException)2 AnyType (org.apache.syncope.core.persistence.api.entity.AnyType)2 User (org.apache.syncope.core.persistence.api.entity.user.User)2 ParseException (java.text.ParseException)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 StringUtils (org.apache.commons.lang3.StringUtils)1 Pair (org.apache.commons.lang3.tuple.Pair)1 AnyObjectTO (org.apache.syncope.common.lib.to.AnyObjectTO)1 GroupTO (org.apache.syncope.common.lib.to.GroupTO)1 NotificationTaskTO (org.apache.syncope.common.lib.to.NotificationTaskTO)1