Index: lams_central/src/java/org/lamsfoundation/lams/web/NotificationAction.java =================================================================== diff -u -r94ed30ec071e295e4a2d469f02778c25f401ca41 -rbca83628ffa34dcc98176b117aa5ff26d166300f --- lams_central/src/java/org/lamsfoundation/lams/web/NotificationAction.java (.../NotificationAction.java) (revision 94ed30ec071e295e4a2d469f02778c25f401ca41) +++ lams_central/src/java/org/lamsfoundation/lams/web/NotificationAction.java (.../NotificationAction.java) (revision bca83628ffa34dcc98176b117aa5ff26d166300f) @@ -61,7 +61,7 @@ Integer limit = WebUtil.readIntParam(req, "limit", true); Integer offset = WebUtil.readIntParam(req, "offset", true); List subscriptions = getEventNotificationService().getNotificationSubscriptions(null, - getUser().getUserID(), limit, offset); + getUser().getUserID(), false, limit, offset); JSONArray responseJSON = new JSONArray(); Event event = null; for (Subscription subscription : subscriptions) { Index: lams_central/src/java/org/lamsfoundation/lams/webservice/xml/NotificationServlet.java =================================================================== diff -u --- lams_central/src/java/org/lamsfoundation/lams/webservice/xml/NotificationServlet.java (revision 0) +++ lams_central/src/java/org/lamsfoundation/lams/webservice/xml/NotificationServlet.java (revision bca83628ffa34dcc98176b117aa5ff26d166300f) @@ -0,0 +1,151 @@ +package org.lamsfoundation.lams.webservice.xml; + +import java.io.IOException; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import org.apache.commons.lang.StringEscapeUtils; +import org.apache.log4j.Logger; +import org.lamsfoundation.lams.events.DeliveryMethodNotification; +import org.lamsfoundation.lams.events.IEventNotificationService; +import org.lamsfoundation.lams.events.Subscription; +import org.lamsfoundation.lams.integration.ExtServerOrgMap; +import org.lamsfoundation.lams.integration.ExtUserUseridMap; +import org.lamsfoundation.lams.integration.security.Authenticator; +import org.lamsfoundation.lams.integration.service.IntegrationService; +import org.lamsfoundation.lams.util.CentralConstants; +import org.lamsfoundation.lams.util.WebUtil; +import org.springframework.web.context.support.WebApplicationContextUtils; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.ls.DOMImplementationLS; +import org.w3c.dom.ls.LSOutput; +import org.w3c.dom.ls.LSSerializer; + +/** + * Allows notifications access for integrated environments. + * + * @author Marcin Cieslak + * + */ +public class NotificationServlet extends HttpServlet { + private static final long serialVersionUID = 4856874776383254865L; + + private static Logger log = Logger.getLogger(NotificationServlet.class); + + private static IntegrationService integrationService = null; + private static IEventNotificationService eventNotificationService = null; + + private static DocumentBuilder docBuilder = null; + private static final Pattern anchorPattern = Pattern.compile("(.*)"); + + static { + try { + NotificationServlet.docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); + } catch (ParserConfigurationException e) { + NotificationServlet.log.error("Error while initialising XML document builder", e); + } + } + + /** + * This method is called when a form has its tag value method equals to get. + */ + @Override + public void doGet(HttpServletRequest request, HttpServletResponse response) { + String serverId = request.getParameter(CentralConstants.PARAM_SERVER_ID); + String datetime = request.getParameter(CentralConstants.PARAM_DATE_TIME); + String hashValue = request.getParameter(CentralConstants.PARAM_HASH_VALUE); + String username = request.getParameter(CentralConstants.PARAM_USERNAME); + + try { + ExtServerOrgMap serverMap = NotificationServlet.integrationService.getExtServerOrgMap(serverId); + Authenticator.authenticate(serverMap, datetime, username, hashValue); + ExtUserUseridMap userMap = NotificationServlet.integrationService.getExtUserUseridMap(serverMap, username); + String method = request.getParameter(CentralConstants.PARAM_METHOD); + if ("getNotifications".equalsIgnoreCase(method)) { + getNotifications(userMap.getUser().getUserId(), request, response); + } + } catch (Exception e) { + NotificationServlet.log.error("Error while getting notifications"); + } + } + + /** + * This method is called when a form has its tag value method equals to post. + */ + @Override + public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + doGet(request, response); + } + + /** + * Initialization of the servlet.
+ */ + @Override + public void init() throws ServletException { + NotificationServlet.integrationService = (IntegrationService) WebApplicationContextUtils + .getRequiredWebApplicationContext(getServletContext()).getBean("integrationService"); + + NotificationServlet.eventNotificationService = (IEventNotificationService) WebApplicationContextUtils + .getRequiredWebApplicationContext(getServletContext()).getBean("eventNotificationService"); + } + + private void getNotifications(Integer userId, HttpServletRequest request, HttpServletResponse response) + throws IOException { + Document doc = NotificationServlet.docBuilder.newDocument(); + Element notificationsElement = doc.createElement("Notifications"); + doc.appendChild(notificationsElement); + + Long lessonId = WebUtil.readLongParam(request, CentralConstants.PARAM_LESSON_ID, true); + Integer limit = WebUtil.readIntParam(request, "limit", true); + Integer offset = WebUtil.readIntParam(request, "offset", true); + Boolean pendingOnly = WebUtil.readBooleanParam(request, "pendingOnly", true); + + List subscriptions = NotificationServlet.eventNotificationService + .getNotificationSubscriptions(lessonId, userId, pendingOnly, limit, offset); + for (Subscription subscription : subscriptions) { + Element notificationElement = doc.createElement("Notification"); + + notificationElement.setAttribute("id", subscription.getUid().toString()); + + Boolean pending = !DeliveryMethodNotification.LAST_OPERATION_SEEN + .equals(subscription.getLastOperationMessage()); + notificationElement.setAttribute("pending", pending.toString()); + + Long notificationLessonId = subscription.getEvent().getEventSessionId(); + if (notificationLessonId != null) { + notificationElement.setAttribute("lessonId", notificationLessonId.toString()); + } + + String message = subscription.getEvent().getMessage(); + Matcher matcher = NotificationServlet.anchorPattern.matcher(message); + if (matcher.find()) { + String href = StringEscapeUtils.escapeXml(matcher.group(2)); + notificationElement.setAttribute("href", href); + message = matcher.group(3); + } + notificationElement.appendChild(doc.createCDATASection(message)); + + notificationsElement.appendChild(notificationElement); + } + + response.setContentType("text/xml"); + response.setCharacterEncoding("UTF-8"); + + DOMImplementationLS domImplementation = (DOMImplementationLS) doc.getImplementation(); + LSSerializer lsSerializer = domImplementation.createLSSerializer(); + LSOutput lsOutput = domImplementation.createLSOutput(); + lsOutput.setEncoding("UTF-8"); + lsOutput.setByteStream(response.getOutputStream()); + lsSerializer.write(doc, lsOutput); + } +} \ No newline at end of file Index: lams_central/web/WEB-INF/web.xml =================================================================== diff -u -raff99b7f037bdf5f01ff883893eb0d69a319cfeb -rbca83628ffa34dcc98176b117aa5ff26d166300f --- lams_central/web/WEB-INF/web.xml (.../web.xml) (revision aff99b7f037bdf5f01ff883893eb0d69a319cfeb) +++ lams_central/web/WEB-INF/web.xml (.../web.xml) (revision bca83628ffa34dcc98176b117aa5ff26d166300f) @@ -367,6 +367,13 @@ org.lamsfoundation.lams.web.RuntimeStatsServlet + + + NotificationServlet + + org.lamsfoundation.lams.webservice.xml.NotificationServlet + + LearningDesignRestServlet @@ -473,6 +480,10 @@ RuntimeStatsServlet /runtimeStats + + NotificationServlet + /services/xml/NotificationManager + csv @@ -610,6 +621,7 @@ /services/CourseGroupManager /services/getServerTime /services/Register/* + /services/xml/NotificationManager /ForgotPasswordRequest /forgotPassword.jsp /forgotPasswordChange.jsp Index: lams_common/src/java/org/lamsfoundation/lams/events/EventNotificationService.java =================================================================== diff -u -r94ed30ec071e295e4a2d469f02778c25f401ca41 -rbca83628ffa34dcc98176b117aa5ff26d166300f --- lams_common/src/java/org/lamsfoundation/lams/events/EventNotificationService.java (.../EventNotificationService.java) (revision 94ed30ec071e295e4a2d469f02778c25f401ca41) +++ lams_common/src/java/org/lamsfoundation/lams/events/EventNotificationService.java (.../EventNotificationService.java) (revision bca83628ffa34dcc98176b117aa5ff26d166300f) @@ -239,9 +239,9 @@ } @Override - public List getNotificationSubscriptions(Long lessonId, Integer userId, Integer limit, - Integer offset) { - return eventDAO.getLessonEventSubscriptions(lessonId, userId, limit, offset); + public List getNotificationSubscriptions(Long lessonId, Integer userId, boolean pendingOnly, + Integer limit, Integer offset) { + return eventDAO.getLessonEventSubscriptions(lessonId, userId, pendingOnly, limit, offset); } @Override Index: lams_common/src/java/org/lamsfoundation/lams/events/IEventNotificationService.java =================================================================== diff -u -r94ed30ec071e295e4a2d469f02778c25f401ca41 -rbca83628ffa34dcc98176b117aa5ff26d166300f --- lams_common/src/java/org/lamsfoundation/lams/events/IEventNotificationService.java (.../IEventNotificationService.java) (revision 94ed30ec071e295e4a2d469f02778c25f401ca41) +++ lams_common/src/java/org/lamsfoundation/lams/events/IEventNotificationService.java (.../IEventNotificationService.java) (revision bca83628ffa34dcc98176b117aa5ff26d166300f) @@ -93,7 +93,8 @@ */ Set getAvailableDeliveryMethods(); - List getNotificationSubscriptions(Long lessonId, Integer userId, Integer limit, Integer offset); + List getNotificationSubscriptions(Long lessonId, Integer userId, boolean pendingOnly, Integer limit, + Integer offset); long getNotificationPendingCount(Long lessonId, Integer userId); Index: lams_common/src/java/org/lamsfoundation/lams/events/dao/EventDAO.java =================================================================== diff -u -r94ed30ec071e295e4a2d469f02778c25f401ca41 -rbca83628ffa34dcc98176b117aa5ff26d166300f --- lams_common/src/java/org/lamsfoundation/lams/events/dao/EventDAO.java (.../EventDAO.java) (revision 94ed30ec071e295e4a2d469f02778c25f401ca41) +++ lams_common/src/java/org/lamsfoundation/lams/events/dao/EventDAO.java (.../EventDAO.java) (revision bca83628ffa34dcc98176b117aa5ff26d166300f) @@ -24,7 +24,8 @@ */ List getEventsToResend(); - List getLessonEventSubscriptions(Long lessonId, Integer userId, Integer limit, Integer offset); + List getLessonEventSubscriptions(Long lessonId, Integer userId, boolean pendingOnly, Integer limit, + Integer offset); long getPendingNotificationCount(Long lessonId, Integer userId); } \ No newline at end of file Index: lams_common/src/java/org/lamsfoundation/lams/events/dao/hibernate/EventDAOHibernate.java =================================================================== diff -u -r94ed30ec071e295e4a2d469f02778c25f401ca41 -rbca83628ffa34dcc98176b117aa5ff26d166300f --- lams_common/src/java/org/lamsfoundation/lams/events/dao/hibernate/EventDAOHibernate.java (.../EventDAOHibernate.java) (revision 94ed30ec071e295e4a2d469f02778c25f401ca41) +++ lams_common/src/java/org/lamsfoundation/lams/events/dao/hibernate/EventDAOHibernate.java (.../EventDAOHibernate.java) (revision bca83628ffa34dcc98176b117aa5ff26d166300f) @@ -49,13 +49,17 @@ @Override @SuppressWarnings("unchecked") - public List getLessonEventSubscriptions(Long lessonId, Integer userId, Integer limit, - Integer offset) { + public List getLessonEventSubscriptions(Long lessonId, Integer userId, boolean pendingOnly, + Integer limit, Integer offset) { String query = EventDAOHibernate.GET_LESSON_EVENT_SUBSCRIPTIONS; if (lessonId != null) { query += " AND s.event.eventSessionId = ?"; } - query += " ORDER BY ISNULL(s.lastOperationMessage) DESC, uid ASC"; + if (pendingOnly) { + query += " AND (s.lastOperationMessage IS NULL OR s.lastOperationMessage != '" + + DeliveryMethodNotification.LAST_OPERATION_SEEN + "')"; + } + query += " ORDER BY ISNULL(s.lastOperationMessage) DESC, uid DESC"; Query queryObject = getSession().createQuery(query); queryObject.setInteger(0, userId); if (lessonId != null) {