View Javadoc

1   /*
2      Licensed under the Apache License, Version 2.0 (the "License");
3      you may not use this file except in compliance with the License.
4      You may obtain a copy of the License at
5   
6        http://www.apache.org/licenses/LICENSE-2.0
7   
8      Unless required by applicable law or agreed to in writing, software
9      distributed under the License is distributed on an "AS IS" BASIS,
10     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11     See the License for the specific language governing permissions and
12     limitations under the License.
13  */
14  package uk.nhs.interoperability.client.samples.notification;
15  
16  import java.io.IOException;
17  import java.text.SimpleDateFormat;
18  import java.util.Date;
19  import java.util.HashMap;
20  
21  import javax.servlet.ServletException;
22  import javax.servlet.http.HttpServlet;
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpServletResponse;
25  import javax.servlet.http.HttpSession;
26  
27  import uk.nhs.interoperability.transform.TransformManager;
28  import uk.nhs.interoperability.util.Logger;
29  
30  /**
31   * The Class NotificationView.
32   *
33   * @author Adam Hatherly
34   * @since 0.1
35   */
36  public class NotificationView extends HttpServlet {
37  	
38  	/** The Constant serialVersionUID. */
39  	private static final long serialVersionUID = 1L;
40         
41      /**
42       * Instantiates a new notification view.
43       *
44       * @see HttpServlet#HttpServlet()
45       */
46      public NotificationView() {
47          super();
48          // TODO Auto-generated constructor stub
49      }
50  
51  	/**
52  	 * Do get.
53  	 *
54  	 * @param request the request
55  	 * @param response the response
56  	 * @throws ServletException the servlet exception
57  	 * @throws IOException Signals that an I/O exception has occurred.
58  	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
59  	 */
60  	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
61  		Logger.debug("this is doGet");
62  		String notificationId= request.getParameter("notificationid");
63  		HttpSession session = request.getSession(true);
64          session.removeAttribute("errorMessage");
65          session.removeAttribute("outcomeMessage");
66          session.removeAttribute("responseBody");
67  		
68  		if (null == notificationId) {
69  			session.setAttribute("outcomeMessage", "Notification List");
70  			session.setAttribute("responseBody", createList());
71  			
72  		} else {
73  			Logger.debug("Notification ID:"+notificationId);
74  			String htmlResponse = process(notificationId);
75  			Logger.trace(htmlResponse);
76  			if (htmlResponse.equals("NOTFOUND")){
77  				session.setAttribute("errorMessage", htmlResponse);
78  			} else {
79  				session.setAttribute("outcomeMessage", "Notification Found");
80  				session.setAttribute("responseBody", htmlResponse);
81  			}
82  		}
83  		
84  		response.sendRedirect("./NotificationView.jsp");
85  		Logger.trace("*** NotificationView: Ending");
86  	}
87  
88  	/**
89  	 * Do post.
90  	 *
91  	 * @param request the request
92  	 * @param response the response
93  	 * @throws ServletException the servlet exception
94  	 * @throws IOException Signals that an I/O exception has occurred.
95  	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
96  	 */
97  	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
98  		// TODO Auto-generated method stub
99  		Logger.debug("this is doPost");
100 		String docId= request.getParameter("docid");
101 		response.getWriter().write("DO POST. DOCID:"+docId);
102 	}
103 	
104 	
105 	/**
106 	 * Process.
107 	 *
108 	 * @param notificationId the notification id
109 	 * @return Content of notification, transformed to HTML
110 	 */
111 	protected String process(String notificationId) {
112 		String notification = NotificationStore.getNotification(notificationId);
113 		Logger.debug("Notification:"+notification);
114 
115 		if (null!=notification){
116 			String htmlResponse = "";
117 			try {
118 				htmlResponse = TransformManager.doTransform("Notification_Renderer.xsl", notification);
119 			} catch (Exception e) {
120 				Logger.error("Notification Transformation Failed.",e);
121 			}
122 
123 			Logger.trace(htmlResponse);
124 			return htmlResponse;
125 			
126 		} else {
127 			Logger.trace("Notification not found");
128 			return "NOTFOUND";
129 		}
130 	}
131 	
132 	/**
133 	 * Creates the list.
134 	 *
135 	 * @return the string
136 	 */
137 	protected String createList() {
138 		
139 		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
140 		
141 		String output = "";
142 		
143 		HashMap<String, Date> notifications = NotificationStore.getMessageList();
144 		if (!notifications.isEmpty()) {
145 			
146 			output = output + "<ul>";
147 			for (String key : notifications.keySet()) {
148 				String url = "./notificationview?notificationid=" + key;
149 				String date = formatter.format(notifications.get(key));
150 				output = output + "<li><a href=\"" + url + "\">ID: " + key + " - Received: " + date + "</a></li>";
151 			}
152 			output = output + "</ul>";
153 		} else {
154 			output = output + "<p>No Notifications Received</p>";
155 		}
156 		
157 		return output;
158 	}
159 
160 }