2020-03-31 14:02:29 +02:00
package eu.dnetlib.uoaadmintools.controllers ;
2020-09-24 14:02:39 +02:00
import eu.dnetlib.uoaadmintools.entities.Layout ;
import eu.dnetlib.uoaadmintools.services.LayoutService ;
2020-12-04 10:56:36 +01:00
import eu.dnetlib.uoaadmintools.services.NotificationsService ;
2020-09-24 14:02:39 +02:00
import eu.dnetlib.uoaadmintools.services.StatisticsService ;
import eu.dnetlib.uoaadmintools.services.SubscriberService ;
import eu.dnetlib.uoaadmintoolslibrary.entities.Portal ;
2021-02-25 20:00:05 +01:00
import eu.dnetlib.uoaadmintoolslibrary.entities.fullEntities.PortalResponse ;
2021-02-25 12:57:22 +01:00
import eu.dnetlib.uoaadmintoolslibrary.handlers.ContentNotFoundException ;
import eu.dnetlib.uoaadmintoolslibrary.handlers.MismatchingContentException ;
2021-02-26 11:14:16 +01:00
import eu.dnetlib.uoaadmintoolslibrary.handlers.utils.RolesUtils ;
2020-09-24 14:02:39 +02:00
import eu.dnetlib.uoaadmintoolslibrary.services.PortalService ;
2021-02-25 20:00:05 +01:00
import eu.dnetlib.uoaauthorizationlibrary.security.AuthorizationService ;
2020-03-31 14:02:29 +02:00
import org.apache.log4j.Logger ;
import org.springframework.beans.factory.annotation.Autowired ;
2021-02-25 12:57:22 +01:00
import org.springframework.security.access.prepost.PreAuthorize ;
2021-02-25 20:00:05 +01:00
import org.springframework.web.bind.annotation.* ;
import java.util.List ;
2020-03-31 14:02:29 +02:00
@RestController
2020-09-24 14:02:39 +02:00
@RequestMapping ( " /community " )
2020-03-31 14:02:29 +02:00
@CrossOrigin ( origins = " * " )
public class CommunityController {
private final Logger log = Logger . getLogger ( this . getClass ( ) ) ;
2021-02-25 12:57:22 +01:00
@Autowired
2021-02-26 11:14:16 +01:00
private RolesUtils rolesUtils ;
2021-02-25 12:57:22 +01:00
2020-03-31 14:02:29 +02:00
@Autowired
2020-09-24 14:02:39 +02:00
private LayoutService layoutService ;
2020-03-31 14:02:29 +02:00
2020-12-04 10:56:36 +01:00
@Autowired
private NotificationsService notificationsService ;
2020-03-31 14:02:29 +02:00
@Autowired
2020-09-24 14:02:39 +02:00
private StatisticsService statisticsService ;
2020-03-31 14:02:29 +02:00
@Autowired
2020-09-24 14:02:39 +02:00
private SubscriberService subscriberService ;
2020-03-31 14:02:29 +02:00
@Autowired
2020-09-24 14:02:39 +02:00
private PortalService portalService ;
2020-03-31 14:02:29 +02:00
2020-09-28 12:30:56 +02:00
@RequestMapping ( value = { " " } , method = RequestMethod . GET )
2020-09-24 14:02:39 +02:00
public List < Portal > getAllCommunities ( ) {
return portalService . getAllPortalsByType ( " community " ) ;
2020-03-31 14:02:29 +02:00
}
2020-09-24 14:02:39 +02:00
@RequestMapping ( value = { " /full " } , method = RequestMethod . GET )
public List < PortalResponse > getAllCommunitiesFull ( ) {
return portalService . getAllPortalsFullByType ( " community " ) ;
2020-03-31 14:02:29 +02:00
}
2021-02-25 12:57:22 +01:00
@PreAuthorize ( " hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN) " )
2020-09-24 14:02:39 +02:00
@RequestMapping ( value = " /update " , method = RequestMethod . POST )
public PortalResponse updateCommunity ( @RequestBody Portal portal ) {
2021-02-25 12:57:22 +01:00
if ( ! portal . getType ( ) . equals ( " community " ) ) {
// EXCEPTION - MismatchingContent
throw new MismatchingContentException ( " Update Community: Portal with id: " + portal . getId ( ) + " has type: " + portal . getType ( ) + " instead of community " ) ;
}
2020-12-04 10:56:36 +01:00
String old_pid = portalService . getPortalById ( portal . getId ( ) ) . getPid ( ) ;
String new_pid = portal . getPid ( ) ;
2020-09-24 14:02:39 +02:00
PortalResponse portalResponse = portalService . updatePortal ( portal ) ;
2020-03-31 14:02:29 +02:00
2020-09-24 14:02:39 +02:00
if ( ! old_pid . equals ( new_pid ) ) {
2020-12-04 10:56:36 +01:00
log . debug ( " update portal pid - old: " + old_pid + " - new: " + new_pid ) ;
2020-09-24 14:02:39 +02:00
statisticsService . updatePid ( old_pid , new_pid ) ;
subscriberService . updatePid ( old_pid , new_pid ) ;
layoutService . updatePid ( old_pid , new_pid ) ;
2020-12-04 10:56:36 +01:00
notificationsService . updatePid ( old_pid , new_pid ) ;
2020-03-31 14:02:29 +02:00
}
2020-09-24 14:02:39 +02:00
return portalResponse ;
2020-03-31 14:02:29 +02:00
}
2021-02-25 12:57:22 +01:00
@PreAuthorize ( " hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN) " )
2020-09-24 14:02:39 +02:00
@RequestMapping ( value = " /save " , method = RequestMethod . POST )
public PortalResponse insertCommunity ( @RequestBody Portal portal ) {
2021-02-25 12:57:22 +01:00
if ( ! portal . getType ( ) . equals ( " community " ) ) {
// EXCEPTION - MismatchingContent
throw new MismatchingContentException ( " Save Community: Portal with id: " + portal . getId ( ) + " has type: " + portal . getType ( ) + " instead of community " ) ;
}
2020-09-24 14:02:39 +02:00
PortalResponse portalResponse = portalService . insertPortal ( portal ) ;
2020-03-31 14:02:29 +02:00
2020-09-24 14:02:39 +02:00
statisticsService . createPortalStatistics ( portal . getPid ( ) ) ;
subscriberService . createPortalSubscribers ( portal . getPid ( ) ) ;
2020-03-31 14:02:29 +02:00
2020-09-24 14:02:39 +02:00
return portalResponse ;
2020-03-31 14:02:29 +02:00
}
2021-02-25 12:57:22 +01:00
@PreAuthorize ( " hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN) " )
2020-09-24 14:02:39 +02:00
@RequestMapping ( value = " /delete " , method = RequestMethod . POST )
public Boolean deleteCommunities ( @RequestBody List < String > portals ) {
2021-02-26 11:14:16 +01:00
List < String > roles = rolesUtils . getRoles ( ) ;
2021-02-25 12:57:22 +01:00
2020-09-24 14:02:39 +02:00
for ( String id : portals ) {
2021-02-25 12:57:22 +01:00
Portal portal = portalService . getPortalById ( id ) ;
if ( portal = = null ) {
// EXCEPTION - Entity Not Found
throw new ContentNotFoundException ( " Delete community: Portal with id: " + id + " not found " ) ;
}
if ( ! portal . getType ( ) . equals ( " community " ) ) {
// EXCEPTION - MismatchingContent
throw new MismatchingContentException ( " Delete Community: Portal with id: " + id + " has type: " + portal . getType ( ) + " instead of community " ) ;
}
2020-09-24 14:02:39 +02:00
String pid = portalService . deletePortal ( id ) ;
2020-03-31 14:02:29 +02:00
2020-09-24 14:02:39 +02:00
statisticsService . deleteByPid ( pid ) ;
subscriberService . deletePortalSubscribers ( pid ) ;
layoutService . deleteByPid ( pid ) ;
2020-12-04 10:56:36 +01:00
notificationsService . deleteByPid ( pid ) ;
2020-03-31 14:02:29 +02:00
}
return true ;
}
2020-09-24 14:02:39 +02:00
@RequestMapping ( value = " /{pid}/layout " , method = RequestMethod . GET )
2020-03-31 14:02:29 +02:00
public Layout getLayoutForCommunity ( @PathVariable ( value = " pid " ) String pid ) {
2021-02-25 12:57:22 +01:00
Portal portal = portalService . getPortal ( pid ) ;
if ( portal = = null ) {
// EXCEPTION - Entity Not Found
throw new ContentNotFoundException ( " CommunityController - Get layout: Portal with pid: " + pid + " not found " ) ;
}
if ( ! portal . getType ( ) . equals ( " community " ) ) {
// EXCEPTION - MismatchingContent
throw new MismatchingContentException ( " CommunityController - Get layout: Portal with pid: " + pid + " has type: " + portal . getType ( ) + " instead of community " ) ;
}
2020-09-24 14:02:39 +02:00
return layoutService . findByPid ( pid ) ;
2020-03-31 14:02:29 +02:00
}
2021-02-25 12:57:22 +01:00
@PreAuthorize ( " hasAnyAuthority( " +
" @AuthorizationService.PORTAL_ADMIN, " +
" @AuthorizationService.curator('community'), @AuthorizationService.manager('community', #pid)) " )
2020-09-24 14:02:39 +02:00
@RequestMapping ( value = " /{pid}/layout " , method = RequestMethod . POST )
2020-03-31 14:02:29 +02:00
public Layout updateLayoutForCommunity ( @PathVariable ( value = " pid " ) String pid , @RequestBody Layout layout ) {
2021-02-25 12:57:22 +01:00
Portal portal = portalService . getPortal ( pid ) ;
if ( portal = = null ) {
// EXCEPTION - Entity Not Found
throw new ContentNotFoundException ( " CommunityController - Update layout: Portal with pid: " + pid + " not found " ) ;
}
if ( ! portal . getType ( ) . equals ( " community " ) ) {
// EXCEPTION - MismatchingContent
throw new MismatchingContentException ( " CommunityController - Update layout: Portal with pid: " + pid + " has type: " + portal . getType ( ) + " instead of community " ) ;
}
if ( ! pid . equals ( layout . getPortalPid ( ) ) ) {
// EXCEPTION - MismatchingContent
throw new MismatchingContentException ( " CommunityController - Update layout: Portal has pid: " + pid + " while layout has portalPid: " + layout . getPortalPid ( ) ) ;
}
2020-09-24 14:02:39 +02:00
return layoutService . save ( layout ) ;
2020-03-31 14:02:29 +02:00
}
}