Search in sources :

Example 46 with MotechDataService

use of org.motechproject.mds.service.MotechDataService in project motech by motech.

the class CsvImporterExporter method getRelatedObject.

private Object getRelatedObject(Long id, String entityClass) {
    MotechDataService dataService = DataServiceHelper.getDataService(getBundleContext(), entityClass);
    Object obj = dataService.findById(id);
    if (obj == null) {
        LOGGER.warn("Unable to find {} instance with id {}. Ignoring, you will have to create this relationship manually", entityClass, id);
    }
    return obj;
}
Also used : MotechDataService(org.motechproject.mds.service.MotechDataService)

Example 47 with MotechDataService

use of org.motechproject.mds.service.MotechDataService in project motech by motech.

the class TrashListener method preDelete.

@Override
public void preDelete(InstanceLifecycleEvent event) {
    Object instance = event.getSource();
    String className = instance.getClass().getName();
    getLogger().trace("Received pre-delete for: {}", instance);
    // omit events for trash and history instances
    // get the schema version from the data service
    MotechDataService dataService = ServiceUtil.getServiceFromAppContext(getApplicationContext(), className);
    Long schemaVersion = dataService.getSchemaVersion();
    if (getService().isTrashMode()) {
        getLogger().debug("Moving to trash {}, schema version {}", new Object[] { instance, schemaVersion });
        getService().moveToTrash(instance, schemaVersion);
    }
}
Also used : MotechDataService(org.motechproject.mds.service.MotechDataService)

Example 48 with MotechDataService

use of org.motechproject.mds.service.MotechDataService in project motech by motech.

the class ReflectionsUtil method getMdsInterfaces.

/**
 * Finds all interfaces that extend the {@link MotechDataService} interface.
 *
 * @param bundle A bundle to look in.
 * @return a list of classes that extend the MotechDataService interface.
 */
public static List<Class<? extends MotechDataService>> getMdsInterfaces(Bundle bundle) {
    LOGGER.debug("Looking for MDS interfaces in bundle: {}", bundle.getSymbolicName());
    Reflections reflections = configureReflection(bundle, new WrappedBundleClassLoader(bundle), new SubTypesScanner());
    Set<Class<? extends MotechDataService>> set = reflections.getSubTypesOf(MotechDataService.class);
    return new ArrayList<>(set);
}
Also used : SubTypesScanner(org.reflections.scanners.SubTypesScanner) ArrayList(java.util.ArrayList) MotechDataService(org.motechproject.mds.service.MotechDataService) Reflections(org.reflections.Reflections)

Example 49 with MotechDataService

use of org.motechproject.mds.service.MotechDataService in project motech by motech.

the class MDSDataProvider method findUsingLookup.

private Object findUsingLookup(String type, String lookupName, Map<String, String> lookupMap) {
    Object obj = null;
    LookupDto lookup = null;
    EntityDto entity = entityService.getEntityByClassName(type);
    if (entity != null) {
        lookup = entityService.getLookupByName(entity.getId(), lookupName);
    }
    if (entity != null && lookup != null) {
        String serviceName = MotechClassPool.getInterfaceName(type);
        MotechDataService service = OSGiServiceUtils.findService(bundleContext, serviceName);
        if (service != null) {
            Map<String, FieldDto> fieldsByName = entityService.getLookupFieldsMapping(entity.getId(), lookupName);
            LookupExecutor executor = new LookupExecutor(service, lookup, fieldsByName);
            obj = executor.execute(lookupMap);
        } else {
            getLogger().error("Service %s not found", serviceName);
        }
    }
    // we allow executing lookups that return multiple objects
    // if such a lookup returns more then 1 object we throw an exception
    Object result = null;
    if (obj instanceof Collection) {
        Collection collection = (Collection) obj;
        if (collection.size() == 1) {
            result = collection.iterator().next();
        } else if (collection.size() > 1) {
            throw new IllegalArgumentException(String.format("Data provided lookup for %s returned more then 1 object, number of objects found: %d", type, collection.size()));
        }
    } else {
        result = obj;
    }
    return result;
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) LookupDto(org.motechproject.mds.dto.LookupDto) Collection(java.util.Collection) LookupExecutor(org.motechproject.mds.lookup.LookupExecutor) MotechDataService(org.motechproject.mds.service.MotechDataService) FieldDto(org.motechproject.mds.dto.FieldDto)

Example 50 with MotechDataService

use of org.motechproject.mds.service.MotechDataService in project motech by motech.

the class InstanceServiceTest method shouldUpdateRelatedFields.

