package cl.ciochile.test;
import org.apache.http.entity.StringEntity;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import cl.ciochile.victoraravena.ApiAuthRest;
/**
*
* @author Victor Aravena victor.aravena@ciochile.cl
*
*/
public class Test {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
/*
* SET VALUE FOR CONNECT TO OPENMRS
*/
ApiAuthRest.setURLBase("http://DOMAIN/openmrs/ws/rest/v1/");
ApiAuthRest.setUsername("USER");
ApiAuthRest.setPassword("PASS");
/*
* Edit the value de gender and birthdate the one record of person
*/
StringEntity inputJsonEdit = new StringEntity(
"{\"gender\":\"F\",\"birthdate\":\"1988-08-08T00:00:00.000-0300\"}"
);
inputJsonEdit.setContentType("application/json");
System.out.println("EditPerson = " +
ApiAuthRest.getRequestPost(
"person/d0c9bd52-99dd-11e1-acb0-00188b78ce12",
inputJsonEdit));
System.out.println("########################");
/*
* Edit the value de gender and birthdate the one record of person
*/
StringEntity inputAddPerson = new StringEntity(
"{\"names\":[{\"givenName\": \"John\",\"familyName\":\"Soto\"}],\"gender\":\"F\",\"age\":40}"
);
inputAddPerson.setContentType("application/json");
System.out.println("AddPerson = " +
ApiAuthRest.getRequestPost(
"person",
inputAddPerson));
System.out.println("########################");
/*
* Example how parse json return session
*/
Object objSessionJson = JSONValue.parse( ApiAuthRest.getRequestGet("session"));
JSONObject jsonObjectSessionJson= (JSONObject) objSessionJson;
String sessionId = (String) jsonObjectSessionJson.get("sessionId");
Boolean authenticated = (Boolean) jsonObjectSessionJson.get("authenticated");
System.out.println("Session:"+sessionId+" Authenticated:"+authenticated);
System.out.println("########################");
System.out.println("Search the persons that have name JOHN");
Object obj = JSONValue.parse( ApiAuthRest.getRequestGet("person?q=john"));
JSONObject jsonObject = (JSONObject) obj;
JSONArray arrayResult = (JSONArray) jsonObject.get("results");
/*
* Leemo primera posicion
*/
System.out.println("########################");
int largoArray = arrayResult.size();
int contador;
for(contador=0; contador < largoArray; contador ++){
JSONObject registro = (JSONObject) arrayResult.get(contador);
String uuid = (String) registro.get("uuid");
String display = (String) registro.get("display");
System.out.println("Rows "+ contador + " => Result Persons UUID:" + uuid +" Display:"+display);
//Show ROWS LINKS
JSONArray arrayResultLinks = (JSONArray) registro.get("links");
int largoArrayLinks = arrayResultLinks.size();
int contadorLinks;
for(contadorLinks=0; contadorLinks < largoArrayLinks; contadorLinks ++){
JSONObject registroLink = (JSONObject) arrayResultLinks.get(contadorLinks);
String uri = (String) registroLink.get("uri");
String rel = (String) registroLink.get("rel");
System.out.println("==>Record Row "+ contador + "."+ contadorLinks
+" => URI:" + uri +" REL:"+rel);
}
System.out.println("########################");
}
}
}