Skip to content

Commit

Permalink
Initial commit for the TaskDefinition Resource
Browse files Browse the repository at this point in the history
  • Loading branch information
suthagar23 committed Aug 16, 2017
1 parent e12f8c7 commit cbd8195
Showing 1 changed file with 172 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<TaskDefinition> {

@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<TaskDefinition> doGetAll(RequestContext context) throws ResponseException {
return new NeedsPaging<TaskDefinition>(new ArrayList<TaskDefinition>(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<TaskDefinition> taskDefinitions = null;
if (query == "scheduled") {
taskDefinitions = (ArrayList<TaskDefinition>) Context.getSchedulerService().getScheduledTasks();
} else {
taskDefinitions = (ArrayList<TaskDefinition>) Context.getSchedulerService().getRegisteredTasks();
}
return new NeedsPaging<TaskDefinition>(taskDefinitions, context);
}

}

0 comments on commit cbd8195

Please sign in to comment.