diff --git a/buisnesslogic/src/main/java/com/plannaplan/services/ConfiguratorService.java b/buisnesslogic/src/main/java/com/plannaplan/services/ConfiguratorService.java index 8f550f4..11a4898 100755 --- a/buisnesslogic/src/main/java/com/plannaplan/services/ConfiguratorService.java +++ b/buisnesslogic/src/main/java/com/plannaplan/services/ConfiguratorService.java @@ -7,6 +7,7 @@ import com.plannaplan.repositories.AppConfigRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +import java.io.InputStream; import java.util.Comparator; import java.util.List; import java.util.stream.Collectors; @@ -62,4 +63,14 @@ public class ConfiguratorService { return repsonse.get(0); } + + /** + * + * @param inputStream This input stream contains new courses to import. + */ + public void importCoursesStream(InputStream inputStream) { + FileReader reader = new FileReader(inputStream); + FileData coursesData = reader.read(); + migrator.migrate(coursesData); + } } \ No newline at end of file diff --git a/restservice/src/main/java/com/plannaplan/controllers/ConfigController.java b/restservice/src/main/java/com/plannaplan/controllers/ConfigController.java index a94f1ca..31f52d9 100755 --- a/restservice/src/main/java/com/plannaplan/controllers/ConfigController.java +++ b/restservice/src/main/java/com/plannaplan/controllers/ConfigController.java @@ -82,6 +82,48 @@ public class ConfigController { } } + @PostMapping(path = "/config/courses", consumes = { "multipart/form-data" }) + @PreAuthorize("hasRole('ROLE_ADMIN')") + @ApiOperation("Imports data to system. To call you need to provide ADMIN token") + public ResponseEntity configAppChangeCources( + @RequestParam("file") @ApiParam(value = "file .xlsx that contains courses and groups with apoinnted rules") MultipartFile file) + { + try { + this.contrl.importCoursesStream(file.getInputStream()); + return new ResponseEntity<>("Sucess", HttpStatus.OK); + } catch (IOException e) { + return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); + } + } + + @PostMapping(path = "/config/tours", consumes = { "multipart/form-data" }) + @PreAuthorize("hasRole('ROLE_ADMIN')") + @ApiOperation("Imports data to system. To call you need to provide ADMIN token") + public ResponseEntity configAppsasaas( + @RequestParam("file") @ApiParam(value = "file .xlsx that contains courses and groups with apoinnted rules") MultipartFile file, + @RequestParam("firstTourBegin") @DateTimeFormat(pattern = "dd.MM.yyyy") @ApiParam(value = "Date when first tour begin in format dd.MM.yyyy") Date firstTourBegin, + @RequestParam("firstTourEnd") @DateTimeFormat(pattern = "dd.MM.yyyy") @ApiParam(value = "Date when first tour ends in format dd.MM.yyyy") Date firstTourEnd, + @RequestParam("secondTourBegin") @DateTimeFormat(pattern = "dd.MM.yyyy") @ApiParam(value = "Date when second tour begin in format dd.MM.yyyy") Date secondTourBegin, + @RequestParam("secondTourEnd") @DateTimeFormat(pattern = "dd.MM.yyyy") @ApiParam(value = "Date when second tour ends in format dd.MM.yyyy") Date secondTourEnd) { + try { + if (!(firstTourBegin.before(firstTourEnd) + && (firstTourEnd.before(secondTourBegin) || firstTourEnd.equals(secondTourBegin)) + && secondTourBegin.before(secondTourEnd))) { + return new ResponseEntity<>("Bad dates", HttpStatus.BAD_REQUEST); + } + + final TourData firstTour = new TourData(firstTourBegin, firstTourEnd); + final TourData secondTour = new TourData(secondTourBegin, secondTourEnd); + + final ConfigData data = new ConfigData(firstTour, secondTour, file.getInputStream()); + this.contrl.config(data); + return new ResponseEntity<>("Sucess", HttpStatus.OK); + } catch (IOException e) { + return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); + } + } + + @PostMapping(path = "/admin/init") @ApiOperation("It can be run only in the initialization of the application. It will create admin user to manage the application.") public ResponseEntity initAdmin(@RequestParam("ticket") @ApiParam(value = "Ticket for validation.") String ticket){ @@ -106,4 +148,54 @@ public class ConfigController { } } + @PostMapping(path = "/admin/addnewdeanery", consumes = { "multipart/form-data" }) + @PreAuthorize("hasRole('ROLE_ADMIN')") + @ApiOperation("Adding the new deanery user into application") + public ResponseEntity AddNewDeaneryUser(@RequestParam("ticket") @ApiParam(value = "Ticket for validation.") String ticket){ + // if (this.userService.adminExists()){ + // return new ResponseEntity<>("Admin had been already created.", HttpStatus.FORBIDDEN); + // } + + final CasValidator validator = isDev ? new DefaultUAMCasValidator(serviceUrl, ticket) + : new CustomUAMCasValidator(serviceUrl, ticket); + + try { + final CasUserIdentity casUserIdentity = validator.validate(); + final String usosId = casUserIdentity.getUsosId(); + final String authority = casUserIdentity.getEmail(); + this.userService.save(new User(null, null, authority, usosId, UserRoles.DEANERY)); + + return new ResponseEntity<>("Success", HttpStatus.OK); + } catch (CasValidationExcepiton e) { + return new ResponseEntity<>("CAS validation failed", HttpStatus.UNAUTHORIZED); + } catch (Exception e) { + return new ResponseEntity<>("Internal Server Error", HttpStatus.INTERNAL_SERVER_ERROR); + } + } + + @PostMapping(path = "/admin/addnewadmin", consumes = { "multipart/form-data" }) + @PreAuthorize("hasRole('ROLE_ADMIN')") + @ApiOperation("Adding the new administrator user into application") + public ResponseEntity AddNewAdminUser(@RequestParam("ticket") @ApiParam(value = "Ticket for validation.") String ticket){ + // if (this.userService.adminExists()){ + // return new ResponseEntity<>("Admin had been already created.", HttpStatus.FORBIDDEN); + // } + + final CasValidator validator = isDev ? new DefaultUAMCasValidator(serviceUrl, ticket) + : new CustomUAMCasValidator(serviceUrl, ticket); + + try { + final CasUserIdentity casUserIdentity = validator.validate(); + final String usosId = casUserIdentity.getUsosId(); + final String authority = casUserIdentity.getEmail(); + this.userService.save(new User(null, null, authority, usosId, UserRoles.ADMIN)); + + return new ResponseEntity<>("Success", HttpStatus.OK); + } catch (CasValidationExcepiton e) { + return new ResponseEntity<>("CAS validation failed", HttpStatus.UNAUTHORIZED); + } catch (Exception e) { + return new ResponseEntity<>("Internal Server Error", HttpStatus.INTERNAL_SERVER_ERROR); + } + } + } \ No newline at end of file