<%@page import="com.plumtree.remote.prc.IRemoteSession, com.plumtree.remote.prc.RemoteSessionFactory, com.plumtree.remote.prc.collaboration.*,com.plumtree.remote.prc.collaboration.project.*, com.plumtree.remote.portlet.*,java.util.*,java.text.*" %>

<%@ include file="include.jsp" %>

<%
	//boolean for create, remove search
		  boolean createDiscussionMessage = false;
		  boolean removeDiscussionMessage = false;
		  boolean searchDiscussionMessages = false;

		//global discussion variable for us to get details and create discussion messages
		  IDiscussion discussion = null;

		//subject and body for create
		  String subject = null;
		  String body = null;

		//discussion message id required for remove discussion message
		  String discussionMessageIDStr = null;
		  int discussionMessageID = -1;

      //get the page parameters
 			//we should be getting the discussion id from the query string from Discussion.aspx
			//put it in session if it exists
			String discussionIDStr = request.getParameter(SESSION_DISCUSSION_KEY);
			if (null != discussionIDStr)
			{
				//get the discussion and put it into the session
				int discussionID = Integer.parseInt(discussionIDStr);
				IDiscussionManager discussionManager = getDiscussionManager(request, response);
				discussion = discussionManager.getDiscussion(discussionID);			
				session.setAttribute(SESSION_DISCUSSION_KEY, discussion);
			}
			else
			{
				//assume that it has already been put in. No error checking for session timeouts. 
				discussion = (IDiscussion) session.getAttribute(SESSION_DISCUSSION_KEY);
			}

			//retrieve values from submit
			//determine which value, if any, was chosen in the select
			String select = request.getParameter("discussionMessageMethod");
			if (null != select)
			{
				if (select.equals("createMessage"))
				{
					createDiscussionMessage = true;
				}
				else if (select.equals("removeMessage"))  
				{
					removeDiscussionMessage = true;
				}
				else if (select.equals("searchMessages"))
				{
					searchDiscussionMessages = true;
				}
			}


				IPortletContext portletContext = PortletContextFactory.createPortletContext(request, response);
				IPortletResponse portletResponse = portletContext.getResponse();
				portletResponse.setHostedDisplayMode(HostedDisplayMode.Hosted);
		

			//see if we have a discussion message id- if so, convert it to an int.
			discussionMessageIDStr = request.getParameter(SESSION_DISCUSSION_MESSAGE_KEY );
			if (null != discussionMessageIDStr)
			{
				discussionMessageID = Integer.parseInt(discussionMessageIDStr); 
			} 

			subject = request.getParameter("subject");
			body = request.getParameter("body");
		

%>
	<!--select for search discussions, create discussions, remote discussions, search discussion messages-->
		<form id="selectForm" method="post" action="DiscussionMessage.jsp">
		 <table>
			 	 <tr>
			 		<td colspan="2">
			 	  <table cellpadding="1" cellspacing="1" border="1">
						<tr>
		 					<td colspan="6"><b>Discussion Details</b>
		 					</td>
						</tr>
						<tr>
							<td>Name</td>
							<td>Description</td>
							<td>ID</td>
							<td>Created Date</td>
							<td>Last Modified Date</td>
							<td>Owner User ID</td>
						</tr>
						<tr>
							<td><%=discussion.getName()%></td>
							<td><%=discussion.getDescription()%></td>
							<td><%=discussion.getID()%></td>
							<td><%=discussion.getCreatedDate()%></td>
							<td><%=discussion.getLastModifiedDate()%></td>
							<td><%=discussion.getOwnerUserID()%></td>
						</tr>	
		 		 </table>
		  </td>
		</tr>	 
		 <tr>
			<td colspan="2">
		  </td>
		 </tr>
		 <tr>
		 <td>
			<!--select for create, remove, search-->
			<select name="discussionMessageMethod">
			<option value="createMessage" <% if(createDiscussionMessage)out.println(" SELECTED ");%>>create Message</option>
			<option value="removeMessage" <% if(removeDiscussionMessage)out.println(" SELECTED ");%>>remove Message</option>
			<option value="searchMessages" <% if(searchDiscussionMessages)out.println(" SELECTED ");%>>search Messages</option>
			</select>
		</td>
		<td>
			<input type="submit" name="selectGo" value="go"/>
		</td>
	</tr>

