Imports Plumtree.Remote.Portlet 
Imports Plumtree.Remote.PRC 
Imports Plumtree.Remote.PRC.Collaboration 
Imports Plumtree.Remote.PRC.Collaboration.Discussion 
Imports Plumtree.Remote.PRC.Collaboration.Project 
Public Class Discussion
  Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

  'This call is required by the Web Form Designer.
  <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()


  End Sub
  Protected DiscussionMethodDropDownList As System.Web.UI.WebControls.DropDownList
  Protected WithEvents SelectGoButton As System.Web.UI.WebControls.Button
  Protected DiscussionNameBox As System.Web.UI.WebControls.TextBox
  Protected DiscussionDescriptionBox As System.Web.UI.WebControls.TextBox
  Protected WithEvents CreateSubmitButton As System.Web.UI.WebControls.Button
  Protected DiscussionIDStrBox As System.Web.UI.WebControls.TextBox
  Protected WithEvents RemoveSubmitButton As System.Web.UI.WebControls.Button

  'NOTE: The following placeholder declaration is required by the Web Form Designer.
  'Do not delete or move it.
  Private designerPlaceholderDeclaration As System.Object

  Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
    'CODEGEN: This method call is required by the Web Form Designer
    'Do not modify it Imports the code editor.
    InitializeComponent()
  End Sub

#End Region
  'Booleans for create, remove and search
  Public createDiscussion As Boolean = False
  Public removeDiscussion As Boolean = False
  Public searchDiscussion As Boolean = False
  Public searchDiscussionMessages As Boolean = False

  'key for discussion
  Public Shared ReadOnly SESSION_DISCUSSION_KEY As String = "discussionID"

  'key for discussion message
  Public Shared ReadOnly SESSION_DISCUSSION_MESSAGE_KEY As String = "discussionMessageID"

  'key for project
  Public Shared ReadOnly SESSION_PROJECT_KEY As String = "edk_sample_project"

  'optional name and description for create
  Public name As String = Nothing
  Public description As String = Nothing

  'portlet id
  Public portletID As Integer

  'discussion id required for remove discussion
  Public strDiscussionID As String = Nothing
  Public discussionID As Integer = -1
  'gets the discussion manager
  Public Function GetDiscussionManager(ByVal req As HttpRequest, ByVal res As HttpResponse) As IDiscussionManager
    Dim discussionManager As IDiscussionManager = Nothing
    Dim portletContext As IPortletContext = PortletContextFactory.CreatePortletContext(req, res)
    Dim portletRequest As IPortletRequest = portletContext.GetRequest()
    Dim loginToken As String = portletRequest.GetLoginToken()
    If loginToken Is Nothing Then
      res.Write("Unable to retrieve the login token. Confirm that the login token has been checked in the Advanced Settings page of the Web Service.")
    End If
    'get the remote session
    Dim portalSession As Plumtree.Remote.PRC.IRemoteSession = portletContext.GetRemotePortalSession()

    'get a collab factory and a discussion manager
    Dim collabFactory As ICollaborationFactory = portalSession.GetCollaborationFactory()
    discussionManager = collabFactory.GetDiscussionManager()

    Return discussionManager
  End Function

  Public Sub New()
    AddHandler Page.Init, AddressOf Page_Init
  End Sub

  Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'Put user code to initialize the page here
    'redirect to the project page if we don't have a project in session
    'check if the session has a project id. If not, get the default project id for the edk.
    Dim project As Plumtree.Remote.PRC.Collaboration.Project.IProject = CType(Session.Item("edk_sample_project"), Plumtree.Remote.PRC.Collaboration.Project.IProject)
    'if this is Nothing, store, and get out of the session again
    If (project Is Nothing) Then
      Response.Redirect("../project/ProjectCreatorVB.aspx?source=../discussion/Discussion.aspx")
    End If
    GetPostBackEventReference(Me)
    Dim portletContext As IPortletContext = PortletContextFactory.CreatePortletContext(Request, Response)
    Dim portletRequest As IPortletRequest = portletContext.GetRequest
    portletID = portletRequest.GetPortletID




  End Sub

  Private Sub SelectGoButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SelectGoButton.Click
    'see if something was chosen in select. If so, assign the appropriate Booleanean
    Dim selectStr As String = DiscussionMethodDropDownList.SelectedValue

    If Not selectStr Is Nothing Then
      If selectStr.Equals("create") Then
        createDiscussion = True
      ElseIf selectStr.Equals("remove") Then
        removeDiscussion = True
      ElseIf selectStr.Equals("search") Then
        searchDiscussion = True
      ElseIf selectStr.Equals("searchDiscussionMessages") Then
        searchDiscussionMessages = True
      End If
    End If

  End Sub
  Private Sub CreateSubmitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CreateSubmitButton.Click
    name = DiscussionNameBox.Text
    description = DiscussionDescriptionBox.Text
    createDiscussion = True

  End Sub

  Private Sub RemoveSubmitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RemoveSubmitButton.Click
    'see if we have a discussion id- if so, convert it to an Integer.
    strDiscussionID = DiscussionIDStrBox.Text
    If Not strDiscussionID Is Nothing Then
      discussionID = Int32.Parse(strDiscussionID)
    End If
    removeDiscussion = True
  End Sub

End Class
