2019-11-22 16:50:59 +01:00
package eu.dnetlib.uoamonitorservice.controllers ;
2021-02-24 20:22:19 +01:00
import eu.dnetlib.uoaadmintoolslibrary.handlers.utils.RolesUtils ;
2019-11-22 16:50:59 +01:00
import eu.dnetlib.uoamonitorservice.dao.* ;
import eu.dnetlib.uoamonitorservice.entities.* ;
import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException ;
[Trunk | Monitor Service]:
1. StakeholderController.java & TopicController.java & CategoryController.java & SubCategoryController.java && SectionController.java && IndicatorController.java:
a. Comment logs for get requests.
b. Use "ForbiddenException" instead of "AccessDeniedException"
c. On /save, if full entity has id (already in DB), if not found in DB throw EntityNotFoundException.
d. Get children (e.g. when saving a Topic, get its categories) from DB.
2. TopicController.java & CategoryController.java & SubCategoryController.java & SectionController.java:
In /reorder, if there are in DB, ids that are missing from reordered list, do reordering and add in the end of list the missing ids.
3. ReorderEvent.java: [NEW] Added class ReorderEvent with fields "action" (String), "target" (String), "ids" (List<String>) (used in IndicatorController.java).
4. IndicatorController.java:
a. In /reorder, @RequestBody changed from List<String> indicators to ReorderEvent reorderEvent.
b. If there are in DB, ids that are missing from reordered list AND missing id is not moved to other section (action = removed and target = missing id), do reordering and add in the end of list the missing ids.
5. ExceptionsHandler.java: exception handler methods "invalidInput()", "nullPointerException()", "notFoundException()" moved to "Admin Tools Library" - "accessDeniedException()" is removed.
6. responses/ExceptionResponse.java: File and folder deleted (moved to "Admin Tools Library").
7. RolesUtils.java: Added method "isLoggedIn()" (checks if no roles for user, or user has role "ROLE_ANONYMOUS").
2020-12-09 15:24:27 +01:00
import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException ;
2019-11-22 16:50:59 +01:00
import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException ;
import org.apache.log4j.Logger ;
import org.springframework.beans.factory.annotation.Autowired ;
2020-11-11 13:45:59 +01:00
import org.springframework.security.access.AccessDeniedException ;
import org.springframework.security.access.prepost.PreAuthorize ;
2019-11-22 16:50:59 +01:00
import org.springframework.web.bind.annotation.* ;
import java.util.ArrayList ;
2020-11-11 13:45:59 +01:00
import java.util.Date ;
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 ( ) ) ;
2020-11-11 13:45:59 +01:00
@Autowired
private RolesUtils rolesUtils ;
2019-11-22 16:50:59 +01:00
@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
2020-11-11 13:45:59 +01:00
Date date = new Date ( ) ;
subCategory . setCreationDate ( date ) ;
subCategory . setUpdateDate ( date ) ;
subcategoryFull . setCreationDate ( date ) ;
subcategoryFull . setUpdateDate ( date ) ;
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 ;
}
2020-11-11 13:45:59 +01:00
@PreAuthorize ( " isAuthenticated() " )
2019-11-22 16:50:59 +01:00
@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
2020-11-11 13:45:59 +01:00
SubCategory < String > subCategory = new SubCategory < > ( subcategoryFull ) ;
Date date = new Date ( ) ;
subCategory . setUpdateDate ( date ) ;
subcategoryFull . setUpdateDate ( date ) ;
[Trunk | Monitor Service]:
1. StakeholderController.java & TopicController.java & CategoryController.java & SubCategoryController.java && SectionController.java && IndicatorController.java:
a. Comment logs for get requests.
b. Use "ForbiddenException" instead of "AccessDeniedException"
c. On /save, if full entity has id (already in DB), if not found in DB throw EntityNotFoundException.
d. Get children (e.g. when saving a Topic, get its categories) from DB.
2. TopicController.java & CategoryController.java & SubCategoryController.java & SectionController.java:
In /reorder, if there are in DB, ids that are missing from reordered list, do reordering and add in the end of list the missing ids.
3. ReorderEvent.java: [NEW] Added class ReorderEvent with fields "action" (String), "target" (String), "ids" (List<String>) (used in IndicatorController.java).
4. IndicatorController.java:
a. In /reorder, @RequestBody changed from List<String> indicators to ReorderEvent reorderEvent.
b. If there are in DB, ids that are missing from reordered list AND missing id is not moved to other section (action = removed and target = missing id), do reordering and add in the end of list the missing ids.
5. ExceptionsHandler.java: exception handler methods "invalidInput()", "nullPointerException()", "notFoundException()" moved to "Admin Tools Library" - "accessDeniedException()" is removed.
6. responses/ExceptionResponse.java: File and folder deleted (moved to "Admin Tools Library").
7. RolesUtils.java: Added method "isLoggedIn()" (checks if no roles for user, or user has role "ROLE_ANONYMOUS").
2020-12-09 15:24:27 +01:00
List < String > chartSections = new ArrayList < > ( ) ;
List < String > numberSections = new ArrayList < > ( ) ;
2020-05-18 11:17:05 +02:00
SubCategory < String > oldSubcategory = null ;
if ( subcategoryFull . getId ( ) ! = null ) {
oldSubcategory = subCategoryDAO . findById ( subcategoryFull . getId ( ) ) ;
[Trunk | Monitor Service]:
1. StakeholderController.java & TopicController.java & CategoryController.java & SubCategoryController.java && SectionController.java && IndicatorController.java:
a. Comment logs for get requests.
b. Use "ForbiddenException" instead of "AccessDeniedException"
c. On /save, if full entity has id (already in DB), if not found in DB throw EntityNotFoundException.
d. Get children (e.g. when saving a Topic, get its categories) from DB.
2. TopicController.java & CategoryController.java & SubCategoryController.java & SectionController.java:
In /reorder, if there are in DB, ids that are missing from reordered list, do reordering and add in the end of list the missing ids.
3. ReorderEvent.java: [NEW] Added class ReorderEvent with fields "action" (String), "target" (String), "ids" (List<String>) (used in IndicatorController.java).
4. IndicatorController.java:
a. In /reorder, @RequestBody changed from List<String> indicators to ReorderEvent reorderEvent.
b. If there are in DB, ids that are missing from reordered list AND missing id is not moved to other section (action = removed and target = missing id), do reordering and add in the end of list the missing ids.
5. ExceptionsHandler.java: exception handler methods "invalidInput()", "nullPointerException()", "notFoundException()" moved to "Admin Tools Library" - "accessDeniedException()" is removed.
6. responses/ExceptionResponse.java: File and folder deleted (moved to "Admin Tools Library").
7. RolesUtils.java: Added method "isLoggedIn()" (checks if no roles for user, or user has role "ROLE_ANONYMOUS").
2020-12-09 15:24:27 +01:00
if ( oldSubcategory = = null ) {
// EXCEPTION - SubCategory not found
throw new EntityNotFoundException ( " save subcategory: SubCategory with id: " + subcategoryFull . getId ( ) + " not found " ) ;
}
for ( String chartSectionId : oldSubcategory . getCharts ( ) ) {
Section section = sectionDAO . findById ( chartSectionId ) ;
if ( section = = null ) {
// EXCEPTION - Section not found
throw new EntityNotFoundException ( " Save subcategory: Chart section with id: " + chartSectionId + " not found (section exists in subcategory: " + subCategory . getId ( ) + " ) " ) ;
}
chartSections . add ( section . getId ( ) ) ;
}
for ( String numberSectionId : oldSubcategory . getNumbers ( ) ) {
Section section = sectionDAO . findById ( numberSectionId ) ;
if ( section = = null ) {
// EXCEPTION - Section not found
throw new EntityNotFoundException ( " Save subcategory: Number section with id: " + numberSectionId + " not found (section exists in subcategory: " + subCategory . getId ( ) + " ) " ) ;
}
numberSections . add ( section . getId ( ) ) ;
}
2020-11-11 13:45:59 +01:00
} else { // subcategory does not exist in DB
subCategory . setCreationDate ( date ) ;
subcategoryFull . setCreationDate ( date ) ;
[Trunk | Monitor Service]:
1. StakeholderController.java & TopicController.java & CategoryController.java & SubCategoryController.java && SectionController.java && IndicatorController.java:
a. Comment logs for get requests.
b. Use "ForbiddenException" instead of "AccessDeniedException"
c. On /save, if full entity has id (already in DB), if not found in DB throw EntityNotFoundException.
d. Get children (e.g. when saving a Topic, get its categories) from DB.
2. TopicController.java & CategoryController.java & SubCategoryController.java & SectionController.java:
In /reorder, if there are in DB, ids that are missing from reordered list, do reordering and add in the end of list the missing ids.
3. ReorderEvent.java: [NEW] Added class ReorderEvent with fields "action" (String), "target" (String), "ids" (List<String>) (used in IndicatorController.java).
4. IndicatorController.java:
a. In /reorder, @RequestBody changed from List<String> indicators to ReorderEvent reorderEvent.
b. If there are in DB, ids that are missing from reordered list AND missing id is not moved to other section (action = removed and target = missing id), do reordering and add in the end of list the missing ids.
5. ExceptionsHandler.java: exception handler methods "invalidInput()", "nullPointerException()", "notFoundException()" moved to "Admin Tools Library" - "accessDeniedException()" is removed.
6. responses/ExceptionResponse.java: File and folder deleted (moved to "Admin Tools Library").
7. RolesUtils.java: Added method "isLoggedIn()" (checks if no roles for user, or user has role "ROLE_ANONYMOUS").
2020-12-09 15:24:27 +01:00
for ( Section chartSection : subcategoryFull . getCharts ( ) ) {
chartSections . add ( chartSection . getId ( ) ) ;
}
for ( Section numberSection : subcategoryFull . getNumbers ( ) ) {
numberSections . add ( numberSection . getId ( ) ) ;
}
2020-05-18 11:17:05 +02: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);
[Trunk | Monitor Service]:
1. StakeholderController.java & TopicController.java & CategoryController.java & SubCategoryController.java && SectionController.java && IndicatorController.java:
a. Comment logs for get requests.
b. Use "ForbiddenException" instead of "AccessDeniedException"
c. On /save, if full entity has id (already in DB), if not found in DB throw EntityNotFoundException.
d. Get children (e.g. when saving a Topic, get its categories) from DB.
2. TopicController.java & CategoryController.java & SubCategoryController.java & SectionController.java:
In /reorder, if there are in DB, ids that are missing from reordered list, do reordering and add in the end of list the missing ids.
3. ReorderEvent.java: [NEW] Added class ReorderEvent with fields "action" (String), "target" (String), "ids" (List<String>) (used in IndicatorController.java).
4. IndicatorController.java:
a. In /reorder, @RequestBody changed from List<String> indicators to ReorderEvent reorderEvent.
b. If there are in DB, ids that are missing from reordered list AND missing id is not moved to other section (action = removed and target = missing id), do reordering and add in the end of list the missing ids.
5. ExceptionsHandler.java: exception handler methods "invalidInput()", "nullPointerException()", "notFoundException()" moved to "Admin Tools Library" - "accessDeniedException()" is removed.
6. responses/ExceptionResponse.java: File and folder deleted (moved to "Admin Tools Library").
7. RolesUtils.java: Added method "isLoggedIn()" (checks if no roles for user, or user has role "ROLE_ANONYMOUS").
2020-12-09 15:24:27 +01:00
subCategory . setCharts ( chartSections ) ;
2020-01-10 11:48:24 +01:00
subCategory . setNumbers ( numberSections ) ;
Stakeholder < String > stakeholder = stakeholderDAO . findById ( stakeholderId ) ;
if ( stakeholder . getDefaultId ( ) = = null ) {
if ( subcategoryFull . getId ( ) = = null ) {
2020-11-11 13:45:59 +01:00
subCategoryDAO . save ( subCategory ) ;
2020-01-10 11:48:24 +01:00
onSaveDefaultSubCategory ( subCategory , categoryId ) ;
2019-11-22 16:50:59 +01:00
} else {
2020-05-18 11:17:05 +02:00
onUpdateDefaultSubCategory ( subCategory , oldSubcategory ) ;
2020-11-11 13:45:59 +01:00
subCategoryDAO . save ( subCategory ) ;
2019-11-22 16:50:59 +01:00
}
2020-11-11 13:45:59 +01:00
} else {
subCategoryDAO . save ( subCategory ) ;
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());
2020-11-11 13:45:59 +01:00
subCategoryBasedOnDefault . setUpdateDate ( subCategory . getUpdateDate ( ) ) ;
2019-12-19 16:04:25 +01:00
subCategoryDAO . save ( subCategoryBasedOnDefault ) ;
}
}
2020-11-11 13:45:59 +01:00
@PreAuthorize ( " isAuthenticated() " )
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 ) {
2020-11-11 13:45:59 +01:00
Stakeholder < String > stakeholder = stakeholderDAO . findById ( stakeholderId ) ;
List < String > roles = rolesUtils . getRoles ( ) ;
if ( subcategory . getDefaultId ( ) ! = null & & ! rolesUtils . hasCreateAndDeleteAuthority ( roles , stakeholder . getType ( ) ) ) {
// EXCEPTION - Access denied
[Trunk | Monitor Service]:
1. StakeholderController.java & TopicController.java & CategoryController.java & SubCategoryController.java && SectionController.java && IndicatorController.java:
a. Comment logs for get requests.
b. Use "ForbiddenException" instead of "AccessDeniedException"
c. On /save, if full entity has id (already in DB), if not found in DB throw EntityNotFoundException.
d. Get children (e.g. when saving a Topic, get its categories) from DB.
2. TopicController.java & CategoryController.java & SubCategoryController.java & SectionController.java:
In /reorder, if there are in DB, ids that are missing from reordered list, do reordering and add in the end of list the missing ids.
3. ReorderEvent.java: [NEW] Added class ReorderEvent with fields "action" (String), "target" (String), "ids" (List<String>) (used in IndicatorController.java).
4. IndicatorController.java:
a. In /reorder, @RequestBody changed from List<String> indicators to ReorderEvent reorderEvent.
b. If there are in DB, ids that are missing from reordered list AND missing id is not moved to other section (action = removed and target = missing id), do reordering and add in the end of list the missing ids.
5. ExceptionsHandler.java: exception handler methods "invalidInput()", "nullPointerException()", "notFoundException()" moved to "Admin Tools Library" - "accessDeniedException()" is removed.
6. responses/ExceptionResponse.java: File and folder deleted (moved to "Admin Tools Library").
7. RolesUtils.java: Added method "isLoggedIn()" (checks if no roles for user, or user has role "ROLE_ANONYMOUS").
2020-12-09 15:24:27 +01:00
throw new ForbiddenException ( " Delete subcategory: You are not authorized to delete a default SubCategory in stakeholder with id: " + stakeholderId ) ;
2020-11-11 13:45:59 +01:00
}
2020-01-10 11:48:24 +01:00
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-11-11 13:45:59 +01:00
@PreAuthorize ( " isAuthenticated() " )
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 ) ;
[Trunk | Monitor Service]:
1. StakeholderController.java & TopicController.java & CategoryController.java & SubCategoryController.java && SectionController.java && IndicatorController.java:
a. Comment logs for get requests.
b. Use "ForbiddenException" instead of "AccessDeniedException"
c. On /save, if full entity has id (already in DB), if not found in DB throw EntityNotFoundException.
d. Get children (e.g. when saving a Topic, get its categories) from DB.
2. TopicController.java & CategoryController.java & SubCategoryController.java & SectionController.java:
In /reorder, if there are in DB, ids that are missing from reordered list, do reordering and add in the end of list the missing ids.
3. ReorderEvent.java: [NEW] Added class ReorderEvent with fields "action" (String), "target" (String), "ids" (List<String>) (used in IndicatorController.java).
4. IndicatorController.java:
a. In /reorder, @RequestBody changed from List<String> indicators to ReorderEvent reorderEvent.
b. If there are in DB, ids that are missing from reordered list AND missing id is not moved to other section (action = removed and target = missing id), do reordering and add in the end of list the missing ids.
5. ExceptionsHandler.java: exception handler methods "invalidInput()", "nullPointerException()", "notFoundException()" moved to "Admin Tools Library" - "accessDeniedException()" is removed.
6. responses/ExceptionResponse.java: File and folder deleted (moved to "Admin Tools Library").
7. RolesUtils.java: Added method "isLoggedIn()" (checks if no roles for user, or user has role "ROLE_ANONYMOUS").
2020-12-09 15:24:27 +01:00
List < String > oldSubcategories = category . getSubCategories ( ) ;
for ( String subcategoryId : oldSubcategories ) {
if ( ! subCategories . contains ( subcategoryId ) ) {
subCategories . add ( subcategoryId ) ;
}
}
2020-06-25 11:55:46 +02:00
category . setSubCategories ( subCategories ) ;
List < SubCategory > subCategoriesFull = new ArrayList < > ( ) ;
for ( String subCategoryId : subCategories ) {
[Trunk | Monitor Service]:
1. StakeholderController.java & TopicController.java & CategoryController.java & SubCategoryController.java && SectionController.java && IndicatorController.java:
a. Comment logs for get requests.
b. Use "ForbiddenException" instead of "AccessDeniedException"
c. On /save, if full entity has id (already in DB), if not found in DB throw EntityNotFoundException.
d. Get children (e.g. when saving a Topic, get its categories) from DB.
2. TopicController.java & CategoryController.java & SubCategoryController.java & SectionController.java:
In /reorder, if there are in DB, ids that are missing from reordered list, do reordering and add in the end of list the missing ids.
3. ReorderEvent.java: [NEW] Added class ReorderEvent with fields "action" (String), "target" (String), "ids" (List<String>) (used in IndicatorController.java).
4. IndicatorController.java:
a. In /reorder, @RequestBody changed from List<String> indicators to ReorderEvent reorderEvent.
b. If there are in DB, ids that are missing from reordered list AND missing id is not moved to other section (action = removed and target = missing id), do reordering and add in the end of list the missing ids.
5. ExceptionsHandler.java: exception handler methods "invalidInput()", "nullPointerException()", "notFoundException()" moved to "Admin Tools Library" - "accessDeniedException()" is removed.
6. responses/ExceptionResponse.java: File and folder deleted (moved to "Admin Tools Library").
7. RolesUtils.java: Added method "isLoggedIn()" (checks if no roles for user, or user has role "ROLE_ANONYMOUS").
2020-12-09 15:24:27 +01:00
SubCategory subCategory = subCategoryDAO . findById ( subCategoryId ) ;
if ( subCategory = = null ) {
// EXCEPTION - SubCategory not found
throw new EntityNotFoundException ( " Reorder subCategories: subCategory with id: " + subCategoryId + " not found " ) ;
}
subCategoriesFull . add ( subCategory ) ;
2020-06-25 11:55:46 +02:00
}
[Trunk | Monitor Service]:
1. StakeholderController.java & TopicController.java & CategoryController.java & SubCategoryController.java && SectionController.java && IndicatorController.java:
a. Comment logs for get requests.
b. Use "ForbiddenException" instead of "AccessDeniedException"
c. On /save, if full entity has id (already in DB), if not found in DB throw EntityNotFoundException.
d. Get children (e.g. when saving a Topic, get its categories) from DB.
2. TopicController.java & CategoryController.java & SubCategoryController.java & SectionController.java:
In /reorder, if there are in DB, ids that are missing from reordered list, do reordering and add in the end of list the missing ids.
3. ReorderEvent.java: [NEW] Added class ReorderEvent with fields "action" (String), "target" (String), "ids" (List<String>) (used in IndicatorController.java).
4. IndicatorController.java:
a. In /reorder, @RequestBody changed from List<String> indicators to ReorderEvent reorderEvent.
b. If there are in DB, ids that are missing from reordered list AND missing id is not moved to other section (action = removed and target = missing id), do reordering and add in the end of list the missing ids.
5. ExceptionsHandler.java: exception handler methods "invalidInput()", "nullPointerException()", "notFoundException()" moved to "Admin Tools Library" - "accessDeniedException()" is removed.
6. responses/ExceptionResponse.java: File and folder deleted (moved to "Admin Tools Library").
7. RolesUtils.java: Added method "isLoggedIn()" (checks if no roles for user, or user has role "ROLE_ANONYMOUS").
2020-12-09 15:24:27 +01:00
categoryDAO . save ( category ) ;
log . debug ( " SubCategories reordered! " ) ;
2020-06-25 11:55:46 +02:00
return subCategoriesFull ;
}
2020-11-11 13:45:59 +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();
// }
2019-12-23 12:45:07 +01:00
2020-11-11 13:45:59 +01:00
@PreAuthorize ( " isAuthenticated() " )
@RequestMapping ( value = " /{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/change-visibility " , method = RequestMethod . POST )
2022-09-14 17:02:47 +02:00
public SubCategory changeSubCategoryVisibility ( @PathVariable ( " stakeholderId " ) String stakeholderId ,
2020-11-11 13:45:59 +01:00
@PathVariable ( " topicId " ) String topicId ,
@PathVariable ( " categoryId " ) String categoryId ,
@PathVariable ( " subcategoryId " ) String subcategoryId ,
2022-09-14 17:02:47 +02:00
@RequestParam ( " visibility " ) Visibility visibility , @RequestParam ( required = false ) Boolean propagate ) {
log . debug ( " change subCategory visibility: " + visibility + " - toggle propagate: " + ( ( propagate ! = null & & propagate ) ? " true " : " false " ) ) ;
2019-12-23 12:45:07 +01:00
log . debug ( " Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId + " - SubCategory: " + subcategoryId ) ;
SubCategory subCategory = subCategoryDAO . findById ( subcategoryId ) ;
if ( subCategory = = null ) {
// EXCEPTION - SubCategory not found
2020-11-11 13:45:59 +01:00
throw new EntityNotFoundException ( " Change subCategory visibility: SubCategory with id: " + subcategoryId + " not found " ) ;
2019-12-23 12:45:07 +01:00
}
2020-01-10 11:48:24 +01:00
Category category = checkForExceptions ( stakeholderId , topicId , categoryId ) ;
2019-12-23 12:45:07 +01:00
2022-09-14 17:02:47 +02:00
if ( category . getSubCategories ( ) . contains ( subcategoryId ) ) {
return changeVisibilityTree ( subcategoryId , visibility , propagate ) ;
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();
2022-09-14 17:02:47 +02:00
throw new PathNotValidException ( " Toggle subCategory: SubCategory with id: " + subcategoryId + " not found in Category: " + categoryId ) ;
2020-01-10 11:48:24 +01:00
}
}
2022-09-14 17:02:47 +02:00
public SubCategory changeVisibilityTree ( String subCategoryId , Visibility visibility , Boolean propagate ) {
SubCategory < String > subCategory = subCategoryDAO . findById ( subCategoryId ) ;
if ( subCategory = = null ) {
// EXCEPTION - SubCategory not found
throw new EntityNotFoundException ( " Change subCategory visibility: SubCategory with id: " + subCategoryId + " not found " ) ;
}
SubCategory < Section > subCategoryFull = new SubCategory ( subCategory ) ;
List < Section > chartsFull = new ArrayList ( ) ;
List < Section > numbersFull = new ArrayList ( ) ;
if ( propagate ! = null & & propagate ) {
for ( String sectionId : subCategory . getCharts ( ) ) {
chartsFull . add ( sectionController . changeVisibilityTree ( sectionId , visibility ) ) ;
}
for ( String sectionId : subCategory . getNumbers ( ) ) {
numbersFull . add ( sectionController . changeVisibilityTree ( sectionId , visibility ) ) ;
}
}
subCategory . setVisibility ( visibility ) ;
subCategoryDAO . save ( subCategory ) ;
log . debug ( " SubCategory toggled! " ) ;
subCategoryFull . setVisibility ( visibility ) ;
subCategoryFull . setCharts ( chartsFull ) ;
subCategoryFull . setNumbers ( numbersFull ) ;
return subCategoryFull ;
}
2020-01-10 11:48:24 +01:00
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 " ) ;
}
2020-11-11 13:45:59 +01:00
List < String > roles = rolesUtils . getRoles ( ) ;
if ( ! rolesUtils . hasUpdateAuthority ( roles , stakeholder . getType ( ) , stakeholder . getAlias ( ) ) ) {
// EXCEPTION - Access denied
[Trunk | Monitor Service]:
1. StakeholderController.java & TopicController.java & CategoryController.java & SubCategoryController.java && SectionController.java && IndicatorController.java:
a. Comment logs for get requests.
b. Use "ForbiddenException" instead of "AccessDeniedException"
c. On /save, if full entity has id (already in DB), if not found in DB throw EntityNotFoundException.
d. Get children (e.g. when saving a Topic, get its categories) from DB.
2. TopicController.java & CategoryController.java & SubCategoryController.java & SectionController.java:
In /reorder, if there are in DB, ids that are missing from reordered list, do reordering and add in the end of list the missing ids.
3. ReorderEvent.java: [NEW] Added class ReorderEvent with fields "action" (String), "target" (String), "ids" (List<String>) (used in IndicatorController.java).
4. IndicatorController.java:
a. In /reorder, @RequestBody changed from List<String> indicators to ReorderEvent reorderEvent.
b. If there are in DB, ids that are missing from reordered list AND missing id is not moved to other section (action = removed and target = missing id), do reordering and add in the end of list the missing ids.
5. ExceptionsHandler.java: exception handler methods "invalidInput()", "nullPointerException()", "notFoundException()" moved to "Admin Tools Library" - "accessDeniedException()" is removed.
6. responses/ExceptionResponse.java: File and folder deleted (moved to "Admin Tools Library").
7. RolesUtils.java: Added method "isLoggedIn()" (checks if no roles for user, or user has role "ROLE_ANONYMOUS").
2020-12-09 15:24:27 +01:00
throw new ForbiddenException ( " CheckForExceptions SubCategory: You are not authorized to update stakeholder with id: " + stakeholderId ) ;
2020-11-11 13:45:59 +01:00
}
2020-01-10 11:48:24 +01:00
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
}