2024-03-08 19:55:51 +01:00
/ *
2020-01-10 11:48:24 +01:00
package eu.dnetlib.uoamonitorservice.controllers ;
2021-02-24 20:22:19 +01:00
import eu.dnetlib.uoaadmintoolslibrary.handlers.utils.RolesUtils ;
2020-01-10 11:48:24 +01:00
import eu.dnetlib.uoamonitorservice.dao.* ;
2024-03-08 19:55:51 +01:00
import eu.dnetlib.uoamonitorservice.dto.SectionFull ;
2020-01-10 11:48:24 +01:00
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 ;
2020-01-10 11:48:24 +01:00
import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException ;
2024-03-08 19:55:51 +01:00
import eu.dnetlib.uoamonitorservice.primitives.Visibility ;
import eu.dnetlib.uoamonitorservice.service.SectionService ;
import eu.dnetlib.uoamonitorservice.service.SubCategoryService ;
2022-10-10 14:27:17 +02:00
import org.apache.logging.log4j.LogManager ;
import org.apache.logging.log4j.Logger ;
2020-01-10 11:48:24 +01:00
import org.springframework.beans.factory.annotation.Autowired ;
2020-11-11 13:45:59 +01:00
import org.springframework.security.access.prepost.PreAuthorize ;
2020-01-10 11:48:24 +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-01-10 11:48:24 +01:00
import java.util.List ;
@RestController
@CrossOrigin ( origins = " * " )
public class SectionController {
2022-10-10 14:27:17 +02:00
private final Logger log = LogManager . getLogger ( this . getClass ( ) ) ;
2020-01-10 11:48:24 +01:00
2024-03-08 19:55:51 +01:00
private SectionService service ;
2020-01-10 11:48:24 +01:00
2020-11-11 13:45:59 +01:00
@PreAuthorize ( " isAuthenticated() " )
2020-01-17 12:43:31 +01:00
@RequestMapping ( value = " /{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/save/{index} " , method = RequestMethod . POST )
2024-03-08 19:55:51 +01:00
public SectionFull saveSection ( @PathVariable ( " stakeholderId " ) String stakeholderId ,
@PathVariable ( " topicId " ) String topicId ,
@PathVariable ( " categoryId " ) String categoryId ,
@PathVariable ( " subcategoryId " ) String subcategoryId ,
@PathVariable ( " index " ) String index ,
@RequestBody SectionFull sectionFull ) {
2020-01-10 11:48:24 +01:00
log . debug ( " save section " ) ;
log . debug ( " Name: " + sectionFull . getTitle ( ) + " - Id: " + sectionFull . getId ( ) + " - Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId + " - SubCategory: " + subcategoryId ) ;
2024-03-08 19:55:51 +01:00
SubCategory subCategory = this . subCategoryService . checkForExceptions ( stakeholderId , topicId , categoryId , subcategoryId , " Save Section " ) ;
2020-05-18 11:17:05 +02:00
2024-03-08 19:55:51 +01:00
Section section = new Section ( sectionFull ) ;
2020-11-11 13:45:59 +01:00
Date date = new Date ( ) ;
section . setUpdateDate ( date ) ;
sectionFull . 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 > indicators = new ArrayList < > ( ) ;
2020-05-18 11:17:05 +02:00
Section < String > oldSection = null ;
if ( sectionFull . getId ( ) ! = null ) {
oldSection = sectionDAO . findById ( sectionFull . 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 ( oldSection = = null ) {
// EXCEPTION - Section not found
throw new EntityNotFoundException ( " save section: Section with id: " + sectionFull . getId ( ) + " not found " ) ;
}
for ( String indicatorId : oldSection . getIndicators ( ) ) {
Indicator indicator = indicatorDAO . findById ( indicatorId ) ;
if ( indicator = = null ) {
// EXCEPTION - Indicator not found
throw new EntityNotFoundException ( " Save section: Indicator with id: " + indicatorId + " not found (indicator exists in section: " + section . getId ( ) + " ) " ) ;
}
indicators . add ( indicator . getId ( ) ) ;
}
2020-11-11 13:45:59 +01:00
} else { // section does not exist in DB
section . setCreationDate ( date ) ;
sectionFull . 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 ( Indicator indicator : sectionFull . getIndicators ( ) ) {
indicators . add ( indicator . getId ( ) ) ;
}
2020-05-18 11:17:05 +02:00
}
2020-01-10 11:48:24 +01:00
String sectionId = sectionFull . 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
2020-01-10 11:48:24 +01:00
section . setIndicators ( indicators ) ;
2024-03-08 19:55:51 +01:00
Stakeholder stakeholder = stakeholderDAO . findById ( stakeholderId ) ;
2020-01-10 11:48:24 +01:00
// this section belongs in default profile and it is new or it is updated
if ( stakeholder . getDefaultId ( ) = = null ) {
if ( sectionId = = null ) {
2020-11-11 13:45:59 +01:00
sectionDAO . save ( section ) ;
2020-05-13 15:25:02 +02:00
onSaveDefaultSection ( section , topicId , categoryId , subcategoryId , stakeholder ) ;
2020-01-10 11:48:24 +01:00
}
else {
2020-05-18 11:17:05 +02:00
onUpdateDefaultSection ( section , stakeholder , oldSection ) ;
2020-11-11 13:45:59 +01:00
sectionDAO . save ( section ) ;
2020-01-10 11:48:24 +01:00
}
2020-11-11 13:45:59 +01:00
} else {
sectionDAO . save ( section ) ;
2020-01-10 11:48:24 +01:00
}
List < String > sections = null ;
if ( sectionFull . getType ( ) . equals ( " chart " ) ) {
sections = subCategory . getCharts ( ) ;
} else if ( sectionFull . getType ( ) . equals ( " number " ) ) {
sections = subCategory . getNumbers ( ) ;
}
2020-05-13 15:25:02 +02:00
int existing_index = sections . indexOf ( section . getId ( ) ) ;
2020-01-17 12:43:31 +01:00
if ( existing_index = = - 1 ) {
2020-05-13 15:25:02 +02:00
if ( Integer . parseInt ( index ) ! = - 1 ) {
sections . add ( Integer . parseInt ( index ) , section . getId ( ) ) ;
2020-01-17 12:43:31 +01:00
} else {
2020-05-13 15:25:02 +02:00
sections . add ( section . getId ( ) ) ;
2020-01-17 12:43:31 +01:00
}
2020-01-10 11:48:24 +01:00
subCategoryDAO . save ( subCategory ) ;
log . debug ( " Section saved! " ) ;
sectionFull . setId ( section . getId ( ) ) ;
}
return sectionFull ;
}
public void onSaveDefaultSection ( Section section ,
String defaultTopicId , String defaultCategoryId ,
String defaultSubcategoryId , Stakeholder defaultStakeholder ) {
log . debug ( " On save default section " ) ;
// new section in default profile - add it on profiles of the same type
List < SubCategory > subCategories = subCategoryDAO . findByDefaultId ( defaultSubcategoryId ) ;
for ( SubCategory subCategory : subCategories ) {
2020-05-18 11:17:05 +02:00
Category parentCategory = categoryDAO . findBySubCategoriesContaining ( subCategory . getId ( ) ) ;
Topic parentTopic = topicDAO . findByCategoriesContaining ( parentCategory . getId ( ) ) ;
Stakeholder parentStakeholder = stakeholderDAO . findByTopicsContaining ( parentTopic . getId ( ) ) ;
2020-01-10 11:48:24 +01:00
Section sectionNew = new Section ( ) ;
sectionNew . copyFromDefault ( section ) ;
2020-05-18 11:17:05 +02:00
sectionNew . setStakeholderAlias ( parentStakeholder . getAlias ( ) ) ;
2020-01-10 11:48:24 +01:00
sectionDAO . save ( sectionNew ) ;
List < String > sections = null ;
if ( section . getType ( ) . equals ( " chart " ) ) {
sections = subCategory . getCharts ( ) ;
} else if ( section . getType ( ) . equals ( " number " ) ) {
sections = subCategory . getNumbers ( ) ;
}
sections . add ( sectionNew . getId ( ) ) ;
subCategoryDAO . save ( subCategory ) ;
}
}
2020-05-18 11:17:05 +02:00
public void onUpdateDefaultSection ( Section section , Stakeholder stakeholder , Section oldSection ) {
2020-01-10 11:48:24 +01:00
log . debug ( " On update default section " ) ;
}
private SubCategory checkForExceptions ( String stakeholderId , String topicId , String categoryId , String subcategoryId ) {
2024-03-08 19:55:51 +01:00
Stakeholder stakeholder = stakeholderDAO . findById ( stakeholderId ) . orElseThrow ( ( ) - > new EntityNotFoundException ( " Save indicator: Stakeholder with id: " + stakeholderId + " not found " ) ) ;
2023-07-04 15:02:34 +02:00
if ( ! rolesUtils . hasUpdateAuthority ( stakeholder . getType ( ) , stakeholder . getAlias ( ) ) ) {
2020-11-11 13:45:59 +01:00
// 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 Section: You are not authorized to update stakeholder with id: " + stakeholderId ) ;
2020-11-11 13:45:59 +01:00
}
2024-03-08 19:55:51 +01:00
Topic topic = topicDAO . findById ( topicId ) . orElseThrow ( ( ) - > new EntityNotFoundException ( " Save indicator: Topic with id: " + topicId + " not found " ) ) ;
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 subcategory ;
}
2020-06-22 21:58:40 +02:00
public void deleteTree ( SubCategory subCategory ) {
List < String > sections = subCategory . getCharts ( ) ;
for ( String sectionId : sections ) {
Section section = sectionDAO . findById ( sectionId ) ;
if ( section = = null ) {
// EXCEPTION - Section not found
throw new EntityNotFoundException ( " Section delete tree: Chart Section with id: " + sectionId + " not found (section exists in subCategory: " + subCategory . getId ( ) + " ) " ) ;
}
indicatorController . deleteTree ( section ) ;
sectionDAO . delete ( sectionId ) ;
}
sections = subCategory . getNumbers ( ) ;
for ( String sectionId : sections ) {
Section section = sectionDAO . findById ( sectionId ) ;
if ( section = = null ) {
// EXCEPTION - Section not found
throw new EntityNotFoundException ( " Section delete tree: Number Section with id: " + sectionId + " not found (section exists in subCategory: " + subCategory . getId ( ) + " ) " ) ;
}
indicatorController . deleteTree ( section ) ;
sectionDAO . delete ( sectionId ) ;
}
}
public void disConnectTree ( SubCategory subCategory ) {
List < String > sections = subCategory . getCharts ( ) ;
for ( String sectionId : sections ) {
Section section = sectionDAO . findById ( sectionId ) ;
if ( section = = null ) {
// EXCEPTION - Section not found
throw new EntityNotFoundException ( " Section disconnect tree: Chart Section with id: " + sectionId + " not found (section exists in subCategory: " + subCategory . getId ( ) + " ) " ) ;
}
indicatorController . disConnectTree ( section ) ;
section . setDefaultId ( null ) ;
sectionDAO . save ( section ) ;
}
sections = subCategory . getNumbers ( ) ;
for ( String sectionId : sections ) {
Section section = sectionDAO . findById ( sectionId ) ;
if ( section = = null ) {
// EXCEPTION - Section not found
throw new EntityNotFoundException ( " Section disconnect tree: Number Section with id: " + sectionId + " not found (section exists in subCategory: " + subCategory . getId ( ) + " ) " ) ;
}
indicatorController . disConnectTree ( section ) ;
section . setDefaultId ( null ) ;
sectionDAO . save ( section ) ;
}
}
2020-01-10 11:48:24 +01:00
}
2024-03-08 19:55:51 +01:00
* /