Search in sources :

Example 1 with MotechDataService

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

the class MdsRestBundleIT method testBasicCrud.

@Test
public void testBasicCrud() throws Exception {
    final MotechDataService dataService = getDataService();
    // CREATE
    // create 11 records using REST
    getLogger().info("Creating instance via REST");
    for (int i = 0; i < 11; i++) {
        HttpPost post = new HttpPost(ENTITY_URL);
        post.setEntity(new StringEntity(recordJsonString("string" + i, i), ContentType.APPLICATION_JSON));
        HttpResponse response = getHttpClient().execute(post);
        assertNotNull(response);
        assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
    }
    // UPDATE
    // update records 4 & 5
    // we add "updated" at the end of the string
    // GET object Ids
    // TODO: should we return ID in REST responses?
    // TODO: invoke this lookup through REST
    QueryParams queryParams = new QueryParams(new Order("intField", Order.Direction.ASC));
    List objList = (List) MethodUtils.invokeMethod(dataService, "byIntSet", new Object[] { new HashSet<>(asList(4, 5)), queryParams });
    // verify that the lookup works correctly just in case
    assertNotNull(objList);
    assertEquals(2, objList.size());
    for (Object record : objList) {
        HttpPut put = new HttpPut(ENTITY_URL);
        put.setEntity(new StringEntity(recordJsonString(PropertyUtil.getProperty(record, "strField") + "Updated", (int) PropertyUtil.getProperty(record, "intField"), (long) PropertyUtil.getProperty(record, "id"))));
        HttpResponse response = getHttpClient().execute(put);
        assertNotNull(response);
        assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
    }
    // READ
    // read records from 5 to 1
    getLogger().info("Reading instances via REST");
    String body = getHttpClient().get(ENTITY_URL + "?page=2&pageSize=5&sort=intField&order=desc", new BasicResponseHandler());
    assertNotNull(body);
    RestResponse response = OBJECT_MAPPER.readValue(body, RestResponse.class);
    assertEquals(5, response.getData().size());
    for (int i = 0; i < 5; i++) {
        int expectedIntField = 5 - i;
        // we updated the int fields X 10 for these two records
        boolean fieldWasUpdated = expectedIntField == 5 || expectedIntField == 4;
        // and added "Updated" to the string field
        String expectedStrField = "string" + expectedIntField;
        if (fieldWasUpdated) {
            expectedStrField += "Updated";
        }
        Object record = response.getData().get(i);
        assertEquals(expectedStrField, PropertyUtils.getProperty(record, "strField"));
        assertEquals(expectedIntField, PropertyUtils.getProperty(record, "intField"));
    }
    for (Object rec1 : response.getData()) {
        String responseBody = getHttpClient().get(ENTITY_URL + "?id=" + PropertyUtils.getProperty(rec1, "id"), new BasicResponseHandler());
        assertNotNull(responseBody);
        RestResponse response2 = OBJECT_MAPPER.readValue(responseBody, RestResponse.class);
        assertEquals(response2.getData().get(0).get("strField"), PropertyUtils.getProperty(rec1, "strField"));
    }
    // DELETE
    // delete 5 records using REST
    getLogger().info("Delete instances via REST");
    List list2 = dataService.retrieveAll();
    for (int i = 0; i < 5; i++) {
        Object record2 = list2.get(i);
        long id = (long) PropertyUtils.getProperty(record2, "id");
        HttpDelete delete = new HttpDelete(ENTITY_URL + "/" + id);
        HttpResponse response2 = getHttpClient().execute(delete);
        assertNotNull(response2);
        assertEquals(HttpStatus.SC_OK, response2.getStatusLine().getStatusCode());
        assertNull(dataService.findById(id));
    }
    assertEquals(dataService.retrieveAll().size(), 6);
}
Also used : Order(org.motechproject.mds.util.Order) HttpPost(org.apache.http.client.methods.HttpPost) HttpDelete(org.apache.http.client.methods.HttpDelete) RestResponse(org.motechproject.mds.rest.RestResponse) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler) HttpResponse(org.apache.http.HttpResponse) MotechDataService(org.motechproject.mds.service.MotechDataService) HttpPut(org.apache.http.client.methods.HttpPut) StringEntity(org.apache.http.entity.StringEntity) QueryParams(org.motechproject.mds.query.QueryParams) Arrays.asList(java.util.Arrays.asList) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with MotechDataService

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

the class InstanceServiceImpl method saveInstance.

