From b710e4730baff3e87552127071a6c3c90ef028d8 Mon Sep 17 00:00:00 2001
From: ahmadaldhalaan <46313624+ahmadaldhalaan@users.noreply.github.com>
Date: Sat, 28 Nov 2020 18:29:34 +1100
Subject: [PATCH] added filter code to message inbox

---
 .../WebRoot/auth/hcp-patient/messageInbox.jsp | 248 +++++++++++++++++-
 1 file changed, 241 insertions(+), 7 deletions(-)

diff --git a/iTrust/WebRoot/auth/hcp-patient/messageInbox.jsp b/iTrust/WebRoot/auth/hcp-patient/messageInbox.jsp
index c38fc2f..e0840ee 100644
--- a/iTrust/WebRoot/auth/hcp-patient/messageInbox.jsp
+++ b/iTrust/WebRoot/auth/hcp-patient/messageInbox.jsp
@@ -3,25 +3,259 @@
 <%@page import="java.util.List"%>
 
 <%@page import="edu.ncsu.csc.itrust.action.ViewMyMessagesAction"%>
+<%@page import="edu.ncsu.csc.itrust.action.EditPersonnelAction"%>
 <%@page import="edu.ncsu.csc.itrust.beans.MessageBean"%>
 <%@page import="edu.ncsu.csc.itrust.dao.DAOFactory"%>
-<%@page import="edu.ncsu.csc.itrust.dao.mysql.PatientDAO"%>
+<%@page import="edu.ncsu.csc.itrust.dao.mysql.PersonnelDAO"%>
+<%@page import="java.util.ArrayList"%>
+<%@page import="java.text.DateFormat"%>
+<%@page import="java.text.SimpleDateFormat"%>
+<%@page import="java.util.Date"%>
+<%@page import="java.util.Calendar"%>
 
 <%@include file="/global.jsp" %>
 
 <%
 pageTitle = "iTrust - View My Message ";
-session.setAttribute("outbox",false);
-session.setAttribute("isHCP",userRole.equals("hcp"));
-loggingAction.logEvent(TransactionType.INBOX_VIEW, loggedInMID.longValue(), 0L, "");
 %>
 
 <%@include file="/header.jsp" %>
 
 <div align=center>
 	<h2>My Messages</h2>
