From cbd8195d830f3c220cbdba1a07d3c9eaade15336 Mon Sep 17 00:00:00 2001 From: suthagar23 Date: Wed, 16 Aug 2017 22:34:51 +0530 Subject: [PATCH] Initial commit for the TaskDefinition Resource --- .../openmrs1_8/TaskDefinitionResource1_8.java | 172 ++++++++++++++++++ 1 file changed, 172 insertions(+) create mode 100644 omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/TaskDefinitionResource1_8.java diff --git a/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/TaskDefinitionResource1_8.java b/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/TaskDefinitionResource1_8.java new file mode 100644 index 0000000000..87b2431ef2 --- /dev/null +++ b/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/TaskDefinitionResource1_8.java @@ -0,0 +1,172 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under + * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. + * + * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS + * graphic logo is a trademark of OpenMRS Inc. + */ +package org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_8; + +import org.openmrs.module.webservices.rest.web.annotation.PropertyGetter; +import org.openmrs.module.webservices.rest.web.representation.RefRepresentation; +import org.openmrs.module.webservices.rest.web.resource.api.PageableResult; +import org.openmrs.module.webservices.rest.web.resource.impl.NeedsPaging; +import org.openmrs.scheduler.TaskDefinition; +import org.openmrs.api.context.Context; +import org.openmrs.module.webservices.rest.web.RequestContext; +import org.openmrs.module.webservices.rest.web.RestConstants; +import org.openmrs.module.webservices.rest.web.annotation.Resource; +import org.openmrs.module.webservices.rest.web.representation.DefaultRepresentation; +import org.openmrs.module.webservices.rest.web.representation.FullRepresentation; +import org.openmrs.module.webservices.rest.web.representation.Representation; +import org.openmrs.module.webservices.rest.web.resource.impl.MetadataDelegatingCrudResource; +import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingResourceDescription; +import org.openmrs.module.webservices.rest.web.response.ResponseException; + +import java.util.*; + +/** + * Resource for Task Definitions, supporting standard CRUD operations + */ +@Resource(name = RestConstants.VERSION_1 + "/taskdefinition", supportedClass = TaskDefinition.class, supportedOpenmrsVersions = { + "1.8.*", "1.9.*", "1.10.*", "1.11.*", "1.12.*", "2.0.*", "2.1.*" }) +public class TaskDefinitionResource1_8 extends MetadataDelegatingCrudResource { + + @Override + public TaskDefinition newDelegate() { + return new TaskDefinition(); + } + + @Override + public DelegatingResourceDescription getRepresentationDescription(Representation rep) { + if (rep instanceof DefaultRepresentation) { + DelegatingResourceDescription description = new DelegatingResourceDescription(); + description.addProperty("uuid"); + description.addProperty("name"); + description.addProperty("description"); + description.addProperty("taskClass"); + description.addProperty("startTime"); + description.addProperty("started"); + description.addLink("ref", ".?v=" + RestConstants.REPRESENTATION_REF); + description.addLink("full", ".?v=" + RestConstants.REPRESENTATION_FULL); + return description; + } else if (rep instanceof FullRepresentation) { + DelegatingResourceDescription description = new DelegatingResourceDescription(); + description.addProperty("uuid"); + description.addProperty("name"); + description.addProperty("description"); + description.addProperty("taskClass"); + description.addProperty("startTime"); + description.addProperty("lastExecutionTime"); + description.addProperty("repeatInterval"); + description.addProperty("startOnStartup"); + description.addProperty("startTimePattern"); + description.addProperty("started"); + description.addProperty("properties"); + description.addSelfLink(); + description.addLink("ref", ".?v=" + RestConstants.REPRESENTATION_REF); + return description; + } else if (rep instanceof RefRepresentation) { + DelegatingResourceDescription description = new DelegatingResourceDescription(); + description.addProperty("uuid"); + description.addProperty("name"); + description.addProperty("description"); + description.addProperty("taskClass"); + return description; + } + return null; + } + + /** + * @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#getCreatableProperties() + */ + @Override + public DelegatingResourceDescription getCreatableProperties() { + DelegatingResourceDescription description = new DelegatingResourceDescription(); + description.addRequiredProperty("name"); + description.addRequiredProperty("description"); + description.addRequiredProperty("taskClass"); + description.addRequiredProperty("startTime"); + description.addRequiredProperty("repeatInterval"); + description.addRequiredProperty("startOnStartup"); + description.addRequiredProperty("properties"); + return description; + } + + /** + * @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#getUpdatableProperties() + */ + @Override + public DelegatingResourceDescription getUpdatableProperties() { + DelegatingResourceDescription description = new DelegatingResourceDescription(); + description.addRequiredProperty("name"); + description.addRequiredProperty("description"); + description.addRequiredProperty("taskClass"); + description.addRequiredProperty("repeatInterval"); + description.addRequiredProperty("startOnStartup"); + description.addRequiredProperty("properties"); + return description; + } + + /** + * @see org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource#save(java.lang.Object) + */ + @Override + public TaskDefinition save(TaskDefinition taskDefinition) { + Context.getSchedulerService().saveTaskDefinition(taskDefinition); + return Context.getSchedulerService().getTaskByName(taskDefinition.getName()); + } + + @Override + public TaskDefinition getByUniqueId(String uniqueId) { + TaskDefinition taskDefinition = Context.getSchedulerService().getTaskByName(uniqueId); + if (taskDefinition == null) { + taskDefinition = Context.getSchedulerService().getTask(Integer.parseInt(uniqueId)); + } + return taskDefinition; + } + + @Override + public void purge(TaskDefinition delegate, RequestContext context) throws ResponseException { + return; + } + + /** + * @see org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource#doGetAll(org.openmrs.module.webservices.rest.web.RequestContext) + */ + @Override + public NeedsPaging doGetAll(RequestContext context) throws ResponseException { + return new NeedsPaging(new ArrayList(Context.getSchedulerService() + .getScheduledTasks()), context); + } + + @PropertyGetter("uuid") + public static String getUuid(TaskDefinition instance) { + return instance.getUuid(); + } + + @PropertyGetter("display") + public static String getDisplay(TaskDefinition instance) { + return instance.getName(); + } + + /** + * @see org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource#doSearch(org.openmrs.module.webservices.rest.web.RequestContext) + * A query string and/or a tag uuid can be passed in; if both are passed in, returns an + * intersection of the results; excludes retired locations + */ + @Override + protected PageableResult doSearch(RequestContext context) { + String query = context.getParameter("q"); + List taskDefinitions = null; + if (query == "scheduled") { + taskDefinitions = (ArrayList) Context.getSchedulerService().getScheduledTasks(); + } else { + taskDefinitions = (ArrayList) Context.getSchedulerService().getRegisteredTasks(); + } + return new NeedsPaging(taskDefinitions, context); + } + +}