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 ;
2022-10-10 14:27:17 +02:00
import org.apache.logging.log4j.LogManager ;
import org.apache.logging.log4j.Logger ;
2019-11-22 16:50:59 +01:00
import org.springframework.beans.factory.annotation.Autowired ;
2020-11-11 13:45:59 +01:00
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 CategoryController {
2022-10-10 14:27:17 +02:00
private final Logger log = LogManager . getLogger ( this . getClass ( ) ) ;
2019-11-22 16:50:59 +01:00
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 ;
@Autowired
private SubCategoryController subCategoryController ;
public Category < SubCategory > buildCategory ( Category < SubCategory > categoryFull ) {
Category < String > category = new Category < > ( categoryFull ) ;
List < String > subCategories = new ArrayList < > ( ) ;
List < SubCategory > subCategoriesFull = new ArrayList < > ( ) ;
2020-01-10 11:48:24 +01:00
for ( SubCategory < Section < Indicator > > subCategory : categoryFull . getSubCategories ( ) ) {
SubCategory < Section < Indicator > > subcategoryFull = subCategoryController . buildSubCategory ( subCategory ) ;
2019-11-22 16:50:59 +01:00
subCategoriesFull . add ( subcategoryFull ) ;
subCategories . add ( subcategoryFull . getId ( ) ) ;
}
categoryFull . setSubCategories ( subCategoriesFull ) ;
category . setSubCategories ( subCategories ) ;
2020-11-11 13:45:59 +01:00
Date date = new Date ( ) ;
category . setCreationDate ( date ) ;
category . setUpdateDate ( date ) ;
categoryFull . setCreationDate ( date ) ;
categoryFull . setUpdateDate ( date ) ;
2019-12-19 16:04:25 +01:00
categoryDAO . save ( category ) ;
2019-11-22 16:50:59 +01:00
2019-12-19 16:04:25 +01:00
categoryFull . setId ( category . getId ( ) ) ;
2019-11-22 16:50:59 +01:00
return categoryFull ;
}
2020-11-11 13:45:59 +01:00
@PreAuthorize ( " isAuthenticated() " )
2019-11-22 16:50:59 +01:00
@RequestMapping ( value = " /{stakeholderId}/{topicId}/save " , method = RequestMethod . POST )
public Category < SubCategory > saveCategory ( @PathVariable ( " stakeholderId " ) String stakeholderId ,
@PathVariable ( " topicId " ) String topicId ,
@RequestBody Category < SubCategory > categoryFull ) {
log . debug ( " save category " ) ;
2019-12-19 16:04:25 +01:00
log . debug ( " Alias: " + categoryFull . getAlias ( ) + " - Id: " + categoryFull . getId ( ) + " - Stakeholder: " + stakeholderId + " - Topic: " + topicId ) ;
2019-11-22 16:50:59 +01:00
Stakeholder < String > stakeholder = stakeholderDAO . findById ( stakeholderId ) ;
if ( stakeholder ! = null ) {
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 ( " Save Category: You are not authorized to update stakeholder with id: " + stakeholderId ) ;
2020-11-11 13:45:59 +01:00
}
2020-05-18 11:17:05 +02:00
Category < String > oldCategory = null ;
if ( categoryFull . getId ( ) ! = null ) {
oldCategory = categoryDAO . findById ( categoryFull . 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 ( oldCategory = = null ) {
// EXCEPTION - Category not found
throw new EntityNotFoundException ( " save category: Category with id: " + categoryFull . getId ( ) + " not found " ) ;
}
2020-05-18 11:17:05 +02:00
}
2019-11-22 16:50:59 +01:00
Topic < String > topic = topicDAO . findById ( topicId ) ;
if ( topic ! = null ) {
if ( stakeholder . getTopics ( ) . contains ( topicId ) ) {
2020-11-11 13:45:59 +01:00
Category < String > category = new Category < > ( categoryFull ) ;
Date date = new Date ( ) ;
category . setUpdateDate ( date ) ;
categoryFull . 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 > subCategories = new ArrayList < > ( ) ;
2019-11-22 16:50:59 +01:00
// if category not exists (no id), create a new default subcategory, identical to category
if ( categoryFull . getId ( ) = = null ) {
2020-11-11 13:45:59 +01:00
category . setCreationDate ( date ) ;
categoryFull . setCreationDate ( date ) ;
2019-11-22 16:50:59 +01:00
SubCategory < String > subCategory = new SubCategory < > ( ) ;
2019-12-19 16:04:25 +01:00
subCategory . createOverviewSubCategory ( categoryFull ) ;
subCategoryDAO . save ( subCategory ) ;
2019-11-22 16:50:59 +01: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
List < SubCategory > subCategoriesFull = categoryFull . getSubCategories ( ) ;
subCategoriesFull . add ( subCategory ) ;
for ( SubCategory oldSubCategory : subCategoriesFull ) {
subCategories . add ( oldSubCategory . getId ( ) ) ;
}
} else {
for ( String subCategoryId : oldCategory . getSubCategories ( ) ) {
SubCategory subCategory = subCategoryDAO . findById ( subCategoryId ) ;
if ( subCategory = = null ) {
// EXCEPTION - SubCategory not found
throw new EntityNotFoundException ( " Save category: SubCategory with id: " + subCategoryId + " not found (subcategory exists in category: " + category . getId ( ) + " ) " ) ;
}
subCategories . add ( subCategory . getId ( ) ) ;
}
2019-11-22 16:50:59 +01: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
2019-11-22 16:50:59 +01:00
category . setSubCategories ( subCategories ) ;
2019-12-19 16:04:25 +01:00
if ( stakeholder . getDefaultId ( ) = = null ) {
if ( categoryFull . getId ( ) = = null ) {
2020-11-11 13:45:59 +01:00
categoryDAO . save ( category ) ;
2019-12-19 16:04:25 +01:00
onSaveDefaultCategory ( category , topicId ) ;
} else {
2020-05-18 11:17:05 +02:00
onUpdateDefaultCategory ( category , oldCategory ) ;
2020-11-11 13:45:59 +01:00
categoryDAO . save ( category ) ;
2019-12-19 16:04:25 +01:00
}
2020-11-11 13:45:59 +01:00
} else {
categoryDAO . save ( category ) ;
2019-12-19 16:04:25 +01:00
}
2019-11-22 16:50:59 +01:00
List < String > categories = topic . getCategories ( ) ;
2019-12-19 16:04:25 +01:00
int index = categories . indexOf ( category . getId ( ) ) ;
2019-11-22 16:50:59 +01:00
if ( index = = - 1 ) {
2019-12-19 16:04:25 +01:00
categories . add ( category . getId ( ) ) ;
2019-11-22 16:50:59 +01:00
topicDAO . save ( topic ) ;
log . debug ( " Category saved! " ) ;
2019-12-19 16:04:25 +01:00
categoryFull . setId ( category . getId ( ) ) ;
2019-11-22 16:50:59 +01:00
}
subCategories = null ;
category = null ;
} else {
// EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
throw new PathNotValidException ( " Save category: Topic with id: " + topicId + " not found in Stakeholder: " + stakeholderId ) ;
}
} else {
// EXCEPTION - Topic not found
throw new EntityNotFoundException ( " Save category: Topic with id: " + topicId + " not found " ) ;
}
} else {
// EXCEPTION - Stakeholder not found
throw new EntityNotFoundException ( " Save category: Stakeholder with id: " + stakeholderId + " not found " ) ;
}
return categoryFull ;
}
2019-12-19 16:04:25 +01:00
public void onSaveDefaultCategory ( Category < String > category , String topicId ) {
log . debug ( " On save default category " ) ;
List < Topic > topics = topicDAO . findByDefaultId ( topicId ) ;
for ( Topic topic : topics ) {
Category categoryNew = new Category ( ) ;
categoryNew . copyFromDefault ( category ) ;
categoryDAO . save ( categoryNew ) ;
List < String > categories = topic . getCategories ( ) ;
categories . add ( categoryNew . getId ( ) ) ;
topicDAO . save ( topic ) ;
}
String subCategoryOverviewId = category . getSubCategories ( ) . get ( 0 ) ;
SubCategory subCategoryOverview = subCategoryDAO . findById ( subCategoryOverviewId ) ;
subCategoryController . onSaveDefaultSubCategory ( subCategoryOverview , category . getId ( ) ) ;
}
2020-05-18 11:17:05 +02:00
public void onUpdateDefaultCategory ( Category category , Category oldCategory ) {
2019-12-19 16:04:25 +01:00
log . debug ( " On update default category " ) ;
List < Category > categories = categoryDAO . findByDefaultId ( category . getId ( ) ) ;
boolean changed = false ;
for ( Category categoryBasedOnDefault : categories ) {
2020-05-18 11:17:05 +02:00
if ( category . getName ( ) ! = null & & ! category . getName ( ) . equals ( categoryBasedOnDefault . getName ( ) )
& & ( oldCategory . getName ( ) = = null | | oldCategory . getName ( ) . equals ( categoryBasedOnDefault . getName ( ) ) ) ) {
categoryBasedOnDefault . setName ( category . getName ( ) ) ;
2020-09-28 13:59:00 +02:00
categoryBasedOnDefault . setAlias ( category . getAlias ( ) ) ;
2019-12-19 16:04:25 +01:00
changed = true ;
}
2020-05-18 11:17:05 +02:00
if ( category . getDescription ( ) ! = null & & ! category . getDescription ( ) . equals ( categoryBasedOnDefault . getDescription ( ) )
& & ( oldCategory . getDescription ( ) = = null | | oldCategory . getDescription ( ) . equals ( categoryBasedOnDefault . getDescription ( ) ) ) ) {
categoryBasedOnDefault . setDescription ( category . 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
// categoryBasedOnDefault.setName(category.getName());
// categoryBasedOnDefault.setDescription(category.getDescription());
2020-11-11 13:45:59 +01:00
categoryBasedOnDefault . setUpdateDate ( category . getUpdateDate ( ) ) ;
2019-12-19 16:04:25 +01:00
categoryDAO . save ( categoryBasedOnDefault ) ;
}
}
2020-11-11 13:45:59 +01:00
@PreAuthorize ( " isAuthenticated() " )
2019-11-22 16:50:59 +01:00
@RequestMapping ( value = " /{stakeholderId}/{topicId}/{categoryId}/delete " , method = RequestMethod . DELETE )
public boolean deleteCategory ( @PathVariable ( " stakeholderId " ) String stakeholderId ,
@PathVariable ( " topicId " ) String topicId ,
2020-06-22 21:58:40 +02:00
@PathVariable ( " categoryId " ) String categoryId ,
@RequestParam ( required = false ) String children ) {
2019-11-22 16:50:59 +01:00
log . debug ( " delete category " ) ;
2019-12-19 16:04:25 +01:00
log . debug ( " Id: " + categoryId + " - Stakeholder: " + stakeholderId + " - Topic: " + topicId ) ;
2019-11-22 16:50:59 +01:00
Stakeholder < String > stakeholder = stakeholderDAO . findById ( stakeholderId ) ;
if ( stakeholder ! = null ) {
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 ( " Delete category: You are not authorized to update stakeholder with id: " + stakeholderId ) ;
2020-11-11 13:45:59 +01:00
}
2019-11-22 16:50:59 +01:00
Topic < String > topic = topicDAO . findById ( topicId ) ;
if ( topic ! = null ) {
if ( stakeholder . getTopics ( ) . contains ( topicId ) ) {
Category < String > category = categoryDAO . findById ( categoryId ) ;
if ( category ! = null ) {
2020-11-11 13:45:59 +01:00
if ( category . 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 category: You are not authorized to delete a default Category in stakeholder with id: " + stakeholderId ) ;
2020-11-11 13:45:59 +01:00
}
2019-11-22 16:50:59 +01:00
List < String > categories = topic . getCategories ( ) ;
int index = categories . indexOf ( categoryId ) ;
if ( index ! = - 1 ) {
2020-06-22 21:58:40 +02:00
// this category belongs in default profile
if ( topic . getDefaultId ( ) = = null & & children ! = null ) {
onDeleteDefaultCategory ( categoryId , topicId , children ) ;
2019-11-22 16:50:59 +01:00
}
2020-06-22 21:58:40 +02:00
// for(String subCategoryId : category.getSubCategories()) {
// SubCategory<String> subcategory = subCategoryDAO.findById(subCategoryId);
// if(subcategory == null) {
// // EXCEPTION - SubCategory not found
// throw new EntityNotFoundException("Delete category: SubCategory with id: "+subCategoryId+" not found (subcategory exists in category: "+categoryId+")");
// }
//
// for(String chartSectionId : subcategory.getCharts()) {
// Section<String> chartSection = sectionDAO.findById(chartSectionId);
// if (chartSection == null) {
// // EXCEPTION - Section not found
// throw new EntityNotFoundException("Delete topic: 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 topic: 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);
// }
//
// subCategoryDAO.delete(subCategoryId);
// }
subCategoryController . deleteTree ( category ) ;
2019-11-22 16:50:59 +01:00
category . setSubCategories ( null ) ;
categories . remove ( index ) ;
topicDAO . save ( topic ) ;
categoryDAO . delete ( categoryId ) ;
log . debug ( " Category deleted! " ) ;
} else {
// EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias();
throw new PathNotValidException ( " Delete category: Category with id: " + categoryId + " not found in Topic: " + topicId ) ;
}
} else {
// EXCEPTION - Category not found
throw new EntityNotFoundException ( " Delete category: Category with id: " + categoryId + " not found " ) ;
}
} else {
// EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
throw new PathNotValidException ( " Delete category: Topic with id: " + topicId + " not found in Stakeholder: " + stakeholderId ) ;
}
} else {
// EXCEPTION - Topic not found
throw new EntityNotFoundException ( " Delete category: Topic with id: " + topicId + " not found " ) ;
}
} else {
// EXCEPTION - Stakeholder not found
throw new EntityNotFoundException ( " Delete category: Stakeholder with id: " + stakeholderId + " not found " ) ;
}
return true ;
}
2019-12-23 12:45:07 +01:00
2020-06-22 21:58:40 +02:00
public boolean onDeleteDefaultCategory ( String defaultCategoryId , String defaultTopicId , String children ) {
if ( children . equals ( " delete " ) ) {
List < Topic > topics = topicDAO . findByDefaultId ( defaultTopicId ) ;
List < Category > categories = categoryDAO . findByDefaultId ( defaultCategoryId ) ;
for ( Topic topic : topics ) {
Iterator < Category > categoriesIterator = categories . iterator ( ) ;
while ( categoriesIterator . hasNext ( ) ) {
Category category = categoriesIterator . next ( ) ;
String categoryId = category . getId ( ) ;
if ( topic . getCategories ( ) ! = null & & topic . getCategories ( ) . contains ( categoryId ) ) {
categoriesIterator . remove ( ) ;
topic . getCategories ( ) . remove ( categoryId ) ;
topicDAO . save ( topic ) ;
subCategoryController . deleteTree ( category ) ;
categoryDAO . delete ( categoryId ) ;
log . debug ( " Category with id: " + categoryId + " deleted! " ) ;
break ;
}
}
}
} else if ( children . equals ( " disconnect " ) ) {
List < Category > categories = categoryDAO . findByDefaultId ( defaultCategoryId ) ;
for ( Category category : categories ) {
subCategoryController . disConnectTree ( category ) ;
category . setDefaultId ( null ) ;
categoryDAO . save ( category ) ;
log . debug ( " DefaultId for Category with id: " + category . 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}/reorder " , method = RequestMethod . POST )
public List < Category > reorderCategories ( @PathVariable ( " stakeholderId " ) String stakeholderId ,
@PathVariable ( " topicId " ) String topicId ,
@RequestBody List < String > categories ) {
log . debug ( " reorder categories " ) ;
log . debug ( " Stakeholder: " + stakeholderId + " - Topic: " + topicId ) ;
Topic < String > topic = checkForExceptions ( stakeholderId , topicId ) ;
[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 > oldCategories = topic . getCategories ( ) ;
for ( String categoryId : oldCategories ) {
if ( ! categories . contains ( categoryId ) ) {
categories . add ( categoryId ) ;
}
}
2020-06-25 11:55:46 +02:00
topic . setCategories ( categories ) ;
List < Category > categoriesFull = new ArrayList < > ( ) ;
for ( String categoryId : categories ) {
[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
Category category = categoryDAO . findById ( categoryId ) ;
if ( category = = null ) {
// EXCEPTION - Category not found
throw new EntityNotFoundException ( " Reorder Categories: Category with id: " + categoryId + " not found " ) ;
}
categoriesFull . add ( category ) ;
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
topicDAO . save ( topic ) ;
log . debug ( " Categories reordered! " ) ;
2020-06-25 11:55:46 +02:00
return categoriesFull ;
}
2020-11-11 13:45:59 +01:00
// @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/toggle-status", method = RequestMethod.POST)
// public Boolean toggleCategoryStatus(@PathVariable("stakeholderId") String stakeholderId,
// @PathVariable("topicId") String topicId,
// @PathVariable("categoryId") String categoryId) {
// log.debug("toggle category status (isActive)");
// log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId);
//
// Category category = categoryDAO.findById(categoryId);
// if (category == null) {
// // EXCEPTION - Category not found
// throw new EntityNotFoundException("Toggle category status: Category with id: "+categoryId+" not found");
// }
// category.setIsActive(!category.getIsActive());
//
// this.toggleCategory(stakeholderId, topicId, category);
//
// return category.getIsActive();
// }
//
// @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/toggle-access", method = RequestMethod.POST)
// public Boolean toggleCategoryAccess(@PathVariable("stakeholderId") String stakeholderId,
// @PathVariable("topicId") String topicId,
// @PathVariable("categoryId") String categoryId) {
// log.debug("toggle category access (isPublic)");
// log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId);
//
// Category category = categoryDAO.findById(categoryId);
// if (category == null) {
// // EXCEPTION - Category not found
// throw new EntityNotFoundException("Toggle category access: Category with id: "+categoryId+" not found");
// }
// category.setIsPublic(!category.getIsPublic());
//
// this.toggleCategory(stakeholderId, topicId, category);
//
// return category.getIsPublic();
// }
@PreAuthorize ( " isAuthenticated() " )
@RequestMapping ( value = " /{stakeholderId}/{topicId}/{categoryId}/change-visibility " , method = RequestMethod . POST )
2022-09-14 17:02:47 +02:00
public Category changeCategoryVisibility ( @PathVariable ( " stakeholderId " ) String stakeholderId ,
2020-11-11 13:45:59 +01:00
@PathVariable ( " topicId " ) String topicId ,
@PathVariable ( " categoryId " ) String categoryId ,
2022-09-14 17:02:47 +02:00
@RequestParam ( " visibility " ) Visibility visibility , @RequestParam ( required = false ) Boolean propagate ) {
log . debug ( " change category 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 ) ;
Stakeholder < String > stakeholder = stakeholderDAO . findById ( stakeholderId ) ;
if ( stakeholder ! = null ) {
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 ( " Toggle category: You are not authorized to update stakeholder with id: " + stakeholderId ) ;
2020-11-11 13:45:59 +01:00
}
2019-12-23 12:45:07 +01:00
Topic < String > topic = topicDAO . findById ( topicId ) ;
if ( topic ! = null ) {
if ( stakeholder . getTopics ( ) . contains ( topicId ) ) {
2022-09-14 17:02:47 +02:00
if ( topic . getCategories ( ) . contains ( categoryId ) ) {
return changeVisibilityTree ( categoryId , visibility , propagate ) ;
2019-12-23 12:45:07 +01:00
} else {
// EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias();
2022-09-14 17:02:47 +02:00
throw new PathNotValidException ( " Toggle category: Category with id: " + categoryId + " not found in Topic: " + topicId ) ;
2019-12-23 12:45:07 +01:00
}
} else {
// EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
throw new PathNotValidException ( " Toggle category: Topic with id: " + topicId + " not found in Stakeholder: " + stakeholderId ) ;
}
} else {
// EXCEPTION - Topic not found
throw new EntityNotFoundException ( " Toggle category: Topic with id: " + topicId + " not found " ) ;
}
} else {
// EXCEPTION - Stakeholder not found
throw new EntityNotFoundException ( " Toggle category: Stakeholder with id: " + stakeholderId + " not found " ) ;
}
}
2020-06-22 21:58:40 +02:00
2022-09-14 17:02:47 +02:00
public Category changeVisibilityTree ( String categoryId , Visibility visibility , Boolean propagate ) {
Category < String > category = categoryDAO . findById ( categoryId ) ;
if ( category = = null ) {
// EXCEPTION - Category not found
throw new EntityNotFoundException ( " Change category visibility: Category with id: " + categoryId + " not found " ) ;
}
Category < SubCategory > categoryFull = new Category ( category ) ;
List < SubCategory > subCategoriesFull = new ArrayList < > ( ) ;
if ( propagate ! = null & & propagate ) {
for ( String subCategoryId : category . getSubCategories ( ) ) {
subCategoriesFull . add ( subCategoryController . changeVisibilityTree ( subCategoryId , visibility , propagate ) ) ;
}
}
category . setVisibility ( visibility ) ;
categoryDAO . save ( category ) ;
log . debug ( " Category toggled! " ) ;
categoryFull . setVisibility ( visibility ) ;
categoryFull . setSubCategories ( subCategoriesFull ) ;
return categoryFull ;
}
2020-06-25 11:55:46 +02:00
private Topic checkForExceptions ( String stakeholderId , String topicId ) {
Stakeholder < String > stakeholder = stakeholderDAO . findById ( stakeholderId ) ;
if ( stakeholder = = null ) {
// EXCEPTION - Stakeholder not found
throw new EntityNotFoundException ( " checkForExceptions category: 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 category: You are not authorized to update stakeholder with id: " + stakeholderId ) ;
2020-11-11 13:45:59 +01:00
}
2020-06-25 11:55:46 +02:00
Topic < String > topic = topicDAO . findById ( topicId ) ;
if ( topic = = null ) {
// EXCEPTION - Topic not found
throw new EntityNotFoundException ( " checkForExceptions category: Topic with id: " + topicId + " not found " ) ;
}
if ( ! stakeholder . getTopics ( ) . contains ( topicId ) ) {
// EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
throw new PathNotValidException ( " checkForExceptions category: Topic with id: " + topicId + " not found in Stakeholder: " + stakeholderId ) ;
}
return topic ;
}
2020-06-22 21:58:40 +02:00
public void deleteTree ( Topic topic ) {
List < String > categories = topic . getCategories ( ) ;
for ( String categoryId : categories ) {
Category category = categoryDAO . findById ( categoryId ) ;
if ( category = = null ) {
// EXCEPTION - Category not found
throw new EntityNotFoundException ( " Category delete tree: Category with id: " + categoryId + " not found (category exists in topic: " + topic . getId ( ) + " ) " ) ;
}
subCategoryController . deleteTree ( category ) ;
categoryDAO . delete ( categoryId ) ;
}
}
public void disConnectTree ( Topic topic ) {
List < String > categories = topic . getCategories ( ) ;
for ( String categoryId : categories ) {
Category category = categoryDAO . findById ( categoryId ) ;
if ( category = = null ) {
// EXCEPTION - Category not found
throw new EntityNotFoundException ( " Category disconnect tree: Category with id: " + categoryId + " not found (category exists in topic: " + topic . getId ( ) + " ) " ) ;
}
subCategoryController . disConnectTree ( category ) ;
category . setDefaultId ( null ) ;
categoryDAO . save ( category ) ;
}
}
2019-11-22 16:50:59 +01:00
}