|
| 1 | +package com.essi.Dependency.Controller; |
| 2 | + |
| 3 | +import com.essi.Dependency.Components.Grammar; |
| 4 | +import com.essi.Dependency.Service.GrammarService; |
| 5 | +import io.swagger.annotations.*; |
| 6 | +import org.springframework.beans.factory.annotation.Autowired; |
| 7 | +import org.springframework.http.HttpEntity; |
| 8 | +import org.springframework.http.HttpStatus; |
| 9 | +import org.springframework.http.MediaType; |
| 10 | +import org.springframework.http.ResponseEntity; |
| 11 | +import org.springframework.web.bind.annotation.*; |
| 12 | + |
| 13 | +@RestController |
| 14 | +@RequestMapping("/upc/cross-reference-detection") |
| 15 | +@Api(value = "GrammarControllerAPI", produces = MediaType.APPLICATION_JSON_VALUE) |
| 16 | +public class GrammarController { |
| 17 | + |
| 18 | + @Autowired |
| 19 | + private GrammarService grammarService; |
| 20 | + |
| 21 | + @PostMapping("/grammar") |
| 22 | + @ApiOperation(value = "Store grammar", |
| 23 | + notes = "Given a company, stores its list of prefixes to use as grammar rules to extract requirement cross-references", |
| 24 | + response = String.class) |
| 25 | + @ApiResponses(value = { @ApiResponse(code = 0, message = "Non content: There is no content to submit."), |
| 26 | + @ApiResponse(code = 200, message = "OK: The request has succeeded."), |
| 27 | + @ApiResponse(code = 201, |
| 28 | + message = "Created: The request has been fulfilled and has resulted in one or more new resources being created.", |
| 29 | + response = String.class), |
| 30 | + @ApiResponse(code = 401, |
| 31 | + message = "Unauthorized: The request has not been applied because it lacks valid authentication credentials for the target resource."), |
| 32 | + @ApiResponse(code = 403, |
| 33 | + message = "Forbidden: The server understood the request but refuses to authorize it."), |
| 34 | + @ApiResponse(code = 404, |
| 35 | + message = "Not Found: The server could not find what was requested by the client."), |
| 36 | + @ApiResponse(code = 500, |
| 37 | + message = "Internal Server Error. For more information see ‘message’ in the Response Body.") }) |
| 38 | + public ResponseEntity<?> uploadGrammar( |
| 39 | + @ApiParam(value = "The grammar", |
| 40 | + required = true) @RequestBody Grammar grammar, |
| 41 | + @ApiParam(value = "Company", required = true) @RequestParam("company") String company) { |
| 42 | + try { |
| 43 | + grammarService.uploadGrammar(company, grammar); |
| 44 | + return new ResponseEntity<>(HttpStatus.OK); |
| 45 | + } catch (Exception e) { |
| 46 | + e.printStackTrace(); |
| 47 | + return new ResponseEntity<>(new HttpEntity<>(e.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + @PutMapping("/grammar") |
| 52 | + @ApiOperation(value = "Update grammar", |
| 53 | + notes = "Given a company, updates its existing list of prefixes to use as grammar rules to extract requirement cross-references", |
| 54 | + response = String.class) |
| 55 | + @ApiResponses(value = { @ApiResponse(code = 0, message = "Non content: There is no content to submit."), |
| 56 | + @ApiResponse(code = 200, message = "OK: The request has succeeded."), |
| 57 | + @ApiResponse(code = 201, |
| 58 | + message = "Created: The request has been fulfilled and has resulted in one or more new resources being created.", |
| 59 | + response = String.class), |
| 60 | + @ApiResponse(code = 401, |
| 61 | + message = "Unauthorized: The request has not been applied because it lacks valid authentication credentials for the target resource."), |
| 62 | + @ApiResponse(code = 403, |
| 63 | + message = "Forbidden: The server understood the request but refuses to authorize it."), |
| 64 | + @ApiResponse(code = 404, |
| 65 | + message = "Not Found: The server could not find what was requested by the client."), |
| 66 | + @ApiResponse(code = 500, |
| 67 | + message = "Internal Server Error. For more information see ‘message’ in the Response Body.") }) |
| 68 | + public ResponseEntity<?> updateGrammar( |
| 69 | + @ApiParam(value = "The grammar", |
| 70 | + required = true) @RequestBody Grammar grammar, |
| 71 | + @ApiParam(value = "Company", required = true) @RequestParam("company") String company) { |
| 72 | + try { |
| 73 | + grammarService.updateGrammar(company, grammar); |
| 74 | + return new ResponseEntity<>(HttpStatus.OK); |
| 75 | + } catch (Exception e) { |
| 76 | + e.printStackTrace(); |
| 77 | + return new ResponseEntity<>(new HttpEntity<>(e.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + @DeleteMapping("/grammar") |
| 82 | + @ApiOperation(value = "Delete grammar", |
| 83 | + notes = "Deletes grammar rules of a given company", |
| 84 | + response = String.class) |
| 85 | + @ApiResponses(value = { @ApiResponse(code = 0, message = "Non content: There is no content to submit."), |
| 86 | + @ApiResponse(code = 200, message = "OK: The request has succeeded."), |
| 87 | + @ApiResponse(code = 201, |
| 88 | + message = "Created: The request has been fulfilled and has resulted in one or more new resources being created.", |
| 89 | + response = String.class), |
| 90 | + @ApiResponse(code = 401, |
| 91 | + message = "Unauthorized: The request has not been applied because it lacks valid authentication credentials for the target resource."), |
| 92 | + @ApiResponse(code = 403, |
| 93 | + message = "Forbidden: The server understood the request but refuses to authorize it."), |
| 94 | + @ApiResponse(code = 404, |
| 95 | + message = "Not Found: The server could not find what was requested by the client."), |
| 96 | + @ApiResponse(code = 500, |
| 97 | + message = "Internal Server Error. For more information see ‘message’ in the Response Body.") }) |
| 98 | + public ResponseEntity<?> deleteGrammar( |
| 99 | + @ApiParam(value = "Company", required = true) @RequestParam String company) { |
| 100 | + try { |
| 101 | + grammarService.deleteGrammar(company); |
| 102 | + return new ResponseEntity<>(HttpStatus.OK); |
| 103 | + } catch (Exception e) { |
| 104 | + e.printStackTrace(); |
| 105 | + return new ResponseEntity<>(new HttpEntity<>(e.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + @GetMapping("/grammar") |
| 110 | + @ApiOperation(value = "Get grammar", |
| 111 | + notes = "Gets the list of prefixes of a given company", |
| 112 | + response = String.class) |
| 113 | + @ApiResponses(value = { @ApiResponse(code = 0, message = "Non content: There is no content to submit."), |
| 114 | + @ApiResponse(code = 200, message = "OK: The request has succeeded."), |
| 115 | + @ApiResponse(code = 201, |
| 116 | + message = "Created: The request has been fulfilled and has resulted in one or more new resources being created.", |
| 117 | + response = String.class), |
| 118 | + @ApiResponse(code = 401, |
| 119 | + message = "Unauthorized: The request has not been applied because it lacks valid authentication credentials for the target resource."), |
| 120 | + @ApiResponse(code = 403, |
| 121 | + message = "Forbidden: The server understood the request but refuses to authorize it."), |
| 122 | + @ApiResponse(code = 404, |
| 123 | + message = "Not Found: The server could not find what was requested by the client."), |
| 124 | + @ApiResponse(code = 500, |
| 125 | + message = "Internal Server Error. For more information see ‘message’ in the Response Body.") }) |
| 126 | + public Object getGrammar( |
| 127 | + @ApiParam(value = "Company", required = true) @RequestParam String company) { |
| 128 | + try { |
| 129 | + Grammar grammar = grammarService.getGrammar(company); |
| 130 | + return grammar; |
| 131 | + } catch (Exception e) { |
| 132 | + e.printStackTrace(); |
| 133 | + return new ResponseEntity<>(new HttpEntity<>(e.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | +} |
0 commit comments