Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@
<jdbc.groupId>org.hsqldb</jdbc.groupId>
<jdbc.artifactId>hsqldb</jdbc.artifactId>
<jdbc.version>2.2.9</jdbc.version>

<!-- Utility libraries -->
<jackson.version>2.9.5</jackson.version>
<lombok.version>1.16.20</lombok.version>
</properties>

<url>http://www.ja-sig.org/wiki/display/PLT/Announcements+Portlet</url>
Expand Down Expand Up @@ -255,6 +259,23 @@
</dependency>
-->

<!-- Utility libraries -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like to see lombok being used.
Is it being used?

<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>

<dependency>
<groupId>com.rometools</groupId>
<artifactId>rome</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlType;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.jasig.portlet.announcements.xml.Namespaces;

/** @author Erik A. Olsson ([email protected]) */
Expand Down Expand Up @@ -95,6 +97,7 @@ public void setId(Long id) {
}

/** @return the parent */
@JsonIgnore
@XmlElement(name = "parent")
public Topic getParent() {
return parent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,25 @@
*/
package org.jasig.portlet.announcements.mvc.portlet.display;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Set;

import javax.portlet.EventRequest;
import javax.portlet.EventResponse;
import javax.portlet.PortletException;
import javax.portlet.PortletPreferences;
import javax.portlet.PortletRequest;
import javax.portlet.RenderRequest;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;
import javax.xml.namespace.QName;
import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.log4j.Logger;
import org.jasig.portlet.announcements.UnauthorizedException;
import org.jasig.portlet.announcements.model.Announcement;
Expand All @@ -51,8 +55,6 @@
import org.jasig.portlet.notice.NotificationQuery;
import org.jasig.portlet.notice.NotificationResponse;
import org.jasig.portlet.notice.NotificationResult;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.stereotype.Controller;
Expand All @@ -62,6 +64,7 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.portlet.bind.annotation.EventMapping;
import org.springframework.web.portlet.bind.annotation.RenderMapping;
import org.springframework.web.portlet.bind.annotation.ResourceMapping;

/** @author eolsson */
@Controller
Expand Down Expand Up @@ -114,6 +117,8 @@ public class AnnouncementsViewController {

@Autowired private UserIdService userIdService;

private final ObjectMapper mapper = new ObjectMapper();

private final Logger logger = Logger.getLogger(getClass());

/**
Expand Down Expand Up @@ -142,27 +147,10 @@ public String mainView(

final PortletPreferences prefs = request.getPreferences();

List<Announcement> announcements;
List<Announcement> emergencyAnnouncements;

final String userId = userIdService.getUserId(request);

// create a new announcement list
announcements = new ArrayList<>();
emergencyAnnouncements = new ArrayList<>();
List[] lists = getLists(request);

// fetch the user's topic subscription from the database
List<TopicSubscription> myTopics = tss.getTopicSubscription(request);

// add all the published announcements of each subscribed topic to the announcement list
// to emergency announcements into their own list
for (TopicSubscription ts : myTopics) {
if (ts.getSubscribed() && ts.getTopic().getSubscriptionMethod() != Topic.EMERGENCY) {
announcements.addAll(ts.getTopic().getPublishedAnnouncements());
} else if (ts.getSubscribed() && ts.getTopic().getSubscriptionMethod() == Topic.EMERGENCY) {
emergencyAnnouncements.addAll(ts.getTopic().getPublishedAnnouncements());
}
}
List<Announcement> announcements = lists[0];
List<Announcement> emergencyAnnouncements = lists[1];

// sort the list (since they are not sorted from the database)
Comparator<Announcement> sortStrategy =
Expand Down Expand Up @@ -190,6 +178,46 @@ public String mainView(
return viewNameSelector.select(request, "displayAnnouncements");
}

private List[] getLists(PortletRequest request) throws PortletException {
final String userId = userIdService.getUserId(request);

// create a new announcement list
List<Announcement> announcements1 = new ArrayList<>();
List<Announcement> emergencyAnnouncements1 = new ArrayList<>();

// fetch the user's topic subscription from the database
List<TopicSubscription> myTopics = tss.getTopicSubscription(request);

// add all the published announcements of each subscribed topic to the announcement list
// to emergency announcements into their own list
for (TopicSubscription ts : myTopics) {
if (ts.getSubscribed() && ts.getTopic().getSubscriptionMethod() != Topic.EMERGENCY) {
announcements1.addAll(ts.getTopic().getPublishedAnnouncements());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I thought that the stream() version would be longer and harder to understand; however, after digging in and refining, I was able to remove other lines to make the stream() version smaller. Thanks for the push. This was also the first time I used flatMap() in Java.

} else if (ts.getSubscribed() && ts.getTopic().getSubscriptionMethod() == Topic.EMERGENCY) {
emergencyAnnouncements1.addAll(ts.getTopic().getPublishedAnnouncements());
}
}
return new List[]{announcements1, emergencyAnnouncements1};
}

@ResourceMapping(value = "emergencies")
public void emergenciesResource(ResourceRequest request, ResourceResponse response)
throws IOException, PortletException {
logger.debug("Processing AJAX resource request for emergency alerts");
List[] lists = getLists(request);
final String json = mapper.writeValueAsString(lists[1]);
response.getWriter().write(json);
}

@ResourceMapping(value = "announcements")
public void announcementsResource(ResourceRequest request, ResourceResponse response)
throws IOException, PortletException {
logger.debug("Processing AJAX resource request for announcements");
List[] lists = getLists(request);
final String json = mapper.writeValueAsString(lists[0]);
response.getWriter().write(json);
}

@RenderMapping(params = "action=" + ACTION_DISPLAY_FULL_ANNOUNCEMENT)
public String displayFullAnnouncement(
Model model, RenderRequest request, @RequestParam("announcementId") String announcementId)
Expand Down