-	<%@include file="/auth/hcp-patient/mailbox.jsp" %>
-
+	
+<%
+	loggingAction.logEvent(TransactionType.INBOX_VIEW, loggedInMID.longValue(), 0, "");
+	
+	ViewMyMessagesAction action = new ViewMyMessagesAction(prodDAO, loggedInMID.longValue());
+	EditPersonnelAction f_action = new EditPersonnelAction(prodDAO, loggedInMID.longValue(), loggedInMID.toString());
+	PersonnelDAO dao = new PersonnelDAO(prodDAO);
+	List<MessageBean> messages = null;
+	
+	//Edit Filter backend
+	boolean editing = false;
+	String headerMessage = "";
+	String[] fields = new String[6];
+	if(request.getParameter("edit") != null && request.getParameter("edit").equals("true")) {
+		editing = true;
+		
+		int i;
+		for(i=0; i<6; i++) {
+			fields[i] = "";
+		}
+		
+		if(request.getParameter("cancel") != null) 
+			response.sendRedirect("messageInbox.jsp"); 
+		else if(request.getParameter("test") != null || request.getParameter("save") != null) {
+			boolean error = false;
+			String nf = "";
+			nf += request.getParameter("sender").replace(",","")+",";
+			nf += request.getParameter("subject").replace(",","")+",";
+			nf += request.getParameter("hasWords").replace(",","")+",";
+			nf += request.getParameter("notWords").replace(",","")+",";
+			nf += request.getParameter("startDate").replace(",","")+",";
+			nf += request.getParameter("endDate");
+			
+			//Validate Filter
+			nf = action.validateAndCreateFilter(nf);
+			if(nf.startsWith("Error")) {
+				error = true;
+				headerMessage = nf;
+			}
+			
+			if(!error) {
+				if(request.getParameter("test") != null) {
+					response.sendRedirect("messageInbox.jsp?edit=true&testFilter="+nf);
+				} else if(request.getParameter("save") != null) {
+					f_action.editMessageFilter(nf);
+					response.sendRedirect("messageInbox.jsp?filter=true"); 
+				}
+			}
+		}
+		
+		if(request.getParameter("testFilter") != null) {
+			String filter = request.getParameter("testFilter");
+			String[] f = filter.split(",", -1);
+			for(i=0; i<6; i++) {
+				try {
+					fields[i] = f[i];
+				} catch(ArrayIndexOutOfBoundsException e) {
+					//do nothing
+				}
+			}
+		} else {
+			String filter = dao.getPersonnel(loggedInMID.longValue()).getMessageFilter();
+			if(!filter.equals("")) {
+				String[] f = filter.split(",", -1);
+				for(i=0; i<6; i++) {
+					try {
+						fields[i] = f[i];
+					} catch(ArrayIndexOutOfBoundsException e) {
+						//do nothing
+					}
+				}
+			}
+		}
+	}
+	
+	//Sorts messages
+	if(request.getParameter("sort") != null) {
+		if(request.getParameter("sortby").equals("name")) {
+			if(request.getParameter("sorthow").equals("asce")) {
+				messages = action.getAllMyMessagesNameAscending();
+			} else {
+				messages = action.getAllMyMessagesNameDescending();
+			}
+		} else if(request.getParameter("sortby").equals("time")) {
+			if(request.getParameter("sorthow").equals("asce")) {
+				messages = action.getAllMyMessagesTimeAscending();
+			} else {
+				messages = action.getAllMyMessages();
+			}
+		}
+	}
+	else {
+		messages = action.getAllMyMessages();
+	}
+	
+	//Filters Messages
+	boolean is_filtered = false;
+	if((request.getParameter("filter") != null && request.getParameter("filter").equals("true")) || request.getParameter("testFilter") != null) {
+		String filter = "";
+		if(request.getParameter("testFilter") != null) {
+			filter = request.getParameter("testFilter");
+		} else {
+			filter = dao.getPersonnel(loggedInMID.longValue()).getMessageFilter();
+		}
+		if(!filter.equals("") && !filter.equals(",,,,,")) {
+			List<MessageBean> filtered = action.filterMessages(messages, filter);
+			messages = filtered;
+			is_filtered = true;
+		}
+	}
+	
+	session.setAttribute("messages", messages);
+	%>
+	
+	<%
+	
+	if(editing) {
+		%>
+		<div class="filterEdit">
+			<div align="center">
+				<span style="font-size: 13pt; font-weight: bold;">Edit Message Filter</span>
+				<%= headerMessage.equals("") ? "" : "<br /><span class=\"iTrustMessage\">"+headerMessage+"</span><br /><br />" %>
+				<form method="post" action="messageInbox.jsp?edit=true">
+					<table>
+						<tr style="text-align: right;">
+							<td>
+								<label for="sender">Sender: </label>
+								<input type="text" name="sender" id="sender" value="<%= StringEscapeUtils.escapeHtml("" + (fields[0] )) %>" />
+							</td>
+							<td style="padding-left: 10px; padding-right: 10px;">
+								<label for="hasWords">Has the words: </label>
+								<input type="text" name="hasWords" id="hasWords" value="<%= StringEscapeUtils.escapeHtml("" + (fields[2] )) %>" />
+							</td>
+							<td>
+								<label for="startDate">Start Date: </label>
+								<input type="text" name="startDate" id="startDate" value="<%= StringEscapeUtils.escapeHtml("" + (fields[4] )) %>" />
+								<input type="button" value="Select Date" onclick="displayDatePicker('startDate');" />
+							</td>
+						</tr>
+						<tr style="text-align: right;">
+							<td>
+								<label for="subject">Subject: </label>
+								<input type="text" name="subject" id="subject" value="<%= StringEscapeUtils.escapeHtml("" + (fields[1] )) %>" />
+							</td>
+							<td style="padding-left: 10px; padding-right: 10px;">
+								<label for="notWords">Does not have the words: </label>
+								<input type="text" name="notWords" id="notWords" value="<%= StringEscapeUtils.escapeHtml("" + (fields[3] )) %>" />
+							</td>
+							<td>
+								<label for="endDate">End Date: </label>
+								<input type="text" name="endDate" id="endDate" value="<%= StringEscapeUtils.escapeHtml("" + (fields[5] )) %>" />
+								<input type="button" value="Select Date" onclick="displayDatePicker('endDate');" />
+							</td>
+						</tr>
+						<tr style="text-align: center;">
+							<td colspan="3">
+								<input type="submit" name="test" value="Test Filter" />
+								<input type="submit" name="save" value="Save" />
+								<input type="submit" name="cancel" value="Cancel" />
+							</td>
+						</tr>
+					</table>
+				</form>
+			</div>
+		</div>
+		<br />
+		<%
+	}
+	
+	%>
+	
+	<form method="post" action="messageInbox.jsp<%= StringEscapeUtils.escapeHtml("" + (is_filtered?"?filter=true":"" )) %>">	
+	<table>
+	<tr>
+		<td>
+			<select name="sortby">
+					<option value="time">Sort</option>
+					<option value="name">Name</option>
+					<option value="time">Time</option>
+			</select>
+		</td>
+		<td>
+			<select name="sorthow">
+					<option value="desc">Order</option>
+					<option value="asce">Ascending</option>
+					<option value="desc">Descending</option>
+			</select>
+		</td>
+		<td>
+			<input type="submit" name="sort" value="Sort" />
+		</td>
+	</tr>
+	<tr>
+		<td><a href="messageInbox.jsp?edit=true" >Edit Filter</a></td>
+		<td><a href="messageInbox.jsp?filter=true" >Apply Filter</a></td>
+	</tr>
+	</table>
+	</form>
+	<br />
+	<%if(messages.size() > 0) { %>
+	<table class="fancyTable">
+		<tr>
+			<th>Sender</th>
+			<th>Subject</th>
+			<th>Received</th>
+			<th></th>
+		</tr>
+<%		int index = 0; 
+		for(MessageBean message : messages) {
+		if(message.getRead() == 0) {%>
+		<tr style="font-weight: bold;" <%=(index%2 == 1)?"class=\"alt\"":"" %>>
+			<td><%= StringEscapeUtils.escapeHtml("" + ( action.getName(message.getFrom()) )) %></td>
+			<td><%= StringEscapeUtils.escapeHtml("" + ( message.getSubject() )) %></td>
+			<td><%= StringEscapeUtils.escapeHtml("" + ( message.getSentDate() )) %></td>
+			<td><a href="viewMessageInbox.jsp?msg=<%= StringEscapeUtils.escapeHtml("" + ( index )) %>">Read</a></td>
+		</tr>
+<% 			   } else { %>
+		<tr <%=(index%2 == 1)?"class=\"alt\"":"" %>>
+			<td><%= StringEscapeUtils.escapeHtml("" + ( action.getName(message.getFrom()) )) %></td>
+			<td><%= StringEscapeUtils.escapeHtml("" + ( message.getSubject() )) %></td>
+			<td><%= StringEscapeUtils.escapeHtml("" + ( message.getSentDate() )) %></td>
+			<td><a href="viewMessageInbox.jsp?msg=<%= StringEscapeUtils.escapeHtml("" + ( index )) %>">Read</a></td>
+		</tr>
+<% 			  } %>
+<%			index ++; %>
+<%		} %>
+	</table>
+<%	} else { %>
+	<div>
+		<i>You have no messages</i>
+	</div>
+<%	} %>	
+	<br />
 </div>
 
-<%@include file="/footer.jsp" %>
+<%@include file="/footer.jsp" %>
\ No newline at end of file
-- 
GitLab