Skip to content

Commit

Permalink
Add new client JSON form post endpoint
Browse files Browse the repository at this point in the history
- Add clientForm/add POST endpoint that accepts multipart/form-data that adheres to the specification here opensrp/opensrp-server#497
  • Loading branch information
ekigamba committed Mar 19, 2020
1 parent 76df950 commit b7674e7
Showing 1 changed file with 58 additions and 17 deletions.
75 changes: 58 additions & 17 deletions src/main/java/org/opensrp/web/rest/ClientFormResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.github.zafarkhaja.semver.Version;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import edu.umd.cs.findbugs.annotations.NonNull;
import org.apache.http.util.TextUtils;
import org.joda.time.DateTime;
import org.opensrp.domain.IdVersionTuple;
Expand All @@ -17,13 +16,13 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

@Controller
Expand Down Expand Up @@ -63,7 +62,7 @@ private ResponseEntity<String> searchForFormByFormVersion(@RequestParam(value =

// Check if the form identifier with that version exists
ClientFormMetadata clientFormMetadata = clientFormService.getClientFormMetatdataByIdentifierAndVersion(formIdentifier, formVersion);
CompleteClientForm completeClientForm = null;
ClientFormService.CompleteClientForm completeClientForm = null;

int formId;

Expand Down Expand Up @@ -97,7 +96,7 @@ private ResponseEntity<String> searchForFormByFormVersion(@RequestParam(value =
return new ResponseEntity<>((String) null, HttpStatus.INTERNAL_SERVER_ERROR);
}

completeClientForm = new CompleteClientForm(clientForm, clientFormMetadata);
completeClientForm = new ClientFormService.CompleteClientForm(clientForm, clientFormMetadata);

return new ResponseEntity<>(gson.toJson(completeClientForm), HttpStatus.OK);
}
Expand Down Expand Up @@ -127,21 +126,63 @@ public List<String> requiredProperties() {
public ClientForm create(ClientForm entity) {
return null;
}

@RequestMapping(headers = { "Accept=multipart/form-data" }, method = RequestMethod.POST, value = "/add")
@ResponseBody
private ResponseEntity<String> addClientForm(@RequestParam("form_version") String formVersion,
@RequestParam("form_identifier") String formIdentifier,
@RequestParam("form_name") String formName,
@RequestParam("form") MultipartFile jsonFile,
@RequestParam(required = false) String module) {
if (TextUtils.isEmpty(formVersion) || TextUtils.isEmpty(formIdentifier) || TextUtils.isEmpty(formName) || jsonFile.isEmpty()) {
return new ResponseEntity<>("Required params is empty", HttpStatus.BAD_REQUEST);
}

@Override
public ClientForm update(ClientForm entity) {
return null;
}
// TOOD: Check the string is a valid version
Version formVersionV = Version.valueOf(formVersion);
if (!jsonFile.getOriginalFilename().contains(".json")) {
return new ResponseEntity<>("The form is not a JSON file", HttpStatus.BAD_REQUEST);
}

if (jsonFile.isEmpty()) {
return new ResponseEntity<>("File is empty", HttpStatus.BAD_REQUEST);
}

ClientForm clientForm = new ClientForm();

public static class CompleteClientForm {
byte[] bytes;
try {
bytes = jsonFile.getBytes();
} catch (IOException e) {
logger.error("Error occurred trying to read uploaded json file", e);
return new ResponseEntity<>("Invalid file", HttpStatus.BAD_REQUEST);
}

String jsonString = new String(bytes);
logger.info(jsonString);
clientForm.setJson(jsonString);
clientForm.setCreatedAt(new Date());

ClientFormMetadata clientFormMetadata = new ClientFormMetadata();
clientFormMetadata.setVersion(formVersion);
clientFormMetadata.setIdentifier(formIdentifier);
clientFormMetadata.setLabel(formName);
clientFormMetadata.setCreatedAt(new Date());
clientFormMetadata.setModule(module);

public ClientForm clientForm;
public ClientFormMetadata clientFormMetadata;
ClientFormService.CompleteClientForm completeClientForm = clientFormService.addClientForm(clientForm, clientFormMetadata);

public CompleteClientForm(@NonNull ClientForm clientForm, @NonNull ClientFormMetadata clientFormMetadata) {
this.clientForm = clientForm;
this.clientFormMetadata = clientFormMetadata;
if (completeClientForm == null) {
return new ResponseEntity<>("Unknown error. Kindly confirm that the form does not already exist on the server", HttpStatus.BAD_REQUEST);
}

return new ResponseEntity<>(HttpStatus.CREATED);
}


@Override
public ClientForm update(ClientForm entity) {
return null;
}

}

0 comments on commit b7674e7

Please sign in to comment.