11package it .sweven .blockcovid .blockchain .controllers ;
22
3+ import io .swagger .v3 .oas .annotations .Parameter ;
34import io .swagger .v3 .oas .annotations .media .Content ;
45import io .swagger .v3 .oas .annotations .media .Schema ;
56import io .swagger .v3 .oas .annotations .responses .ApiResponse ;
67import io .swagger .v3 .oas .annotations .responses .ApiResponses ;
7- import it .sweven .blockcovid .blockchain .services .BlockchainService ;
8- import it .sweven .blockcovid .blockchain .services .DocumentContractService ;
98import it .sweven .blockcovid .blockchain .services .DocumentService ;
9+ import it .sweven .blockcovid .blockchain .services .SignRegistrationService ;
1010import it .sweven .blockcovid .rooms .services .RoomService ;
1111import it .sweven .blockcovid .users .entities .User ;
12- import java .io .FileInputStream ;
1312import java .io .IOException ;
13+ import org .slf4j .Logger ;
14+ import org .slf4j .LoggerFactory ;
1415import org .springframework .beans .factory .annotation .Autowired ;
1516import org .springframework .http .HttpStatus ;
1617import org .springframework .http .MediaType ;
2021import org .springframework .web .bind .annotation .ResponseBody ;
2122import org .springframework .web .bind .annotation .RestController ;
2223import org .springframework .web .server .ResponseStatusException ;
23- import org .web3j .crypto .Credentials ;
24- import org .web3j .documentcontract .DocumentContract ;
2524
2625@ RestController
27- public class AdminCleanerReportController implements ReportsController {
26+ public class CleanerReportController implements ReportsController {
2827 private final RoomService roomService ;
2928 private final DocumentService documentService ;
30- private final BlockchainService blockchainService ;
31- private final DocumentContractService documentContractService ;
32- private final Credentials blockchainCredentials ;
29+ private final SignRegistrationService signRegistrationService ;
30+ private final Logger logger = LoggerFactory .getLogger (CleanerReportController .class );
3331
3432 @ Autowired
35- public AdminCleanerReportController (
33+ public CleanerReportController (
3634 RoomService roomService ,
3735 DocumentService documentService ,
38- BlockchainService blockchainService ,
39- DocumentContractService documentContractService ,
40- Credentials blockchainCredentials ) {
36+ SignRegistrationService signRegistrationService ) {
4137 this .roomService = roomService ;
4238 this .documentService = documentService ;
43- this .blockchainService = blockchainService ;
44- this .documentContractService = documentContractService ;
45- this .blockchainCredentials = blockchainCredentials ;
39+ this .signRegistrationService = signRegistrationService ;
4640 }
4741
4842 @ GetMapping (value = "/cleaner" , produces = MediaType .APPLICATION_PDF_VALUE )
@@ -59,20 +53,25 @@ public AdminCleanerReportController(
5953 content = @ Content (schema = @ Schema (implementation = void .class )))
6054 })
6155 @ PreAuthorize ("#submitter.isAdmin()" )
62- public byte [] report (@ AuthenticationPrincipal User submitter ) {
56+ public byte [] report (@ Parameter ( hidden = true ) @ AuthenticationPrincipal User submitter ) {
6357 try {
6458 String path = documentService .generateCleanerReport (roomService .getAllRooms ());
65- DocumentContract contract =
66- documentContractService .getContractByAccount (blockchainCredentials );
67- blockchainService .registerReport (contract , new FileInputStream (path ));
59+ logger .info ("file saved at path " + path );
60+ Thread registrationThread =
61+ new Thread (
62+ () -> {
63+ try {
64+ signRegistrationService .registerString (documentService .hashOf (path ));
65+ documentService .setAsVerified (path );
66+ } catch (Exception exception ) {
67+ logger .error ("Unable to open file stream for file at path: " + path );
68+ }
69+ });
70+ registrationThread .start ();
6871 return documentService .readReport (path );
6972 } catch (IOException e ) {
7073 throw new ResponseStatusException (
7174 HttpStatus .INTERNAL_SERVER_ERROR , "An error occurred while creating the report" );
72- } catch (Exception e ) {
73- throw new ResponseStatusException (
74- HttpStatus .INTERNAL_SERVER_ERROR ,
75- "An error occurred while registering the document on the provided blockchain" );
7675 }
7776 }
7877}
0 commit comments