Commit c37d4b17 authored by Javi Corvi's avatar Javi Corvi
Browse files

remove documents

parent a3721a60
Pipeline #30681 failed with stage
in 0 seconds
package es.bsc.inb.ades.rest.api.controllers; package es.bsc.inb.ades.rest.api.controllers;
import java.security.Principal;
import java.util.List; import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken;
import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
...@@ -23,6 +27,8 @@ import es.bsc.inb.ades.rest.api.services.DocumentService; ...@@ -23,6 +27,8 @@ import es.bsc.inb.ades.rest.api.services.DocumentService;
@CrossOrigin(origins="*") @CrossOrigin(origins="*")
public class DocumentController { public class DocumentController {
@Autowired @Autowired
public DocumentService documentService; public DocumentService documentService;
...@@ -69,6 +75,8 @@ public class DocumentController { ...@@ -69,6 +75,8 @@ public class DocumentController {
documentService.moveDocument(id, status); documentService.moveDocument(id, status);
return "true"; return "true";
} }
@RequestMapping("/documents/{id}/setExportSRDomain/{srDomainId}/{export_srDomain}") @RequestMapping("/documents/{id}/setExportSRDomain/{srDomainId}/{export_srDomain}")
public String setExportFinding(@PathVariable(value="id") Long id, @PathVariable(value="srDomainId") Integer srDomainId, @PathVariable(value="export_srDomain") Boolean export_srDomain) { public String setExportFinding(@PathVariable(value="id") Long id, @PathVariable(value="srDomainId") Integer srDomainId, @PathVariable(value="export_srDomain") Boolean export_srDomain) {
...@@ -137,10 +145,26 @@ public class DocumentController { ...@@ -137,10 +145,26 @@ public class DocumentController {
return new ResponseEntity<>(content, head,HttpStatus.OK); return new ResponseEntity<>(content, head,HttpStatus.OK);
} }
@RequestMapping(value = "/documents/remove/{id}")
public String removeDocument(@PathVariable("id") long documentId){
documentService.removeDocument(documentId);
return "true";
}
@RequestMapping(value = "/documents/restore/{id}")
public String restoreDocument(@PathVariable("id") long documentId){
documentService.restoreDocument(documentId);
return "true";
}
@RequestMapping("/documents/{id}/tracking") @RequestMapping("/documents/{id}/tracking")
public DocumentTracking findTrackings(@PathVariable(value="id") Long id) { public DocumentTracking findTrackings(@PathVariable(value="id") Long id) {
return documentService.findTrackings(id); return documentService.findTrackings(id);
} }
} }
\ No newline at end of file
...@@ -26,6 +26,8 @@ public class Document{ ...@@ -26,6 +26,8 @@ public class Document{
private Status status; private Status status;
private RecordState recordState;
public Document() { public Document() {
super(); super();
} }
...@@ -90,5 +92,14 @@ public class Document{ ...@@ -90,5 +92,14 @@ public class Document{
this.fileName = fileName; this.fileName = fileName;
} }
public RecordState getRecordState() {
return recordState;
}
public void setRecordState(RecordState recordState) {
this.recordState = recordState;
}
} }
package es.bsc.inb.ades.rest.api.model;
public enum RecordState {
ACTIVE, DELETED;
}
...@@ -33,6 +33,8 @@ public class DocumentRepositoryImpl implements DocumentRepositoryCustom{ ...@@ -33,6 +33,8 @@ public class DocumentRepositoryImpl implements DocumentRepositoryCustom{
query.fields().include("name"); query.fields().include("name");
query.fields().include("processDate"); query.fields().include("processDate");
query.fields().include("status"); query.fields().include("status");
query.fields().include("recordState");
query.addCriteria(Criteria.where("recordState").ne("DELETED"));
//Sort sort = new Sort(Sort.Direction.ASC, "name"); //Sort sort = new Sort(Sort.Direction.ASC, "name");
//query.with(sort); //query.with(sort);
List<Document> documents = mongoTemplate.find(query, Document.class); List<Document> documents = mongoTemplate.find(query, Document.class);
......
...@@ -19,6 +19,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { ...@@ -19,6 +19,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
.authorizeRequests() .authorizeRequests()
.antMatchers(HttpMethod.GET, "/liveness").permitAll() .antMatchers(HttpMethod.GET, "/liveness").permitAll()
.antMatchers(HttpMethod.GET, "/readiness").permitAll() .antMatchers(HttpMethod.GET, "/readiness").permitAll()
.antMatchers(HttpMethod.GET, "/documents/restore/*").permitAll()
.anyRequest().authenticated() .anyRequest().authenticated()
.and() .and()
.sessionManagement() .sessionManagement()
......
...@@ -46,5 +46,9 @@ public interface DocumentService { ...@@ -46,5 +46,9 @@ public interface DocumentService {
Status moveDocument(Long id, Status status); Status moveDocument(Long id, Status status);
DocumentTracking findTrackings(Long documentId); DocumentTracking findTrackings(Long documentId);
void removeDocument(long documentId);
void restoreDocument(long documentId);
} }
package es.bsc.inb.ades.rest.api.services; package es.bsc.inb.ades.rest.api.services;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.security.Principal;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.nimbusds.jwt.JWT;
import es.bsc.inb.ades.rest.api.model.Action; import es.bsc.inb.ades.rest.api.model.Action;
import es.bsc.inb.ades.rest.api.model.Annotation; import es.bsc.inb.ades.rest.api.model.Annotation;
...@@ -21,6 +29,7 @@ import es.bsc.inb.ades.rest.api.model.Document; ...@@ -21,6 +29,7 @@ import es.bsc.inb.ades.rest.api.model.Document;
import es.bsc.inb.ades.rest.api.model.DocumentAnnotations; import es.bsc.inb.ades.rest.api.model.DocumentAnnotations;
import es.bsc.inb.ades.rest.api.model.DocumentTracking; import es.bsc.inb.ades.rest.api.model.DocumentTracking;
import es.bsc.inb.ades.rest.api.model.Finding; import es.bsc.inb.ades.rest.api.model.Finding;
import es.bsc.inb.ades.rest.api.model.RecordState;
import es.bsc.inb.ades.rest.api.model.SRDomainFinding; import es.bsc.inb.ades.rest.api.model.SRDomainFinding;
import es.bsc.inb.ades.rest.api.model.Status; import es.bsc.inb.ades.rest.api.model.Status;
import es.bsc.inb.ades.rest.api.model.Tracking; import es.bsc.inb.ades.rest.api.model.Tracking;
...@@ -33,7 +42,8 @@ import es.bsc.inb.ades.rest.api.util.Constants; ...@@ -33,7 +42,8 @@ import es.bsc.inb.ades.rest.api.util.Constants;
@Service @Service
public class DocumentServiceImpl implements DocumentService { public class DocumentServiceImpl implements DocumentService {
@Autowired
private HttpServletRequest request;
@Autowired @Autowired
public DocumentRepository documentRepository; public DocumentRepository documentRepository;
...@@ -107,7 +117,7 @@ public class DocumentServiceImpl implements DocumentService { ...@@ -107,7 +117,7 @@ public class DocumentServiceImpl implements DocumentService {
//https://stackoverflow.com/questions/47699646/updating-replacing-a-deeply-nested-object-in-mongodb-with-spring-data-mongodb //https://stackoverflow.com/questions/47699646/updating-replacing-a-deeply-nested-object-in-mongodb-with-spring-data-mongodb
findingSelected.setStatus(status); findingSelected.setStatus(status);
documentAnnotationsRepository.save(documentAnnotations); documentAnnotationsRepository.save(documentAnnotations);
createTracking(id, "admin", Action.ACCEPT, findingSelected.getTrackingSummary()); createTracking(id, Action.ACCEPT, findingSelected.getTrackingSummary());
return true; return true;
} }
return false; return false;
...@@ -132,7 +142,7 @@ public class DocumentServiceImpl implements DocumentService { ...@@ -132,7 +142,7 @@ public class DocumentServiceImpl implements DocumentService {
//https://stackoverflow.com/questions/47699646/updating-replacing-a-deeply-nested-object-in-mongodb-with-spring-data-mongodb //https://stackoverflow.com/questions/47699646/updating-replacing-a-deeply-nested-object-in-mongodb-with-spring-data-mongodb
srDomainFindingelected.setStatus(status); srDomainFindingelected.setStatus(status);
documentAnnotationsRepository.save(documentAnnotations); documentAnnotationsRepository.save(documentAnnotations);
createTracking(id, "admin", Action.ACCEPT, srDomainFindingelected.getTrackingSummary()); createTracking(id, Action.ACCEPT, srDomainFindingelected.getTrackingSummary());
return true; return true;
} }
return false; return false;
...@@ -158,7 +168,7 @@ public class DocumentServiceImpl implements DocumentService { ...@@ -158,7 +168,7 @@ public class DocumentServiceImpl implements DocumentService {
//https://stackoverflow.com/questions/47699646/updating-replacing-a-deeply-nested-object-in-mongodb-with-spring-data-mongodb //https://stackoverflow.com/questions/47699646/updating-replacing-a-deeply-nested-object-in-mongodb-with-spring-data-mongodb
sRDomainFindingSelected.setExport(export_srdomainfinding); sRDomainFindingSelected.setExport(export_srdomainfinding);
documentAnnotationsRepository.save(documentAnnotations); documentAnnotationsRepository.save(documentAnnotations);
createTracking(id, "admin", export_srdomainfinding?Action.EXPORT_SR_FINDING:Action.NOT_EXPORT_SR_FINDING, sRDomainFindingSelected.getTrackingSummary()); createTracking(id, export_srdomainfinding?Action.EXPORT_SR_FINDING:Action.NOT_EXPORT_SR_FINDING, sRDomainFindingSelected.getTrackingSummary());
return true; return true;
} }
return false; return false;
...@@ -976,15 +986,16 @@ public class DocumentServiceImpl implements DocumentService { ...@@ -976,15 +986,16 @@ public class DocumentServiceImpl implements DocumentService {
documentAnnotation.setStatus(status); documentAnnotation.setStatus(status);
documentRepository.save(document); documentRepository.save(document);
documentAnnotationsRepository.save(documentAnnotation); documentAnnotationsRepository.save(documentAnnotation);
createTracking(id, Action.MOVE, status.toString());
createTracking(id, "admin", Action.MOVE, status.toString());
return status; return status;
} }
private void createTracking(Long id, String userName , Action action, String comment) { private void createTracking(Long id, Action action, String comment) {
DocumentTracking documentTracking = this.findDocumentTrackingById(id); DocumentTracking documentTracking = this.findDocumentTrackingById(id);
Tracking tracking = new Tracking (userName, action, comment); // Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
// JwtAuthenticationToken token = (JwtAuthenticationToken)principal;
// Tracking tracking = new Tracking (token.getTokenAttributes().get("name").toString(), action, comment);
Tracking tracking = new Tracking (getUserName(), action, comment);
documentTracking.addTracking(tracking); documentTracking.addTracking(tracking);
documentTrackingRepository.save(documentTracking); documentTrackingRepository.save(documentTracking);
} }
...@@ -1003,6 +1014,37 @@ public class DocumentServiceImpl implements DocumentService { ...@@ -1003,6 +1014,37 @@ public class DocumentServiceImpl implements DocumentService {
return documentTracking; return documentTracking;
} }
@Override
public void removeDocument(long documentId) {
Document document = this.findByDocumentId(documentId);
document.setRecordState(RecordState.DELETED);
documentRepository.save(document);
}
@Override
public void restoreDocument(long documentId) {
Document document = this.findByDocumentId(documentId);
document.setRecordState(RecordState.ACTIVE);
documentRepository.save(document);
}
private String getUserName() {
Principal user = request.getUserPrincipal();
String userName = "";
if(user!=null && user instanceof JwtAuthenticationToken) {
JwtAuthenticationToken token = (JwtAuthenticationToken)user;
if(token.getTokenAttributes().get("name")!=null) {
userName = token.getTokenAttributes().get("name").toString();
};
}
return userName;
}
public HttpServletRequest getRequest() {
return request;
}
public void setRequest(HttpServletRequest request) {
this.request = request;
}
} }
...@@ -18,6 +18,8 @@ import com.fasterxml.jackson.databind.ObjectMapper; ...@@ -18,6 +18,8 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase; import com.mongodb.client.MongoDatabase;
import es.bsc.inb.ades.rest.api.model.RecordState;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment