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);
}
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);
}
}
}
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);
}
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);
}
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);
}
Aggregations