Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GAP-2436: download application preview odt #248

Merged
Merged
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,11 @@
<artifactId>aws-java-sdk-sns</artifactId>
<version>1.12.593</version>
</dependency>
<dependency>
<groupId>org.odftoolkit</groupId>
<artifactId>odfdom-java</artifactId>
<version>0.10.0</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
import gov.cabinetoffice.gap.adminbackend.entities.GrantAdmin;
import gov.cabinetoffice.gap.adminbackend.enums.ApplicationStatusEnum;
import gov.cabinetoffice.gap.adminbackend.enums.EventType;
import gov.cabinetoffice.gap.adminbackend.exceptions.ApplicationFormException;
import gov.cabinetoffice.gap.adminbackend.exceptions.InvalidEventException;
import gov.cabinetoffice.gap.adminbackend.exceptions.NotFoundException;
import gov.cabinetoffice.gap.adminbackend.exceptions.UnauthorizedException;
import gov.cabinetoffice.gap.adminbackend.exceptions.*;
import gov.cabinetoffice.gap.adminbackend.models.AdminSession;
import gov.cabinetoffice.gap.adminbackend.security.CheckSchemeOwnership;
import gov.cabinetoffice.gap.adminbackend.services.*;
Expand All @@ -25,7 +22,11 @@
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.odftoolkit.odfdom.doc.OdfTextDocument;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.*;
Expand Down Expand Up @@ -55,7 +56,9 @@ public class ApplicationFormController {
private final EventLogService eventLogService;

private final UserService userService;


private final OdtService odtService;

@PostMapping
@ApiResponses(value = {
@ApiResponse(responseCode = "201", description = "Application form created successfully.",
Expand Down Expand Up @@ -264,6 +267,29 @@ public ResponseEntity<String> getApplicationStatus(@PathVariable final Integer a
return ResponseEntity.ok(applicationStatus.toString());
}

@GetMapping("/{applicationId}/download-summary")
@CheckSchemeOwnership
public ResponseEntity<ByteArrayResource> exportApplication(
@PathVariable final Integer applicationId) {
try (OdfTextDocument odt = applicationFormService.getApplicationFormExport(applicationId)) {

ByteArrayResource odtResource = odtService.odtToResource(odt);

HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"application.odt\"");
headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE);

return ResponseEntity.ok().headers(headers).contentLength(odtResource.contentLength())
.contentType(MediaType.APPLICATION_OCTET_STREAM).body(odtResource);
} catch (RuntimeException e) {
log.error("Could not generate ODT for application " + applicationId + ". Exception: ", e);
throw new OdtException("Could not generate ODT for this application");
} catch (Exception e) {
log.error("Could not convert ODT to resource for application " + applicationId + ". Exception: ", e);
throw new OdtException("Could not download ODT for this application");
}
}


private void logApplicationEvent(EventType eventType, String sessionId, String applicationId) {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package gov.cabinetoffice.gap.adminbackend.exceptions;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public class OdtException extends RuntimeException {

public OdtException() {
}

public OdtException(String message) {
super(message);
}

public OdtException(String message, Throwable cause) {
super(message, cause);
}

public OdtException(Throwable cause) {
super(cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;
import org.odftoolkit.odfdom.doc.OdfTextDocument;

import javax.persistence.EntityNotFoundException;
import javax.servlet.http.HttpSession;
Expand Down Expand Up @@ -54,6 +55,8 @@ public class ApplicationFormService {

private final SessionsService sessionsService;

private final OdtService odtService;

private final Validator validator;

private final Clock clock;
Expand Down Expand Up @@ -418,4 +421,12 @@ public void removeAdminReferenceBySchemeId(GrantAdmin grantAdmin, Integer scheme
applicationFormRepository.save(application);
});
}

public OdfTextDocument getApplicationFormExport(Integer applicationId) {
final ApplicationFormEntity applicationForm = applicationFormRepository.findById(applicationId)
.orElseThrow(() -> new NotFoundException(String.format("No application with ID %s was found", applicationId)));
SchemeEntity scheme = this.schemeRepository.findById(applicationForm.getGrantSchemeId()).orElseThrow(EntityNotFoundException::new);

return odtService.generateSingleOdt(scheme, applicationForm);
}
}
Loading
Loading