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.* ;
2020-01-17 12:43:31 +01:00
import java.io.UnsupportedEncodingException ;
2020-06-22 21:58:40 +02:00
import java.util.* ;
2019-11-22 16:50:59 +01:00
@RestController
@CrossOrigin ( origins = " * " )
public class IndicatorController {
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 ;
2020-01-10 11:48:24 +01:00
@Autowired
private SectionDAO sectionDAO ;
2019-11-22 16:50:59 +01:00
@Autowired
private IndicatorDAO indicatorDAO ;
2021-07-21 11:46:49 +02:00
@Autowired
private SectionController sectionController ;
@Autowired
private StakeholderController stakeholderController ;
@PreAuthorize ( " isAuthenticated() " )
@RequestMapping ( value = " /{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/save-bulk " , method = RequestMethod . POST )
public Stakeholder saveBulkIndicators ( @PathVariable ( " stakeholderId " ) String stakeholderId ,
@PathVariable ( " topicId " ) String topicId ,
@PathVariable ( " categoryId " ) String categoryId ,
@PathVariable ( " subcategoryId " ) String subcategoryId ,
2022-09-08 22:34:25 +02:00
@RequestBody List < Section < Indicator > > sections ) throws UnsupportedEncodingException {
2021-07-21 11:46:49 +02:00
log . debug ( " save bulk indicators " ) ;
log . debug ( " Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId + " - SubCategory: " + subcategoryId ) ;
Stakeholder < String > stakeholder = stakeholderDAO . findById ( stakeholderId ) ;
Date date = new Date ( ) ;
2022-09-08 22:34:25 +02:00
createSectionsAndSaveBulk ( date , sections , stakeholder , topicId , categoryId , subcategoryId ) ;
2021-07-21 11:46:49 +02:00
// createSectionAndSaveBulk(date, "number", "Numbers imported from file", number_indicators, stakeholder, topicId, categoryId, subcategoryId);
2023-07-04 15:02:34 +02:00
return stakeholderController . setFullEntities ( stakeholder ) ;
2021-07-21 11:46:49 +02:00
}
2022-09-08 22:34:25 +02:00
private void createSectionsAndSaveBulk ( Date date , List < Section < Indicator > > old_sections ,
2021-07-21 11:46:49 +02:00
Stakeholder stakeholder , String topicId , String categoryId , String subcategoryId ) throws UnsupportedEncodingException {
2022-09-08 22:34:25 +02:00
for ( Section < Indicator > section : old_sections ) {
2023-07-10 17:00:27 +02:00
if ( section = = null ) {
continue ;
}
2022-09-08 22:34:25 +02:00
Section chart_section = null ;
Section number_section = null ;
List < String > chart_indicators = null ;
List < String > number_indicators = null ;
if ( section . getType ( ) . equals ( " chart " ) ) {
chart_section = createSection ( chart_section , " chart " , ( section . getTitle ( ) ! = null ? section . getTitle ( ) : " Charts " ) + " imported from file " , date , stakeholder , topicId , categoryId , subcategoryId ) ;
chart_indicators = chart_section . getIndicators ( ) ;
} else if ( section . getType ( ) . equals ( " number " ) ) {
number_section = createSection ( number_section , " number " , ( section . getTitle ( ) ! = null ? section . getTitle ( ) : " Numbers " ) + " imported from file " , date , stakeholder , topicId , categoryId , subcategoryId ) ;
number_indicators = number_section . getIndicators ( ) ;
}
2021-07-21 11:46:49 +02:00
2022-09-08 22:34:25 +02:00
List < Indicator > indicators = section . getIndicators ( ) ;
for ( Indicator indicator : indicators ) {
2023-07-10 17:00:27 +02:00
if ( indicator = = null ) {
continue ;
}
2022-09-08 22:34:25 +02:00
if ( indicator . getType ( ) . equals ( " chart " ) ) {
2023-07-03 09:42:46 +02:00
saveIndicatorAndAddInSection ( indicator , date , stakeholder , subcategoryId , chart_section , chart_indicators ) ;
2021-07-21 11:46:49 +02:00
2022-09-08 22:34:25 +02:00
} else if ( indicator . getType ( ) . equals ( " number " ) ) {
2023-07-03 09:42:46 +02:00
saveIndicatorAndAddInSection ( indicator , date , stakeholder , subcategoryId , number_section , number_indicators ) ;
2021-07-21 11:46:49 +02:00
}
}
2022-09-08 22:34:25 +02:00
if ( chart_section ! = null ) {
sectionDAO . save ( chart_section ) ;
}
if ( number_section ! = null ) {
sectionDAO . save ( number_section ) ;
}
2021-07-21 11:46:49 +02:00
}
}
private Section createSection ( Section section , String type , String title , Date date ,
Stakeholder stakeholder , String topicId , String categoryId , String subcategoryId ) {
section = new Section < > ( ) ;
section . setType ( type ) ;
section . setTitle ( title ) ;
section . setStakeholderAlias ( stakeholder . getAlias ( ) ) ;
section . setUpdateDate ( date ) ;
section . setCreationDate ( date ) ;
section . setIndicators ( new ArrayList < > ( ) ) ;
2022-09-08 22:34:25 +02:00
section = sectionController . saveSection ( stakeholder . getId ( ) , topicId , categoryId , subcategoryId , " -1 " , section ) ;
2021-07-21 11:46:49 +02:00
return section ;
}
2023-07-03 09:42:46 +02:00
// private void saveIndicatorAndAddInSection(Indicator indicator, Date date, Stakeholder stakeholder, Section section, List<String> indicators) throws UnsupportedEncodingException {
// saveIndicatorAndAddInSection(indicator, date, stakeholder, section, indicators);
// }
private void saveIndicatorAndAddInSection ( Indicator indicator , Date date , Stakeholder stakeholder , String subcategoryId , Section section , List < String > indicators ) throws UnsupportedEncodingException {
2021-07-21 11:46:49 +02:00
// indicator does not exist in DB
indicator . setCreationDate ( date ) ;
indicator . setUpdateDate ( date ) ;
if ( stakeholder . getDefaultId ( ) = = null ) { // this indicator belongs in default profile and it is new
indicatorDAO . save ( indicator ) ;
2023-07-03 09:42:46 +02:00
onSaveDefaultIndicator ( indicator , section , subcategoryId ) ;
2021-07-21 11:46:49 +02:00
} else { // this indicator belongs in a stakeholder's profile and it is new
indicatorDAO . save ( indicator ) ;
}
indicators . add ( indicator . getId ( ) ) ;
log . debug ( " Indicator saved! " ) ;
}
2019-11-22 16:50:59 +01:00
2020-11-11 13:45:59 +01:00
@PreAuthorize ( " isAuthenticated() " )
2020-01-10 11:48:24 +01:00
@RequestMapping ( value = " /{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/save " , method = RequestMethod . POST )
2019-11-22 16:50:59 +01:00
public Indicator saveIndicator ( @PathVariable ( " stakeholderId " ) String stakeholderId ,
@PathVariable ( " topicId " ) String topicId ,
@PathVariable ( " categoryId " ) String categoryId ,
@PathVariable ( " subcategoryId " ) String subcategoryId ,
2020-01-10 11:48:24 +01:00
@PathVariable ( " sectionId " ) String sectionId ,
2020-01-17 12:43:31 +01:00
@RequestBody Indicator indicator ) throws UnsupportedEncodingException {
2019-11-22 16:50:59 +01:00
log . debug ( " save indicator " ) ;
2020-01-10 11:48:24 +01:00
log . debug ( " Name: " + indicator . getName ( ) + " - Id: " + indicator . getId ( ) + " - Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId + " - SubCategory: " + subcategoryId + " - Section: " + sectionId ) ;
2019-11-22 16:50:59 +01:00
2020-01-10 11:48:24 +01:00
Section < String > section = checkForExceptions ( stakeholderId , topicId , categoryId , subcategoryId , sectionId , indicator . getType ( ) ) ;
2019-11-22 16:50:59 +01:00
2020-11-11 13:45:59 +01:00
Date date = new Date ( ) ;
indicator . setUpdateDate ( date ) ;
2020-05-18 11:17:05 +02:00
Indicator oldIndicator = null ;
if ( indicator . getId ( ) ! = null ) {
oldIndicator = indicatorDAO . findById ( indicator . 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 ( oldIndicator = = null ) {
// EXCEPTION - Indicator not found
throw new EntityNotFoundException ( " save indicator: Indicator with id: " + indicator . getId ( ) + " not found " ) ;
}
2020-11-11 13:45:59 +01:00
} else { // indicator does not exist in DB
indicator . setCreationDate ( date ) ;
2020-05-18 11:17:05 +02:00
}
2020-01-10 11:48:24 +01:00
String indicatorId = indicator . getId ( ) ;
2019-11-22 16:50:59 +01:00
2020-01-10 11:48:24 +01:00
Stakeholder < String > stakeholder = stakeholderDAO . findById ( stakeholderId ) ;
// this indicator belongs in default profile and it is new or it is updated
if ( stakeholder . getDefaultId ( ) = = null ) {
if ( indicatorId = = null ) {
2020-11-11 13:45:59 +01:00
indicatorDAO . save ( indicator ) ;
2023-07-03 09:42:46 +02:00
onSaveDefaultIndicator ( indicator , section , subcategoryId ) ;
2019-11-22 16:50:59 +01:00
}
2020-01-10 11:48:24 +01:00
else {
2020-05-18 11:17:05 +02:00
onUpdateDefaultIndicator ( indicator , stakeholder , oldIndicator ) ;
2020-11-11 13:45:59 +01:00
indicatorDAO . save ( indicator ) ;
2020-01-10 11:48:24 +01:00
}
2020-11-11 13:45:59 +01:00
} else {
indicatorDAO . save ( indicator ) ;
2020-01-10 11:48:24 +01:00
}
List < String > indicators = section . getIndicators ( ) ;
int index = indicators . indexOf ( indicator . getId ( ) ) ;
if ( index = = - 1 ) {
indicators . add ( indicator . getId ( ) ) ;
sectionDAO . save ( section ) ;
log . debug ( " Indicator saved! " ) ;
2019-11-22 16:50:59 +01:00
}
2020-01-10 11:48:24 +01:00
2019-11-22 16:50:59 +01:00
return indicator ;
}
2023-07-03 09:42:46 +02:00
public void onSaveDefaultIndicator ( Indicator indicator , Section defaultSection , String defaultSubcategoryId ) throws UnsupportedEncodingException {
2019-12-19 16:04:25 +01:00
log . debug ( " On save default indicator " ) ;
// new indicator in default profile - add it on profiles of the same type
2023-07-03 09:42:46 +02:00
List < SubCategory > subCategories = subCategoryDAO . findByDefaultId ( defaultSubcategoryId ) ;
for ( SubCategory subCategory : subCategories ) {
List < String > sections = null ;
if ( defaultSection . getType ( ) . equals ( " chart " ) ) {
sections = subCategory . getCharts ( ) ;
} else {
sections = subCategory . getNumbers ( ) ;
2019-12-19 16:04:25 +01:00
}
2023-07-03 09:42:46 +02:00
for ( String sectionId : sections ) {
Section section = sectionDAO . findById ( sectionId ) ;
2023-07-31 09:59:03 +02:00
if ( section . getDefaultId ( ) ! = null & & section . getDefaultId ( ) . equals ( defaultSection . getId ( ) ) ) {
2023-07-03 09:42:46 +02:00
Indicator indicatorNew = new Indicator ( ) ;
indicatorNew . copyFromDefault ( indicator , subCategory . getVisibility ( ) ) ;
for ( IndicatorPath indicatorPath : indicatorNew . getIndicatorPaths ( ) ) {
Stakeholder stakeholder = stakeholderDAO . findByAlias ( section . getStakeholderAlias ( ) ) ;
parameterMapping ( indicatorPath , stakeholder ) ;
}
2019-12-19 16:04:25 +01:00
2023-07-03 09:42:46 +02:00
indicatorDAO . save ( indicatorNew ) ;
2019-12-19 16:04:25 +01:00
2023-07-03 09:42:46 +02:00
List < String > indicators = section . getIndicators ( ) ;
indicators . add ( indicatorNew . getId ( ) ) ;
sectionDAO . save ( section ) ;
}
}
2019-12-19 16:04:25 +01:00
}
}
2020-05-18 11:17:05 +02:00
public void onUpdateDefaultIndicator ( Indicator indicator , Stakeholder stakeholder , Indicator oldIndicator ) throws UnsupportedEncodingException {
2019-12-19 16:04:25 +01:00
log . debug ( " On update default indicator " ) ;
// indicator already exists - check if changed and update all indicators based on it
2020-11-11 13:45:59 +01:00
boolean changed ;
2019-12-19 16:04:25 +01:00
List < Indicator > indicators = indicatorDAO . findByDefaultId ( indicator . getId ( ) ) ;
for ( Indicator indicatorBasedOnDefault : indicators ) {
2020-11-11 13:45:59 +01:00
changed = false ;
2020-12-22 10:29:17 +01:00
// if(indicator.getName() != null && !indicator.getName().equals(indicatorBasedOnDefault.getName())
// && (oldIndicator.getName() == null || oldIndicator.getName().equals(indicatorBasedOnDefault.getName()))) {
if ( (
( indicator . getName ( ) = = null & & oldIndicator . getName ( ) ! = null )
| |
( indicator . getName ( ) ! = null & & ! indicator . getName ( ) . equals ( indicatorBasedOnDefault . getName ( ) ) )
) & & (
( oldIndicator . getName ( ) = = null & & indicatorBasedOnDefault . getName ( ) = = null )
| |
( oldIndicator . getName ( ) ! = null & & oldIndicator . getName ( ) . equals ( indicatorBasedOnDefault . getName ( ) ) )
) ) {
2020-05-18 11:17:05 +02:00
indicatorBasedOnDefault . setName ( indicator . getName ( ) ) ;
changed = true ;
}
2020-12-22 10:29:17 +01:00
if ( indicator . getDescription ( ) ! = null & & ! indicator . getDescription ( ) . equals ( indicatorBasedOnDefault . getDescription ( ) )
| | indicator . getDescription ( ) = = null & & indicatorBasedOnDefault . getDescription ( ) ! = null ) {
2020-05-18 11:17:05 +02:00
2020-06-25 11:55:46 +02:00
indicatorBasedOnDefault . setDescription ( indicator . getDescription ( ) ) ;
2020-05-18 11:17:05 +02:00
changed = true ;
}
2020-12-22 10:29:17 +01:00
// if(indicator.getAdditionalDescription() != null && !indicator.getAdditionalDescription().equals(indicatorBasedOnDefault.getAdditionalDescription())
// && (oldIndicator.getAdditionalDescription() == null || oldIndicator.getAdditionalDescription().equals(indicatorBasedOnDefault.getAdditionalDescription()))) {
if ( (
( indicator . getAdditionalDescription ( ) = = null & & oldIndicator . getAdditionalDescription ( ) ! = null )
| |
( indicator . getAdditionalDescription ( ) ! = null & & ! indicator . getAdditionalDescription ( ) . equals ( indicatorBasedOnDefault . getAdditionalDescription ( ) ) )
) & & (
( oldIndicator . getAdditionalDescription ( ) = = null & & indicatorBasedOnDefault . getAdditionalDescription ( ) = = null )
| |
( oldIndicator . getAdditionalDescription ( ) ! = null & & oldIndicator . getAdditionalDescription ( ) . equals ( indicatorBasedOnDefault . getAdditionalDescription ( ) ) )
) ) {
2020-11-11 13:45:59 +01:00
indicatorBasedOnDefault . setAdditionalDescription ( indicator . getAdditionalDescription ( ) ) ;
changed = true ;
}
2019-12-19 16:04:25 +01:00
int i = 0 ;
List < IndicatorPath > indicatorPaths = indicatorBasedOnDefault . getIndicatorPaths ( ) ;
2020-11-11 13:45:59 +01:00
if ( indicatorPaths = = null & & indicator . getIndicatorPaths ( ) ! = null ) {
indicatorPaths = new ArrayList < > ( ) ;
}
2019-12-19 16:04:25 +01:00
for ( IndicatorPath indicatorPath : indicator . getIndicatorPaths ( ) ) {
2020-11-11 13:45:59 +01:00
IndicatorPath indicatorPathBasedOnDefault = null ;
if ( i < indicatorPaths . size ( ) ) {
indicatorPathBasedOnDefault = indicatorPaths . get ( i ) ;
}
2019-12-19 16:04:25 +01:00
if ( indicatorPathBasedOnDefault = = null ) {
// Add new indicator path in existing indicators
IndicatorPath indicatorPathNew = new IndicatorPath ( indicatorPath ) ;
parameterMapping ( indicatorPathNew , stakeholder ) ;
indicatorPaths . add ( indicatorPathNew ) ;
changed = true ;
} else {
2020-05-18 11:17:05 +02:00
IndicatorPath oldIndicatorPath = oldIndicator . getIndicatorPaths ( ) . get ( i ) ;
2019-12-19 16:04:25 +01:00
// Check if there are changes in indicator path and update existing indicators if needed
2020-11-11 13:45:59 +01:00
log . debug ( " update indicator path: " + i + " (indicator id: " + indicatorBasedOnDefault . getId ( ) + " ) " ) ;
2020-06-17 13:37:42 +02:00
2020-12-22 10:29:17 +01:00
// if(indicatorPath.getType() != null
// && !indicatorPath.getType().equals(indicatorPathBasedOnDefault.getType())
// && (oldIndicatorPath.getType().equals(indicatorPathBasedOnDefault.getType()))) {
if ( (
( indicatorPath . getType ( ) = = null & & oldIndicatorPath . getType ( ) ! = null )
| |
( indicatorPath . getType ( ) ! = null & & ! indicatorPath . getType ( ) . equals ( indicatorPathBasedOnDefault . getType ( ) ) )
) & & (
( oldIndicatorPath . getType ( ) = = null & & indicatorPathBasedOnDefault . getType ( ) = = null )
| |
( oldIndicatorPath . getType ( ) ! = null & & oldIndicatorPath . getType ( ) . equals ( indicatorPathBasedOnDefault . getType ( ) ) )
) ) {
2019-12-19 16:04:25 +01:00
indicatorPathBasedOnDefault . setType ( indicatorPath . getType ( ) ) ;
2020-05-18 11:17:05 +02:00
changed = true ; // parameter "type" needs to be changed as well
2019-12-19 16:04:25 +01:00
}
2020-05-18 11:17:05 +02:00
log . debug ( " After type check: " + changed ) ;
2023-05-03 12:02:56 +02:00
if ( (
( indicatorPath . getFormat ( ) = = null & & oldIndicatorPath . getFormat ( ) ! = null )
| |
( indicatorPath . getFormat ( ) ! = null & & ! indicatorPath . getFormat ( ) . equals ( indicatorPathBasedOnDefault . getFormat ( ) ) )
) & & (
( oldIndicatorPath . getFormat ( ) = = null & & indicatorPathBasedOnDefault . getFormat ( ) = = null )
| |
( oldIndicatorPath . getFormat ( ) ! = null & & oldIndicatorPath . getFormat ( ) . equals ( indicatorPathBasedOnDefault . getFormat ( ) ) )
) ) {
indicatorPathBasedOnDefault . setFormat ( indicatorPath . getFormat ( ) ) ;
changed = true ;
}
log . debug ( " After type check: " + changed ) ;
2020-05-18 11:17:05 +02:00
2020-12-22 10:29:17 +01:00
// if(indicatorPath.getSource() != null
// && !indicatorPath.getSource().equals(indicatorPathBasedOnDefault.getSource())
// && (oldIndicatorPath.getSource().equals(indicatorPathBasedOnDefault.getSource()))) {
if ( (
( indicatorPath . getSource ( ) = = null & & oldIndicatorPath . getSource ( ) ! = null )
| |
( indicatorPath . getSource ( ) ! = null & & ! indicatorPath . getSource ( ) . equals ( indicatorPathBasedOnDefault . getSource ( ) ) )
) & & (
( oldIndicatorPath . getSource ( ) = = null & & indicatorPathBasedOnDefault . getSource ( ) = = null )
| |
( oldIndicatorPath . getSource ( ) ! = null & & oldIndicatorPath . getSource ( ) . equals ( indicatorPathBasedOnDefault . getSource ( ) ) )
) ) {
2019-12-19 16:04:25 +01:00
indicatorPathBasedOnDefault . setSource ( indicatorPath . getSource ( ) ) ;
changed = true ;
}
2020-05-18 11:17:05 +02:00
log . debug ( " After source check: " + changed ) ;
2020-12-22 10:29:17 +01:00
// if(indicatorPath.getUrl() != null
// && !indicatorPath.getUrl().equals(indicatorPathBasedOnDefault.getUrl())
// && (oldIndicatorPath.getUrl().equals(indicatorPathBasedOnDefault.getUrl()))) {
if ( (
( indicatorPath . getUrl ( ) = = null & & oldIndicatorPath . getUrl ( ) ! = null )
| |
( indicatorPath . getUrl ( ) ! = null & & ! indicatorPath . getUrl ( ) . equals ( indicatorPathBasedOnDefault . getUrl ( ) ) )
) & & (
( oldIndicatorPath . getUrl ( ) = = null & & indicatorPathBasedOnDefault . getUrl ( ) = = null )
| |
( oldIndicatorPath . getUrl ( ) ! = null & & oldIndicatorPath . getUrl ( ) . equals ( indicatorPathBasedOnDefault . getUrl ( ) ) )
) ) {
2019-12-19 16:04:25 +01:00
indicatorPathBasedOnDefault . setUrl ( indicatorPath . getUrl ( ) ) ;
changed = true ;
}
2020-05-18 11:17:05 +02:00
log . debug ( " After url check: " + changed ) ;
2020-12-22 10:29:17 +01:00
if ( (
( indicatorPath . getChartObject ( ) = = null & & oldIndicatorPath . getChartObject ( ) ! = null )
| |
( indicatorPath . getChartObject ( ) ! = null & & ! indicatorPath . getChartObject ( ) . equals ( indicatorPathBasedOnDefault . getChartObject ( ) ) )
) & & (
( oldIndicatorPath . getChartObject ( ) = = null & & indicatorPathBasedOnDefault . getChartObject ( ) = = null )
| |
( oldIndicatorPath . getChartObject ( ) ! = null & & oldIndicatorPath . getChartObject ( ) . equals ( indicatorPathBasedOnDefault . getChartObject ( ) ) )
) ) {
2020-05-18 11:17:05 +02:00
2019-12-19 16:04:25 +01:00
indicatorPathBasedOnDefault . setChartObject ( indicatorPath . getChartObject ( ) ) ;
changed = true ;
}
2020-05-18 11:17:05 +02:00
log . debug ( " After chartObject check: " + changed ) ;
if ( indicatorPath . getParameters ( ) ! = null ) {
if ( indicatorPathBasedOnDefault . getParameters ( ) = = null ) {
indicatorPathBasedOnDefault . setParameters ( new HashMap < > ( ) ) ;
2019-12-19 16:04:25 +01:00
}
2020-05-18 11:17:05 +02:00
//if (indicatorPath.getParameters().size() != indicatorPathBasedOnDefault.getParameters().size()) {
//log.debug("Different number of parameters");
2021-04-22 12:05:43 +02:00
for ( Map . Entry < String , String > parameter : indicatorPath . getParameters ( ) . entrySet ( ) ) {
log . debug ( " \ nindicatorPath: parameter.getKey(): " + parameter . getKey ( ) + " - value: " + parameter . getValue ( )
+ " \ nindicatorPathBasedOnDefault:parameters:key: " + indicatorPathBasedOnDefault . getParameters ( ) . get ( parameter . getKey ( ) )
+ " \ noldIndicatorPath:parameters:key: " + ( oldIndicatorPath . getParameters ( ) = = null ? " null " : oldIndicatorPath . getParameters ( ) . get ( parameter . getKey ( ) ) ) ) ;
if ( ! indicatorPathBasedOnDefault . getParameters ( ) . containsKey ( parameter . getKey ( ) )
| | ( oldIndicatorPath . getParameters ( ) = = null | | oldIndicatorPath . getParameters ( ) . get ( parameter . getKey ( ) ) = = null
| | ( oldIndicatorPath . getParameters ( ) . get ( parameter . getKey ( ) ) . equals ( indicatorPathBasedOnDefault . getParameters ( ) . get ( parameter . getKey ( ) ) )
& & ! parameter . getValue ( ) . equals ( indicatorPathBasedOnDefault . getParameters ( ) . get ( parameter . getKey ( ) ) ) ) )
) {
indicatorPathBasedOnDefault . getParameters ( ) . put ( parameter . getKey ( ) , parameter . getValue ( ) ) ;
changed = true ;
}
// else if(parameter.getKey().equals("type")) {
// indicatorPathBasedOnDefault.getParameters().put(parameter.getKey(), parameter.getValue());
// changed = true;
// }
}
// When deleting indicator path parameters in a default profile, delete them also from all children profiles
if ( oldIndicatorPath . getParameters ( ) ! = null & & indicatorPath . getParameters ( ) . size ( ) < oldIndicatorPath . getParameters ( ) . size ( ) ) {
for ( Map . Entry < String , String > parameter : oldIndicatorPath . getParameters ( ) . entrySet ( ) ) {
if ( ! indicatorPath . getParameters ( ) . containsKey ( parameter . getKey ( ) ) ) {
indicatorPathBasedOnDefault . getParameters ( ) . remove ( parameter . getKey ( ) ) ;
2020-05-18 11:17:05 +02:00
}
}
2021-04-22 12:05:43 +02:00
}
2020-11-11 13:45:59 +01:00
parameterMapping ( indicatorPathBasedOnDefault , stakeholder ) ;
2020-05-18 11:17:05 +02:00
//}
2019-12-19 16:04:25 +01:00
}
2020-11-11 13:45:59 +01:00
log . debug ( " After parameters check: " + changed ) ;
2020-05-18 11:17:05 +02:00
if ( indicatorPath . getJsonPath ( ) ! = null ) {
2020-11-11 13:45:59 +01:00
boolean jsonPathChanged = false ;
boolean breaked = false ;
int oldJsonPathSize = 0 ;
if ( oldIndicatorPath . getJsonPath ( ) ! = null ) {
oldJsonPathSize = oldIndicatorPath . getJsonPath ( ) . size ( ) ;
}
int basedOnDefaultJsonPathSize = 0 ;
if ( indicatorPathBasedOnDefault . getJsonPath ( ) ! = null ) {
basedOnDefaultJsonPathSize = indicatorPathBasedOnDefault . getJsonPath ( ) . size ( ) ;
}
log . debug ( " old: " + oldJsonPathSize + " - based on default: " + basedOnDefaultJsonPathSize + " - new: " + indicatorPath . getJsonPath ( ) . size ( ) ) ;
if ( oldJsonPathSize = = basedOnDefaultJsonPathSize ) {
if ( indicatorPathBasedOnDefault . getJsonPath ( ) = = null & & indicatorPath . getJsonPath ( ) . size ( ) > 0 ) {
2020-05-18 11:17:05 +02:00
indicatorPathBasedOnDefault . setJsonPath ( new ArrayList < > ( ) ) ;
}
2020-11-11 13:45:59 +01:00
int basedOnDefaultIndex = 0 ;
int oldIndex = 0 ;
Iterator < String > jsonStringBasedOnDefaultIterator = indicatorPathBasedOnDefault . getJsonPath ( ) . iterator ( ) ;
while ( jsonStringBasedOnDefaultIterator . hasNext ( ) ) {
String jsonStringBasedOnDefault = jsonStringBasedOnDefaultIterator . next ( ) ;
if ( oldIndicatorPath . getJsonPath ( ) . get ( oldIndex ) . equals ( jsonStringBasedOnDefault ) ) {
if ( basedOnDefaultIndex > = indicatorPath . getJsonPath ( ) . size ( ) ) { // string deleted
jsonStringBasedOnDefaultIterator . remove ( ) ;
jsonPathChanged = true ;
} else { // check if string changed
if ( ! indicatorPath . getJsonPath ( ) . get ( basedOnDefaultIndex ) . equals ( jsonStringBasedOnDefault ) ) {
indicatorPathBasedOnDefault . getJsonPath ( ) . set ( basedOnDefaultIndex , indicatorPath . getJsonPath ( ) . get ( basedOnDefaultIndex ) ) ;
jsonPathChanged = true ;
}
basedOnDefaultIndex + + ;
}
oldIndex + + ;
} else {
breaked = true ;
jsonPathChanged = false ;
log . debug ( " not the same: " + oldIndex ) ;
break ;
}
}
int index = 0 ;
if ( ! breaked & & indicatorPath . getJsonPath ( ) . size ( ) > indicatorPathBasedOnDefault . getJsonPath ( ) . size ( ) ) { // strings added
jsonPathChanged = true ;
for ( index = indicatorPathBasedOnDefault . getJsonPath ( ) . size ( ) ; index < indicatorPath . getJsonPath ( ) . size ( ) ; index + + ) {
indicatorPathBasedOnDefault . getJsonPath ( ) . add ( indicatorPath . getJsonPath ( ) . get ( index ) ) ;
}
}
if ( jsonPathChanged ) {
2020-05-18 11:17:05 +02:00
changed = true ;
}
2019-12-19 16:04:25 +01:00
}
2021-04-22 12:05:43 +02:00
// TODO when deleting indicator path json path strings... --> is this done? (line 327)
2019-12-19 16:04:25 +01:00
}
2020-11-11 13:45:59 +01:00
log . debug ( " After jsonPath check: " + changed ) ;
2019-12-19 16:04:25 +01:00
}
i + + ;
}
2020-11-11 13:45:59 +01:00
// TODO when deleting indicator paths...
2020-05-18 11:17:05 +02:00
2019-12-19 16:04:25 +01:00
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
2020-11-11 13:45:59 +01:00
indicatorBasedOnDefault . setUpdateDate ( indicator . getUpdateDate ( ) ) ;
2019-12-19 16:04:25 +01:00
indicatorDAO . save ( indicatorBasedOnDefault ) ;
}
}
2020-01-17 12:43:31 +01:00
public void parameterMapping ( IndicatorPath indicatorPath , Stakeholder stakeholder ) throws UnsupportedEncodingException {
2020-06-17 13:37:42 +02:00
if ( indicatorPath . getParameters ( ) ! = null ) {
if ( indicatorPath . getParameters ( ) . containsKey ( " index_name " ) ) {
indicatorPath . getParameters ( ) . put ( " index_name " , stakeholder . getIndex_name ( ) ) ;
} else if ( indicatorPath . getParameters ( ) . containsKey ( " index_shortName " ) ) {
2023-12-13 14:10:13 +01:00
if ( stakeholder . getIndex_shortName ( ) ! = null ) {
indicatorPath . getParameters ( ) . put ( " index_shortName " , stakeholder . getIndex_shortName ( ) . toLowerCase ( ) ) ;
} else {
indicatorPath . getParameters ( ) . remove ( " index_shortName " ) ;
}
2020-06-17 13:37:42 +02:00
} else if ( indicatorPath . getParameters ( ) . containsKey ( " index_id " ) ) {
indicatorPath . getParameters ( ) . put ( " index_id " , stakeholder . getIndex_id ( ) ) ;
}
2019-12-19 16:04:25 +01:00
}
2020-01-17 12:43:31 +01:00
2020-06-17 13:37:42 +02:00
// // url encoding for number indicators
// String url = indicatorPath.getUrl();
// String encoded_index_id = urlEncode(URLEncoder.encode(stakeholder.getIndex_id(), "UTF-8"));
// url = url.replace("index_id", encoded_index_id);
// String encoded_index_name = urlEncode(URLEncoder.encode(stakeholder.getIndex_name(), "UTF-8"));
// url = url.replace("index_name", encoded_index_name);
// String encoded_index_shortName = urlEncode(URLEncoder.encode(stakeholder.getIndex_shortName(), "UTF-8"));
// url = url.replace("index_shortName", encoded_index_shortName);
// indicatorPath.setUrl(url);
2020-01-17 12:43:31 +01:00
}
public String urlEncode ( String encodedIndicatorPathField ) {
String indicatorPathField = " " ;
for ( int i = 0 ; i < encodedIndicatorPathField . length ( ) ; i + + ) {
String character = encodedIndicatorPathField . substring ( i , i + 1 ) ;
if ( character . equals ( " + " ) ) {
indicatorPathField = indicatorPathField . concat ( " %20 " ) ;
} else if ( character . equals ( " % " ) ) {
//grab the hex in pairs
String output = encodedIndicatorPathField . substring ( i + 1 , ( i + 3 ) ) ;
if ( output . equals ( " 7E " ) | | output . equals ( " 27 " ) | | output . equals ( " 28 " ) | | output . equals ( " 29 " ) | | output . equals ( " 21 " ) ) {
//convert hex to decimal
int decimal = Integer . parseInt ( output , 16 ) ;
//convert the decimal to character
StringBuilder sb = new StringBuilder ( ) ;
sb . append ( ( char ) decimal ) ;
indicatorPathField = indicatorPathField . concat ( sb . toString ( ) ) ;
} else {
indicatorPathField = indicatorPathField . concat ( character + output ) ;
}
i + = 2 ;
} else {
indicatorPathField = indicatorPathField . concat ( character ) ;
}
}
return indicatorPathField ;
2019-12-19 16:04:25 +01:00
}
2020-11-11 13:45:59 +01:00
@PreAuthorize ( " isAuthenticated() " )
2020-01-10 11:48:24 +01:00
@RequestMapping ( value = " /{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{indicatorId}/delete " , method = RequestMethod . DELETE )
2019-11-22 16:50:59 +01:00
public boolean deleteIndicator ( @PathVariable ( " stakeholderId " ) String stakeholderId ,
@PathVariable ( " topicId " ) String topicId ,
@PathVariable ( " categoryId " ) String categoryId ,
@PathVariable ( " subcategoryId " ) String subcategoryId ,
2020-01-10 11:48:24 +01:00
@PathVariable ( " sectionId " ) String sectionId ,
2020-06-22 21:58:40 +02:00
@PathVariable ( " indicatorId " ) String indicatorId ,
@RequestParam ( required = false ) String children ) {
2019-11-22 16:50:59 +01:00
log . debug ( " delete indicator " ) ;
2020-01-10 11:48:24 +01:00
log . debug ( " Id: " + indicatorId + " - Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId + " - SubCategory: " + subcategoryId + " - Section: " + sectionId ) ;
2019-11-22 16:50:59 +01:00
2020-01-10 11:48:24 +01:00
Indicator indicator = indicatorDAO . findById ( indicatorId ) ;
if ( indicator ! = null ) {
Section < String > section = checkForExceptions ( stakeholderId , topicId , categoryId , subcategoryId , sectionId , indicator . getType ( ) ) ;
2019-11-22 16:50:59 +01:00
2020-11-11 13:45:59 +01:00
Stakeholder < String > stakeholder = stakeholderDAO . findById ( stakeholderId ) ;
2023-07-04 15:02:34 +02:00
if ( indicator . getDefaultId ( ) ! = null & & ! rolesUtils . hasCreateAndDeleteAuthority ( stakeholder . getType ( ) ) ) {
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 ( " Delete indicator: You are not authorized to delete a default Indicator in stakeholder with id: " + stakeholderId ) ;
2020-11-11 13:45:59 +01:00
}
2020-01-10 11:48:24 +01:00
List < String > indicators = section . getIndicators ( ) ;
2019-11-22 16:50:59 +01:00
2020-01-10 11:48:24 +01:00
int index = indicators . indexOf ( indicatorId ) ;
if ( index ! = - 1 ) {
2020-06-22 21:58:40 +02:00
// this indicator belongs in default profile
if ( section . getDefaultId ( ) = = null & & children ! = null ) {
onDeleteDefaultIndicator ( indicatorId , sectionId , children ) ;
}
2020-01-10 11:48:24 +01:00
indicators . remove ( index ) ;
sectionDAO . save ( section ) ;
2019-11-22 16:50:59 +01:00
2020-01-10 11:48:24 +01:00
indicatorDAO . delete ( indicatorId ) ;
log . debug ( " Indicator deleted! " ) ;
} else {
// EXCEPTION - Indicator not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subcategory.getAlias(); -> Section: section.getTitle();
throw new PathNotValidException ( " Delete indicator: Indicator with id: " + indicatorId + " not found in Sectiom: " + sectionId ) ;
2019-11-22 16:50:59 +01:00
}
} else {
2020-01-10 11:48:24 +01:00
// EXCEPTION - Indicator not found
throw new EntityNotFoundException ( " Delete indicator: Indicator with id: " + indicatorId + " not found " ) ;
2019-11-22 16:50:59 +01:00
}
return true ;
}
2020-06-22 21:58:40 +02:00
public boolean onDeleteDefaultIndicator ( String defaultIndicatorId , String defaultSectionId , String children ) {
if ( children . equals ( " delete " ) ) {
// // 1st way
// List<Section> sections = sectionDAO.findByDefaultId(defaultSectionId);
//
// for(Section section : sections) {
// List<String> indicators = section.getIndicators();
//
// Iterator<String> indicatorsIterator = indicators.iterator();
// while(indicatorsIterator.hasNext()) {
// String indicatorId = indicatorsIterator.next();
//
// Indicator indicator = indicatorDAO.findById(indicatorId);
// if (indicator.getDefaultId().equals(defaultIndicatorId)) {
// indicatorsIterator.remove();
// sectionDAO.save(section);
//
// indicatorDAO.delete(indicatorId);
// log.debug("Indicator deleted!");
//
// break;
// }
// }
// }
// 2nd way
List < Section > sections = sectionDAO . findByDefaultId ( defaultSectionId ) ;
List < Indicator > indicators = indicatorDAO . findByDefaultId ( defaultIndicatorId ) ;
for ( Section section : sections ) {
Iterator < Indicator > indicatorsIterator = indicators . iterator ( ) ;
while ( indicatorsIterator . hasNext ( ) ) {
String indicatorId = indicatorsIterator . next ( ) . getId ( ) ;
if ( section . getIndicators ( ) . contains ( indicatorId ) ) {
indicatorsIterator . remove ( ) ;
section . getIndicators ( ) . remove ( indicatorId ) ;
sectionDAO . save ( section ) ;
indicatorDAO . delete ( indicatorId ) ;
log . debug ( " Indicator with id: " + indicatorId + " deleted! " ) ;
break ;
}
}
}
// // 3rd way - parentId
// List<Indicator> indicators = indicatorDAO.findByDefaultId(defaultIndicatorId);
// for(Indicator indicator : indicators) {
// Section section = sectionDAO.findById(indicator.getParent());
// List<String> sectionIndicators = section.getIndicators();
//
// sectionIndicators.remove(indicator.getId());
// sectionDAO.save(section);
//
// indicatorDAO.delete(indicator.getId());
// log.debug("Indicator deleted!");
// }
} else if ( children . equals ( " disconnect " ) ) {
List < Indicator > indicators = indicatorDAO . findByDefaultId ( defaultIndicatorId ) ;
for ( Indicator indicator : indicators ) {
indicator . setDefaultId ( null ) ;
indicatorDAO . save ( indicator ) ;
log . debug ( " DefaultId for Indicator with id: " + indicator . getId ( ) + " empty! " ) ;
}
}
return true ;
}
2020-01-10 11:48:24 +01:00
// @RequestMapping(value = "/{stakeholderId}/charts/delete", method = RequestMethod.DELETE)
// public boolean deleteAllChartIndicators(@PathVariable("stakeholderId") String stakeholderId) {
// log.debug("delete all chart indicators of stakeholder");
// log.debug("Stakeholder: "+stakeholderId);
//
// Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
// if(stakeholder != null) {
//
// for(String topicId : stakeholder.getTopics()) {
// Topic<String> topic = topicDAO.findById(topicId);
// if(topic != null) {
// for(String categoryId : topic.getCategories()) {
// Category<String> category = categoryDAO.findById(categoryId);
// if(category != null) {
// for(String subcategoryId : category.getSubCategories()) {
// SubCategory<String> subcategory = subCategoryDAO.findById(subcategoryId);
// if(subcategory != null) {
//
// for(String sectionId : subcategory.getCharts()) {
// Section<String> section = sectionDAO.findById(sectionId);
// if (section != null) {
//
// List<String> indicators = section.getIndicators();
// Iterator<String> indicatorsIterator = section.getIndicators().iterator();
//
// while (indicatorsIterator.hasNext()) {
// String indicatorId = indicatorsIterator.next();
// Indicator indicator = indicatorDAO.findById(indicatorId);
// if (indicator != null) {
// int index = indicators.indexOf(indicatorId);
// if (index != -1) {
// indicatorsIterator.remove();
// //indicators.remove(index);
//
// indicatorDAO.delete(indicatorId);
// log.debug("Indicator deleted!");
// } else {
// // EXCEPTION - Indicator not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subcategory.getAlias(); -> Section: section.getTitle();
// throw new PathNotValidException("Delete indicator: Indicator with id: " + indicatorId + " not found in Section: " + sectionId);
// }
// } else {
// // EXCEPTION - Indicator not found
// throw new EntityNotFoundException("Delete indicator: Indicator with id: " + indicatorId + " not found");
// }
// }
// sectionDAO.save(section);
// } else {
// // EXCEPTION - Section not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subcategory.getAlias();
// throw new PathNotValidException("Delete indicator: Section with id: " + sectionId + " not found in SubCategory: " + subcategoryId);
// }
// }
// } else {
// // EXCEPTION - SubCategory not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias();
// throw new PathNotValidException("Delete indicator: SubCategory with id: "+subcategoryId+" not found in Category: "+categoryId);
// }
// }
// } else {
// // EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias();
// throw new PathNotValidException("Delete indicator: Category with id: "+categoryId+" not found in Topic: "+topicId);
// }
// }
// } else {
// // EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
// throw new PathNotValidException("Delete indicator: Topic with id: "+topicId+" not found in Stakeholder: "+stakeholderId);
// }
// }
// } else {
// // EXCEPTION - Stakeholder not found
// throw new EntityNotFoundException("Delete indicator: Stakeholder with id: "+stakeholderId+" not found");
// }
// return true;
// }
2020-11-11 13:45:59 +01:00
@PreAuthorize ( " isAuthenticated() " )
2020-01-10 11:48:24 +01:00
@RequestMapping ( value = " /{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{type}/reorder " , method = RequestMethod . POST )
2019-11-26 12:08:36 +01:00
public List < Indicator > reorderIndicators ( @PathVariable ( " stakeholderId " ) String stakeholderId ,
2020-01-10 11:48:24 +01:00
@PathVariable ( " topicId " ) String topicId ,
@PathVariable ( " categoryId " ) String categoryId ,
@PathVariable ( " subcategoryId " ) String subcategoryId ,
@PathVariable ( " sectionId " ) String sectionId ,
@PathVariable ( " type " ) String type ,
[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
@RequestBody ReorderEvent reorderEvent ) {
2019-12-19 16:04:25 +01:00
log . debug ( " reorder indicators of type: " + type ) ;
2020-01-10 11:48:24 +01:00
log . debug ( " Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId + " - SubCategory: " + subcategoryId + " - Section: " + sectionId ) ;
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 < String > indicators = reorderEvent . getIds ( ) ;
String actionType = reorderEvent . getAction ( ) ;
String targetId = reorderEvent . getTarget ( ) ;
2020-01-10 11:48:24 +01:00
Section < String > section = checkForExceptions ( stakeholderId , topicId , categoryId , subcategoryId , sectionId , type ) ;
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 < String > oldIndicators = section . getIndicators ( ) ;
for ( String indicatorId : oldIndicators ) {
if ( ( ! actionType . equals ( " removed " ) | | ! targetId . equals ( indicatorId ) ) & & ! indicators . contains ( indicatorId ) ) {
indicators . add ( indicatorId ) ;
}
}
2020-01-10 11:48:24 +01:00
section . setIndicators ( indicators ) ;
2019-11-26 12:08:36 +01:00
List < Indicator > indicatorsFull = new ArrayList < > ( ) ;
for ( String indicatorId : indicators ) {
[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
Indicator indicator = indicatorDAO . findById ( indicatorId ) ;
if ( indicator = = null ) {
// EXCEPTION - Indicator not found
throw new EntityNotFoundException ( " Reorder indicators: Indicator with id: " + indicatorId + " not found " ) ;
}
indicatorsFull . add ( indicator ) ;
2019-11-26 12:08:36 +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
sectionDAO . save ( section ) ;
log . debug ( " Indicators reordered! " ) ;
2019-11-26 12:08:36 +01:00
return indicatorsFull ;
2019-11-22 16:50:59 +01:00
}
2019-12-19 16:04:25 +01:00
2020-11-11 13:45:59 +01:00
// @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{indicatorId}/toggle-status", method = RequestMethod.POST)
// public Boolean toggleIndicatorStatus(@PathVariable("stakeholderId") String stakeholderId,
// @PathVariable("topicId") String topicId,
// @PathVariable("categoryId") String categoryId,
// @PathVariable("subcategoryId") String subcategoryId,
// @PathVariable("sectionId") String sectionId,
// @PathVariable("indicatorId") String indicatorId) {
// log.debug("toggle indicator status (isActive)");
// log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId + " - Section: "+sectionId+ " - Indicator: "+indicatorId);
//
// Indicator indicator = indicatorDAO.findById(indicatorId);
// if (indicator == null) {
// // EXCEPTION - Indicator not found
// throw new EntityNotFoundException("Toggle indicator status: Indicator with id: "+indicatorId+" not found");
// }
// indicator.setIsActive(!indicator.getIsActive());
//
// this.toggleIndicator(stakeholderId, topicId, categoryId, subcategoryId, sectionId, indicator);
//
// return indicator.getIsActive();
// }
//
// @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{indicatorId}/toggle-access", method = RequestMethod.POST)
// public Boolean toggleIndicatorAccess(@PathVariable("stakeholderId") String stakeholderId,
// @PathVariable("topicId") String topicId,
// @PathVariable("categoryId") String categoryId,
// @PathVariable("subcategoryId") String subcategoryId,
// @PathVariable("sectionId") String sectionId,
// @PathVariable("indicatorId") String indicatorId) {
// log.debug("toggle indicator access (isPublic)");
// log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId + " - Section: "+sectionId+ " - Indicator: "+indicatorId);
//
// Indicator indicator = indicatorDAO.findById(indicatorId);
// if (indicator == null) {
// // EXCEPTION - Indicator not found
// throw new EntityNotFoundException("Toggle indicator access: Indicator with id: "+indicatorId+" not found");
// }
// indicator.setIsPublic(!indicator.getIsPublic());
//
// this.toggleIndicator(stakeholderId, topicId, categoryId, subcategoryId, sectionId, indicator);
//
// return indicator.getIsPublic();
// }
2019-12-19 16:04:25 +01:00
2020-11-11 13:45:59 +01:00
@PreAuthorize ( " isAuthenticated() " )
@RequestMapping ( value = " /{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/{indicatorId}/change-visibility " , method = RequestMethod . POST )
2022-09-14 17:02:47 +02:00
public Indicator changeIndicatorVisibility ( @PathVariable ( " stakeholderId " ) String stakeholderId ,
2020-11-11 13:45:59 +01:00
@PathVariable ( " topicId " ) String topicId ,
@PathVariable ( " categoryId " ) String categoryId ,
@PathVariable ( " subcategoryId " ) String subcategoryId ,
@PathVariable ( " sectionId " ) String sectionId ,
@PathVariable ( " indicatorId " ) String indicatorId ,
@RequestParam ( " visibility " ) Visibility visibility ) {
log . debug ( " change indicator visibility: " + visibility ) ;
2020-01-10 11:48:24 +01:00
log . debug ( " Stakeholder: " + stakeholderId + " - Topic: " + topicId + " - Category: " + categoryId + " - SubCategory: " + subcategoryId + " - Section: " + sectionId + " - Indicator: " + indicatorId ) ;
2019-12-19 16:04:25 +01:00
Indicator indicator = indicatorDAO . findById ( indicatorId ) ;
if ( indicator = = null ) {
// EXCEPTION - Indicator not found
2022-09-14 17:02:47 +02:00
throw new EntityNotFoundException ( " Change indicator visibility: Indicator with id: " + indicatorId + " not found " ) ;
2019-12-19 16:04:25 +01:00
}
2020-01-10 11:48:24 +01:00
Section < String > section = checkForExceptions ( stakeholderId , topicId , categoryId , subcategoryId , sectionId , indicator . getType ( ) ) ;
List < String > indicators = section . getIndicators ( ) ;
2019-12-19 16:04:25 +01:00
2022-09-14 17:02:47 +02:00
if ( indicators . contains ( indicatorId ) ) {
return changeVisibilityTree ( indicatorId , indicator , visibility ) ;
2019-12-19 16:04:25 +01:00
} else {
2020-01-10 11:48:24 +01:00
// EXCEPTION - Indicator not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subCategory.getAlias(); -> Section: section.getTitle();
2022-09-14 17:02:47 +02:00
throw new PathNotValidException ( " Toggle indicators: Indicator with id: " + indicatorId + " not found in Section: " + sectionId ) ;
2020-01-10 11:48:24 +01:00
}
2022-09-14 17:02:47 +02:00
}
2020-01-10 11:48:24 +01:00
2022-09-14 17:02:47 +02:00
public Indicator changeVisibilityTree ( String indicatorId , Indicator indicator , Visibility visibility ) {
if ( indicator = = null ) {
indicator = indicatorDAO . findById ( indicatorId ) ;
if ( indicator = = null ) {
// EXCEPTION - Indicator not found
throw new EntityNotFoundException ( " Change indicator visibility: Indicator with id: " + indicatorId + " not found " ) ;
}
}
indicator . setVisibility ( visibility ) ;
indicatorDAO . save ( indicator ) ;
log . debug ( " Indicator toggled! " ) ;
return indicator ;
2020-01-10 11:48:24 +01:00
}
private Section checkForExceptions ( String stakeholderId , String topicId , String categoryId , String subcategoryId , String sectionId , String indicatorType ) {
Stakeholder < String > stakeholder = stakeholderDAO . findById ( stakeholderId ) ;
if ( stakeholder = = null ) {
2019-12-19 16:04:25 +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 " ) ;
}
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 Indicator: 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-19 16:04:25 +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 ) ;
}
Section < String > section = sectionDAO . findById ( sectionId ) ;
if ( section = = null ) {
// EXCEPTION - Section not found
throw new EntityNotFoundException ( " Save indicator: Section with id: " + sectionId + " not found " ) ;
}
if ( indicatorType . equals ( " chart " ) ) {
if ( ! subcategory . getCharts ( ) . contains ( sectionId ) ) {
// EXCEPTION - Section not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subcategory.getAlias();
throw new PathNotValidException ( " Save indicator: SubCategory with id: " + subcategoryId + " not found in Category: " + categoryId ) ;
}
} else if ( indicatorType . equals ( " number " ) ) {
if ( ! subcategory . getNumbers ( ) . contains ( sectionId ) ) {
// EXCEPTION - Section not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subcategory.getAlias();
throw new PathNotValidException ( " Save indicator: SubCategory with id: " + subcategoryId + " not found in Category: " + categoryId ) ;
}
}
return section ;
2019-12-19 16:04:25 +01:00
}
2020-06-22 21:58:40 +02:00
public void deleteTree ( Section section ) {
List < String > indicators = section . getIndicators ( ) ;
for ( String indicatorId : indicators ) {
indicatorDAO . delete ( indicatorId ) ;
}
}
public void disConnectTree ( Section section ) {
List < String > indicators = section . getIndicators ( ) ;
for ( String indicatorId : indicators ) {
Indicator indicator = indicatorDAO . findById ( indicatorId ) ;
indicator . setDefaultId ( null ) ;
indicatorDAO . save ( indicator ) ;
}
}
2019-11-22 16:50:59 +01:00
}