<!--show a search box for search, and a box to box to enter the discussion id if remove-->
<% 
if (createDiscussionMessage)
{
  //if no subject or body, show text boxes for subject and body, and a submit button
  if (null == subject || null == body)
  {
    %>
	<tr>
	 <td>
	   Discussion Message Subject:
	 </td>
	 <td>
	   <input type="text" name="subject" value="<%=(null==subject)?"":subject%>"/>
	 </td>
	</tr>
	<tr>
	 <td>
	   Discussion Message Body:
	 </td>
	 <td>
	   <input type="text" name="body" value="<%=(null==body)?"":body%>"/>
	 </td>
	</tr>	
	<tr>
	 <td>
	   <input type="submit" name="createSubmit" value="submit"/>
	 </td>
	</tr>		
   <%
  }
  else
  //create a discussion message and print out the discussion message id
  {
		subject = (null == subject) ? "ExampleDiscussionMessage" : subject;
		body = (null == body) ? "Example Discussion Body" : body;
		
		//create the discussion	message		
		IDiscussionMessage discussionMessage = discussion.createDiscussionMessage(subject, body);
		//if you want to set additional properties, make sure that store() is called or the changes will not be persisted.
		//for example:
		/*
		discussionMessage.getDescription() = "Sample Description";
		*/
		//call store before asking for the id. 
		discussionMessage.store();
		int id = discussionMessage.getID();
		String url = discussionMessage.getDetailsURL();

		%>
		<tr>
			<td>
			<%
				out.println("<a href=\"" + url + "\">Link to collab message " + id + "</a>");
			%>
			</td>
		</tr>
        
  <%
  }
}
if (removeDiscussionMessage)
{
	//if no discussion Message ID, add a text box for discussionMessageID, and a submit button
	if (discussionMessageID == -1)
	{
		%>
		<tr>
			<td>
				Discussion Message ID:
			</td>
			<td>
				<input type="text" name="<%=SESSION_DISCUSSION_MESSAGE_KEY%>" value="<%=(null==discussionMessageIDStr)?"":discussionMessageIDStr%>"/>
			</td>
		</tr>	
		<tr>
			<td>
				<input type="submit" name="removeSubmit" value="submit"/>
			</td>
		</tr>
		<%
		} 
		else
		{
		//remove the discussion Message 
        //then get the discussion manager to retrieve the discussion message
         IDiscussionManager discussionManager = getDiscussionManager(request, response);
        //then get the discussion message from the manager    	
        IDiscussionMessage discussionMessage = discussionManager.getDiscussionMessage(discussionMessageID);
        
		
		//squawk if the discussion could not be retrieved
		if (null == discussionMessage)
		{
			%>
			<tr>
				<td>
					<%out.println("Unable to retrieve discussion  Message with ID of " + discussionMessageID);%>
				</td>
			</tr>

			<%
		}
		else
		{
			//remove
			discussion.removeDiscussionMessage(discussionMessage);
			%>
			<tr>
				<td>
					<%out.println("Discussion Message with id of " + discussionMessageID + " removed.");%>
				</td>
			</tr>

		<%
		}
	}
}


if (searchDiscussionMessages)
{
       	//get the project getID() out of session- this should never be null as it is added in the page load event
	 IProject project = (IProject) session.getAttribute(SESSION_PROJECT_KEY);
		
		//perform the search
    	IDiscussionManager discussionManager = getDiscussionManager(request, response);
		IDiscussionMessageFilter discussionMessageFilter = discussionManager.createDiscussionMessageFilter();

		//hard-code the max results to 10
		discussionMessageFilter.setMaximumResults(10);

		//optionally, set the query orders

		//execute the search and print out the results
		IDiscussionMessage[] discussionMessages = discussionManager.queryDiscussionMessages(project, discussionMessageFilter);
		if (discussionMessages.length > 0)
		{
			%>
			<tr>
				<td colspan="2">
					Search Results
				</td>

			</tr>
			<tr>
				<td>
					Discussion Message Name- Link to Discussion Message
				</td>
				<td>
					Discussion ID
				</td>
			</tr>
			<%
			for (int i = 0; i < discussionMessages.length; i++)
			{
				IDiscussionMessage discussionMessage = discussionMessages[i];
				int id = discussionMessage.getID();
				String name = discussionMessage.getSubject();
				String url = discussionMessage.getDetailsURL();
				%>
				<tr>
				<td>
			  	<%out.println("<a href=\"" + url + "\">" + name + "</a>");%>
				</td>
				<td>
					<%out.println(id);%>
				</td>
				</tr>
			<%	
			}
		}
		else
		{
			out.println("No discussion messages found.");
		}

}

%>


</form>
</table>