use of org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent in project sw360portal by sw360.
the class AttachmentRepository method vacuumAttachmentDB.
public RequestSummary vacuumAttachmentDB(User user, final Set<String> usedIds) {
final RequestSummary requestSummary = new RequestSummary();
if (!PermissionUtils.isAdmin(user))
return requestSummary.setRequestStatus(RequestStatus.FAILURE);
final List<AttachmentContent> allAttachmentContents = getAll();
final Set<AttachmentContent> unusedAttachmentContents = allAttachmentContents.stream().filter(input -> !usedIds.contains(input.getId())).collect(Collectors.toSet());
requestSummary.setTotalElements(allAttachmentContents.size());
requestSummary.setTotalAffectedElements(unusedAttachmentContents.size());
final List<DocumentOperationResult> documentOperationResults = deleteBulk(unusedAttachmentContents);
if (documentOperationResults.isEmpty()) {
requestSummary.setRequestStatus(RequestStatus.SUCCESS);
} else {
requestSummary.setRequestStatus(RequestStatus.FAILURE);
}
return requestSummary;
}
use of org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent in project sw360portal by sw360.
the class AttachmentHandlerTest method testVacuum_AllIdsUsedIsNoop.
@Test
public void testVacuum_AllIdsUsedIsNoop() throws Exception {
final RequestSummary requestSummary = handler.vacuumAttachmentDB(new User("a", "a", "a").setUserGroup(UserGroup.ADMIN), ImmutableSet.of("A1", "A2"));
assertThat(requestSummary.requestStatus, is(RequestStatus.SUCCESS));
assertThat(requestSummary.totalElements, is(2));
assertThat(requestSummary.totalAffectedElements, is(0));
final AttachmentContent a1 = handler.getAttachmentContent("A1");
assertNotNull(a1);
final AttachmentContent a2 = handler.getAttachmentContent("A2");
assertNotNull(a2);
}
use of org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent in project sw360portal by sw360.
the class AttachmentHandler method getAttachmentContent.
@Override
public AttachmentContent getAttachmentContent(String id) throws TException {
assertNotEmpty(id);
AttachmentContent attachment = attachmentRepository.get(id);
assertNotNull(attachment, "Cannot find " + id + " in database.");
validateAttachment(attachment);
return attachment;
}
use of org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent in project sw360portal by sw360.
the class AttachmentDatabaseHandler method getAttachmentContent.
public AttachmentContent getAttachmentContent(String id) throws TException {
AttachmentContent attachment = repository.get(id);
assertNotNull(attachment, "Cannot find " + id + " in database.");
validateAttachment(attachment);
return attachment;
}
use of org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent in project sw360portal by sw360.
the class ProjectController method downloadClearingReports.
@RequestMapping(value = PROJECTS_URL + "/{projectId}/attachments/clearingReports", method = RequestMethod.GET, produces = "application/zip")
public void downloadClearingReports(@PathVariable("projectId") String projectId, HttpServletResponse response, OAuth2Authentication oAuth2Authentication) throws TException {
final User sw360User = restControllerHelper.getSw360UserFromAuthentication(oAuth2Authentication);
final Project project = projectService.getProjectForUserById(projectId, sw360User);
final String filename = "Clearing-Reports-" + project.getName() + ".zip";
final Set<Attachment> attachments = project.getAttachments();
final Set<AttachmentContent> clearingAttachments = new HashSet<>();
for (final Attachment attachment : attachments) {
if (attachment.getAttachmentType().equals(AttachmentType.CLEARING_REPORT)) {
clearingAttachments.add(attachmentService.getAttachmentContent(attachment.getAttachmentContentId()));
}
}
try (InputStream attachmentStream = attachmentService.getStreamToAttachments(clearingAttachments, sw360User, project)) {
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
response.setHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", filename));
FileCopyUtils.copy(attachmentStream, response.getOutputStream());
} catch (final TException | IOException e) {
log.error(e.getMessage());
}
}
Aggregations