Search in sources :

Example 11 with MotechDataService

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

the class MDSDataProvider method findById.

private Object findById(String type, String idParam) {
    Long id = parseId(idParam);
    String serviceName = MotechClassPool.getInterfaceName(type);
    MotechDataService service = OSGiServiceUtils.findService(bundleContext, serviceName);
    if (null != service) {
        return service.findById(id);
    } else {
        getLogger().error("Service %s not found", serviceName);
        return null;
    }
}
Also used : MotechDataService(org.motechproject.mds.service.MotechDataService)

Example 12 with MotechDataService

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

the class ComboboxValueRepositoryContextIT method setUpTestData.

private void setUpTestData() throws Exception {
    MotechDataService service = getService();
    final Object instance1 = objectInstance("one", asList("one", "two"));
    final Object instance2 = objectInstance("two", asList("two", "one", "four"));
    final Object instance3 = objectInstance("four", asList("four", "four", "four"));
    final Object instance4 = objectInstance("five", singletonList("five"));
    final Object instance5 = objectInstance("one", asList("one", "two", "four", "three"));
    final Object instance6 = objectInstance(null, null);
    final Object instance7 = objectInstance(null, singletonList(null));
    final Object instance8 = objectInstance("", singletonList(""));
    service.doInTransaction(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            service.create(instance1);
            service.create(instance2);
            service.create(instance3);
            service.create(instance4);
            service.create(instance5);
            service.create(instance6);
            service.create(instance7);
            service.create(instance8);
        }
    });
    assertEquals("There were issues creating test data", 8, service.count());
}
Also used : TransactionStatus(org.springframework.transaction.TransactionStatus) MotechDataService(org.motechproject.mds.service.MotechDataService) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult)

Example 13 with MotechDataService

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

the class ActionHandlerServiceImpl method setRelationshipInstanceProperty.

private void setRelationshipInstanceProperty(Object instance, Field field, Object value) throws ActionHandlerException {
    RelationshipHolder relationshipHolder = new RelationshipHolder(field);
    try {
        if (null != value) {
            String relatedClassName = relationshipHolder.getRelatedClass();
            MotechDataService relatedClassDataService = getEntityDataService(relatedClassName);
            if (relationshipHolder.isManyToOne() || relationshipHolder.isOneToOne()) {
                Object relatedInstance = getRelatedInstance(relatedClassDataService, value);
                PropertyUtil.safeSetProperty(instance, field.getName(), relatedInstance);
            } else if (relationshipHolder.isManyToMany() || relationshipHolder.isOneToMany()) {
                List<Object> relatedInstances = getRelatedInstances(relatedClassDataService, value);
                PropertyUtil.safeSetCollectionProperty(instance, field.getName(), relatedInstances);
            }
        } else {
            PropertyUtil.safeSetProperty(instance, field.getName(), null);
        }
    } catch (RuntimeException e) {
        throw new ActionHandlerException("Cannot set instance property " + field.getName() + " with value " + value, e);
    }
}
Also used : ActionHandlerException(org.motechproject.mds.exception.action.ActionHandlerException) RelationshipHolder(org.motechproject.mds.domain.RelationshipHolder) ArrayList(java.util.ArrayList) List(java.util.List) MotechDataService(org.motechproject.mds.service.MotechDataService)

Example 14 with MotechDataService

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

the class ActionHandlerServiceImpl method create.

@Override
public void create(Map<String, Object> parameters) throws ActionHandlerException {
    LOGGER.debug("Action CREATE: params: {}", parameters);
    String entityClassName = getEntityClassName(parameters);
    MotechDataService dataService = getEntityDataService(entityClassName);
    Entity entity = getEntity(entityClassName);
    Object instance = createEntityInstance(dataService);
    setInstanceProperties(instance, entity.getFields(), parameters);
    dataService.create(instance);
}
Also used : Entity(org.motechproject.mds.domain.Entity) MotechDataService(org.motechproject.mds.service.MotechDataService)

Example 15 with MotechDataService

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

the class ActionHandlerServiceImpl method createOrUpdate.

@Override
public void createOrUpdate(Map<String, Object> parameters) throws ActionHandlerException {
    LOGGER.debug("Action CREATE OR UPDATE: params {}", parameters);
    String entityClassName = getEntityClassName(parameters);
    MotechDataService dataService = getEntityDataService(entityClassName);
    Entity entity = getEntity(entityClassName);
    Long instanceId = getInstanceId(parameters, false);
    Object instance = createEntityInstance(dataService);
    PropertyUtil.safeSetProperty(instance, Constants.Util.ID_FIELD_NAME, instanceId);
    setInstanceProperties(instance, entity.getFields(), parameters);
    dataService.createOrUpdate(instance);
}
Also used : Entity(org.motechproject.mds.domain.Entity) MotechDataService(org.motechproject.mds.service.MotechDataService)

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