<%@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" %>

<%
		//booleans for create, remove and search
		 boolean createDiscussion = false;
		 boolean removeDiscussion = false;
		 boolean searchDiscussion = false;
		 boolean searchDiscussionMessages = false;

		//optional name and description for create
		 String name = null;
		 String description = null;

		//discussion id required for remove discussion
		 String strDiscussionID = null;
		 int discussionID = -1;
		 
		 //project
		 IProject project = null;

     //load the page variables
			project = (IProject) session.getAttribute(SESSION_PROJECT_KEY);
	//attempt to retrieve the sample project; if not found, create it, then redirect back to the current page    
    if(project == null)
    {
    	response.sendRedirect("../project/ProjectCreator.jsp");
    }

			//get the parameter map out of session- keep in mind that the values are String[]
			Map parameterMap = (Map) session.getAttribute(SESSION_PARAMETER_MAP);
			

			//see if something was chosen in select. If so, assign the appropriate boolean
			String select = getParameter(parameterMap, "discussionMethod");
			if (null != select)
			{
				if (select.equals("create"))
				{
					createDiscussion = true;
				}
				else if (select.equals("remove"))  
				{
					removeDiscussion = true;
				}
				else if (select.equals("search"))
				{
					searchDiscussion = true;
				}
				else if (select.equals("searchDiscussionMessages"))
				{
					searchDiscussionMessages = true;
				}
	
				//if any of the select booleans are true, set hosted display mode
				if (createDiscussion || removeDiscussion || searchDiscussion || searchDiscussionMessages)
				{
					IPortletContext portletContext = PortletContextFactory.createPortletContext(request, response);
					IPortletResponse portletResponse = portletContext.getResponse();
					portletResponse.setHostedDisplayMode(HostedDisplayMode.Hosted);
				}

				//see if we have a discussion id- if so, convert it to an int.
				strDiscussionID = getParameter(parameterMap, SESSION_DISCUSSION_KEY);
				if (null != strDiscussionID)
				{
					discussionID = Integer.parseInt(strDiscussionID); 
				}


				//if we got a name or description from create discussion, assign them to the name and description variables
				name = getParameter(parameterMap, "name");
				description = getParameter(parameterMap, "description");
			}

%>
	<!--select for search discussions, create discussions, remote discussions, search discussion messages-->
		<table>
			<form id="selectForm" method="post" action="action.jsp">		
				<tr>
					<td>
					<!--select for create, remove, search-->
						<select name="discussionMethod">
							<option value="create" <% if(createDiscussion)out.print(" SELECTED ");%>>Create Discussion</option>
							<option value="remove" <% if(removeDiscussion)out.print(" SELECTED ");%>>Remove Discussion</option>
							<option value="search" <% if(searchDiscussion)out.print(" SELECTED ");%>>Search Discussions</option>
							<option value="searchDiscussionMessages" <% if(searchDiscussionMessages)out.print(" 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 (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>
	   <input type="text" name="name" value="<%=(null==name)?"":name%>"/>
	 </td>
	</tr>
	<tr>
	 <td>
	   Discussion Description:
	 </td>
	 <td>
	   <input type="text" name="description" value="<%=(null==description)?"":description%>"/>
	 </td>
	</tr>	
	<tr>
	 <td>
	   <input type="submit" name="createSubmit" value="submit"/>
	 </td>
	</tr>		
   <%
  }
  else
  //create a discussion and print out the discussion id
  {
		name = (null == name) ? "ExampleDiscussion" : name;
		description = (null == description) ? "ExampleDiscussionDescription" : description;
		
	
		//create the discussion			
		IDiscussionManager discussionManager = getDiscussionManager(request, response);
		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.getDetailsURL();
		int id = discussion.getID();
		String detailsUrl = "DiscussionMessage.jsp?" + SESSION_DISCUSSION_KEY + "=" + id;

		%>
		<tr>
			<td>
			<%
				out.print("<a href=\"" + url + "\">Link to Collab discussion  " + id + "</a>");
			%>
			</td>
			<td>
			<%
				out.print("<a href=\"#\" onclick=\"window.open('" + 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>
				<input type="text" name="discussionID" value="<%=(null==strDiscussionID)?"":strDiscussionID%>"/>
			</td>
		</tr>	
		<tr>
			<td>
				<input type="submit" name="removeSubmit" value="submit"/>
			</td>
		</tr>
		<%
		} 
		else
		{
		//remove the discussion
		IDiscussionManager discussionManager = getDiscussionManager(request, response);

	  IDiscussion discussion = discussionManager.getDiscussion(discussionID);
		//squawk if the discussion could not be retrieved
		if (null == discussion)
		{
			%>
			<tr>
				<td>
					<%out.print("Unable to retrieve discussion with ID of " + discussionID);%>
				</td>
			</tr>

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

		<%
		}
	}
}

if (searchDiscussion)
{
		
		//perform the search
    IDiscussionManager discussionManager = getDiscussionManager(request, response);
		IDiscussionFilter discussionFilter = discussionManager.createDiscussionFilter();

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

		//there is no query we can set with discussions

		//execute the search and print out the results
		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++)
			{
				IDiscussion discussion = discussions[i];
				int id = discussion.getID();
				name = discussion.getName();
				String url = discussion.getDetailsURL();
				String detailsUrl = "DiscussionMessage.jsp?" + SESSION_DISCUSSION_KEY + "=" + id;
				
				%>
				<tr>
				<td>
			  	<%out.print("<a href=\"" + url + "\">" + name + "</a>");%>
				</td>
				<td>
			<%out.print("<a href=\"#\" onclick=\"window.open('" + detailsUrl + "')\">" + id + "</a>");%>
				</td>
				</tr>
			<%	
			}
		}
		else
		{
			out.print("No discussions found.");
		}
}
if (searchDiscussionMessages)
{
		
		//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();
				name = discussionMessage.getSubject();
				String url = discussionMessage.getDetailsURL();
				%>
				<tr>
				<td>
			  	<%out.print("<a href=\"" + url + "\">" + name + "</a>");%>
				</td>
				<td>
					<%out.print(id);%>
				</td>
				</tr>
			<%	
			}
		}
		else
		{
			out.print("No discussion messages found.");
		}
}
%>

	</form>
</table>

