<%@ Page Language="vb" AutoEventWireup="false" Codebehind="Discussion.aspx.vb" Inherits="DiscussionSampleVB.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"  runat="server">
			<table>
				<tr>
					<td>
						<!--select for create, remove, search-->
						<asp:dropdownlist id="DiscussionMethodDropDownList" Runat="server">
							<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 then
  'if no name or description, show text boxes for name and description, and a submit button
  if  name is Nothing  or description is Nothing then
   %>
				<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
		if name is nothing then
		   name = "ExampleDiscussion"
		end if   
   if description is nothing then
      description = "ExampleDiscussionDescription"
  end if    
		
		'get the project ID out of session- this should never be Nothing as it is added in the page load event
		dim project as Plumtree.Remote.PRC.Collaboration.Project.IProject  = CType(Session.Item(SESSION_PROJECT_KEY),Plumtree.Remote.PRC.Collaboration.Project.IProject)  
	
		'create the discussion			
		dim discussionManager as Plumtree.Remote.PRC.Collaboration.Discussion.IDiscussionManager  = GetDiscussionManager(Request, Response) 
		dim discussion as Plumtree.Remote.PRC.Collaboration.Discussion.IDiscussion  =	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() 
		dim url as String  = discussion.DetailsURL 
		dim id as Integer  = discussion.ID 
		dim detailsUrl as String  = "DiscussionMessage.aspx?" & SESSION_DISCUSSION_KEY & "=" & CStr(id )

		%>
				<tr>
					<td>
						<%
				Response.Write("<a href=""" & url & """>Link to Collab discussion  " & CStr(id) & "</a>") 
			%>
					</td>
					<td>
						<%Response.Write("<a href=""#"" onclick=""openWin" & portletID & "('" & detailsUrl & "')"">Discussion details for " & Cstr(id) & "</a>") %>			
					</td>
				</tr>
				<%
  end if
end if

if removeDiscussion then
	'if no discussion ID, add a text box for discussionID, and a submit button
	if discussionID = -1 then
		%>
				<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
		dim discussionManager as Plumtree.Remote.PRC.Collaboration.Discussion.IDiscussionManager  = GetDiscussionManager(Request, Response) 

		dim discussion as Plumtree.Remote.PRC.Collaboration.Discussion.IDiscussion  = discussionManager.GetDiscussion(discussionID) 
		'squawk if the discussion could not be retrieved
		if  discussion is Nothing then
			%>
				<tr>
					<td>
						<%Response.Write("Unable to retrieve discussion with ID of " & CStr(discussionID)) %>
					</td>
				</tr>
				<%
		else
			'remove
			discussionManager.RemoveDiscussion(discussion) 
			%>
				<tr>
					<td>
						<%Response.Write("Discussion with id of " & CStr(discussionID ) & " removed.") %>
					</td>
				</tr>
				<%
		end if
	end if
end if

if searchDiscussion then
       	'get the project ID out of session- this should never be Nothing as it is added in the page load event
		dim project as Plumtree.Remote.PRC.Collaboration.Project.IProject  = CType(Session.Item(SESSION_PROJECT_KEY),Plumtree.Remote.PRC.Collaboration.Project.IProject) 
		
		'perform the search
    	dim discussionManager as Plumtree.Remote.PRC.Collaboration.Discussion.IDiscussionManager  = GetDiscussionManager(Request, Response) 
		dim discussionFilter as Plumtree.Remote.PRC.Collaboration.Discussion.IDiscussionFilter  = 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
		dim discussions() as Plumtree.Remote.PRC.Collaboration.Discussion.IDiscussion  = discussionManager.QueryDiscussions(project, discussionFilter) 
		if discussions.Length > 0 then
			%>
				<tr>
					<td colspan="2">
						Search Results
					</td>
				</tr>
				<tr>
					<td>
						Discussion Name- Link to Discussion
					</td>
					<td>
						Discussion ID- Link to Details
					</td>
				</tr>
				<%
			dim i as Integer
			For i = 0 to  discussions.Length -1
				dim discussion as Plumtree.Remote.PRC.Collaboration.Discussion.IDiscussion  = discussions(i) 
				dim id as Integer = discussion.ID 
				dim name as String  = discussion.Name 
				dim url as String  = discussion.DetailsURL 
				dim detailsUrl as String  = "DiscussionMessage.aspx?" & SESSION_DISCUSSION_KEY & "=" & CStr(id )
				%>
				<tr>
					<td>
						<%Response.Write("<a href=""" & url & """>" & name & "</a>") %>
					</td>
					<td>
						<%Response.Write("<a href=""#"" onclick=""openWin" & portletID & "('" & detailsUrl & "')"">" & Cstr(id) & "</a>") %>			
						
					</td>
				</tr>
				<%	
			Next
		else
			Response.Write("No discussions found.") 
    end if
end if 
    
if searchDiscussionMessages then
   	'get the project ID out of session- this should never be Nothing as it is added in the page load event
		dim project as Plumtree.Remote.PRC.Collaboration.Project.IProject  = CType(Session.Item(SESSION_PROJECT_KEY),Plumtree.Remote.PRC.Collaboration.Project.IProject) 
		
		'perform the search
    	dim discussionManager as Plumtree.Remote.PRC.Collaboration.Discussion.IDiscussionManager  = GetDiscussionManager(Request, Response) 
		dim discussionMessageFilter as Plumtree.Remote.PRC.Collaboration.Discussion.IDiscussionMessageFilter  = 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
		dim discussionMessages() as Plumtree.Remote.PRC.Collaboration.Discussion.IDiscussionMessage  = discussionManager.QueryDiscussionMessages(project, discussionMessageFilter) 
		if discussionMessages.Length > 0 then
			%>
				<tr>
					<td colspan="2">
						Search Results
					</td>
				</tr>
				<tr>
					<td>
						Discussion Message Name- Link to Discussion Message
					</td>
					<td>
						Discussion  Message ID
					</td>
				</tr>
				<%
      dim i as Integer
			for i = 0 to discussionMessages.Length -1 
				dim discussionMessage as Plumtree.Remote.PRC.Collaboration.Discussion.IDiscussionMessage  = discussionMessages(i) 
				dim id as Integer = discussionMessage.ID 
				dim name as String  = discussionMessage.Subject 
				dim url as String  = discussionMessage.DetailsURL 
				%>
				<tr>
					<td>
						<%Response.Write("<a href=""" & url & """>" & name & "</a>") %>
					</td>
					<td>
						<%Response.Write(CStr(id)) %>
					</td>
				</tr>
				<%	
      Next
		else
			Response.Write("No discussion messages found.") 
   end if
end if   
%>
			</table>
		</form>

