Search in sources :

Example 6 with MotechListener

use of org.motechproject.event.listener.annotations.MotechListener in project motech by motech.

the class TaskServiceImpl method validateTasksAfterChannelUpdate.

@MotechListener(subjects = CHANNEL_UPDATE_SUBJECT)
@Transactional
public void validateTasksAfterChannelUpdate(MotechEvent event) {
    String moduleName = event.getParameters().get(CHANNEL_MODULE_NAME).toString();
    Channel channel = channelService.getChannel(moduleName);
    LOGGER.debug("Handling Channel update: {} for module: {}", channel.getDisplayName(), moduleName);
    List<Task> tasks = findTasksDependentOnModule(moduleName);
    for (Task task : tasks) {
        Set<TaskError> errors;
        if (task.getTrigger() != null) {
            errors = validateTrigger(task);
            handleValidationErrors(task, errors, TASK_TRIGGER_VALIDATION_ERRORS);
        }
        errors = validateActions(task, channel);
        handleValidationErrors(task, errors, TASK_ACTION_VALIDATION_ERRORS);
    }
}
Also used : Task(org.motechproject.tasks.domain.mds.task.Task) Channel(org.motechproject.tasks.domain.mds.channel.Channel) HandlerPredicates.tasksWithRegisteredChannel(org.motechproject.tasks.service.util.HandlerPredicates.tasksWithRegisteredChannel) TaskError(org.motechproject.tasks.domain.mds.task.TaskError) MotechListener(org.motechproject.event.listener.annotations.MotechListener) Transactional(org.springframework.transaction.annotation.Transactional)

Example 7 with MotechListener

use of org.motechproject.event.listener.annotations.MotechListener in project motech by motech.

the class InvalidMessageEventListener method handle.

@MotechListener(subjects = MESSAGE_REDELIVERY_TEST)
public synchronized void handle(MotechEvent motechEvent) {
    this.motechEvent = motechEvent;
    handledTimes.add(new DateTime());
    throw new RuntimeException("Message redelivery test.");
}
Also used : DateTime(org.joda.time.DateTime) MotechListener(org.motechproject.event.listener.annotations.MotechListener)

Example 8 with MotechListener

use of org.motechproject.event.listener.annotations.MotechListener in project motech by motech.

the class PasswordExpirationCheckEventHandler method handleEvent.

/**
 * Handles PASSWORD_EXPIRATION_CHECK_EVENT event. Checks every user for the date of the last password change and sends an
 * event if the user should be notified about the required password change.
 *
 * @param event  the event to be handled
 */
@MotechListener(subjects = PASSWORD_EXPIRATION_CHECK_EVENT)
public void handleEvent(MotechEvent event) {
    if (settingService.isPasswordResetReminderEnabled()) {
        LOGGER.info("Daily password reset reminder triggered");
        final int passwordExpirationInDays = settingService.getNumberOfDaysToChangePassword();
        final int daysBeforeExpirationToSendReminder = settingService.getNumberOfDaysForReminder();
        final int daysWithNoPassChangeForReminder = passwordExpirationInDays - daysBeforeExpirationToSendReminder;
        for (MotechUser user : allUsers.retrieveAll()) {
            final int daysWithoutPasswordChange = daysWithoutPasswordChange(user);
            LOGGER.debug("User {} hasn't changed password in {} days. Notification is being after {} days without" + " password change, {} days before expiration", user.getUserName(), daysWithoutPasswordChange, daysWithNoPassChangeForReminder, daysBeforeExpirationToSendReminder);
            if (daysWithoutPasswordChange == daysWithNoPassChangeForReminder) {
                if (StringUtils.isNotBlank(user.getEmail())) {
                    sendPasswordReminderEvent(user, passwordExpirationInDays, daysBeforeExpirationToSendReminder);
                } else {
                    LOGGER.debug("User {} doesn't have an email address set, skipping sending of reminder", user.getUserName());
                }
            }
        }
    } else {
        LOGGER.info("Daily password reset reminder is disabled, skipping processing of users");
    }
}
Also used : MotechUser(org.motechproject.security.domain.MotechUser) MotechListener(org.motechproject.event.listener.annotations.MotechListener)

Example 9 with MotechListener

use of org.motechproject.event.listener.annotations.MotechListener in project motech by motech.

the class TaskServiceImpl method validateTasksAfterTaskDataProviderUpdate.

@MotechListener(subjects = DATA_PROVIDER_UPDATE_SUBJECT)
@Transactional
public void validateTasksAfterTaskDataProviderUpdate(MotechEvent event) {
    String providerName = event.getParameters().get(DATA_PROVIDER_NAME).toString();
    TaskDataProvider provider = providerService.getProvider(providerName);
    LOGGER.debug("Handling a task data provider update: {}", providerName);
    for (Task task : getAllTasks()) {
        SortedSet<DataSource> dataSources = task.getTaskConfig().getDataSources(provider.getName());
        if (isNotEmpty(dataSources)) {
            Set<TaskError> errors = new HashSet<>();
            for (DataSource dataSource : dataSources) {
                errors.addAll(validateProvider(provider, dataSource, task, new HashMap<Long, TaskDataProvider>()));
            }
            errors.addAll(validateActions(task));
            handleValidationErrors(task, errors, TASK_DATA_PROVIDER_VALIDATION_ERRORS);
        }
    }
}
Also used : TaskDataProvider(org.motechproject.tasks.domain.mds.task.TaskDataProvider) Task(org.motechproject.tasks.domain.mds.task.Task) HashMap(java.util.HashMap) TaskError(org.motechproject.tasks.domain.mds.task.TaskError) DataSource(org.motechproject.tasks.domain.mds.task.DataSource) HashSet(java.util.HashSet) MotechListener(org.motechproject.event.listener.annotations.MotechListener) Transactional(org.springframework.transaction.annotation.Transactional)

Example 10 with MotechListener

use of org.motechproject.event.listener.annotations.MotechListener in project motech by motech.

the class SendEmailEventHandlerImplTest method testIfThereIsHandlerMethodForSendEmailEvent.

@Test
public void testIfThereIsHandlerMethodForSendEmailEvent() throws NoSuchMethodException {
    Method handleMethod = emailEventHandler.getClass().getDeclaredMethod("handle", MotechEvent.class);
    assertTrue("MotechListener annotation missing", handleMethod.isAnnotationPresent(MotechListener.class));
    MotechListener annotation = handleMethod.getAnnotation(MotechListener.class);
    assertArrayEquals(new String[] { SEND_EMAIL_SUBJECT }, annotation.subjects());
}
Also used : MotechListener(org.motechproject.event.listener.annotations.MotechListener) Method(java.lang.reflect.Method) Test(org.junit.Test)

Aggregations

MotechListener (org.motechproject.event.listener.annotations.MotechListener)10 DateTime (org.joda.time.DateTime)3 Method (java.lang.reflect.Method)2 MotechEvent (org.motechproject.event.MotechEvent)2 Task (org.motechproject.tasks.domain.mds.task.Task)2 TaskError (org.motechproject.tasks.domain.mds.task.TaskError)2 Transactional (org.springframework.transaction.annotation.Transactional)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Test (org.junit.Test)1 Level (org.motechproject.admin.messages.Level)1 MotechListenerAbstractProxy (org.motechproject.event.listener.annotations.MotechListenerAbstractProxy)1 MotechListenerEventProxy (org.motechproject.event.listener.annotations.MotechListenerEventProxy)1 MotechListenerNamedParametersProxy (org.motechproject.event.listener.annotations.MotechListenerNamedParametersProxy)1 JobId (org.motechproject.scheduler.contract.JobId)1 RepeatingJobId (org.motechproject.scheduler.contract.RepeatingJobId)1 RunOnceSchedulableJob (org.motechproject.scheduler.contract.RunOnceSchedulableJob)1 MotechUser (org.motechproject.security.domain.MotechUser)1 Channel (org.motechproject.tasks.domain.mds.channel.Channel)1 DataSource (org.motechproject.tasks.domain.mds.task.DataSource)1