Skip to content

Commit

Permalink
TaskDefinition and TaskAction REST Implementation
Browse files Browse the repository at this point in the history
update

TaskDefinition and TaskAction Implementation
  • Loading branch information
suthagar23 committed Aug 30, 2017
1 parent 7a39d69 commit 1bb91de
Show file tree
Hide file tree
Showing 9 changed files with 1,062 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
/**
* 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 io.swagger.models.Model;
import org.openmrs.api.APIException;
import org.openmrs.module.webservices.helper.TaskAction;
import org.openmrs.module.webservices.helper.TaskServiceWrapper;
import org.openmrs.module.webservices.rest.SimpleObject;
import org.openmrs.module.webservices.rest.web.ConversionUtil;
import org.openmrs.module.webservices.rest.web.RequestContext;
import org.openmrs.module.webservices.rest.web.RestConstants;
import org.openmrs.module.webservices.rest.web.annotation.PropertyGetter;
import org.openmrs.module.webservices.rest.web.annotation.Resource;
import org.openmrs.module.webservices.rest.web.representation.Representation;
import org.openmrs.module.webservices.rest.web.resource.api.Creatable;
import org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource;
import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingResourceDescription;
import org.openmrs.module.webservices.rest.web.response.IllegalRequestException;
import org.openmrs.module.webservices.rest.web.response.ResourceDoesNotSupportOperationException;
import org.openmrs.module.webservices.rest.web.response.ResponseException;
import org.openmrs.scheduler.SchedulerException;
import org.openmrs.scheduler.TaskDefinition;

import java.util.ArrayList;
import java.util.Collection;

@Resource(name = RestConstants.VERSION_1 + "/taskaction", supportedClass = TaskAction.class, supportedOpenmrsVersions = {
"1.8.*", "1.9.*", "1.10.*", "1.11.*", "1.12.*", "2.0.*", "2.1.*" })
public class TaskActionResource1_8 extends BaseDelegatingResource<TaskAction> implements Creatable {

private TaskServiceWrapper taskServiceWrapper = new TaskServiceWrapper();

public void setTaskServiceWrapper(TaskServiceWrapper taskServiceWrapper) {
this.taskServiceWrapper = taskServiceWrapper;
}

@Override
public Object create(SimpleObject post, RequestContext context) throws ResponseException {
TaskAction action = newDelegate();
setConvertedProperties(action, post, getCreatableProperties(), true);

Collection<TaskDefinition> taskDefinitions;
if (action.isAllTasks()) {
taskDefinitions = taskServiceWrapper.getRegisteredTasks();
action.setTasks(new ArrayList<TaskDefinition>(taskDefinitions));
} else {
taskDefinitions = action.getTasks();
}

TaskAction.Action actionType = action.getAction();
if (actionType != TaskAction.Action.RESCHEDULEALLTASKS) {
if (taskDefinitions == null || taskDefinitions.isEmpty()) {
throw new IllegalRequestException("Cannot execute action " + actionType
+ " on empty set of task definitions.");
}
}

switch (action.getAction()) {
case SCHEDULETASK:
scheduleTasks(taskDefinitions);
break;
case RESCHEDULETASK:
reScheduleTasks(taskDefinitions);
break;
case DELETE:
deleteTasks(taskDefinitions);
break;
case SHUTDOWNTASK:
shutDownTasks(taskDefinitions);
break;
case RESCHEDULEALLTASKS:
reScheduleAllTasks();
break;
}
return ConversionUtil.convertToRepresentation(action, Representation.DEFAULT);
}

private void scheduleTasks(Collection<TaskDefinition> taskDefs) {
for (TaskDefinition taskDef : taskDefs) {
try {
taskServiceWrapper.scheduleTask(taskDef);
}
catch (SchedulerException e) {
throw new APIException("Errors occurred while scheduling task", e);
}
}
}

private void shutDownTasks(Collection<TaskDefinition> taskDefs) {
for (TaskDefinition taskDef : taskDefs) {
try {
taskServiceWrapper.shutDownTask(taskDef);
}
catch (SchedulerException e) {
throw new APIException("Errors occurred while shutdowning task", e);
}
}
}

// Stop and start a set of scheduled tasks.
private void reScheduleTasks(Collection<TaskDefinition> taskDefs) {
for (TaskDefinition taskDef : taskDefs) {
try {
taskServiceWrapper.reScheduleTask(taskDef);
}
catch (SchedulerException e) {
throw new APIException("Errors occurred while rescheduling task", e);
}
}
}

// Stop and start all the tasks.
private void reScheduleAllTasks() {
try {
taskServiceWrapper.reScheduleAllTasks();
}
catch (SchedulerException e) {
throw new APIException("Errors occurred while rescheduling all tasks", e);
}

}

private void deleteTasks(Collection<TaskDefinition> taskDefs) {
for (TaskDefinition taskDef : taskDefs) {
try {
taskServiceWrapper.deleteTask(taskDef);
}
catch (SchedulerException e) {
throw new APIException("Errors occurred while deleting task", e);
}
}
}

@Override
public TaskAction newDelegate() {
return new TaskAction();
}

@Override
public TaskAction save(TaskAction delegate) {
throw new UnsupportedOperationException("TaskAction cannot be saved");
}

@Override
public DelegatingResourceDescription getRepresentationDescription(Representation rep) {
DelegatingResourceDescription description = new DelegatingResourceDescription();
description.addProperty("tasks", Representation.REF);
description.addProperty("action", "action");
return description;
}

@Override
public TaskAction getByUniqueId(String uniqueId) {
throw new UnsupportedOperationException("TaskAction can not get by id");
}

@Override
protected void delete(TaskAction delegate, String reason, RequestContext context) throws ResponseException {
throw new UnsupportedOperationException("TaskAction can not be deleted");
}

@Override
public void purge(TaskAction delegate, RequestContext context) throws ResponseException {
throw new UnsupportedOperationException("TaskAction can not be purged");
}

@Override
public DelegatingResourceDescription getCreatableProperties() throws ResourceDoesNotSupportOperationException {
DelegatingResourceDescription description = new DelegatingResourceDescription();
description.addProperty("tasks");
description.addProperty("allTasks");
description.addRequiredProperty("action", "action");
return description;
}

@Override
public Model getCREATEModel(Representation rep) {
return null;
}

/**
* Converter does not handle getters starting with 'is' instead of 'get'
*/
@PropertyGetter("allModules")
public Boolean isAllModules(TaskAction action) {
return action.isAllTasks();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/**
* 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.helper.TaskServiceWrapper;
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.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.ArrayList;
import java.util.List;

/**
* 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> {

private TaskServiceWrapper taskServiceWrapper = new TaskServiceWrapper();

public void setTaskServiceWrapper(TaskServiceWrapper taskServiceWrapper) {
this.taskServiceWrapper = taskServiceWrapper;
}

@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);
description.addSelfLink();
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");
description.addSelfLink();
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.DelegatingCrudResource#save(java.lang.Object)
*/
@Override
public TaskDefinition save(TaskDefinition taskDefinition) {
taskServiceWrapper.saveTaskDefinition(taskDefinition);
return taskDefinition;
}

@Override
public TaskDefinition getByUniqueId(String uniqueId) {
TaskDefinition taskDefinition = taskServiceWrapper.getTaskByName(uniqueId);
if (taskDefinition == null) {
taskDefinition = taskServiceWrapper.getTaskById(Integer.parseInt(uniqueId));
}
return taskDefinition;
}

@Override
public void purge(TaskDefinition delegate, RequestContext context) throws ResponseException {
throw new UnsupportedOperationException("TaskAction cannot be purged");
}

/**
* @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>(taskServiceWrapper.getRegisteredTasks()),
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)
* It will return only the scheduled tasks, if query contains string as scheduled otherwise
* it will return the registered tasks
*/
@Override
protected PageableResult doSearch(RequestContext context) {
String query = context.getParameter("q");
List<TaskDefinition> taskDefinitions = null;
if (query == "registered") {
taskDefinitions = (ArrayList<TaskDefinition>) taskServiceWrapper.getRegisteredTasks();
} else {
taskDefinitions = (ArrayList<TaskDefinition>) taskServiceWrapper.getScheduledTasks();
}
return new NeedsPaging<TaskDefinition>(taskDefinitions, context);
}

}
Loading

0 comments on commit 1bb91de

Please sign in to comment.