<%@ Page language="c#" Codebehind="Discussion.aspx.cs" AutoEventWireup="false" Inherits="DiscussionSample.Discussion" %>
<Script language="javascript">
function openWin<%=portletID%>(url)
{
  window.open(url);
}
</Script>	
	<!--select for search discussions, create discussions, remote discussions, search discussion messages-->
		<form id="selectForm" method="post" runat="server">
			<table>
				<tr>
					<td>
					<!--select for create, remove, search-->
					<asp:dropdownlist id="DiscussionMethodDropDownList" Runat="server" ptrender="true">
							<asp:listitem value="create">Create Discussion</asp:listitem>
							<asp:listitem value="remove">Remove Discussion</asp:listitem>
							<asp:listitem value="search">Search Discussions</asp:listitem>
							<asp:listitem value="searchDiscussionMessages">Search Messages</asp:listitem>
						</asp:dropdownlist>
						</td>
						<td>
						<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 (createDiscussion)
{
  //if no name or description, show text boxes for name and description, and a submit button
  if (null == name || null == description)
  {
    %>
	<tr>
	 <td>
	   Discussion Name:
	 </td>
	 <td>
	  <asp:TextBox ID="DiscussionNameBox" runat="server" />
	 </td>
	</tr>
	<tr>
	 <td>
	   Discussion Description:
	 </td>
	 <td>
	   <asp:TextBox ID="DiscussionDescriptionBox" runat="server" />
	 </td>
	</tr>	
	<tr>
	 <td>
	  <asp:Button id="CreateSubmitButton" Text="submit" Runat="server" ptrender="true"/>
	 </td>
	</tr>		
   <%
  }
  else
  //create a discussion and print out the discussion id
  {
		name = (null == name) ? "ExampleDiscussion" : name;
		description = (null == description) ? "ExampleDiscussionDescription" : description;
		
		//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];
	
		//create the discussion			
		Plumtree.Remote.PRC.Collaboration.Discussion.IDiscussionManager discussionManager = GetDiscussionManager(Request, Response);
		Plumtree.Remote.PRC.Collaboration.Discussion.IDiscussion discussion =
						discussionManager.CreateDiscussion(project, name, description);

		//if you want to set additional properties, make sure that store() is called or the changes will not be persisted.
		//for example:
		/*
		discussion.SetAccessLevel(RoleTypes.Member,AccessLevels.Write );

		*/
		//call store before asking for the id. 
		discussion.Store();
		String url = discussion.DetailsURL;
		int id = discussion.ID;
		String detailsUrl = "DiscussionMessage.aspx?" + SESSION_DISCUSSION_KEY + "=" +  id;

		%>
		<tr>
			<td>
			<%
				Response.Write("<a href=\"" + url + "\">Link to Collab discussion  " + id + "</a>");
			%>
			</td>
			<td>
			<%
				Response.Write("<a href=\"#\" onclick=\"openWin"  +  portletID + "('"  +  detailsUrl +  "')\">Discussion details for  " +  id +  "</a>");
			%>
			</td>			
		</tr>
        
  <%
  }
}
if (removeDiscussion)
{
	//if no discussion ID, add a text box for discussionID, and a submit button
	if (discussionID == -1)
	{
		%>
		<tr>
			<td>
				Discussion ID:
			</td>
			<td>
				<asp:TextBox ID="DiscussionIDStrBox" runat="server" />
			</td>
		</tr>	
		<tr>
			<td>
				<asp:Button id="RemoveSubmitButton" Text="submit" Runat="server" ptrender="true"/>
			</td>
		</tr>
		<%
		} 
		else
		{
		//remove the discussion
		Plumtree.Remote.PRC.Collaboration.Discussion.IDiscussionManager discussionManager = GetDiscussionManager(Request, Response);

		Plumtree.Remote.PRC.Collaboration.Discussion.IDiscussion discussion = discussionManager.GetDiscussion(discussionID);
		//squawk if the discussion could not be retrieved
		if (null == discussion)
		{
			%>
			<tr>
				<td>
					<%Response.Write("Unable to retrieve discussion with ID of " + discussionID);%>
				</td>
			</tr>

			<%
		}
		else
		{
			//remove
			discussionManager.RemoveDiscussion(discussion);
			%>
			<tr>
				<td>
					<%Response.Write("Discussion with id of " + discussionID + " removed.");%>
				</td>
			</tr>

		<%
		}
	}
}

if (searchDiscussion)
{
       	//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.IDiscussionFilter discussionFilter = discussionManager.CreateDiscussionFilter();

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

		//there is no query we can set with discussions

		//execute the search and print out the results
		Plumtree.Remote.PRC.Collaboration.Discussion.IDiscussion[] discussions = discussionManager.QueryDiscussions(project, discussionFilter);
		if (discussions.Length > 0)
		{
			%>
			<tr>
				<td colspan="2">
					Search Results
				</td>

			</tr>
			<tr>
				<td>
					Discussion Name- Link to Discussion
				</td>
				<td>
					Discussion ID- Link to Details
				</td>
			</tr>
			<%
			for (int i = 0; i < discussions.Length; i++)
			{
				Plumtree.Remote.PRC.Collaboration.Discussion.IDiscussion discussion = discussions[i];
				int id = discussion.ID;
				String name = discussion.Name;
				String url = discussion.DetailsURL;
				String detailsUrl = "DiscussionMessage.aspx?" + SESSION_DISCUSSION_KEY + "=" + id;
				%>
				<tr>
				<td>
			  	<%Response.Write("<a href=\"" + url + "\">" + name + "</a>");%>
				</td>
				<td>
			<%
				Response.Write("<a href=\"#\" onclick=\"openWin"  +  portletID + "('"  +  detailsUrl +  "')\">" +  id +  "</a>");
				
			%>
				</td>
				</tr>
			<%	
			}
		}
		else
		{
			Response.Write("No discussions found.");
		}
}
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="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++)
			{
				Plumtree.Remote.PRC.Collaboration.Discussion.IDiscussionMessage discussionMessage = discussionMessages[i];
				int id = discussionMessage.ID;
				String name = discussionMessage.Subject;
				String url = discussionMessage.DetailsURL;
				%>
				<tr>
				<td>
			  	<%Response.Write("<a href=\"" + url + "\">" + name + "</a>");%>
				</td>
				<td>
					<%Response.Write(id);%>
				</td>
				</tr>
			<%	
			}
		}
		else
		{
			Response.Write("No discussion messages found.");
		}
}
%>
			</table>
		</form>



