package hycom; import hycom.model.Form; import hycom.model.FormsResoult; import hycom.model.UsersResoult; import hycom.service.DatabaseService; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.ByteArrayResource; import org.springframework.core.io.Resource; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; @RestController public class FormsController { @Autowired DatabaseService databaseService; public final String USER = "username=c2VydmljZVVzZXI="; public final String PASSWORD = "password=UTJyVDg5JHE="; @RequestMapping(value = "/rest/form/{id}", method = RequestMethod.GET, headers = {USER, PASSWORD}) public @ResponseBody Form getForm(@PathVariable("id") int formId) { return databaseService.getForm(formId); } @RequestMapping(value = "/rest/forms", method = RequestMethod.GET, headers = {USER, PASSWORD}) public @ResponseBody List
getForms() { return databaseService.getForms(); } @RequestMapping(value = "/rest/forms/{offset}/{limit}/{fromDate}/{toDate}/{formName}/{category}/{pillar}/{name}", method = RequestMethod.GET, headers = {USER, PASSWORD}) public @ResponseBody FormsResoult getForms(@PathVariable("offset") int offset, @PathVariable("limit") int limit, @PathVariable("fromDate") String fromDate, @PathVariable("toDate") String toDate, @PathVariable("formName") String formName, @PathVariable("category") String category, @PathVariable("pillar") String pillar, @PathVariable("name") String name) { FormsResoult formsResoult = new FormsResoult(); formsResoult.setForms( databaseService.getFormsUsingPagination(offset, limit, fromDate, toDate, formName, category, pillar, name)); formsResoult.setCount(databaseService.getFormsCount(fromDate, toDate, formName, category, pillar, name)); return formsResoult; } @RequestMapping(value = "/rest/users", method = RequestMethod.GET, headers = {USER, PASSWORD}) @ResponseBody private UsersResoult getUsers() { UsersResoult usersResoult = new UsersResoult(); usersResoult.setUsers(databaseService.getUsersContainsAttachments()); usersResoult.setCount(Long.valueOf(usersResoult.getUsers().size())); return usersResoult; } @RequestMapping(value = "/rest/attachments/{userName:.+}", method = RequestMethod.GET, headers = {USER, PASSWORD}) public @ResponseBody ResponseEntity getAttachments(@PathVariable("userName") String userName) { try { File file = new File(databaseService.getAttachments(userName)); Path path = Paths.get(file.getAbsolutePath()); ByteArrayResource resource = new ByteArrayResource(Files.readAllBytes(path)); HttpHeaders headers = new HttpHeaders(); headers.add("Cache-Control", "no-cache, no-store, must-revalidate"); headers.add("Pragma", "no-cache"); headers.add("Expires", "0"); headers.add("Content-Disposition", "attachment, filename=\"" + userName + "\".zip"); return ResponseEntity.ok().headers(headers).contentLength(file.length()) .contentType(MediaType.parseMediaType("application/octet-stream")).body(resource); } catch (IOException e) { return (ResponseEntity) ResponseEntity.notFound(); } } @RequestMapping(value = "/rest/attachments/delete/{userName:.+}", method = RequestMethod.GET, headers = {USER, PASSWORD}) public @ResponseBody ResponseEntity removeAttachments(@PathVariable("userName") String userName) { Boolean success = databaseService.removeAttachments(userName); if (success) { return ResponseEntity.ok().body(Boolean.TRUE); } else { return ResponseEntity.noContent().build(); } } @RequestMapping(value = "/rest/form/submit", method = RequestMethod.POST, headers = {USER, PASSWORD}) public @ResponseBody Form submitForm(@RequestBody Form form) { return form; } @RequestMapping(value = "/rest/form/submit/{id}", method = RequestMethod.PUT) public @ResponseBody String submitFormId(@PathVariable("id") String id) { databaseService.submitForm(id); return id; } @RequestMapping(value = "/rest/attachment/remove/{id}", method = RequestMethod.GET, headers = {USER, PASSWORD}) public @ResponseBody String removeAttachment(@PathVariable("id") int attachmentId) { return null; } @CrossOrigin(origins = "http://localhost:8080") @RequestMapping(value = "/rest/form/issubmitted/{id}", method = RequestMethod.GET) public @ResponseBody ResponseEntity isFormSubmitted(@PathVariable("id") String id) { Boolean success = databaseService.isFormSubmitted(id); if (success) { return ResponseEntity.ok().body(Boolean.TRUE); } else { return ResponseEntity.ok().body(Boolean.FALSE); } } @CrossOrigin(origins = "http://localhost:8080") @RequestMapping(value = "/rest/form/hasremovedattachments/{id}", method = RequestMethod.GET) public @ResponseBody ResponseEntity hasRemovedAttachments(@PathVariable("id") String id) { Boolean hasRemovedAttachments = databaseService.hasRemovedAttachments(id); if (hasRemovedAttachments) { return ResponseEntity.ok().body(Boolean.TRUE); } else { return ResponseEntity.ok().body(Boolean.FALSE); } } }