@Override
@Transactional
public Object saveInstance(EntityRecord entityRecord, Long deleteValueFieldId) {
    EntityDto entity = getEntity(entityRecord.getEntitySchemaId());
    validateCredentials(entity);
    validateNonEditableProperty(entity);
    List<FieldDto> entityFields = getEntityFields(entityRecord.getEntitySchemaId());
    try {
        MotechDataService service = getServiceForEntity(entity);
        Class<?> entityClass = getEntityClass(entity);
        boolean newObject = entityRecord.getId() == null;
        Object instance;
        if (newObject) {
            instance = newInstanceFromEntityRecord(entityClass, entityFields, entityRecord.getFields(), service);
            return service.create(instance);
        } else {
            instance = service.retrieve(ID_FIELD_NAME, entityRecord.getId());
            if (instance == null) {
                throw new ObjectNotFoundException(entity.getName(), entityRecord.getId());
            }
            updateFields(instance, entityRecord.getFields(), service, deleteValueFieldId, true);
            return service.update(instance);
        }
    } catch (Exception e) {
        if (entityRecord.getId() == null) {
            throw new ObjectCreateException(entity.getName(), e);
        } else {
            throw new ObjectUpdateException(entity.getName(), entityRecord.getId(), e);
        }
    }
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) ObjectNotFoundException(org.motechproject.mds.exception.object.ObjectNotFoundException) MotechDataService(org.motechproject.mds.service.MotechDataService) ObjectReadException(org.motechproject.mds.exception.object.ObjectReadException) FieldReadOnlyException(org.motechproject.mds.exception.field.FieldReadOnlyException) InvocationTargetException(java.lang.reflect.InvocationTargetException) LookupNotFoundException(org.motechproject.mds.exception.lookup.LookupNotFoundException) ObjectUpdateException(org.motechproject.mds.exception.object.ObjectUpdateException) LookupExecutorException(org.motechproject.mds.exception.lookup.LookupExecutorException) SecurityException(org.motechproject.mds.exception.object.SecurityException) EntityNotFoundException(org.motechproject.mds.exception.entity.EntityNotFoundException) LookupExecutionException(org.motechproject.mds.exception.lookup.LookupExecutionException) ObjectCreateException(org.motechproject.mds.exception.object.ObjectCreateException) FieldNotFoundException(org.motechproject.mds.exception.field.FieldNotFoundException) ObjectNotFoundException(org.motechproject.mds.exception.object.ObjectNotFoundException) InstanceNotFoundException(javax.management.InstanceNotFoundException) EntityInstancesNonEditableException(org.motechproject.mds.exception.entity.EntityInstancesNonEditableException) CannotCompileException(javassist.CannotCompileException) ObjectCreateException(org.motechproject.mds.exception.object.ObjectCreateException) ObjectUpdateException(org.motechproject.mds.exception.object.ObjectUpdateException) FieldDto(org.motechproject.mds.dto.FieldDto) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with MotechDataService

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

the class InstanceServiceImpl method revertPreviousVersion.

@Override
public void revertPreviousVersion(Long entityId, Long instanceId, Long historyId) {
    validateNonEditableProperty(entityId);
    EntityDto entity = getEntity(entityId);
    MotechDataService service = getServiceForEntity(entity);
    service.revertToHistoricalRevision(instanceId, historyId);
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) MotechDataService(org.motechproject.mds.service.MotechDataService)

Example 4 with MotechDataService

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

the class InstanceServiceImpl method getHistoryRecord.

@Override
public HistoryRecord getHistoryRecord(Long entityId, Long instanceId, Long historyId) {
    EntityDto entity = getEntity(entityId);
    validateCredentialsForReading(entity);
    MotechDataService service = getServiceForEntity(entity);
    Object instance = service.retrieve(ID_FIELD_NAME, instanceId);
    Object historyInstance = historyService.getSingleHistoryInstance(instance, historyId);
    return convertToHistoryRecord(historyInstance, entity, instanceId, service);
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) MotechDataService(org.motechproject.mds.service.MotechDataService)

Example 5 with MotechDataService

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

the class InstanceServiceImpl method getInstanceField.

@Override
public Object getInstanceField(Long entityId, Long instanceId, String fieldName) throws InstanceNotFoundException {
    EntityDto entity = getEntity(entityId);
    MotechDataService service = getServiceForEntity(entity);
    validateCredentialsForReading(entity);
    Object instance = service.findById(instanceId);
    if (instance == null) {
        throw new InstanceNotFoundException(String.format("Cannot find instance with id: %d", instanceId));
    }
    return service.getDetachedField(instance, fieldName);
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) InstanceNotFoundException(javax.management.InstanceNotFoundException) 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