-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6f3df0b
commit 9e763fd
Showing
15 changed files
with
635 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package amu.zhcet.announcement; | ||
|
||
import amu.zhcet.common.model.BaseIdEntity; | ||
import amu.zhcet.data.user.User; | ||
import amu.zhcet.email.LinkMessage; | ||
import lombok.*; | ||
import org.hibernate.envers.Audited; | ||
import javax.persistence.*; | ||
import javax.validation.constraints.NotBlank; | ||
import javax.validation.constraints.NotNull; | ||
import javax.validation.constraints.Size; | ||
import java.time.LocalDateTime; | ||
import java.util.function.Function; | ||
|
||
@Data | ||
@Entity | ||
@Audited | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@EqualsAndHashCode(callSuper = false) | ||
public class Announcement extends BaseIdEntity { | ||
|
||
@OneToOne(fetch = FetchType.LAZY) | ||
private User sender; | ||
@Size(max = 150) | ||
private String title; | ||
@NotBlank | ||
@Size(max = 500) | ||
private String message; | ||
@NotNull | ||
private boolean scheduled; | ||
private LocalDateTime sentTime = LocalDateTime.now(); | ||
private boolean automated; | ||
|
||
private transient boolean stopEmailPropagation; | ||
private transient boolean stopFirebasePropagation; | ||
private transient String icon = "https://zhcet-backend.firebaseapp.com/static/img/icon.png"; | ||
private transient Function<Announcement, LinkMessage> linkMessageConverter; | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/amu/zhcet/announcement/AnnouncementNotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package amu.zhcet.announcement; | ||
|
||
import amu.zhcet.data.ItemNotFoundException; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
public class AnnouncementNotFoundException extends ItemNotFoundException { | ||
public AnnouncementNotFoundException() { | ||
super("Announcement"); | ||
} | ||
|
||
public AnnouncementNotFoundException(@Nullable String id) { | ||
super("Announcement", id); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/amu/zhcet/announcement/AnnouncementRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package amu.zhcet.announcement; | ||
|
||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.repository.PagingAndSortingRepository; | ||
|
||
public interface AnnouncementRepository extends PagingAndSortingRepository<Announcement, Long> { | ||
|
||
Page<Announcement> findBySenderUserId(String userId, Pageable Page); | ||
|
||
Page<Announcement> findAll(String userId, Pageable pageable); | ||
|
||
} |
63 changes: 63 additions & 0 deletions
63
src/main/java/amu/zhcet/announcement/management/AnnouncementEditController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package amu.zhcet.announcement.management; | ||
|
||
import amu.zhcet.announcement.Announcement; | ||
import amu.zhcet.common.utils.NotificationUtils; | ||
import amu.zhcet.core.error.ErrorUtils; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.ui.Model; | ||
import org.springframework.validation.BindingResult; | ||
import org.springframework.web.bind.annotation.*; | ||
import org.springframework.web.servlet.mvc.support.RedirectAttributes; | ||
|
||
import javax.validation.Valid; | ||
|
||
@Slf4j | ||
@Controller | ||
@RequestMapping("/management/announcements/{announcement}/edit") | ||
public class AnnouncementEditController { | ||
|
||
private final AnnouncementManagementService announcementManagementService; | ||
|
||
@Autowired | ||
public AnnouncementEditController (AnnouncementManagementService announcementManagementService) { | ||
this.announcementManagementService = announcementManagementService; | ||
} | ||
|
||
@GetMapping | ||
public String editAnnouncement(@PathVariable Announcement announcement, Model model) { | ||
ErrorUtils.requireNonNullAnnouncement(announcement); | ||
|
||
model.addAttribute("page_title", "Edit Announcement"); | ||
model.addAttribute("page_subtitle", "Announcement Manager"); | ||
model.addAttribute("page_description", "Edit sent Announcement"); | ||
|
||
if (!model.containsAttribute("announcement")) | ||
model.addAttribute("announcement", announcement); | ||
return "management/edit_announcement"; | ||
} | ||
|
||
@PostMapping | ||
public String saveEditAnnouncement(@RequestParam(required = false) Integer page, @PathVariable Announcement announcement, | ||
@Valid Announcement edited, BindingResult result, | ||
RedirectAttributes redirectAttributes) | ||
{ | ||
ErrorUtils.requireNonNullAnnouncement(announcement); | ||
int currentPage = NotificationUtils.normalizePage(page); | ||
|
||
String redirectUrl = String.format("redirect:/management/announcements/%d/edit?page=%d", announcement.getId(), currentPage); | ||
|
||
if (result.hasErrors()) { | ||
redirectAttributes.addFlashAttribute("announcement", edited); | ||
redirectAttributes.addFlashAttribute("org.springframework.validation.BindingResult.announcement", result); | ||
return redirectUrl; | ||
} | ||
|
||
announcement.setTitle(edited.getTitle()); | ||
announcement.setMessage(edited.getMessage()); | ||
announcementManagementService.saveAnnouncement(announcement); | ||
redirectAttributes.addFlashAttribute("announcement_success", "Announcement Edited"); | ||
return "redirect:/management/announcements?page=" + currentPage; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/main/java/amu/zhcet/announcement/management/AnnouncementManagementController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package amu.zhcet.announcement.management; | ||
|
||
import amu.zhcet.announcement.Announcement; | ||
import amu.zhcet.common.utils.NotificationUtils; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.ui.Model; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
|
||
import java.util.List; | ||
|
||
@Slf4j | ||
@Controller | ||
@RequestMapping("/management/announcements") | ||
public class AnnouncementManagementController { | ||
|
||
private final AnnouncementManagementService announcementManagementService; | ||
|
||
@Autowired | ||
public AnnouncementManagementController(AnnouncementManagementService announcementManagementService) { | ||
this.announcementManagementService = announcementManagementService; | ||
} | ||
|
||
@GetMapping | ||
public String manageAnnouncements(@RequestParam(required = false) Integer page, Model model) { | ||
model.addAttribute("page_title", "Manage Announcements"); | ||
model.addAttribute("page_subtitle", "Announcement Manager"); | ||
model.addAttribute("page_description", "Manage and monitor sent announcements"); | ||
|
||
int currentPage = NotificationUtils.normalizePage(page); | ||
Page<Announcement> announcementPage = announcementManagementService.getAnnouncements(currentPage); | ||
|
||
NotificationUtils.prepareNotifications(model, announcementPage, currentPage); | ||
List<Announcement> announcements = announcementPage.getContent(); | ||
model.addAttribute("announcements", announcements); | ||
|
||
return "management/manage_announcements"; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/amu/zhcet/announcement/management/AnnouncementManagementService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package amu.zhcet.announcement.management; | ||
|
||
import amu.zhcet.announcement.Announcement; | ||
import amu.zhcet.announcement.AnnouncementRepository; | ||
import amu.zhcet.auth.Auditor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.PageRequest; | ||
import org.springframework.data.domain.Sort; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Slf4j | ||
@Service | ||
public class AnnouncementManagementService { | ||
|
||
private static final int PAGE_SIZE = 10; | ||
|
||
private final AnnouncementRepository announcementRepository; | ||
|
||
@Autowired | ||
public AnnouncementManagementService(AnnouncementRepository announcementRepository) { | ||
this.announcementRepository = announcementRepository; | ||
} | ||
|
||
public Page<Announcement> getAnnouncements(int page) { | ||
PageRequest pageRequest = PageRequest.of(page - 1, PAGE_SIZE, Sort.Direction.DESC, "sentTime"); | ||
return announcementRepository.findBySenderUserId(Auditor.getLoggedInUsername(), pageRequest); | ||
} | ||
|
||
public void deleteAnnouncement(Announcement announcement) { announcementRepository.delete(announcement);} | ||
|
||
public void saveAnnouncement(Announcement announcement) { announcementRepository.save(announcement);} | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/amu/zhcet/announcement/reading/AnnouncementController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package amu.zhcet.announcement.reading; | ||
|
||
import amu.zhcet.announcement.Announcement; | ||
import amu.zhcet.common.utils.NotificationUtils; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.ui.Model; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
|
||
@Slf4j | ||
@Controller | ||
@RequestMapping("/announcements") | ||
public class AnnouncementController { | ||
|
||
private final AnnouncementReadingService announcementReadingService; | ||
|
||
@Autowired | ||
public AnnouncementController(AnnouncementReadingService announcementReadingService) { | ||
this.announcementReadingService = announcementReadingService; | ||
} | ||
|
||
private static void prepareModel(Model model) { | ||
model.addAttribute("page_title", "Announcements"); | ||
model.addAttribute("page_subtitle", "Announcement Manager"); | ||
model.addAttribute("page_description", "View and manage announcements"); | ||
} | ||
|
||
@GetMapping | ||
public String getAnnouncements(@RequestParam(required = false) Integer page, Model model) { | ||
int currentPage = NotificationUtils.normalizePage(page); | ||
Page<Announcement> announcementPage = announcementReadingService.getAnnouncements(currentPage); | ||
|
||
prepareModel(model); | ||
NotificationUtils.prepareNotifications(model, announcementPage, currentPage); | ||
model.addAttribute("announcements", announcementPage.getContent()); | ||
model.addAttribute("favorite_page", false); | ||
|
||
return "user/announcements"; | ||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/amu/zhcet/announcement/reading/AnnouncementReadingService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package amu.zhcet.announcement.reading; | ||
|
||
import amu.zhcet.announcement.Announcement; | ||
import amu.zhcet.announcement.AnnouncementRepository; | ||
import amu.zhcet.auth.Auditor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Scope; | ||
import org.springframework.context.annotation.ScopedProxyMode; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.PageRequest; | ||
import org.springframework.data.domain.Sort; | ||
import org.springframework.stereotype.Service; | ||
|
||
import javax.transaction.Transactional; | ||
|
||
@Slf4j | ||
@Service | ||
@Transactional | ||
@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS) | ||
public class AnnouncementReadingService { | ||
|
||
public static final int PAGE_SIZE = 10; | ||
|
||
private final AnnouncementRepository announcementRepository; | ||
|
||
@Autowired | ||
public AnnouncementReadingService(AnnouncementRepository announcementRepository) { | ||
this.announcementRepository = announcementRepository; | ||
} | ||
|
||
public Page<Announcement> getAnnouncements(int page) { | ||
String userId = Auditor.getLoggedInUsername(); | ||
PageRequest pageRequest = PageRequest.of(page - 1, PAGE_SIZE, Sort.Direction.DESC, "announcement.sentTime"); | ||
return announcementRepository.findAll(userId, pageRequest); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/main/resources/db/migration/V7.0.0__add_announcement_schema.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
CREATE TABLE announcement | ||
( | ||
id BIGINT(20) PRIMARY KEY AUTO_INCREMENT, | ||
sender_user_id VARCHAR(255) NOT NULL, | ||
title VARCHAR(150), | ||
message VARCHAR(500) NOT NULL, | ||
scheduled BIT(1) DEFAULT 0, | ||
sent_time DATETIME DEFAULT NOW(), | ||
automated BIT(1) DEFAULT FALSE, | ||
created_at DATETIME, | ||
updated_at DATETIME, | ||
created_by VARCHAR(255), | ||
modified_by VARCHAR(255), | ||
version int(11) DEFAULT 0, | ||
CONSTRAINT announcement__sender_fk FOREIGN KEY (sender_user_id) REFERENCES user(user_id) ON DELETE CASCADE ON UPDATE CASCADE | ||
); | ||
|
||
CREATE TABLE announcement_aud | ||
( | ||
id BIGINT(20) NOT NULL, | ||
rev integer not null, | ||
revtype tinyint, | ||
sender_user_id VARCHAR(255), | ||
title VARCHAR(150), | ||
message VARCHAR(500), | ||
scheduled BIT(1), | ||
sent_time DATETIME, | ||
automated BIT(1) DEFAULT FALSE, | ||
PRIMARY KEY (id, rev) | ||
); |
Oops, something went wrong.