Search in sources :

Example 1 with Announcement

use of ca.corefacility.bioinformatics.irida.model.announcements.Announcement in project irida by phac-nml.

the class AnnouncementsController method submitCreateAnnouncement.

/**
 * Create a new announcement
 *
 * @param message
 *              The content of the message for the announcement
 * @param model
 *              The model for the view
 * @param principal
 *              The currently logged in user creating the announcement
 * @return A redirect to the announcement control centre page
 */
@RequestMapping(value = "/create", method = RequestMethod.POST)
@PreAuthorize("hasRole('ROLE_ADMIN')")
public String submitCreateAnnouncement(@RequestParam(required = true) String message, Model model, Principal principal) {
    User creator = userService.getUserByUsername(principal.getName());
    Announcement a = new Announcement(message, creator);
    try {
        announcementService.create(a);
    } catch (Exception e) {
        model.addAttribute("errors", "Announcement was not created successfully");
    }
    return "redirect:/announcements/admin";
}
Also used : DTAnnouncementUser(ca.corefacility.bioinformatics.irida.ria.web.models.datatables.DTAnnouncementUser) User(ca.corefacility.bioinformatics.irida.model.user.User) Announcement(ca.corefacility.bioinformatics.irida.model.announcements.Announcement) IOException(java.io.IOException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with Announcement

use of ca.corefacility.bioinformatics.irida.model.announcements.Announcement in project irida by phac-nml.

the class AnnouncementsController method getUnreadAnnouncementsForUser.

/**
 * Gets a list of Announcements that the current user hasn't read yet
 *
 * @param model
 *              Model for the view
 * @param principal
 *              The current user
 * @return the fragment for viewing announcements in the dashboard
 */
@RequestMapping(value = "/user/unread")
public String getUnreadAnnouncementsForUser(final Model model, Principal principal) {
    User user = userService.getUserByUsername(principal.getName());
    List<Announcement> unreadAnnouncements = announcementService.getUnreadAnnouncementsForUser(user);
    Collections.sort(unreadAnnouncements, Collections.reverseOrder());
    model.addAttribute("announcements", unreadAnnouncements);
    return ANNOUNCEMENT_VIEW;
}
Also used : DTAnnouncementUser(ca.corefacility.bioinformatics.irida.ria.web.models.datatables.DTAnnouncementUser) User(ca.corefacility.bioinformatics.irida.model.user.User) Announcement(ca.corefacility.bioinformatics.irida.model.announcements.Announcement) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with Announcement

use of ca.corefacility.bioinformatics.irida.model.announcements.Announcement in project irida by phac-nml.

the class AnnouncementsController method getAnnouncementsAdmin.

/**
 * Get all announcements to be displayed in a DataTables for admin control centre
 *
 * @param params
 * 		{@link DataTablesParams} for the current DataTable.
 *
 * @return {@link DataTablesResponse}
 */
@RequestMapping(value = "/control/ajax/list")
@PreAuthorize("hasRole('ROLE_ADMIN')")
@ResponseBody
public DataTablesResponse getAnnouncementsAdmin(@DataTablesRequest DataTablesParams params) {
    final Page<Announcement> page = announcementService.search(AnnouncementSpecification.searchAnnouncement(params.getSearchValue()), new PageRequest(params.getCurrentPage(), params.getLength(), params.getSort()));
    final List<DataTablesResponseModel> announcements = page.getContent().stream().map(DTAnnouncementAdmin::new).collect(Collectors.toList());
    return new DataTablesResponse(params, page, announcements);
}
Also used : PageRequest(org.springframework.data.domain.PageRequest) Announcement(ca.corefacility.bioinformatics.irida.model.announcements.Announcement) DataTablesResponseModel(ca.corefacility.bioinformatics.irida.ria.web.components.datatables.models.DataTablesResponseModel) DataTablesResponse(ca.corefacility.bioinformatics.irida.ria.web.components.datatables.DataTablesResponse) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 4 with Announcement

use of ca.corefacility.bioinformatics.irida.model.announcements.Announcement in project irida by phac-nml.

the class AnnouncementsControllerTest method testSubmitUpdatedAnnouncement.

@Test
public void testSubmitUpdatedAnnouncement() {
    long id = 1L;
    String message = "Updated message and announcement.";
    ExtendedModelMap model = new ExtendedModelMap();
    Announcement a = new Announcement(message, user);
    when(announcementService.read(any(Long.class))).thenReturn(a);
    when(announcementService.update(any(Announcement.class))).thenReturn(a);
    String page = announcementsController.submitUpdatedAnnouncement(id, message, model);
    assertTrue("Unexpected redirect to page", page.equals("redirect:/announcements/admin"));
    verify(announcementService, times(1)).update(any(Announcement.class));
    verify(announcementService, times(1)).read(any(Long.class));
}
Also used : ExtendedModelMap(org.springframework.ui.ExtendedModelMap) Announcement(ca.corefacility.bioinformatics.irida.model.announcements.Announcement) Test(org.junit.Test)

Example 5 with Announcement

use of ca.corefacility.bioinformatics.irida.model.announcements.Announcement in project irida by phac-nml.

the class AnnouncementsControllerTest method testGetAnnouncementDetailsPage.

@Test
public void testGetAnnouncementDetailsPage() {
    long id = 1L;
    ExtendedModelMap model = new ExtendedModelMap();
    String message = "A new announcement";
    String page = null;
    Announcement a = new Announcement(message, user);
    when(announcementService.read(id)).thenReturn(a);
    when(announcementService.countReadsForOneAnnouncement(any(Announcement.class))).thenReturn(0L);
    when(userService.count()).thenReturn(1L);
    page = announcementsController.getAnnouncementDetailsPage(id, model);
    assertTrue("Unexpected redirect to a page", page.equals(ANNOUNCEMENT_DETAILS));
    assertEquals("Unexpected number of users", 1L, (model.get("numTotal")));
    assertEquals("Unexpected number of reads", 0L, (model.get("numReads")));
    assertTrue("Unexpected announcement", message.equals(((Announcement) model.get("announcement")).getMessage()));
    verify(announcementService, times(1)).read(id);
    verify(announcementService).countReadsForOneAnnouncement(a);
    verify(userService).count();
}
Also used : ExtendedModelMap(org.springframework.ui.ExtendedModelMap) Announcement(ca.corefacility.bioinformatics.irida.model.announcements.Announcement) Test(org.junit.Test)

Aggregations

Announcement (ca.corefacility.bioinformatics.irida.model.announcements.Announcement)29 Test (org.junit.Test)20 WithMockUser (org.springframework.security.test.context.support.WithMockUser)17 User (ca.corefacility.bioinformatics.irida.model.user.User)15 Authentication (org.springframework.security.core.Authentication)9 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)7 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)6 PageRequest (org.springframework.data.domain.PageRequest)5 DTAnnouncementUser (ca.corefacility.bioinformatics.irida.ria.web.models.datatables.DTAnnouncementUser)4 IOException (java.io.IOException)3 Sort (org.springframework.data.domain.Sort)3 ExtendedModelMap (org.springframework.ui.ExtendedModelMap)3 EntityExistsException (ca.corefacility.bioinformatics.irida.exceptions.EntityExistsException)2 AnnouncementUserJoin (ca.corefacility.bioinformatics.irida.model.announcements.AnnouncementUserJoin)2 DataTablesResponse (ca.corefacility.bioinformatics.irida.ria.web.components.datatables.DataTablesResponse)2 DataTablesResponseModel (ca.corefacility.bioinformatics.irida.ria.web.components.datatables.models.DataTablesResponseModel)2 AnnouncementService (ca.corefacility.bioinformatics.irida.service.AnnouncementService)2 UserService (ca.corefacility.bioinformatics.irida.service.user.UserService)2 Principal (java.security.Principal)2 AccessDeniedException (org.springframework.security.access.AccessDeniedException)2