Refactor visibilityRuleService

This commit is contained in:
George Kalampokis 2022-02-09 15:23:16 +02:00
parent 6cdf560713
commit 4740fbe8e0
6 changed files with 31 additions and 48 deletions

View File

@ -15,6 +15,7 @@ import eu.eudat.logic.proxy.config.configloaders.ConfigLoader;
import eu.eudat.logic.security.claims.ClaimedAuthorities; import eu.eudat.logic.security.claims.ClaimedAuthorities;
import eu.eudat.logic.services.ApiContext; import eu.eudat.logic.services.ApiContext;
import eu.eudat.logic.services.forms.VisibilityRuleService; import eu.eudat.logic.services.forms.VisibilityRuleService;
import eu.eudat.logic.services.forms.VisibilityRuleServiceImpl;
import eu.eudat.logic.utilities.documents.helpers.FileEnvelope; import eu.eudat.logic.utilities.documents.helpers.FileEnvelope;
import eu.eudat.logic.utilities.documents.pdf.PDFUtils; import eu.eudat.logic.utilities.documents.pdf.PDFUtils;
import eu.eudat.models.data.dataset.DatasetOverviewModel; import eu.eudat.models.data.dataset.DatasetOverviewModel;
@ -126,11 +127,11 @@ public class Datasets extends BaseController {
public @ResponseBody public @ResponseBody
ResponseEntity getSingle(@PathVariable String id, @RequestHeader("Content-Type") String contentType, @ClaimedAuthorities(claims = {Authorities.ADMIN, Authorities.MANAGER, Authorities.USER, Authorities.ANONYMOUS}) Principal principal) throws IllegalAccessException, IOException, InstantiationException { ResponseEntity getSingle(@PathVariable String id, @RequestHeader("Content-Type") String contentType, @ClaimedAuthorities(claims = {Authorities.ADMIN, Authorities.MANAGER, Authorities.USER, Authorities.ANONYMOUS}) Principal principal) throws IllegalAccessException, IOException, InstantiationException {
try { try {
VisibilityRuleService visibilityRuleService = new VisibilityRuleServiceImpl();
if (contentType.equals("application/xml")) { if (contentType.equals("application/xml")) {
VisibilityRuleService visibilityRuleService = this.getApiContext().getUtilitiesService().getVisibilityRuleService();
return this.datasetManager.getDocument(id, visibilityRuleService, contentType, principal); return this.datasetManager.getDocument(id, visibilityRuleService, contentType, principal);
} else if (contentType.equals("application/msword")) { } else if (contentType.equals("application/msword")) {
FileEnvelope file = datasetManager.getWordDocumentFile(this.configLoader, id, this.getApiContext().getUtilitiesService().getVisibilityRuleService(), principal); FileEnvelope file = datasetManager.getWordDocumentFile(this.configLoader, id, visibilityRuleService, principal);
InputStream resource = new FileInputStream(file.getFile()); InputStream resource = new FileInputStream(file.getFile());
HttpHeaders responseHeaders = new HttpHeaders(); HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.setContentLength(file.getFile().length()); responseHeaders.setContentLength(file.getFile().length());
@ -218,7 +219,7 @@ public class Datasets extends BaseController {
@RequestMapping(method = RequestMethod.GET, value = {"/getPDF/{id}"}) @RequestMapping(method = RequestMethod.GET, value = {"/getPDF/{id}"})
public @ResponseBody public @ResponseBody
ResponseEntity<byte[]> getPDFDocument(@PathVariable String id, @ClaimedAuthorities(claims = {Authorities.ADMIN, Authorities.MANAGER, Authorities.USER, Authorities.ANONYMOUS}) Principal principal) throws IllegalAccessException, IOException, InstantiationException, InterruptedException { ResponseEntity<byte[]> getPDFDocument(@PathVariable String id, @ClaimedAuthorities(claims = {Authorities.ADMIN, Authorities.MANAGER, Authorities.USER, Authorities.ANONYMOUS}) Principal principal) throws IllegalAccessException, IOException, InstantiationException, InterruptedException {
FileEnvelope file = datasetManager.getWordDocumentFile(this.configLoader, id, this.getApiContext().getUtilitiesService().getVisibilityRuleService(), principal); FileEnvelope file = datasetManager.getWordDocumentFile(this.configLoader, id, new VisibilityRuleServiceImpl(), principal);
String fileName = file.getFilename().replace(" ", "_").replace(",", "_"); String fileName = file.getFilename().replace(" ", "_").replace(",", "_");
if (fileName.endsWith(".docx")){ if (fileName.endsWith(".docx")){
fileName = fileName.substring(0, fileName.length() - 5); fileName = fileName.substring(0, fileName.length() - 5);

View File

@ -1,7 +1,5 @@
package eu.eudat.logic.managers; package eu.eudat.logic.managers;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import eu.eudat.configurations.dynamicgrant.DynamicGrantConfiguration; import eu.eudat.configurations.dynamicgrant.DynamicGrantConfiguration;
import eu.eudat.configurations.dynamicgrant.entities.Property; import eu.eudat.configurations.dynamicgrant.entities.Property;
@ -33,6 +31,7 @@ import eu.eudat.logic.mapper.elastic.criteria.DmpCriteriaMapper;
import eu.eudat.logic.proxy.config.configloaders.ConfigLoader; import eu.eudat.logic.proxy.config.configloaders.ConfigLoader;
import eu.eudat.logic.services.ApiContext; import eu.eudat.logic.services.ApiContext;
import eu.eudat.logic.services.forms.VisibilityRuleService; import eu.eudat.logic.services.forms.VisibilityRuleService;
import eu.eudat.logic.services.forms.VisibilityRuleServiceImpl;
import eu.eudat.logic.services.operations.DatabaseRepository; import eu.eudat.logic.services.operations.DatabaseRepository;
import eu.eudat.logic.services.utilities.UtilitiesService; import eu.eudat.logic.services.utilities.UtilitiesService;
import eu.eudat.logic.utilities.builders.XmlBuilder; import eu.eudat.logic.utilities.builders.XmlBuilder;
@ -106,7 +105,6 @@ public class DataManagementPlanManager {
private ApiContext apiContext; private ApiContext apiContext;
private DatasetManager datasetManager; private DatasetManager datasetManager;
private UtilitiesService utilitiesService;
private DatabaseRepository databaseRepository; private DatabaseRepository databaseRepository;
private Environment environment; private Environment environment;
private RDAManager rdaManager; private RDAManager rdaManager;
@ -117,7 +115,6 @@ public class DataManagementPlanManager {
public DataManagementPlanManager(ApiContext apiContext, DatasetManager datasetManager, Environment environment, RDAManager rdaManager, UserManager userManager, MetricsManager metricsManager) { public DataManagementPlanManager(ApiContext apiContext, DatasetManager datasetManager, Environment environment, RDAManager rdaManager, UserManager userManager, MetricsManager metricsManager) {
this.apiContext = apiContext; this.apiContext = apiContext;
this.datasetManager = datasetManager; this.datasetManager = datasetManager;
this.utilitiesService = apiContext.getUtilitiesService();
this.databaseRepository = apiContext.getOperationsContext().getDatabaseRepository(); this.databaseRepository = apiContext.getOperationsContext().getDatabaseRepository();
this.environment = environment; this.environment = environment;
this.rdaManager = rdaManager; this.rdaManager = rdaManager;
@ -1172,7 +1169,7 @@ public class DataManagementPlanManager {
public FileEnvelope getWordDocument(String id, Principal principal, ConfigLoader configLoader, Boolean versioned) throws IOException { public FileEnvelope getWordDocument(String id, Principal principal, ConfigLoader configLoader, Boolean versioned) throws IOException {
WordBuilder wordBuilder = new WordBuilder(); WordBuilder wordBuilder = new WordBuilder();
VisibilityRuleService visibilityRuleService = this.utilitiesService.getVisibilityRuleService(); VisibilityRuleService visibilityRuleService = new VisibilityRuleServiceImpl();
DatasetWizardModel dataset = new DatasetWizardModel(); DatasetWizardModel dataset = new DatasetWizardModel();
XWPFDocument document = configLoader.getDocument(); XWPFDocument document = configLoader.getDocument();
@ -1341,7 +1338,7 @@ public class DataManagementPlanManager {
private FileEnvelope getXmlDocument(String id, Principal principal) throws InstantiationException, IllegalAccessException, IOException { private FileEnvelope getXmlDocument(String id, Principal principal) throws InstantiationException, IllegalAccessException, IOException {
ExportXmlBuilder xmlBuilder = new ExportXmlBuilder(); ExportXmlBuilder xmlBuilder = new ExportXmlBuilder();
VisibilityRuleService visibilityRuleService = utilitiesService.getVisibilityRuleService(); VisibilityRuleService visibilityRuleService = new VisibilityRuleServiceImpl();
eu.eudat.data.entities.DMP dmp = databaseRepository.getDmpDao().find(UUID.fromString(id)); eu.eudat.data.entities.DMP dmp = databaseRepository.getDmpDao().find(UUID.fromString(id));
if (!dmp.isPublic() && dmp.getUsers().stream().filter(userInfo -> userInfo.getUser().getId() == principal.getId()).collect(Collectors.toList()).size() == 0) if (!dmp.isPublic() && dmp.getUsers().stream().filter(userInfo -> userInfo.getUser().getId() == principal.getId()).collect(Collectors.toList()).size() == 0)
throw new UnauthorisedException(); throw new UnauthorisedException();

View File

@ -26,6 +26,7 @@ import eu.eudat.logic.mapper.elastic.criteria.DmpCriteriaMapper;
import eu.eudat.logic.proxy.config.configloaders.ConfigLoader; import eu.eudat.logic.proxy.config.configloaders.ConfigLoader;
import eu.eudat.logic.services.ApiContext; import eu.eudat.logic.services.ApiContext;
import eu.eudat.logic.services.forms.VisibilityRuleService; import eu.eudat.logic.services.forms.VisibilityRuleService;
import eu.eudat.logic.services.forms.VisibilityRuleServiceImpl;
import eu.eudat.logic.services.operations.DatabaseRepository; import eu.eudat.logic.services.operations.DatabaseRepository;
import eu.eudat.logic.utilities.documents.helpers.FileEnvelope; import eu.eudat.logic.utilities.documents.helpers.FileEnvelope;
import eu.eudat.logic.utilities.documents.types.ParagraphStyle; import eu.eudat.logic.utilities.documents.types.ParagraphStyle;
@ -49,7 +50,6 @@ import eu.eudat.models.data.user.composite.PagedDatasetProfile;
import eu.eudat.queryable.QueryableList; import eu.eudat.queryable.QueryableList;
import eu.eudat.types.Authorities; import eu.eudat.types.Authorities;
import eu.eudat.types.MetricNames; import eu.eudat.types.MetricNames;
import org.apache.commons.io.IOUtils;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor; import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFParagraph;
@ -60,11 +60,11 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.core.io.FileSystemResource; import org.springframework.http.HttpHeaders;
import org.springframework.http.*; import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.w3c.dom.Element; import org.w3c.dom.Element;
@ -91,8 +91,6 @@ import java.time.temporal.ChronoUnit;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
@Component @Component
public class DatasetManager { public class DatasetManager {
@ -527,7 +525,7 @@ public class DatasetManager {
public String getWordDocumentText (Dataset datasetEntity) throws Exception { public String getWordDocumentText (Dataset datasetEntity) throws Exception {
DatasetWizardModel datasetWizardModel = new DatasetWizardModel().fromDataModel(datasetEntity); DatasetWizardModel datasetWizardModel = new DatasetWizardModel().fromDataModel(datasetEntity);
datasetWizardModel.setDatasetProfileDefinition(this.getPagedProfile(datasetWizardModel, datasetEntity)); datasetWizardModel.setDatasetProfileDefinition(this.getPagedProfile(datasetWizardModel, datasetEntity));
XWPFDocument document = getLightWordDocument(this.configLoader, datasetWizardModel, this.apiContext.getUtilitiesService().getVisibilityRuleService()); XWPFDocument document = getLightWordDocument(this.configLoader, datasetWizardModel, new VisibilityRuleServiceImpl());
XWPFWordExtractor extractor = new XWPFWordExtractor(document); XWPFWordExtractor extractor = new XWPFWordExtractor(document);
return extractor.getText();/*.replaceAll("\n\\s*", " ");*/ return extractor.getText();/*.replaceAll("\n\\s*", " ");*/
} }
@ -657,7 +655,7 @@ public class DatasetManager {
JSONObject obj = new JSONObject(dataset.getProperties()); JSONObject obj = new JSONObject(dataset.getProperties());
VisibilityRuleService visibilityRuleService = this.apiContext.getUtilitiesService().getVisibilityRuleService(); VisibilityRuleService visibilityRuleService = new VisibilityRuleServiceImpl();
visibilityRuleService.setProperties(obj.toMap()); visibilityRuleService.setProperties(obj.toMap());
dataset.setProfile(profile); dataset.setProfile(profile);

View File

@ -1,30 +1,26 @@
package eu.eudat.logic.services.forms; package eu.eudat.logic.services.forms;
import eu.eudat.models.data.user.components.commons.Rule; import eu.eudat.models.data.user.components.commons.Rule;
import org.springframework.stereotype.Service;
import java.util.*; import java.util.*;
/** /**
* Created by ikalyvas on 3/5/2018. * Created by ikalyvas on 3/5/2018.
*/ */
@Service("visibilityRuleService")
public class VisibilityRuleServiceImpl implements VisibilityRuleService { public class VisibilityRuleServiceImpl implements VisibilityRuleService {
private Map<String, Boolean> elementVisibility = new HashMap<>(); private final Map<String, Boolean> elementVisibility = new HashMap<>();
private VisibilityContext visibilityContext;
private Map<String, Object> properties; private Map<String, Object> properties;
public boolean isElementVisible(String id) { public boolean isElementVisible(String id) {
if (!this.elementVisibility.containsKey(id) || this.elementVisibility.get(id)) return true; return !this.elementVisibility.containsKey(id) || this.elementVisibility.get(id);
return false;
} }
public void setProperties(Map<String, Object> properties) { public void setProperties(Map<String, Object> properties) {
this.properties = properties; this.properties = properties;
this.properties.entrySet().stream() this.properties.entrySet().stream()
.filter(stringObjectEntry -> stringObjectEntry.getValue() instanceof String && ((String) stringObjectEntry.getValue()).startsWith("[") && ((String) stringObjectEntry.getValue()).endsWith("]")).forEach(stringObjectEntry -> { .filter(stringObjectEntry -> stringObjectEntry.getValue() instanceof String && ((String) stringObjectEntry.getValue()).startsWith("[")
stringObjectEntry.setValue(parseArray((String) stringObjectEntry.getValue())); && ((String) stringObjectEntry.getValue()).endsWith("]"))
}); .forEach(stringObjectEntry -> stringObjectEntry.setValue(parseArray((String) stringObjectEntry.getValue())));
} }
private List<String> parseArray(String original) { private List<String> parseArray(String original) {
@ -33,22 +29,20 @@ public class VisibilityRuleServiceImpl implements VisibilityRuleService {
} }
public void buildVisibilityContext(List<Rule> sources) { public void buildVisibilityContext(List<Rule> sources) {
this.visibilityContext = new VisibilityContext(); VisibilityContext visibilityContext = new VisibilityContext();
this.visibilityContext.buildVisibilityContext(sources); visibilityContext.buildVisibilityContext(sources);
this.visibilityContext.getVisibilityRules().forEach(item-> this.evaluateVisibility(item)); visibilityContext.getVisibilityRules().forEach(this::evaluateVisibility);
} }
private void evaluateVisibility(VisibilityRule rule) { private void evaluateVisibility(VisibilityRule rule) {
List<VisibilityRuleSource> sources = rule.getVisibilityRuleSources(); List<VisibilityRuleSource> sources = rule.getVisibilityRuleSources();
for(int i = 0; i < sources.size(); i++){ for(VisibilityRuleSource source: sources){
if (properties.containsKey(sources.get(i).getVisibilityRuleSourceId()) && (isContained(properties.get(sources.get(i).getVisibilityRuleSourceId()), sources.get(i).getVisibilityRuleSourceValue()) || properties.get(sources.get(i).getVisibilityRuleSourceId()).equals(sources.get(i).getVisibilityRuleSourceValue()))) { if (properties.containsKey(source.getVisibilityRuleSourceId())
&& isContained(properties.get(source.getVisibilityRuleSourceId()), source.getVisibilityRuleSourceValue())) {
this.elementVisibility.put(rule.getVisibilityRuleTargetId(), true); this.elementVisibility.put(rule.getVisibilityRuleTargetId(), true);
} else { } else {
if (this.elementVisibility.containsKey(rule.getVisibilityRuleTargetId())) { this.elementVisibility.put(rule.getVisibilityRuleTargetId(),
this.elementVisibility.put(rule.getVisibilityRuleTargetId(), this.elementVisibility.get(rule.getVisibilityRuleTargetId()) || false); this.elementVisibility.getOrDefault(rule.getVisibilityRuleTargetId(), false));
} else {
this.elementVisibility.put(rule.getVisibilityRuleTargetId(), false);
}
} }
} }
} }
@ -56,7 +50,8 @@ public class VisibilityRuleServiceImpl implements VisibilityRuleService {
private Boolean isContained(Object values, String source) { private Boolean isContained(Object values, String source) {
if (values instanceof List) { if (values instanceof List) {
return ((Collection<?>) values).contains(source); return ((Collection<?>) values).contains(source);
} else {
return values.equals(source);
} }
return false;
} }
} }

View File

@ -11,7 +11,5 @@ public interface UtilitiesService {
MailService getMailService(); MailService getMailService();
VisibilityRuleService getVisibilityRuleService();
ConfirmationEmailService getConfirmationEmailService(); ConfirmationEmailService getConfirmationEmailService();
} }

View File

@ -1,6 +1,7 @@
package eu.eudat.logic.services.utilities; package eu.eudat.logic.services.utilities;
import eu.eudat.logic.services.forms.VisibilityRuleService; import eu.eudat.logic.services.forms.VisibilityRuleService;
import eu.eudat.logic.services.forms.VisibilityRuleServiceImpl;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -12,22 +13,15 @@ public class UtilitiesServiceImpl implements UtilitiesService {
private InvitationService invitationService; private InvitationService invitationService;
private MailService mailService; private MailService mailService;
private VisibilityRuleService visibilityRuleService;
private ConfirmationEmailService confirmationEmailService; private ConfirmationEmailService confirmationEmailService;
@Autowired @Autowired
public UtilitiesServiceImpl(InvitationService invitationService, MailService mailService, VisibilityRuleService visibilityRuleService, ConfirmationEmailService confirmationEmailService) { public UtilitiesServiceImpl(InvitationService invitationService, MailService mailService, ConfirmationEmailService confirmationEmailService) {
this.invitationService = invitationService; this.invitationService = invitationService;
this.mailService = mailService; this.mailService = mailService;
this.visibilityRuleService = visibilityRuleService;
this.confirmationEmailService = confirmationEmailService; this.confirmationEmailService = confirmationEmailService;
} }
@Override
public VisibilityRuleService getVisibilityRuleService() {
return visibilityRuleService;
}
@Override @Override
public ConfirmationEmailService getConfirmationEmailService() { public ConfirmationEmailService getConfirmationEmailService() {
return confirmationEmailService; return confirmationEmailService;