Search in sources :

Example 6 with MotechLifecycleListener

use of org.motechproject.mds.listener.MotechLifecycleListener in project motech by motech.

the class InstanceLifecycleListenerProcessorTest method shouldReturnCorrectListeners.

@Test
public void shouldReturnCorrectListeners() {
    processor.processAnnotations(bundle);
    jdoListenerRegistryService.updateEntityNames();
    jdoListenerRegistryService.removeInactiveListeners(ENTITY_NAMES);
    List<MotechLifecycleListener> listeners = jdoListenerRegistryService.getListeners();
    Set<String> postCreateMethods = new HashSet<>();
    postCreateMethods.add("correctListener");
    Set<String> postDeleteMethods = new HashSet<>();
    postDeleteMethods.add("correctListener");
    postDeleteMethods.add("correctPostDeleteListener");
    assertEquals(2, listeners.size());
    for (MotechLifecycleListener listener : listeners) {
        if (!listener.getPackageName().isEmpty()) {
            assertEquals(Arrays.asList("org.motechproject.Test"), listener.getEntityNames());
        } else {
            assertEquals(Sample.class.getName(), listener.getParameterType());
            assertEquals(Sample.class, listener.getService());
            assertEquals(postCreateMethods, listener.getMethodsByType().get(InstanceLifecycleListenerType.POST_CREATE));
            assertEquals(postDeleteMethods, listener.getMethodsByType().get(InstanceLifecycleListenerType.POST_DELETE));
        }
    }
}
Also used : Sample(org.motechproject.mds.annotations.internal.samples.Sample) MotechLifecycleListener(org.motechproject.mds.listener.MotechLifecycleListener) Matchers.anyString(org.mockito.Matchers.anyString) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 7 with MotechLifecycleListener

use of org.motechproject.mds.listener.MotechLifecycleListener in project motech by motech.

the class JdoListenerRegistryServiceImpl method getEntitiesListenerStr.

@Override
public String getEntitiesListenerStr() {
    StringBuilder entityListenerNames = new StringBuilder();
    Set<String> entityNames = new HashSet<>();
    // duplicate entity names using set collection.
    for (MotechLifecycleListener listener : listeners) {
        entityNames.addAll(listener.getEntityNames());
    }
    entityNames.addAll(entitiesWithListeners);
    for (String entityName : entityNames) {
        entityListenerNames.append(entityName).append('\n');
    }
    return entityListenerNames.toString();
}
Also used : MotechLifecycleListener(org.motechproject.mds.listener.MotechLifecycleListener) HashSet(java.util.HashSet)

Example 8 with MotechLifecycleListener

use of org.motechproject.mds.listener.MotechLifecycleListener in project motech by motech.

the class JdoListenerRegistryServiceImpl method removeInactiveListeners.

@Override
public void removeInactiveListeners(String entitiesNames) {
    Set<String> entitiesList = getEntities(entitiesNames);
    List<MotechLifecycleListener> listenersToRemove = new ArrayList<>();
    for (MotechLifecycleListener listener : listeners) {
        if (Collections.disjoint(entitiesList, listener.getEntityNames())) {
            listenersToRemove.add(listener);
            if (!listener.getPackageName().isEmpty() && listener.getEntityNames().isEmpty()) {
                LOGGER.warn("The InstanceLifecycleListener from service {} for {} was removed, " + "because in its package {} there were no persistable classes.", listener.getService(), listener.getParameterType(), listener.getPackageName());
            } else {
                LOGGER.warn("The InstanceLifecycleListener from service {} for {} was removed, " + "because {} is not a persistable class.", listener.getService(), listener.getParameterType(), listener.getParameterType());
            }
        }
    }
    listeners.removeAll(listenersToRemove);
}
Also used : ArrayList(java.util.ArrayList) MotechLifecycleListener(org.motechproject.mds.listener.MotechLifecycleListener)

Aggregations

MotechLifecycleListener (org.motechproject.mds.listener.MotechLifecycleListener)8 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 InstanceLifecycleListenerType (org.motechproject.mds.annotations.InstanceLifecycleListenerType)2 Sample (org.motechproject.mds.annotations.internal.samples.Sample)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 Before (org.junit.Before)1 Test (org.junit.Test)1 Matchers.anyString (org.mockito.Matchers.anyString)1 InstanceLifecycleListener (org.motechproject.mds.annotations.InstanceLifecycleListener)1 AnotherSample (org.motechproject.mds.annotations.internal.samples.AnotherSample)1 EntityDto (org.motechproject.mds.dto.EntityDto)1 JdoListenerInvocationException (org.motechproject.mds.exception.jdo.JdoListenerInvocationException)1 JdoListenerRegistryService (org.motechproject.mds.service.JdoListenerRegistryService)1