@Test
public void shouldUpdateRelatedFields() {
    TestSample test1 = new TestSample("someString", 4);
    TestSample test2 = new TestSample("otherString", 5);
    TestSample test3 = new TestSample("sample", 6);
    RelationshipsUpdate oneToOneUpdate = new RelationshipsUpdate();
    oneToOneUpdate.getAddedIds().add(6L);
    RelationshipsUpdate oneToManyUpdate = buildRelationshipUpdate();
    List<FieldRecord> fieldRecords = asList(FieldTestHelper.fieldRecord("title", String.class.getName(), "String field", "Default"), FieldTestHelper.fieldRecord(TypeDto.ONE_TO_MANY_RELATIONSHIP, "testSamples", "Related field", oneToManyUpdate), FieldTestHelper.fieldRecord(TypeDto.ONE_TO_ONE_RELATIONSHIP, "testSample", "Other Related field", oneToOneUpdate));
    EntityRecord entityRecord = new EntityRecord(null, ANOTHER_ENTITY_ID, fieldRecords);
    mockSampleFields();
    mockDataService();
    mockEntity();
    EntityDto entityWithRelatedField = mock(EntityDto.class);
    when(entityService.getEntity(ANOTHER_ENTITY_ID)).thenReturn(entityWithRelatedField);
    when(entityWithRelatedField.getClassName()).thenReturn(AnotherSample.class.getName());
    when(entityWithRelatedField.getId()).thenReturn(ENTITY_ID + 1);
    ServiceReference serviceReferenceForClassWithRelatedField = mock(ServiceReference.class);
    MotechDataService serviceForClassWithRelatedField = mock(MotechDataService.class);
    when(bundleContext.getServiceReference(ClassName.getInterfaceName(AnotherSample.class.getName()))).thenReturn(serviceReferenceForClassWithRelatedField);
    when(bundleContext.getService(serviceReferenceForClassWithRelatedField)).thenReturn(serviceForClassWithRelatedField);
    when(motechDataService.findById(4L)).thenReturn(test1);
    when(motechDataService.findById(5L)).thenReturn(test2);
    when(motechDataService.findById(6L)).thenReturn(test3);
    when(motechDataService.findByIds(oneToManyUpdate.getAddedIds())).thenReturn(Arrays.asList(test1, test2));
    when(entityService.getEntityFieldsForUI(ANOTHER_ENTITY_ID)).thenReturn(asList(FieldTestHelper.fieldDto(5L, "title", String.class.getName(), "String field", "Default"), FieldTestHelper.fieldDto(6L, "testSamples", TypeDto.ONE_TO_MANY_RELATIONSHIP.getTypeClass(), "Related field", null)));
    ArgumentCaptor<AnotherSample> captor = ArgumentCaptor.forClass(AnotherSample.class);
    instanceService.saveInstance(entityRecord, null);
    verify(serviceForClassWithRelatedField).create(captor.capture());
    AnotherSample capturedValue = captor.getValue();
    assertEquals(capturedValue.getTestSample(), test3);
    assertEquals(capturedValue.getTestSamples().size(), 3);
    assertEquals(capturedValue.getTitle(), "Default");
    assertTrue(capturedValue.getTestSamples().contains(test1));
    assertFalse(capturedValue.getTestSamples().contains(test3));
    assertTrue(capturedValue.getTestSamples().contains(test2));
}
Also used : BasicEntityRecord(org.motechproject.mds.web.domain.BasicEntityRecord) EntityRecord(org.motechproject.mds.web.domain.EntityRecord) EntityDto(org.motechproject.mds.dto.EntityDto) RelationshipsUpdate(org.motechproject.mds.web.domain.RelationshipsUpdate) FieldRecord(org.motechproject.mds.web.domain.FieldRecord) BasicFieldRecord(org.motechproject.mds.web.domain.BasicFieldRecord) Matchers.anyString(org.mockito.Matchers.anyString) DefaultMotechDataService(org.motechproject.mds.service.DefaultMotechDataService) MotechDataService(org.motechproject.mds.service.MotechDataService) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Aggregations

MotechDataService (org.motechproject.mds.service.MotechDataService)52 EntityDto (org.motechproject.mds.dto.EntityDto)26 FieldDto (org.motechproject.mds.dto.FieldDto)14 ArrayList (java.util.ArrayList)13 List (java.util.List)9 Test (org.junit.Test)8 Entity (org.motechproject.mds.domain.Entity)4 LookupDto (org.motechproject.mds.dto.LookupDto)4 ObjectNotFoundException (org.motechproject.mds.exception.object.ObjectNotFoundException)4 LookupExecutor (org.motechproject.mds.lookup.LookupExecutor)4 BasicEntityRecord (org.motechproject.mds.web.domain.BasicEntityRecord)4 Bundle (org.osgi.framework.Bundle)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 Collection (java.util.Collection)3 LookupExecutionException (org.motechproject.mds.exception.lookup.LookupExecutionException)3 ObjectReadException (org.motechproject.mds.exception.object.ObjectReadException)3 DefaultMotechDataService (org.motechproject.mds.service.DefaultMotechDataService)3 EntityRecord (org.motechproject.mds.web.domain.EntityRecord)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2