backend/restservice/src/main/java/com/plannaplan/controllers/CommisionController.java

275 lines
13 KiB
Java
Executable File

package com.plannaplan.controllers;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.PathVariable;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import java.util.Optional;
import javax.servlet.http.HttpServletResponse;
import com.plannaplan.App;
import com.plannaplan.entities.Assignment;
import com.plannaplan.entities.Commision;
import com.plannaplan.entities.Groups;
import com.plannaplan.entities.User;
import com.plannaplan.exceptions.UserNotFoundException;
import com.plannaplan.models.ExportData;
import com.plannaplan.responses.mappers.CommisionResponseMappers;
import com.plannaplan.responses.models.CommisionResponse;
import com.plannaplan.services.AssignmentService;
import com.plannaplan.services.CommisionService;
import com.plannaplan.services.ConfiguratorService;
import com.plannaplan.services.GroupService;
import com.plannaplan.types.AppState;
import com.plannaplan.types.UserRoles;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import com.plannaplan.responses.mappers.AssignmentResponseMappers;
import com.plannaplan.responses.models.AssignmentResponse;
/**
* Rest controller to Commision and Assignment related endpoints. More detailed
* api docs is available via swagger
*/
@RestController
@CrossOrigin
@RequestMapping("/api/" + App.API_VERSION + "/commisions")
@Api(tags = { "Commisions" }, value = "Commisions", description = "Commision is representation of student selected assignments at time. All assignments are attached to some commision so we can see current assignments and also browse history of changes for given user")
public class CommisionController extends TokenBasedController {
@Autowired
private CommisionService commisionService;
@Autowired
private GroupService groupServcicxe;
@Autowired
private AssignmentService assignmentService;
@Autowired
private ConfiguratorService configuratorService;
public CommisionController() {
}
/**
* @param groups to make assignmetns
* @param userId user to assign to groups
* @return was operations success
*/
@PostMapping(value = { "/user", "/user/{id}" })
@ApiOperation(value = "Create commision with assignents to given groups. If group doesn't exist error will be thrown")
public ResponseEntity<String> addCommision(
@RequestBody @ApiParam(value = "List of groups ids user want to assign to. If group doesnt exisit error will be thrown") List<Long> groups,
@PathVariable(name = "id", required = false) Long userId) {
try {
final AppState appState = this.configuratorService.getCurrentConfig().getCurrentState();
final User asker = this.getCurrentUser()
.orElseThrow(() -> new UserNotFoundException("Invalid token"));
final User user = userId != null
? userService.getById(userId).orElseThrow(
() -> new UserNotFoundException("Given user id not exist"))
: asker;
Assert.isTrue((asker.getRole() == UserRoles.DEANERY && user.getRole() == UserRoles.STUDENT
&& appState == AppState.NO_TOUR
|| (asker.getId().equals(user.getId()) && user.getRole() == UserRoles.STUDENT
&& appState != AppState.NO_TOUR)),
"Incorrect attempt to change plan");
Optional<Long> notExistingGroup = this.groupServcicxe.findNotExistingGroup(groups);
Assert.isTrue(!notExistingGroup.isPresent(), "Group "
+ notExistingGroup.orElse(Long.MIN_VALUE).toString() + "doesn't exist");
final Optional<Commision> comPrev = this.commisionService.getNewestCommision(user);
final Commision com = new Commision(user, asker);
this.commisionService.save(com);
final List<Long> addedGroups = new ArrayList<>();
groups.stream().forEach((groupId) -> {
Groups group = this.groupServcicxe.getGroupById(groupId)
.orElseThrow(() -> new NullPointerException());
Assignment a = new Assignment(group, com);
this.assignmentService.save(a);
addedGroups.add(groupId);
});
boolean isBad = false;
if (comPrev.isPresent()) {
final Iterator<Assignment> it = comPrev.get().getAssignments().iterator();
while (it.hasNext() && !isBad) {
final Assignment a = it.next();
if (a.isAccepted() && !addedGroups.contains(a.getId())) {
isBad = true;
}
}
}
if (isBad) {
return new ResponseEntity<>("Error. You can't delete accepted assignmet",
HttpStatus.BAD_REQUEST);
}
return new ResponseEntity<>("Succes", HttpStatus.OK);
} catch (UserNotFoundException exception) {
return new ResponseEntity<>(exception.getMessage(), HttpStatus.NOT_FOUND);
} catch (IllegalArgumentException exception) {
return new ResponseEntity<>(exception.getMessage(), HttpStatus.BAD_REQUEST);
}
}
/**
* @param groups should include groups list in response
* @return list of user all commisions (history of schedules)
* @throws UserNotFoundException if user was found
*/
@GetMapping("/user")
@ApiOperation("Return list of user all commisions (history of schedules)")
public ResponseEntity<List<? extends CommisionResponse>> getAlCommisions(
@RequestParam(name = "groups", defaultValue = "false") @ApiParam(value = "Boolean if we want to display wiht commision's group ids") Boolean groups)
throws UserNotFoundException {
User user = this.getCurrentUser().orElseThrow(() -> new NullPointerException());
List<? extends CommisionResponse> result;
if (!groups) {
result = CommisionResponseMappers.mapToResponse(this.commisionService.getUsersCommisions(user));
} else {
result = CommisionResponseMappers
.mapToResponseWithGroups(this.commisionService.getUsersCommisions(user));
}
return new ResponseEntity<>(result, HttpStatus.OK);
}
/**
* @return current schedule of user indenified via token
* @throws Exception if incorrect role was trying to see self schedule (for
* example DEANERY don't have a schedule)
*/
@GetMapping("/user/schedule")
@ApiOperation(value = "Return user current assignemts (from newest commision). STUDENT Token needs to be provided.")
public ResponseEntity<List<AssignmentResponse>> getCurrentAssignments() throws Exception {
User user = this.getCurrentUser().orElseThrow(() -> new NullPointerException("User not found"));
Optional<Commision> com = this.commisionService.getNewestCommision(user);
if (com.isPresent()) {
List<Assignment> respone = this.assignmentService.getCommisionAssignments(com.get());
final HashMap<Long, Integer> ammounts = this.groupServcicxe
.getTakenPlacesOfAssignments(respone);
return new ResponseEntity<>(AssignmentResponseMappers.mapToResponse(respone, ammounts),
HttpStatus.OK);
}
return new ResponseEntity<>(new ArrayList<>(), HttpStatus.OK);
}
/**
* @param userId user id in db
* @param groups should commision include assigned groups list
* @return list of commisions for given user
* @throws UserNotFoundException
*/
@PreAuthorize("hasRole('ROLE_DEANERY')")
@GetMapping("/user/{id}")
@ApiOperation("Return list of commisions for given user. To be able to access this data u need to provide DEANERY token")
public ResponseEntity<List<? extends CommisionResponse>> getCommision(@PathVariable(name = "id") Long userId,
@RequestParam(name = "groups", defaultValue = "false") @ApiParam(value = "Boolean if we want to display wiht commision's group ids") Boolean groups)
throws UserNotFoundException {
User user = this.userService.getById(userId).orElseThrow(() -> new NullPointerException());
List<? extends CommisionResponse> result;
if (!groups) {
result = CommisionResponseMappers.mapToResponse(this.commisionService.getUsersCommisions(user));
} else {
result = CommisionResponseMappers
.mapToResponseWithGroups(this.commisionService.getUsersCommisions(user));
}
return new ResponseEntity<>(result, HttpStatus.OK);
}
/**
* @param userId schedule to display owner
* @return user's schedule
* @throws Exception if incorrect access atempt occured
*/
@PreAuthorize("hasRole('ROLE_DEANERY')")
@GetMapping("/user/{id}/schedule")
@ApiOperation(value = "Return given user current assignemts (from newest commision). DEANERY Token needs to be provided.")
public ResponseEntity<List<AssignmentResponse>> getCurrentAssignmentsDeanery(
@PathVariable(name = "id") Long userId) throws Exception {
User user = this.userService.getById(userId).orElseThrow(() -> new NullPointerException());
if (user.getRole() == UserRoles.DEANERY) {
return new ResponseEntity<>(null, HttpStatus.BAD_REQUEST);
}
Optional<Commision> com = this.commisionService.getNewestCommision(user);
if (com.isPresent()) {
List<Assignment> respone = this.assignmentService.getCommisionAssignments(com.get());
return new ResponseEntity<>(AssignmentResponseMappers.mapToResponse(respone), HttpStatus.OK);
}
return new ResponseEntity<>(new ArrayList<>(), HttpStatus.OK);
}
/**
* @param response spring response to set headers
*/
@GetMapping("/export/csv")
@PreAuthorize("hasRole('ROLE_ADMIN')")
@ApiOperation(value = "Export acceptes assignmetns as csv file to import to usos. You need to provide ADMIN token in order to get access to this data.")
public void getFile(HttpServletResponse response) {
try {
String csvString = ExportData.getCSVHeader() + "\n";
final Iterator<ExportData> it = this.commisionService.getExportData().iterator();
while (it.hasNext()) {
final ExportData data = it.next();
csvString += (data.convertToCSVRecord() + "\n");
}
final InputStream is = IOUtils.toInputStream(csvString, "UTF-8");
IOUtils.copy(is, response.getOutputStream());
response.setContentType("application/csv");
response.setHeader("Content-Disposition", "attachment; filename=\"export.csv\"");
response.flushBuffer();
} catch (IOException ex) {
throw new RuntimeException("IOError writing file to output stream");
}
}
}