Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagejs
linenumberstrue
{
"uuid": "0ca78602a9b3acca-737f7cd3-408d4b2a-8ceda8ea-386ad12367db2c37589b2ced",
"name": "Malemales 19aged 20 - 2330",
"description": "Examplemales cohortaged of20 male patients between the age of 19 and 23 who are alive!- 30"
}
  • REST resource location:
Code Block
http://localhost:8081demo2.muzima.org/openmrs-standalone/ws/rest/v1/cohort/0ca78602a9b3acca-737f7cd3-408d4b2a-8ceda8ea-386ad12367db2c37589b2ced?v=custom:(uuid,name,description)

...

Code Block
languagejava
themeEmacs
linenumberstrue
import com.muzima.search.api.model.object.Searchable;
public class Cohort extends Searchable {
	private String uuid;
	private String name;
	private String description;
	public String getUuid() {
		return uuid;
	}
	public void setUuid(final String uuid) {
		this.uuid = uuid;
	}
	public String getName() {
		return name;
	}
	public void setName(final String name) {
		this.name = name;
	}
	public void getDescription() {
		return description;
	}
	public void setDescription(final String description) {
		this.description = description;
	}
}
  • Create CohortResolver class implementing the top level Resolver class. The cohort resolver will help the API to decide where to download the REST resource and what authentication needed to access it.
Code Block
languagejava
themeEmacs
linenumberstrue
import com.muzima.search.api.model.resolver.Resolver;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Map;
public class SearchCohortResolver extends BaseOpenmrsResolver {
	private static final String REPRESENTATION = "?v=custom:(uuid,name,description)";
	/**
 	* Return the full REST resource based on the parameters passed to the method.
 	*
 	* @param resourceParams the parameters of the resource to resolved.
 	* @return full uri to the REST resource.
	*/


	@Override
	public String resolve(final Map<String, String> resourceParams) throws IOException {
		StringBuilder paramBuilder = new StringBuilder();
		for (String key : resourceParams.keySet()) {
    		paramBuilder.append("&").append(key).append("=").append(URLEncoder.encode(resourceParams.get(key), "UTF-8"));
		}
		return getConfiguration().getServer() + "/ws/rest/v1/cohort" + REPRESENTATION + paramBuilder.toString();
	}
}

 

  • Create CohortAlgorithm class implementing the top level Algorithm class. The Algorithm class define a two way conversion between the REST resource's representation into the correct object.
Code Block
languagejava
themeEmacs
linenumberstrue
import com.jayway.jsonpath.JsonPath;
import com.muzima.api.model.Cohort;
import com.muzima.search.api.model.object.Searchable;
import com.muzima.search.api.model.serialization.Algorithmutil.JsonUtils;
import net.minidev.json.JSONObject;

import java.io.IOException;

public class CohortAlgorithm extends AlgorithmBaseOpenmrsAlgorithm {
	private static final String REPRESENTATION = "?v=custom:(uuid,name,voided,size)";


	/**
	* Implementation of this method will define how the object will be serialized from the String representation.
	*
	* @param jsonserialized the string representation
	* @return the concrete object
	*/


	@Override
	public Searchable deserialize(final String jsonserialized, final boolean isFullSerialization) throws IOException {
		Cohort cohort = new Cohort();
Object jsonObject = JsonPath.read(json		cohort.setUuid(JsonUtils.readAsString(serialized, "$['uuid']"));
String uuid = JsonPath 	cohort.read(jsonObjectsetName(JsonUtils.readAsString(serialized, "$['uuidname']");
cohort.setUuid(uuid);
String name = JsonPath 	cohort.read(jsonObjectsetVoided(JsonUtils.readAsBoolean(serialized, "$['namevoided']");
cohort.setName(name);
String description = JsonPath 	cohort.read(jsonObjectsetSize(JsonUtils.readAsInteger(serialized, "$['descriptionsize']");
cohort.setDescription(description);
    	return cohort;
	}
	/**
	* Implementation of this method will define how the object will be de-serialized into the String representation.
	*
	* @param object
the object
	* @return the string representation
	*/

	@Override
	public String serialize(final Searchable object, final boolean isFullSerialization) throws IOException {
// serialize the minimum needed to identify an object for deletion purposes.
		Cohort cohort = (Cohort) object;
		JSONObject jsonObject = new JSONObject();
jsonObject		JsonUtils.putwriteAsString(jsonObject, "uuid", cohort.getUuid());
jsonObject		JsonUtils.putwriteAsString(jsonObject, "name", cohort.getName());
		JsonUtils.writeAsBoolean(jsonObject.put("description, "voided", cohort.isVoided());
		JsonUtils.writeAsInteger(jsonObject, "size", cohort.getDescriptiongetSize());
		return jsonObject.toJSONString();
	}
}

 

  • Create configuration to tie everything together. The configuration have the following element:
    • resource.name: Obviously, the name of the resource. The value of this field will be used to uniquely identify which a the resource in the pool of all registered resources inside ServiceContext class.
    • node.root: The root element in the REST resource's payload JSONPath expression. JSONPath is XPath expression for JSON data. See more about JSONPath.
    • resource.object: The target class of the REST resource's payload conversion process.
    • algorithm.class: The class which will be used to perform two conversion from the REST resource's payload into the correct object (the correct object is represented inin resource.object) and vice versa.
    • resolver.class: The class which will point to the location where the API can get the REST resource and the authentication needed to access it.
    • field.unique: An element in the REST resource's payload which can be used to uniquely identify the resource among collection of resources.
    • field.searchable: Any elements in the REST resource's payload which are searchable.
Code Block
languagejava
themeEmacs
linenumberstrue
{
	"configurations": [
		{
			"resource.name": "Cohort Resource",
			"node.root": "$",
			"resource.object": "Cohort",
			"algorithm.class": "CohortAlgorithm",
			"resolver.class": "CohortResolver",
			"field.unique": "uuid",
			"field.searchable":
			{
				"uuid": "$['uuid']",
				"name": "$['name']",
				"description": "$['description']"
			}
		}
	]
}

 

  • Create module to specify the location where lucene index will be stored
Code Block
languagejava
themeEmacs
linenumberstrue
import com.google.inject.AbstractModule;
import com.google.inject.name.Names;
public class LuceneModule extends AbstractModule {
	public static final String LUCENE_DIRECTORY = File.separator + "lucene";
	/**
	* Configures a {@link com.google.inject.Binder} via the exposed methods.
	*/
	@Override
	protected void configure() {
		String tmpDirectory = System.getProperty("java.io.tmpdir");
		bind(String.class)
		.annotatedWith(Names.named("configuration.lucene.directory"))
		.toInstance(tmpDirectory + LUCENE_DIRECTORY);
		bind(String.class)
		.annotatedWith(Names.named("configuration.lucene.document.key"))
		.toInstance("uuid");
	}
}

 

  • Download and save the object locally

...