98 lines
3.7 KiB
Java
98 lines
3.7 KiB
Java
package eu.dnetlib.uoamonitorservice.service;
|
|
|
|
import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException;
|
|
import eu.dnetlib.uoamonitorservice.dao.IndicatorDAO;
|
|
import eu.dnetlib.uoamonitorservice.dao.SectionDAO;
|
|
import eu.dnetlib.uoamonitorservice.dto.SectionFull;
|
|
import eu.dnetlib.uoamonitorservice.entities.Indicator;
|
|
import eu.dnetlib.uoamonitorservice.entities.Section;
|
|
import eu.dnetlib.uoamonitorservice.entities.SubCategory;
|
|
import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException;
|
|
import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException;
|
|
import eu.dnetlib.uoamonitorservice.primitives.Visibility;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
@Service
|
|
public class IndicatorService {
|
|
|
|
private final SectionDAO sectionDAO;
|
|
private final IndicatorDAO dao;
|
|
|
|
private final CommonService commonService;
|
|
|
|
@Autowired
|
|
public IndicatorService(SectionDAO sectionDAO, IndicatorDAO dao, CommonService commonService) {
|
|
this.sectionDAO = sectionDAO;
|
|
this.dao = dao;
|
|
this.commonService = commonService;
|
|
}
|
|
|
|
public Indicator save(Indicator indicator) {
|
|
return this.dao.save(indicator);
|
|
}
|
|
|
|
public Indicator findByPath(Section section, String indicatorId) {
|
|
if (!section.getIndicators().contains(indicatorId)) {
|
|
throw new PathNotValidException("Indicator with id: " + indicatorId + " not found in Section: " + section.getId());
|
|
}
|
|
return this.dao.findById(indicatorId).orElseThrow(() -> new EntityNotFoundException("Indicator with id: " + indicatorId + " not found"));
|
|
}
|
|
|
|
public Indicator find(String id) {
|
|
return dao.findById(id).orElseThrow(() -> new EntityNotFoundException("Indicator with id: " + id + " not found"));
|
|
}
|
|
|
|
public Indicator getIndicator(String type, String alias, String id) {
|
|
Indicator indicator = this.find(id);
|
|
if(this.commonService.hasVisibilityAuthority(type, alias, indicator)) {
|
|
return indicator;
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public void delete(String type, Indicator indicator, boolean remove) {
|
|
if(this.commonService.hasDeleteAuthority(type)) {
|
|
this.dao.findByDefaultId(indicator.getId()).forEach(child -> {
|
|
this.delete(type, child.getId(), remove);
|
|
});
|
|
if(remove) {
|
|
this.removeIndicator(indicator.getId());
|
|
}
|
|
this.dao.delete(indicator);
|
|
} else {
|
|
throw new ForbiddenException("Delete indicator: You are not authorized to delete indicator with id: " + indicator.getId());
|
|
}
|
|
}
|
|
|
|
|
|
public void delete(String type, String id, boolean remove) {
|
|
Indicator indicator = this.find(id);
|
|
this.delete(type, indicator, remove);
|
|
}
|
|
|
|
public void removeIndicator(String id) {
|
|
this.sectionDAO.findByIndicatorsContaining(id).forEach(section -> {
|
|
section.getIndicators().remove(id);
|
|
this.sectionDAO.save(section);
|
|
});
|
|
}
|
|
|
|
public Indicator changeVisibility(String type, String alias, Indicator indicator, Visibility visibility) {
|
|
if(this.commonService.hasEditAuthority(type, alias)) {
|
|
indicator.setVisibility(visibility);
|
|
return this.save(indicator);
|
|
} else {
|
|
throw new ForbiddenException("Change section visibility: You are not authorized to update section with id: " + indicator.getId());
|
|
}
|
|
}
|
|
|
|
public Indicator changeVisibility(String type, String alias, String id, Visibility visibility) {
|
|
Indicator indicator = this.find(id);
|
|
return this.changeVisibility(type, alias, indicator, visibility);
|
|
}
|
|
}
|