2019-11-22 16:50:59 +01:00
package eu.dnetlib.uoamonitorservice.controllers ;
import eu.dnetlib.uoamonitorservice.dao.* ;
import eu.dnetlib.uoamonitorservice.entities.* ;
import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException ;
import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException ;
import org.apache.log4j.Logger ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.web.bind.annotation.* ;
import java.util.ArrayList ;
2020-06-22 21:58:40 +02:00
import java.util.Iterator ;
2019-11-22 16:50:59 +01:00
import java.util.List ;
@RestController
@CrossOrigin ( origins = " * " )
public class SubCategoryController {
private final Logger log = Logger . getLogger ( this . getClass ( ) ) ;
@Autowired
private StakeholderDAO stakeholderDAO ;
@Autowired
private TopicDAO topicDAO ;
@Autowired
private CategoryDAO categoryDAO ;
@Autowired
private SubCategoryDAO subCategoryDAO ;
2020-01-10 11:48:24 +01:00
@Autowired
private SectionDAO sectionDAO ;
2019-11-22 16:50:59 +01:00
@Autowired
private IndicatorDAO indicatorDAO ;
2020-01-10 11:48:24 +01:00
@Autowired
private SectionController sectionController ;
public SubCategory < Section < Indicator > > buildSubCategory ( SubCategory < Section < Indicator > > subcategoryFull ) {
2019-11-22 16:50:59 +01:00
SubCategory < String > subCategory = new SubCategory < > ( subcategoryFull ) ;
2020-01-10 11:48:24 +01:00
List < String > sectionCharts = new ArrayList < > ( ) ;
List < Section < Indicator > > sectionChartsFull = new ArrayList < > ( ) ;
for ( Section section : subcategoryFull . getCharts ( ) ) {
Section < Indicator > sectionFull = sectionController . buildSection ( section ) ;
sectionChartsFull . add ( sectionFull ) ;
sectionCharts . add ( sectionFull . getId ( ) ) ;
2019-11-22 16:50:59 +01:00
}
2020-01-10 11:48:24 +01:00
subcategoryFull . setCharts ( sectionChartsFull ) ;
subCategory . setCharts ( sectionCharts ) ;
List < String > sectionNumbers = new ArrayList < > ( ) ;
List < Section < Indicator > > sectionNumbersFull = new ArrayList < > ( ) ;
for ( Section section : subcategoryFull . getNumbers ( ) ) {
Section < Indicator > sectionFull = sectionController . buildSection ( section ) ;
sectionNumbersFull . add ( sectionFull ) ;
sectionNumbers . add ( sectionFull . getId ( ) ) ;
2019-11-22 16:50:59 +01:00
}
2020-01-10 11:48:24 +01:00
subcategoryFull . setNumbers ( sectionNumbersFull ) ;
subCategory . setNumbers ( sectionNumbers ) ;
2019-11-22 16:50:59 +01:00
2019-12-19 16:04:25 +01:00
subCategoryDAO . save ( subCategory ) ;
2019-11-22 16:50:59 +01:00
2019-12-19 16:04:25 +01:00
subcategoryFull . setId ( subCategory . getId ( ) ) ;
2019-11-22 16:50:59 +01:00
return subcategoryFull ;
}
@RequestMapping ( value = " /{stakeholderId}/{topicId}/{categoryId}/save " , method = RequestMethod . POST )
2020-01-10 11:48:24 +01:00
public SubCategory < Section < Indicator > > saveSubCategory ( @PathVariable ( " stakeholderId " ) String stakeholderId ,
@PathVariable ( " topicId " ) String topicId ,
@PathVariable ( " categoryId " ) String categoryId ,
@RequestBody SubCategory < Section < Indicator > > subcategoryFull ) {
2019-11-22 16:50:59 +01:00
log . debug ( " save subcategory " ) ;
2019-12-19 16:04:25 +01:00
log . debug ( " Alias: " + subcategoryFull . getAlias ( ) + " - Id: " + subcategoryFull . getId ( ) + " - Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId ) ;
2019-11-22 16:50:59 +01:00
2020-01-10 11:48:24 +01:00
Category category = checkForExceptions ( stakeholderId , topicId , categoryId ) ;
2020-05-18 11:17:05 +02:00
SubCategory < String > oldSubcategory = null ;
if ( subcategoryFull . getId ( ) ! = null ) {
oldSubcategory = subCategoryDAO . findById ( subcategoryFull . getId ( ) ) ;
}
2020-01-10 11:48:24 +01:00
SubCategory < String > subCategory = new SubCategory < > ( subcategoryFull ) ;
2019-11-22 16:50:59 +01:00
2020-01-10 11:48:24 +01:00
// List<String> charts = new ArrayList<>();
// for(Indicator chart : subcategoryFull.getCharts()) {
// charts.add(chart.getId());
// }
// subCategory.setCharts(charts);
//
// List<String> numbers = new ArrayList<>();
// for(Indicator numbr : subcategoryFull.getNumbers()) {
// numbers.add(numbr.getId());
// }
// subCategory.setNumbers(numbers);
List < String > chartSections = new ArrayList < > ( ) ;
for ( Section chartSection : subcategoryFull . getCharts ( ) ) {
chartSections . add ( chartSection . getId ( ) ) ;
}
subCategory . setCharts ( chartSections ) ;
List < String > numberSections = new ArrayList < > ( ) ;
for ( Section numberSection : subcategoryFull . getNumbers ( ) ) {
numberSections . add ( numberSection . getId ( ) ) ;
}
subCategory . setNumbers ( numberSections ) ;
subCategoryDAO . save ( subCategory ) ;
Stakeholder < String > stakeholder = stakeholderDAO . findById ( stakeholderId ) ;
if ( stakeholder . getDefaultId ( ) = = null ) {
if ( subcategoryFull . getId ( ) = = null ) {
onSaveDefaultSubCategory ( subCategory , categoryId ) ;
2019-11-22 16:50:59 +01:00
} else {
2020-05-18 11:17:05 +02:00
onUpdateDefaultSubCategory ( subCategory , oldSubcategory ) ;
2019-11-22 16:50:59 +01:00
}
}
2020-01-10 11:48:24 +01:00
List < String > subcategories = category . getSubCategories ( ) ;
int index = subcategories . indexOf ( subCategory . getId ( ) ) ;
if ( index = = - 1 ) {
subcategories . add ( subCategory . getId ( ) ) ;
categoryDAO . save ( category ) ;
log . debug ( " Subcategory saved! " ) ;
subcategoryFull . setId ( subCategory . getId ( ) ) ;
}
chartSections = null ;
numberSections = null ;
subCategory = null ;
2019-11-22 16:50:59 +01:00
return subcategoryFull ;
}
2019-12-19 16:04:25 +01:00
public void onSaveDefaultSubCategory ( SubCategory subCategory , String categoryId ) {
log . debug ( " On save default subCategory " ) ;
List < Category > categories = categoryDAO . findByDefaultId ( categoryId ) ;
for ( Category category : categories ) {
SubCategory subCategoryNew = new SubCategory ( ) ;
subCategoryNew . copyFromDefault ( subCategory ) ;
subCategoryDAO . save ( subCategoryNew ) ;
List < String > subCategories = category . getSubCategories ( ) ;
subCategories . add ( subCategoryNew . getId ( ) ) ;
categoryDAO . save ( category ) ;
}
}
2020-05-18 11:17:05 +02:00
public void onUpdateDefaultSubCategory ( SubCategory subCategory , SubCategory oldSubcategory ) {
2019-12-19 16:04:25 +01:00
log . debug ( " On update default subCategory " ) ;
List < SubCategory > subCategories = subCategoryDAO . findByDefaultId ( subCategory . getId ( ) ) ;
boolean changed = false ;
for ( SubCategory subCategoryBasedOnDefault : subCategories ) {
2020-05-18 11:17:05 +02:00
if ( subCategory . getName ( ) ! = null & & ! subCategory . getName ( ) . equals ( subCategoryBasedOnDefault . getName ( ) )
& & ( oldSubcategory . getName ( ) = = null | | oldSubcategory . getName ( ) . equals ( subCategoryBasedOnDefault . getName ( ) ) ) ) {
subCategoryBasedOnDefault . setName ( subCategory . getName ( ) ) ;
2020-09-28 13:59:00 +02:00
subCategoryBasedOnDefault . setAlias ( subCategory . getAlias ( ) ) ;
2019-12-19 16:04:25 +01:00
changed = true ;
}
2020-05-18 11:17:05 +02:00
if ( subCategory . getDescription ( ) ! = null & & ! subCategory . getDescription ( ) . equals ( subCategoryBasedOnDefault . getDescription ( ) )
& & ( oldSubcategory . getDescription ( ) = = null | | oldSubcategory . getDescription ( ) . equals ( subCategoryBasedOnDefault . getDescription ( ) ) ) ) {
subCategoryBasedOnDefault . setDescription ( subCategory . getDescription ( ) ) ;
2019-12-19 16:04:25 +01:00
changed = true ;
}
if ( ! changed ) {
2020-05-18 11:17:05 +02:00
// break;
continue ;
2019-12-19 16:04:25 +01:00
}
2020-05-18 11:17:05 +02:00
// subCategoryBasedOnDefault.setName(subCategory.getName());
// subCategoryBasedOnDefault.setDescription(subCategory.getDescription());
2019-12-19 16:04:25 +01:00
subCategoryDAO . save ( subCategoryBasedOnDefault ) ;
}
}
2019-11-22 16:50:59 +01:00
@RequestMapping ( value = " /{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/delete " , method = RequestMethod . DELETE )
public boolean deleteSubCategory ( @PathVariable ( " stakeholderId " ) String stakeholderId ,
@PathVariable ( " topicId " ) String topicId ,
@PathVariable ( " categoryId " ) String categoryId ,
2020-06-22 21:58:40 +02:00
@PathVariable ( " subcategoryId " ) String subcategoryId ,
@RequestParam ( required = false ) String children ) {
2019-11-22 16:50:59 +01:00
log . debug ( " delete subcategory " ) ;
2019-12-19 16:04:25 +01:00
log . debug ( " Id: " + subcategoryId + " - Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId ) ;
2019-11-22 16:50:59 +01:00
2020-01-10 11:48:24 +01:00
Category category = checkForExceptions ( stakeholderId , topicId , categoryId ) ;
SubCategory < String > subcategory = subCategoryDAO . findById ( subcategoryId ) ;
if ( subcategory ! = null ) {
List < String > subcategories = category . getSubCategories ( ) ;
int index = subcategories . indexOf ( subcategoryId ) ;
if ( index ! = - 1 ) {
2020-06-22 21:58:40 +02:00
// this subCategory belongs in default profile
if ( category . getDefaultId ( ) = = null & & children ! = null ) {
onDeleteDefaultSubCategory ( subcategoryId , categoryId , children ) ;
2019-11-22 16:50:59 +01:00
}
2020-01-10 11:48:24 +01:00
2020-06-22 21:58:40 +02:00
// for(String chartSectionId : subcategory.getCharts()) {
// Section<String> chartSection = sectionDAO.findById(chartSectionId);
// if (chartSection == null) {
// // EXCEPTION - Section not found
// throw new EntityNotFoundException("Delete SubCategory: Section with id: "+chartSectionId+" not found (section exists in subcategory: "+subcategoryId+")");
// }
//
// for (String chartId : chartSection.getIndicators()) {
// indicatorDAO.delete(chartId);
// }
// subcategory.setCharts(null);
// sectionDAO.delete(chartSectionId);
// }
//
// for(String numberSectionId : subcategory.getNumbers()) {
// Section<String> numberSection = sectionDAO.findById(numberSectionId);
// if (numberSection == null) {
// // EXCEPTION - Section not found
// throw new EntityNotFoundException("Delete SubCategory: Section with id: "+numberSectionId+" not found (section exists in subcategory: "+subcategoryId+")");
// }
//
// for (String numberId : numberSection.getIndicators()) {
// indicatorDAO.delete(numberId);
// }
// subcategory.setNumbers(null);
// sectionDAO.delete(numberSectionId);
// }
2020-01-10 11:48:24 +01:00
2020-06-22 21:58:40 +02:00
sectionController . deleteTree ( subcategory ) ;
subcategory . setCharts ( null ) ;
subcategory . setNumbers ( null ) ;
2020-01-10 11:48:24 +01:00
subcategories . remove ( index ) ;
categoryDAO . save ( category ) ;
subCategoryDAO . delete ( subcategoryId ) ;
log . debug ( " Subcategory deleted! " ) ;
2019-11-22 16:50:59 +01:00
} else {
2020-01-10 11:48:24 +01:00
// EXCEPTION - SubCategory not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias();
throw new PathNotValidException ( " Delete subcategory: Subcategory with id: " + subcategoryId + " not found in Category: " + categoryId ) ;
2019-11-22 16:50:59 +01:00
}
2020-01-10 11:48:24 +01:00
2019-11-22 16:50:59 +01:00
} else {
2020-01-10 11:48:24 +01:00
// EXCEPTION - SubCategory not found
throw new EntityNotFoundException ( " Delete subcategory: SubCategory with id: " + subcategoryId + " not found " ) ;
2019-11-22 16:50:59 +01:00
}
return true ;
}
2020-06-22 21:58:40 +02:00
public boolean onDeleteDefaultSubCategory ( String defaultSubCategoryId , String defaultCategoryId , String children ) {
if ( children . equals ( " delete " ) ) {
List < Category > categories = categoryDAO . findByDefaultId ( defaultCategoryId ) ;
List < SubCategory > subCategories = subCategoryDAO . findByDefaultId ( defaultSubCategoryId ) ;
for ( Category category : categories ) {
Iterator < SubCategory > subCategoriesIterator = subCategories . iterator ( ) ;
while ( subCategoriesIterator . hasNext ( ) ) {
SubCategory subCategory = subCategoriesIterator . next ( ) ;
String subCategoryId = subCategory . getId ( ) ;
if ( category . getSubCategories ( ) ! = null & & category . getSubCategories ( ) . contains ( subCategoryId ) ) {
subCategoriesIterator . remove ( ) ;
category . getSubCategories ( ) . remove ( subCategoryId ) ;
categoryDAO . save ( category ) ;
sectionController . deleteTree ( subCategory ) ;
subCategoryDAO . delete ( subCategoryId ) ;
log . debug ( " SubCategory with id: " + subCategoryId + " deleted! " ) ;
break ;
}
}
}
} else if ( children . equals ( " disconnect " ) ) {
List < SubCategory > subCategories = subCategoryDAO . findByDefaultId ( defaultSubCategoryId ) ;
for ( SubCategory subCategory : subCategories ) {
sectionController . disConnectTree ( subCategory ) ;
subCategory . setDefaultId ( null ) ;
subCategoryDAO . save ( subCategory ) ;
log . debug ( " DefaultId for SubCategory with id: " + subCategory . getId ( ) + " empty! " ) ;
}
}
return true ;
}
2020-06-25 11:55:46 +02:00
@RequestMapping ( value = " /{stakeholderId}/{topicId}/{categoryId}/reorder " , method = RequestMethod . POST )
public List < SubCategory > reorderSubCategories ( @PathVariable ( " stakeholderId " ) String stakeholderId ,
@PathVariable ( " topicId " ) String topicId ,
@PathVariable ( " categoryId " ) String categoryId ,
@RequestBody List < String > subCategories ) {
log . debug ( " reorder subCategories " ) ;
log . debug ( " Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId ) ;
Category < String > category = checkForExceptions ( stakeholderId , topicId , categoryId ) ;
category . setSubCategories ( subCategories ) ;
categoryDAO . save ( category ) ;
log . debug ( " SubCategories reordered! " ) ;
List < SubCategory > subCategoriesFull = new ArrayList < > ( ) ;
for ( String subCategoryId : subCategories ) {
subCategoriesFull . add ( subCategoryDAO . findById ( subCategoryId ) ) ;
}
return subCategoriesFull ;
}
2019-12-23 12:45:07 +01:00
@RequestMapping ( value = " /{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/toggle-status " , method = RequestMethod . POST )
public Boolean toggleSubCategoryStatus ( @PathVariable ( " stakeholderId " ) String stakeholderId ,
@PathVariable ( " topicId " ) String topicId ,
@PathVariable ( " categoryId " ) String categoryId ,
@PathVariable ( " subcategoryId " ) String subcategoryId ) {
log . debug ( " toggle subCategory status (isActive) " ) ;
log . debug ( " Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId + " - SubCategory: " + subcategoryId ) ;
SubCategory subCategory = subCategoryDAO . findById ( subcategoryId ) ;
if ( subCategory = = null ) {
// EXCEPTION - SubCategory not found
throw new EntityNotFoundException ( " Toggle subCategory status: SubCategory with id: " + subcategoryId + " not found " ) ;
}
subCategory . setIsActive ( ! subCategory . getIsActive ( ) ) ;
this . toggleSubCategory ( stakeholderId , topicId , categoryId , subCategory ) ;
return subCategory . getIsActive ( ) ;
}
@RequestMapping ( value = " /{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/toggle-access " , method = RequestMethod . POST )
public Boolean toggleSubCategoryAccess ( @PathVariable ( " stakeholderId " ) String stakeholderId ,
@PathVariable ( " topicId " ) String topicId ,
@PathVariable ( " categoryId " ) String categoryId ,
@PathVariable ( " subcategoryId " ) String subcategoryId ) {
log . debug ( " toggle subCategory access (isPublic) " ) ;
log . debug ( " Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId + " - SubCategory: " + subcategoryId ) ;
SubCategory subCategory = subCategoryDAO . findById ( subcategoryId ) ;
if ( subCategory = = null ) {
// EXCEPTION - SubCategory not found
throw new EntityNotFoundException ( " Toggle subCategory access: SubCategory with id: " + subcategoryId + " not found " ) ;
}
subCategory . setIsPublic ( ! subCategory . getIsPublic ( ) ) ;
this . toggleSubCategory ( stakeholderId , topicId , categoryId , subCategory ) ;
return subCategory . getIsPublic ( ) ;
}
public void toggleSubCategory ( String stakeholderId , String topicId , String categoryId , SubCategory subcategory ) {
2020-01-10 11:48:24 +01:00
Category category = checkForExceptions ( stakeholderId , topicId , categoryId ) ;
2019-12-23 12:45:07 +01:00
2020-01-10 11:48:24 +01:00
if ( category . getSubCategories ( ) . contains ( subcategory . getId ( ) ) ) {
subCategoryDAO . save ( subcategory ) ;
log . debug ( " SubCategory toggled! " ) ;
2019-12-23 12:45:07 +01:00
} else {
2020-01-10 11:48:24 +01:00
// EXCEPTION - SubCategory not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias();
throw new PathNotValidException ( " Toggle subCategory: SubCategory with id: " + subcategory . getId ( ) + " not found in Category: " + categoryId ) ;
}
}
private Category checkForExceptions ( String stakeholderId , String topicId , String categoryId ) {
Stakeholder < String > stakeholder = stakeholderDAO . findById ( stakeholderId ) ;
if ( stakeholder = = null ) {
2019-12-23 12:45:07 +01:00
// EXCEPTION - Stakeholder not found
2020-01-10 11:48:24 +01:00
throw new EntityNotFoundException ( " Save indicator: Stakeholder with id: " + stakeholderId + " not found " ) ;
}
Topic < String > topic = topicDAO . findById ( topicId ) ;
if ( topic = = null ) {
// EXCEPTION - Topic not found
throw new EntityNotFoundException ( " Save indicator: Topic with id: " + topicId + " not found " ) ;
2019-12-23 12:45:07 +01:00
}
2020-01-10 11:48:24 +01:00
if ( ! stakeholder . getTopics ( ) . contains ( topicId ) ) {
// EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
throw new PathNotValidException ( " Save indicator: Topic with id: " + topicId + " not found in Stakeholder: " + stakeholderId ) ;
}
Category < String > category = categoryDAO . findById ( categoryId ) ;
if ( category = = null ) {
// EXCEPTION - Category not found
throw new EntityNotFoundException ( " Save indicator: Category with id: " + categoryId + " not found " ) ;
}
if ( ! topic . getCategories ( ) . contains ( categoryId ) ) {
// EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias();
throw new PathNotValidException ( " Save indicator: Category with id: " + categoryId + " not found in Topic: " + topicId ) ;
}
// SubCategory<String> subcategory = subCategoryDAO.findById(subcategoryId);
// if(subcategory == null) {
// // EXCEPTION - SubCategory not found
// throw new EntityNotFoundException("Save indicator: SubCategory with id: "+subcategoryId+" not found");
// }
//
// if (!category.getSubCategories().contains(subcategoryId)) {
// // EXCEPTION - SubCategory not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias();
// throw new PathNotValidException("Save indicator: SubCategory with id: "+subcategoryId+" not found in Category: "+categoryId);
// }
return category ;
2019-12-23 12:45:07 +01:00
}
2020-06-22 21:58:40 +02:00
public void deleteTree ( Category category ) {
List < String > subCategories = category . getSubCategories ( ) ;
for ( String subCategoryId : subCategories ) {
SubCategory subCategory = subCategoryDAO . findById ( subCategoryId ) ;
if ( subCategory = = null ) {
// EXCEPTION - SubCategory not found
throw new EntityNotFoundException ( " SubCategory delete tree: SubCategory with id: " + subCategoryId + " not found (subCategory exists in category: " + category . getId ( ) + " ) " ) ;
}
sectionController . deleteTree ( subCategory ) ;
subCategoryDAO . delete ( subCategoryId ) ;
}
}
public void disConnectTree ( Category category ) {
List < String > subCategories = category . getSubCategories ( ) ;
for ( String subCategoryId : subCategories ) {
SubCategory subCategory = subCategoryDAO . findById ( subCategoryId ) ;
if ( subCategory = = null ) {
// EXCEPTION - SubCategory not found
throw new EntityNotFoundException ( " SubCategory disconnect tree: SubCategory with id: " + subCategoryId + " not found (subCategory exists in category: " + category . getId ( ) + " ) " ) ;
}
sectionController . disConnectTree ( subCategory ) ;
subCategory . setDefaultId ( null ) ;
subCategoryDAO . save ( subCategory ) ;
}
}
2019-11-22 16:50:59 +01:00
}