|
| 1 | +package org.openmrs.module.ugandaemrsync.web.resource; |
| 2 | + |
| 3 | +import io.swagger.models.Model; |
| 4 | +import io.swagger.models.ModelImpl; |
| 5 | +import io.swagger.models.properties.*; |
| 6 | +import org.apache.commons.logging.Log; |
| 7 | +import org.openmrs.api.context.Context; |
| 8 | +import org.openmrs.module.ugandaemrsync.api.UgandaEMRSyncService; |
| 9 | +import org.openmrs.module.webservices.docs.swagger.core.property.EnumProperty; |
| 10 | +import org.openmrs.module.webservices.helper.TaskAction; |
| 11 | +import org.openmrs.module.webservices.rest.web.RequestContext; |
| 12 | +import org.openmrs.module.webservices.rest.web.RestConstants; |
| 13 | +import org.openmrs.module.webservices.rest.web.annotation.Resource; |
| 14 | +import org.openmrs.module.webservices.rest.web.representation.DefaultRepresentation; |
| 15 | +import org.openmrs.module.webservices.rest.web.representation.FullRepresentation; |
| 16 | +import org.openmrs.module.webservices.rest.web.representation.RefRepresentation; |
| 17 | +import org.openmrs.module.webservices.rest.web.representation.Representation; |
| 18 | +import org.openmrs.module.webservices.rest.web.resource.api.PageableResult; |
| 19 | +import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource; |
| 20 | +import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingResourceDescription; |
| 21 | +import org.openmrs.module.webservices.rest.web.resource.impl.NeedsPaging; |
| 22 | +import org.openmrs.module.webservices.rest.web.response.ResourceDoesNotSupportOperationException; |
| 23 | +import org.openmrs.module.webservices.rest.web.response.ResponseException; |
| 24 | +import org.openmrs.scheduler.SchedulerService; |
| 25 | +import org.openmrs.scheduler.Task; |
| 26 | +import org.openmrs.scheduler.TaskDefinition; |
| 27 | + |
| 28 | +import java.util.*; |
| 29 | + |
| 30 | +@Resource(name = RestConstants.VERSION_1 + "/ugandaemrsynctask", supportedClass = TaskDefinition.class, supportedOpenmrsVersions = {"1.9.* - 9.*"}) |
| 31 | +public class UgandaEMRSyncTaskResource extends DelegatingCrudResource<TaskDefinition> { |
| 32 | + |
| 33 | + @Override |
| 34 | + public TaskDefinition newDelegate() { |
| 35 | + throw new ResourceDoesNotSupportOperationException("Operation not supported"); |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public TaskDefinition save(TaskDefinition TaskDefinition) { |
| 40 | + throw new ResourceDoesNotSupportOperationException("Operation not supported"); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public TaskDefinition getByUniqueId(String uniqueId) { |
| 45 | + throw new ResourceDoesNotSupportOperationException("Operation not supported"); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public NeedsPaging<TaskDefinition> doGetAll(RequestContext context) throws ResponseException { |
| 50 | + throw new ResourceDoesNotSupportOperationException("Operation not supported"); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public List<Representation> getAvailableRepresentations() { |
| 55 | + return Arrays.asList(Representation.DEFAULT, Representation.FULL); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public DelegatingResourceDescription getRepresentationDescription(Representation rep) { |
| 60 | + DelegatingResourceDescription description = new DelegatingResourceDescription(); |
| 61 | + description.addProperty("name"); |
| 62 | + description.addProperty("taskInstance", Representation.REF); |
| 63 | + description.addProperty("startTime"); |
| 64 | + description.addProperty("lastExecutionTime"); |
| 65 | + description.addProperty("startTimePattern"); |
| 66 | + description.addProperty("started"); |
| 67 | + description.addProperty("taskClass"); |
| 68 | + description.addProperty("taskClass"); |
| 69 | + |
| 70 | + return description; |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + protected void delete(TaskDefinition syncFfhirProfile, String s, RequestContext requestContext) throws ResponseException { |
| 75 | + throw new ResourceDoesNotSupportOperationException("Operation not supported"); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void purge(TaskDefinition syncFfhirProfile, RequestContext requestContext) throws ResponseException { |
| 80 | + throw new ResourceDoesNotSupportOperationException("Operation not supported"); |
| 81 | + } |
| 82 | + @Override |
| 83 | + protected PageableResult doSearch(RequestContext context) { |
| 84 | + Collection<TaskDefinition> allSchedules=Context.getService(SchedulerService.class).getScheduledTasks(); |
| 85 | + List<TaskDefinition> relevantSchedules=new ArrayList<>(); |
| 86 | + |
| 87 | + String module = context.getParameter("module"); |
| 88 | + |
| 89 | + for(TaskDefinition taskDefinition:allSchedules){ |
| 90 | + if(taskDefinition.getTaskClass().contains("ugandaemr")){ |
| 91 | + relevantSchedules.add(taskDefinition); |
| 92 | + } |
| 93 | + } |
| 94 | + return new NeedsPaging<TaskDefinition>(relevantSchedules, context); |
| 95 | + } |
| 96 | +} |
0 commit comments