[new-model]: Add move indicator controller in order to move an indicator from a section to another.
This commit is contained in:
parent
eee0814016
commit
dc383dda0d
|
@ -12,8 +12,8 @@ import org.springframework.data.mongodb.MongoDbFactory;
|
|||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
|
||||
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
@Configuration
|
||||
@EnableMongoRepositories(basePackages = {"eu.dnetlib.uoamonitorservice.dao", "eu.dnetlib.uoaadmintoolslibrary.dao"})
|
||||
public class MongoConnection {
|
||||
|
|
|
@ -4,7 +4,6 @@ package eu.dnetlib.uoamonitorservice.controllers;
|
|||
import eu.dnetlib.uoamonitorservice.dto.SectionFull;
|
||||
import eu.dnetlib.uoamonitorservice.dto.StakeholderFull;
|
||||
import eu.dnetlib.uoamonitorservice.entities.*;
|
||||
import eu.dnetlib.uoamonitorservice.primitives.ReorderEvent;
|
||||
import eu.dnetlib.uoamonitorservice.primitives.Visibility;
|
||||
import eu.dnetlib.uoamonitorservice.service.*;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
|
@ -14,7 +13,6 @@ import org.springframework.security.access.prepost.PreAuthorize;
|
|||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
|
@ -98,22 +96,20 @@ public class IndicatorController {
|
|||
|
||||
|
||||
@PreAuthorize("isAuthenticated()")
|
||||
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{type}/reorder", method = RequestMethod.POST)
|
||||
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/reorder", method = RequestMethod.POST)
|
||||
public List<Indicator> reorderIndicators(@PathVariable("stakeholderId") String stakeholderId,
|
||||
@PathVariable("topicId") String topicId,
|
||||
@PathVariable("categoryId") String categoryId,
|
||||
@PathVariable("subcategoryId") String subcategoryId,
|
||||
@PathVariable("sectionId") String sectionId,
|
||||
@PathVariable("type") String type,
|
||||
@RequestBody ReorderEvent reorderEvent) {
|
||||
log.debug("reorder indicators of type: " + type);
|
||||
@RequestBody List<String> indicators) {
|
||||
log.debug("Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId + " - SubCategory: " + subcategoryId + " - Section: " + sectionId);
|
||||
Stakeholder stakeholder = this.stakeholderService.find(stakeholderId);
|
||||
Topic topic = this.topicService.findByPath(stakeholder, topicId);
|
||||
Category category = this.categoryService.findByPath(topic, categoryId);
|
||||
SubCategory subCategory = this.subCategoryService.findByPath(category, subcategoryId);
|
||||
Section section = this.sectionService.findByPath(subCategory, sectionId);
|
||||
return this.sectionService.reorderIndicators(stakeholder, section, reorderEvent).getIndicators();
|
||||
return this.sectionService.reorderIndicators(stakeholder, section, indicators).getIndicators();
|
||||
}
|
||||
|
||||
@PreAuthorize("isAuthenticated()")
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package eu.dnetlib.uoamonitorservice.controllers;
|
||||
|
||||
import eu.dnetlib.uoamonitorservice.dto.MoveIndicator;
|
||||
import eu.dnetlib.uoamonitorservice.dto.SectionFull;
|
||||
import eu.dnetlib.uoamonitorservice.dto.SubCategoryFull;
|
||||
import eu.dnetlib.uoamonitorservice.entities.*;
|
||||
import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException;
|
||||
import eu.dnetlib.uoamonitorservice.service.*;
|
||||
|
@ -70,25 +72,18 @@ public class SectionController {
|
|||
}
|
||||
|
||||
@PreAuthorize("isAuthenticated()")
|
||||
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{type}/reorder", method = RequestMethod.POST)
|
||||
public List<SectionFull> reorderSections(@PathVariable("stakeholderId") String stakeholderId,
|
||||
@PathVariable("topicId") String topicId,
|
||||
@PathVariable("categoryId") String categoryId,
|
||||
@PathVariable("subcategoryId") String subcategoryId,
|
||||
@PathVariable("type") String type,
|
||||
@RequestBody List<String> sections) {
|
||||
log.debug("reorder sections of type: " + type);
|
||||
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/moveIndicator", method = RequestMethod.POST)
|
||||
public SubCategoryFull moveIndicator(@PathVariable("stakeholderId") String stakeholderId,
|
||||
@PathVariable("topicId") String topicId,
|
||||
@PathVariable("categoryId") String categoryId,
|
||||
@PathVariable("subcategoryId") String subcategoryId,
|
||||
@RequestBody MoveIndicator moveIndicator) {
|
||||
log.debug("move indicator");
|
||||
log.debug("Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId + " - SubCategory: " + subcategoryId);
|
||||
Stakeholder stakeholder = this.stakeholderService.findByPath(stakeholderId);
|
||||
Stakeholder stakeholder = this.stakeholderService.find(stakeholderId);
|
||||
Topic topic = this.topicService.findByPath(stakeholder, topicId);
|
||||
Category category = this.categoryService.findByPath(topic, categoryId);
|
||||
SubCategory subCategory = this.subCategoryService.findByPath(category, subcategoryId);
|
||||
if (type.equals("number")) {
|
||||
return this.subCategoryService.reorderNumbers(stakeholder, subCategory, sections).getNumbers();
|
||||
} else if (type.equals("chart")) {
|
||||
return this.subCategoryService.reorderCharts(stakeholder, subCategory, sections).getCharts();
|
||||
} else {
|
||||
throw new PathNotValidException("Type is not valid");
|
||||
}
|
||||
return this.subCategoryService.moveIndicator(stakeholder, subCategory, moveIndicator);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,40 @@
|
|||
package eu.dnetlib.uoamonitorservice.dto;
|
||||
|
||||
public class MoveIndicator {
|
||||
private String target;
|
||||
private SectionInfo from;
|
||||
private SectionInfo to;
|
||||
|
||||
public MoveIndicator() {
|
||||
}
|
||||
|
||||
public MoveIndicator(String target, SectionInfo from, SectionInfo to) {
|
||||
this.target = target;
|
||||
this.from = from;
|
||||
this.to = to;
|
||||
}
|
||||
|
||||
public String getTarget() {
|
||||
return target;
|
||||
}
|
||||
|
||||
public void setTarget(String target) {
|
||||
this.target = target;
|
||||
}
|
||||
|
||||
public SectionInfo getFrom() {
|
||||
return from;
|
||||
}
|
||||
|
||||
public void setFrom(SectionInfo from) {
|
||||
this.from = from;
|
||||
}
|
||||
|
||||
public SectionInfo getTo() {
|
||||
return to;
|
||||
}
|
||||
|
||||
public void setTo(SectionInfo to) {
|
||||
this.to = to;
|
||||
}
|
||||
}
|
|
@ -3,9 +3,11 @@ package eu.dnetlib.uoamonitorservice.dto;
|
|||
import eu.dnetlib.uoamonitorservice.entities.Indicator;
|
||||
import eu.dnetlib.uoamonitorservice.entities.Section;
|
||||
import eu.dnetlib.uoamonitorservice.generics.SectionGeneric;
|
||||
import io.swagger.models.auth.In;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
public class SectionFull extends SectionGeneric<Indicator> {
|
||||
public SectionFull() {}
|
||||
|
@ -15,4 +17,16 @@ public class SectionFull extends SectionGeneric<Indicator> {
|
|||
indicators.removeIf(Objects::isNull);
|
||||
this.indicators = indicators;
|
||||
}
|
||||
|
||||
public Optional<Indicator> getIndicatorByDefaultId(String id) {
|
||||
return this.indicators.stream().filter(indicator -> indicator.getDefaultId().equals(id)).findFirst();
|
||||
}
|
||||
|
||||
public void removeIndicator(String id) {
|
||||
this.indicators.removeIf(indicator -> indicator.getId().equals(id));
|
||||
}
|
||||
|
||||
public void addIndicator(Indicator indicator) {
|
||||
this.indicators.add(indicator);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,32 @@
|
|||
package eu.dnetlib.uoamonitorservice.dto;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class SectionInfo {
|
||||
private String id;
|
||||
private List<String> indicators;
|
||||
|
||||
public SectionInfo() {
|
||||
}
|
||||
|
||||
public SectionInfo(String id, List<String> indicators) {
|
||||
this.id = id;
|
||||
this.indicators = indicators;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public List<String> getIndicators() {
|
||||
return indicators;
|
||||
}
|
||||
|
||||
public void setIndicators(List<String> indicators) {
|
||||
this.indicators = indicators;
|
||||
}
|
||||
}
|
|
@ -2,8 +2,10 @@ package eu.dnetlib.uoamonitorservice.dto;
|
|||
|
||||
import eu.dnetlib.uoamonitorservice.generics.SubCategoryGeneric;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
public class SubCategoryFull extends SubCategoryGeneric<SectionFull> {
|
||||
public SubCategoryFull() {}
|
||||
|
@ -15,4 +17,10 @@ public class SubCategoryFull extends SubCategoryGeneric<SectionFull> {
|
|||
this.numbers = numbers;
|
||||
this.charts = charts;
|
||||
}
|
||||
|
||||
public Optional<SectionFull> getSectionByDefaultId(String id) {
|
||||
List<SectionFull> sections = new ArrayList<>(this.numbers);
|
||||
sections.addAll(this.charts);
|
||||
return sections.stream().filter(section -> section.getDefaultId().equals(id)).findFirst();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package eu.dnetlib.uoamonitorservice.generics;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import eu.dnetlib.uoamonitorservice.entities.Stakeholder;
|
||||
import eu.dnetlib.uoamonitorservice.primitives.Visibility;
|
||||
import org.springframework.data.annotation.CreatedDate;
|
||||
import org.springframework.data.annotation.Id;
|
||||
|
|
|
@ -1,51 +0,0 @@
|
|||
package eu.dnetlib.uoamonitorservice.primitives;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class ReorderEvent {
|
||||
private String action; // "moved", "added", "removed"
|
||||
private String target;
|
||||
private List<String> ids;
|
||||
|
||||
public ReorderEvent() {
|
||||
}
|
||||
|
||||
public ReorderEvent(String action, String target, List<String> ids) {
|
||||
this.action = action;
|
||||
this.target = target;
|
||||
this.ids = ids;
|
||||
}
|
||||
|
||||
public String getAction() {
|
||||
return action;
|
||||
}
|
||||
|
||||
public void setAction(String action) {
|
||||
this.action = action;
|
||||
}
|
||||
|
||||
public String getTarget() {
|
||||
return target;
|
||||
}
|
||||
|
||||
public void setTarget(String target) {
|
||||
this.target = target;
|
||||
}
|
||||
|
||||
public List<String> getIds() {
|
||||
return ids;
|
||||
}
|
||||
|
||||
public void setIds(List<String> ids) {
|
||||
this.ids = ids;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ReorderEvent{" +
|
||||
"action='" + action + '\'' +
|
||||
", target='" + target + '\'' +
|
||||
", ids=" + ids +
|
||||
'}';
|
||||
}
|
||||
}
|
|
@ -5,7 +5,6 @@ import eu.dnetlib.uoamonitorservice.dao.CategoryDAO;
|
|||
import eu.dnetlib.uoamonitorservice.dao.StakeholderDAO;
|
||||
import eu.dnetlib.uoamonitorservice.dao.TopicDAO;
|
||||
import eu.dnetlib.uoamonitorservice.dto.CategoryFull;
|
||||
import eu.dnetlib.uoamonitorservice.dto.TopicFull;
|
||||
import eu.dnetlib.uoamonitorservice.entities.Category;
|
||||
import eu.dnetlib.uoamonitorservice.entities.Stakeholder;
|
||||
import eu.dnetlib.uoamonitorservice.entities.Topic;
|
||||
|
@ -16,6 +15,7 @@ import eu.dnetlib.uoamonitorservice.primitives.Visibility;
|
|||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
@ -26,7 +26,6 @@ public class CategoryService {
|
|||
private final StakeholderDAO stakeholderDAO;
|
||||
private final TopicDAO topicDAO;
|
||||
private final CategoryDAO dao;
|
||||
|
||||
private final SubCategoryService subCategoryService;
|
||||
private final CommonService commonService;
|
||||
|
||||
|
@ -75,7 +74,10 @@ public class CategoryService {
|
|||
public Category save(Category category) {
|
||||
if(category.getId() != null) {
|
||||
category.setSubCategories(this.find(category.getId()).getSubCategories());
|
||||
} else {
|
||||
category.setCreationDate(new Date());
|
||||
}
|
||||
category.setUpdateDate(new Date());
|
||||
category.getSubCategories().forEach(this.subCategoryService::find);
|
||||
return this.dao.save(category);
|
||||
}
|
||||
|
@ -127,6 +129,7 @@ public class CategoryService {
|
|||
subcategories.forEach(this.subCategoryService::find);
|
||||
if (category.getSubCategories().size() == subcategories.size() && new HashSet<>(category.getSubCategories()).containsAll(subcategories)) {
|
||||
category.setSubCategories(subcategories);
|
||||
category.setUpdateDate(new Date());
|
||||
this.reorderChildren(stakeholder, category, subcategories);
|
||||
return this.getFullCategory(stakeholder.getType(), stakeholder.getAlias(), this.dao.save(category));
|
||||
} else {
|
||||
|
@ -170,12 +173,14 @@ public class CategoryService {
|
|||
|
||||
public void addCategory(Topic topic, String id) {
|
||||
topic.addCategory(id);
|
||||
topic.setUpdateDate(new Date());
|
||||
this.topicDAO.save(topic);
|
||||
}
|
||||
|
||||
public void removeCategory(String id) {
|
||||
this.topicDAO.findByCategoriesContaining(id).forEach(topic -> {
|
||||
topic.removeCategory(id);
|
||||
topic.setUpdateDate(new Date());
|
||||
this.topicDAO.save(topic);
|
||||
});
|
||||
}
|
||||
|
|
|
@ -2,7 +2,6 @@ package eu.dnetlib.uoamonitorservice.service;
|
|||
|
||||
import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException;
|
||||
import eu.dnetlib.uoamonitorservice.dao.*;
|
||||
import eu.dnetlib.uoamonitorservice.dto.SectionFull;
|
||||
import eu.dnetlib.uoamonitorservice.entities.Indicator;
|
||||
import eu.dnetlib.uoamonitorservice.entities.Section;
|
||||
import eu.dnetlib.uoamonitorservice.entities.Stakeholder;
|
||||
|
@ -13,8 +12,8 @@ import eu.dnetlib.uoamonitorservice.primitives.Visibility;
|
|||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
public class IndicatorService {
|
||||
|
@ -60,6 +59,10 @@ public class IndicatorService {
|
|||
}
|
||||
|
||||
public Indicator save(Indicator indicator) {
|
||||
if(indicator.getId() == null) {
|
||||
indicator.setCreationDate(new Date());
|
||||
}
|
||||
indicator.setUpdateDate(new Date());
|
||||
return this.dao.save(indicator);
|
||||
}
|
||||
|
||||
|
@ -127,12 +130,14 @@ public class IndicatorService {
|
|||
|
||||
public void addIndicator(Section section, String id) {
|
||||
section.addIndicator(id);
|
||||
section.setUpdateDate(new Date());
|
||||
this.sectionDAO.save(section);
|
||||
}
|
||||
|
||||
public void removeIndicator(String id) {
|
||||
this.sectionDAO.findByIndicatorsContaining(id).forEach(section -> {
|
||||
section.removeIndicator(id);
|
||||
section.setUpdateDate(new Date());
|
||||
this.sectionDAO.save(section);
|
||||
});
|
||||
}
|
||||
|
|
|
@ -2,16 +2,21 @@ package eu.dnetlib.uoamonitorservice.service;
|
|||
|
||||
import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException;
|
||||
import eu.dnetlib.uoamonitorservice.dao.*;
|
||||
import eu.dnetlib.uoamonitorservice.dto.MoveIndicator;
|
||||
import eu.dnetlib.uoamonitorservice.dto.SectionFull;
|
||||
import eu.dnetlib.uoamonitorservice.entities.*;
|
||||
import eu.dnetlib.uoamonitorservice.dto.SubCategoryFull;
|
||||
import eu.dnetlib.uoamonitorservice.entities.Indicator;
|
||||
import eu.dnetlib.uoamonitorservice.entities.Section;
|
||||
import eu.dnetlib.uoamonitorservice.entities.Stakeholder;
|
||||
import eu.dnetlib.uoamonitorservice.entities.SubCategory;
|
||||
import eu.dnetlib.uoamonitorservice.generics.Common;
|
||||
import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException;
|
||||
import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException;
|
||||
import eu.dnetlib.uoamonitorservice.primitives.ReorderEvent;
|
||||
import eu.dnetlib.uoamonitorservice.primitives.Visibility;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
@ -70,7 +75,10 @@ public class SectionService {
|
|||
public Section save(Section section) {
|
||||
if (section.getId() != null) {
|
||||
section.setIndicators(this.find(section.getId()).getIndicators());
|
||||
} else {
|
||||
section.setCreationDate(new Date());
|
||||
}
|
||||
section.setUpdateDate(new Date());
|
||||
section.getIndicators().forEach(this.indicatorService::find);
|
||||
return this.dao.save(section);
|
||||
}
|
||||
|
@ -130,18 +138,18 @@ public class SectionService {
|
|||
});
|
||||
}
|
||||
|
||||
public SectionFull reorderIndicators(Stakeholder stakeholder, Section section, ReorderEvent event) {
|
||||
public SectionFull reorderIndicators(Stakeholder stakeholder, Section section, List<String> indicators) {
|
||||
return this.reorderIndicators(stakeholder, section, indicators, true);
|
||||
}
|
||||
|
||||
public SectionFull reorderIndicators(Stakeholder stakeholder, Section section, List<String> indicators, boolean reorderChildren) {
|
||||
if (this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) {
|
||||
event.getIds().forEach(this.indicatorService::find);
|
||||
if (event.getAction().equals("added")) {
|
||||
section.addIndicator(event.getTarget());
|
||||
} else if (event.getAction().equals("removed")) {
|
||||
section.removeIndicator(event.getTarget());
|
||||
}
|
||||
if (section.getIndicators().size() == event.getIds().size() && new HashSet<>(section.getIndicators()).containsAll(event.getIds())) {
|
||||
section.setIndicators(event.getIds());
|
||||
if(event.getAction().equals("moved")) {
|
||||
this.reorderChildren(stakeholder, section, event);
|
||||
indicators.forEach(this.indicatorService::find);
|
||||
if (section.getIndicators().size() == indicators.size() && new HashSet<>(section.getIndicators()).containsAll(indicators)) {
|
||||
section.setIndicators(indicators);
|
||||
section.setUpdateDate(new Date());
|
||||
if(reorderChildren) {
|
||||
this.reorderChildren(stakeholder, section, indicators);
|
||||
}
|
||||
return this.getFullSection(stakeholder.getType(), stakeholder.getAlias(), this.dao.save(section));
|
||||
} else {
|
||||
|
@ -152,30 +160,11 @@ public class SectionService {
|
|||
}
|
||||
}
|
||||
|
||||
public void reorderChildren(Stakeholder defaultStakeholder, Section defaultSection, ReorderEvent event) {
|
||||
public void reorderChildren(Stakeholder defaultStakeholder, Section defaultSection, List<String> defaultIndicators) {
|
||||
this.stakeholderDAO.findByDefaultId(defaultStakeholder.getId()).forEach(stakeholder -> {
|
||||
this.dao.findByDefaultId(defaultSection.getId()).stream().map(section -> this.getFullSection(stakeholder.getType(), stakeholder.getAlias(), section)).forEach(section -> {
|
||||
Indicator target;
|
||||
if(section.isNumber()) {
|
||||
target = this.subCategoryDAO.findByNumbersContaining(section.getId()).stream().flatMap(subCategory -> subCategory.getNumbers().stream())
|
||||
.map(id -> this.getFullSection(stakeholder.getType(), stakeholder.getAlias(), id)).flatMap(sectionFull -> section.getIndicators().stream())
|
||||
.filter(indicator -> indicator.getDefaultId().equals(event.getTarget())).findFirst().orElse(null);
|
||||
} else {
|
||||
target = this.subCategoryDAO.findByChartsContaining(section.getId()).stream().flatMap(subCategory -> subCategory.getCharts().stream())
|
||||
.map(id -> this.getFullSection(stakeholder.getType(), stakeholder.getAlias(), id)).flatMap(sectionFull -> section.getIndicators().stream())
|
||||
.filter(indicator -> indicator.getDefaultId().equals(event.getTarget())).findFirst().orElse(null);
|
||||
}
|
||||
if (target != null) {
|
||||
List<Common> indicators = section.getIndicators().stream().map(indicator -> (Common) indicator).collect(Collectors.toList());
|
||||
if(event.getAction().equals("removed")) {
|
||||
indicators.removeIf(indicator -> indicator.getId().equals(target.getId()));
|
||||
} else if(event.getAction().equals("added")) {
|
||||
indicators.add(target);
|
||||
}
|
||||
List<String> ids = this.commonService.reorder(event.getIds(), indicators);
|
||||
ReorderEvent childEvent = new ReorderEvent(event.getAction(), target.getId(), ids);
|
||||
this.reorderIndicators(stakeholder, new Section(section), childEvent);
|
||||
}
|
||||
this.reorderIndicators(stakeholder, new Section(section),
|
||||
this.commonService.reorder(defaultIndicators, section.getIndicators().stream().map(indicator -> (Common) indicator).collect(Collectors.toList())));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
@ -212,16 +201,19 @@ public class SectionService {
|
|||
} else {
|
||||
subCategory.addChart(id, index);
|
||||
}
|
||||
subCategory.setUpdateDate(new Date());
|
||||
this.subCategoryDAO.save(subCategory);
|
||||
}
|
||||
|
||||
public void removeSection(String id) {
|
||||
this.subCategoryDAO.findByNumbersContaining(id).forEach(subCategory -> {
|
||||
subCategory.removeNumber(id);
|
||||
subCategory.setUpdateDate(new Date());
|
||||
this.subCategoryDAO.save(subCategory);
|
||||
});
|
||||
this.subCategoryDAO.findByChartsContaining(id).forEach(subCategory -> {
|
||||
subCategory.removeChart(id);
|
||||
subCategory.setUpdateDate(new Date());
|
||||
this.subCategoryDAO.save(subCategory);
|
||||
});
|
||||
}
|
||||
|
|
|
@ -10,6 +10,7 @@ import eu.dnetlib.uoamonitorservice.primitives.Visibility;
|
|||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
@ -99,7 +100,10 @@ public class StakeholderService {
|
|||
public Stakeholder save(Stakeholder stakeholder) {
|
||||
if (stakeholder.getId() != null) {
|
||||
stakeholder.setTopics(this.find(stakeholder.getId()).getTopics());
|
||||
} else {
|
||||
stakeholder.setCreationDate(new Date());
|
||||
}
|
||||
stakeholder.setUpdateDate(new Date());
|
||||
stakeholder.getTopics().forEach(this.topicService::find);
|
||||
return this.dao.save(stakeholder);
|
||||
}
|
||||
|
@ -109,6 +113,7 @@ public class StakeholderService {
|
|||
topics.forEach(this.topicService::find);
|
||||
if (stakeholder.getTopics().size() == topics.size() && new HashSet<>(stakeholder.getTopics()).containsAll(topics)) {
|
||||
stakeholder.setTopics(topics);
|
||||
stakeholder.setUpdateDate(new Date());
|
||||
this.reorderChildren(stakeholder, topics);
|
||||
return this.getFullStakeholder(this.dao.save(stakeholder));
|
||||
} else {
|
||||
|
|
|
@ -5,18 +5,21 @@ import eu.dnetlib.uoamonitorservice.dao.CategoryDAO;
|
|||
import eu.dnetlib.uoamonitorservice.dao.StakeholderDAO;
|
||||
import eu.dnetlib.uoamonitorservice.dao.SubCategoryDAO;
|
||||
import eu.dnetlib.uoamonitorservice.dao.TopicDAO;
|
||||
import eu.dnetlib.uoamonitorservice.dto.MoveIndicator;
|
||||
import eu.dnetlib.uoamonitorservice.dto.SectionFull;
|
||||
import eu.dnetlib.uoamonitorservice.dto.SectionInfo;
|
||||
import eu.dnetlib.uoamonitorservice.dto.SubCategoryFull;
|
||||
import eu.dnetlib.uoamonitorservice.entities.Category;
|
||||
import eu.dnetlib.uoamonitorservice.entities.Stakeholder;
|
||||
import eu.dnetlib.uoamonitorservice.entities.SubCategory;
|
||||
import eu.dnetlib.uoamonitorservice.entities.*;
|
||||
import eu.dnetlib.uoamonitorservice.generics.Common;
|
||||
import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException;
|
||||
import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException;
|
||||
import eu.dnetlib.uoamonitorservice.primitives.IndicatorType;
|
||||
import eu.dnetlib.uoamonitorservice.primitives.Visibility;
|
||||
import io.swagger.models.auth.In;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
@ -83,7 +86,10 @@ public class SubCategoryService {
|
|||
SubCategory old = this.find(subCategory.getId());
|
||||
subCategory.setNumbers(old.getNumbers());
|
||||
subCategory.setCharts(old.getCharts());
|
||||
} else {
|
||||
subCategory.setCreationDate(new Date());
|
||||
}
|
||||
subCategory.setUpdateDate(new Date());
|
||||
subCategory.getNumbers().forEach(this.sectionService::find);
|
||||
subCategory.getCharts().forEach(this.sectionService::find);
|
||||
return this.dao.save(subCategory);
|
||||
|
@ -128,11 +134,49 @@ public class SubCategoryService {
|
|||
});
|
||||
}
|
||||
|
||||
public SubCategoryFull moveIndicator(Stakeholder stakeholder, SubCategory subCategory, MoveIndicator moveIndicator) {
|
||||
if (this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) {
|
||||
Section from = this.sectionService.findByPath(subCategory, moveIndicator.getFrom().getId());
|
||||
Section to = this.sectionService.findByPath(subCategory, moveIndicator.getTo().getId());
|
||||
from.removeIndicator(moveIndicator.getTarget());
|
||||
to.addIndicator(moveIndicator.getTarget());
|
||||
this.sectionService.reorderIndicators(stakeholder, from, moveIndicator.getFrom().getIndicators(), false);
|
||||
this.sectionService.reorderIndicators(stakeholder, to, moveIndicator.getTo().getIndicators(), false);
|
||||
this.moveIndicatorChildren(stakeholder, subCategory, moveIndicator);
|
||||
return this.getFullSubCategory(stakeholder.getType(), stakeholder.getAlias(), subCategory);
|
||||
} else {
|
||||
throw new ForbiddenException("You are not authorized to move indicators in subCategory with id: " + subCategory.getId());
|
||||
}
|
||||
}
|
||||
|
||||
public void moveIndicatorChildren(Stakeholder defaultStakeholder, SubCategory defaultSubCategory, MoveIndicator moveIndicator) {
|
||||
this.stakeholderDAO.findByDefaultId(defaultStakeholder.getId()).forEach(stakeholder -> {
|
||||
this.dao.findByDefaultId(defaultSubCategory.getId()).stream()
|
||||
.map(subCategory -> this.getFullSubCategory(stakeholder.getType(), stakeholder. getAlias(), subCategory))
|
||||
.collect(Collectors.toList()).forEach(subCategory -> {
|
||||
SectionFull from = subCategory.getSectionByDefaultId(moveIndicator.getFrom().getId()).orElse(null);
|
||||
SectionFull to = subCategory.getSectionByDefaultId(moveIndicator.getTo().getId()).orElse(null);
|
||||
if(from != null && to != null) {
|
||||
Indicator target = from.getIndicatorByDefaultId(moveIndicator.getTarget()).orElse(null);
|
||||
if(target != null) {
|
||||
from.removeIndicator(target.getId());
|
||||
to.addIndicator(target);
|
||||
MoveIndicator moveIndicatorChild = new MoveIndicator(target.getId(),
|
||||
new SectionInfo(from.getId(), this.commonService.reorder(moveIndicator.getFrom().getIndicators(), from.getIndicators().stream().map(section -> (Common) section).collect(Collectors.toList()))),
|
||||
new SectionInfo(to.getId(), this.commonService.reorder(moveIndicator.getTo().getIndicators(), to.getIndicators().stream().map(section -> (Common) section).collect(Collectors.toList()))));
|
||||
this.moveIndicator(stakeholder, new SubCategory(subCategory), moveIndicatorChild);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
public SubCategoryFull reorderNumbers(Stakeholder stakeholder, SubCategory subCategory, List<String> numbers) {
|
||||
if(this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) {
|
||||
numbers.forEach(this.sectionService::find);
|
||||
if (subCategory.getNumbers().size() == numbers.size() && new HashSet<>(subCategory.getNumbers()).containsAll(numbers)) {
|
||||
subCategory.setNumbers(numbers);
|
||||
subCategory.setUpdateDate(new Date());
|
||||
this.reorderChildrenNumbers(stakeholder, subCategory, numbers);
|
||||
return this.getFullSubCategory(stakeholder.getType(), stakeholder.getAlias(), this.dao.save(subCategory));
|
||||
} else {
|
||||
|
@ -204,12 +248,14 @@ public class SubCategoryService {
|
|||
|
||||
public void addSubCategory(Category category, String id) {
|
||||
category.addSubCategory(id);
|
||||
category.setUpdateDate(new Date());
|
||||
this.categoryDAO.save(category);
|
||||
}
|
||||
|
||||
public void removeSubCategory(String id) {
|
||||
this.categoryDAO.findBySubCategoriesContaining(id).forEach(category -> {
|
||||
category.removeSubCategory(id);
|
||||
category.setUpdateDate(new Date());
|
||||
this.categoryDAO.save(category);
|
||||
});
|
||||
}
|
||||
|
|
|
@ -13,6 +13,7 @@ import eu.dnetlib.uoamonitorservice.primitives.Visibility;
|
|||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
@ -69,7 +70,10 @@ public class TopicService {
|
|||
public Topic save(Topic topic) {
|
||||
if(topic.getId() != null) {
|
||||
topic.setCategories(this.find(topic.getId()).getCategories());
|
||||
} else {
|
||||
topic.setCreationDate(new Date());
|
||||
}
|
||||
topic.setUpdateDate(new Date());
|
||||
topic.getCategories().forEach(this.categoryService::find);
|
||||
return this.dao.save(topic);
|
||||
}
|
||||
|
@ -113,6 +117,7 @@ public class TopicService {
|
|||
if (topic.getCategories().size() == categories.size() && new HashSet<>(topic.getCategories()).containsAll(categories)) {
|
||||
topic.setCategories(categories);
|
||||
this.reorderChildren(stakeholder, topic, categories);
|
||||
topic.setUpdateDate(new Date());
|
||||
return this.getFullTopic(stakeholder.getType(), stakeholder.getAlias(), this.dao.save(topic));
|
||||
} else {
|
||||
throw new EntityNotFoundException("Some categories dont exist in the topic with id " + topic.getId());
|
||||
|
@ -155,12 +160,14 @@ public class TopicService {
|
|||
|
||||
public void addTopic(Stakeholder stakeholder, String id) {
|
||||
stakeholder.addTopic(id);
|
||||
stakeholder.setUpdateDate(new Date());
|
||||
this.stakeholderDAO.save(stakeholder);
|
||||
}
|
||||
|
||||
public void removeTopic(String id) {
|
||||
this.stakeholderDAO.findByTopicsContaining(id).forEach(stakeholder -> {
|
||||
stakeholder.removeTopic(id);
|
||||
stakeholder.setUpdateDate(new Date());
|
||||
this.stakeholderDAO.save(stakeholder);
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue