<%@ Page language="c#" Codebehind="DiscussionMessage.aspx.cs" AutoEventWireup="false" Inherits="DiscussionSample.DiscussionMessage" %>

<form id="selectForm" method="post" runat="server">
	<table>
		<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.Name%></td>
			<td><%=discussion.Description%></td>
			<td><%=discussion.ID%></td>
			<td><%=discussion.CreatedDate%></td>
			<td><%=discussion.LastModifiedDate%></td>
			<td><%=discussion.OwnerUserID%></td>
		</tr>	
		</tr>	 
		 <tr>
			<td  colspan="6">
		  </td>
		 </tr>
		 <tr>
		 <td  colspan="2">
			<!--select for create, remove, search-->
								<asp:dropdownlist id="DiscussionMessageMethodDropDownList" Runat="server" ptrender="true">
			<asp:listitem value="createMessage" >create Message</asp:listitem >
			<asp:listitem  value="removeMessage" >remove Message</asp:listitem >
			<asp:listitem  value="searchMessages" >search Messages</asp:listitem >
			</asp:dropdownlist>
			</td>
			<td  colspan="4">
				<asp:Button id="SelectGoButton" Text="go" Runat="server" ptrender="true"/>
		</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  colspan="2">
	   Discussion Message Subject:
	 </td>
	 <td  colspan="4">
		 <asp:TextBox ID="SubjectBox" runat="server" />
	 </td>
	</tr>
	<tr>
	 <td  colspan="2">
	   Discussion Message Body:
	 </td>
	 <td  colspan="4">
	    <asp:TextBox ID="BodyBox" runat="server" />
	 </td>
	</tr>	
	<tr>
	 <td  colspan="6">
	  		<asp:Button id="CreateSubmitButton" Text="Submit" Runat="server" ptrender="true"/>
	 </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		
		Plumtree.Remote.PRC.Collaboration.Discussion.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.Description = "Sample Description";
		*/
		//call store before asking for the id. 
		discussionMessage.Store();
		int id = discussionMessage.ID;
		String url = discussionMessage.DetailsURL;

		%>
		<tr>
			<td  colspan="6">
			<%
				Response.Write("<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  colspan="2">
				Discussion Message ID:
			</td>
			<td  colspan="4">
			 <asp:TextBox ID="DiscussionMessageIDBox" runat="server" />
			</td>
		</tr>	
		<tr>
			<td  colspan="6">
				<asp:Button id="RemoveSubmitButton" Text="Submit" Runat="server" ptrender="true"/>
			</td>
		</tr>
		<%
		} 
		else
		{
		//remove the discussion Message 
        //then get the discussion manager to retrieve the discussion message
            	Plumtree.Remote.PRC.Collaboration.Discussion.IDiscussionManager discussionManager = GetDiscussionManager(Request, Response);
        //then get the discussion message from the manager    	
        Plumtree.Remote.PRC.Collaboration.Discussion.IDiscussionMessage discussionMessage = discussionManager.GetDiscussionMessage(discussionMessageID);
        
		
		//squawk if the discussion could not be retrieved
		if (null == discussionMessage)
		{
			%>
			<tr>
				<td  colspan="6">
					<%Response.Write("Unable to retrieve discussion  Message with ID of " + discussionMessageID);%>
				</td>
			</tr>

			<%
		}
		else
		{
			//remove
			discussion.RemoveDiscussionMessage(discussionMessage);
			%>
			<tr>
				<td  colspan="6">
					<%Response.Write("Discussion Message with id of " + discussionMessageID + " removed.");%>
				</td>
			</tr>

		<%
		}
	}
}


if (searchDiscussionMessages)
{
       	//get the project ID out of session- this should never be null as it is added in the page load event
		Plumtree.Remote.PRC.Collaboration.Project.IProject project = (Plumtree.Remote.PRC.Collaboration.Project.IProject) Session[SESSION_PROJECT_KEY];
		
		//perform the search
    	Plumtree.Remote.PRC.Collaboration.Discussion.IDiscussionManager discussionManager = GetDiscussionManager(Request, Response);
		Plumtree.Remote.PRC.Collaboration.Discussion.IDiscussionMessageFilter discussionMessageFilter = discussionManager.CreateDiscussionMessageFilter();

		//hard-code the max results to 10
		discussionMessageFilter.MaximumResults = 10;

		//optionally, set the query orders

		//execute the search and print out the results
		Plumtree.Remote.PRC.Collaboration.Discussion.IDiscussionMessage[] discussionMessages = discussionManager.QueryDiscussionMessages(project, discussionMessageFilter);
		if (discussionMessages.Length > 0)
		{
			%>
			<tr>
				<td colspan="6">
					Search Results
				</td>

			</tr>
			<tr>
				<td  colspan="2">
					Discussion Message Name- Link to Discussion Message
				</td>
				<td  colspan="4">
					Discussion ID
				</td>
			</tr>
			<%
			for (int i = 0; i < discussionMessages.Length; i++)
			{
				Plumtree.Remote.PRC.Collaboration.Discussion.IDiscussionMessage discussionMessage = discussionMessages[i];
				int id = discussionMessage.ID;
				String name = discussionMessage.Subject;
				String url = discussionMessage.DetailsURL;
				%>
				<tr>
				<td  colspan="2">
			  	<%Response.Write("<a href=\"" + url + "\">" + name + "</a>");%>
				</td>
				<td  colspan="4">
					<%Response.Write(id);%>
				</td>
				</tr>
			<%	
			}
		}
		else
		{
			Response.Write("No discussion messages found.");
		}

}

%>

</table>
</form>




