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

<%!
	//gets the project manager
	private IProjectManager getProjectManager(HttpServletRequest req, HttpServletResponse res, JspWriter pout) throws Exception 
	{
		IProjectManager projectManager = null;
		IPortletContext portletContext = PortletContextFactory.createPortletContext(req, res);
		IPortletRequest portletRequest = portletContext.getRequest();
		String loginToken = portletRequest.getLoginToken();
		if (null == loginToken)
		{
			pout.println("Unable to retrieve the login token. Confirm that the login token has been checked in the Advanced Settings page of the Web Service.");
		}

		//get the remote session
		com.plumtree.remote.prc.IRemoteSession portalSession = portletContext.getRemotePortalSession();

		//get a collab factory and a project manager
		ICollaborationFactory collabFactory = portalSession.getCollaborationFactory();
		projectManager = collabFactory.getProjectManager();

		return projectManager;
	}

%>

<%
//booleans for create, remove and search
boolean create = false;
boolean remove = false;
boolean search = false;

//optional name and description for create
String name = request.getParameter("name");
String description = request.getParameter("description");

//project id required for remove
int projectID = -1;

//search text required for search
String searchText = request.getParameter("searchText");

//determine which option we are using- create, remove, or search.
String select = request.getParameter("projectMethod");
if (null != select)
{
	if (select.equals("create"))
	{
	  create = true;
	}
	else if (select.equals("remove"))  
	{
	  remove = true;
	}
	else if (select.equals("search"))
	{
	  search = true;
	}
}

//if any of these is true, set hosted display mode
if (create || remove || search)
{
		IPortletContext portletContext = PortletContextFactory.createPortletContext(request, response);
		IPortletResponse portletResponse = portletContext.getResponse();
		portletResponse.setHostedDisplayMode(HostedDisplayMode.Hosted);
}

//see if we have a project id- if so, convert it to an int.
String strProjectID = request.getParameter("projectID");
if (null != strProjectID)
{
 try
 {
 	projectID = Integer.parseInt(strProjectID); 
 }
 catch (Exception e) 
 {
  out.println("Unable to parse projectID of " + strProjectID); 
 } 
} 
  
%>


<table>
<form name="selectForm" method="POST" action="projectExample.jsp">
<tr>
<td>
<!--select for create, remove, search-->
<select name="projectMethod">
<option value="create" <% if(create)out.println(" SELECTED ");%>>create</option>
<option value="remove" <% if(remove)out.println(" SELECTED ");%>>remove</option>
<option value="search" <% if(search)out.println(" SELECTED ");%>>search</option>
</select>
<input type="submit" name="selectGo" value="go"/>

</td>
</tr>


<!--show a search box for search, and a box to box to enter the project id if remove-->
<% 
if (create)
{
  //if no name or description, show text boxes for name and description, and a submit button
  if (null == name || null == description)
  {
    %>
	<tr>
	 <td>
	   Project Name:
	 </td>
	 <td>
	   <input type="text" name="name" value="<%=(null==name)?"":name%>"/>
	 </td>
	</tr>
	<tr>
	 <td>
	   Project 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 project and print out the project id
  {
		name = (null == name) ? "ExampleProject" : name;
		description = (null == description) ? "ExampleProjectDescription" : description;
			//create the project
		IProjectManager projectManager = getProjectManager(request, response, out);
		IProject project =
						projectManager.createProject(name, description);

		//if you want to set additional properties, make sure that store() is called or the changes will not be persisted.
		//for example:
		/*
		project.setStatus(ProjectStatus.NOT_STARTED);
		project.setStartDate(new Date());
		*/
		//call store before asking for the id. 
		project.store();

		%>
		<tr>
			<td>
			<%
				out.println("ID of newly created project is " + project.getID());
			%>
			</td>
		</tr>
        
  <%
  }
}
if (remove)
{
	//if no project ID, add a text box for projectID, and a submit button
	if (projectID == -1)
	{
		%>
		<tr>
			<td>
				Project ID:
			</td>
			<td>
				<input type="text" name="projectID" value="<%=(null==strProjectID)?"":strProjectID%>"/>
			</td>
		</tr>	
		<tr>
			<td>
				<input type="submit" name="removeSubmit" value="submit"/>
			</td>
		</tr>
		<%
		} 
		else
		{
		//remove the project
		IProjectManager projectManager = getProjectManager(request, response, out);
		IProject project = projectManager.getProject(projectID);
		//squawk if the project could not be retrieved
		if (null == project)
		{
			%>
			<tr>
				<td>
					<%out.println("Unable to retrieve project with ID of " + projectID);%>
				</td>
			</tr>

			<%
		}
		else
		{
			//remove
			projectManager.removeProject(project);
			%>
			<tr>
				<td>
					<%out.println("Project with id of " + projectID + " removed.");%>
				</td>
			</tr>

		<%
		}
	}
}

if (search)
{
	//if no search text, add a text box for search text, and a submit button
	if (null == searchText)
	{
		%>
		<tr>
			<td>
				Search Text:
			</td>
			<td>
				<input type="text" name="searchText" value="<%=(null==searchText)?"":searchText%>"/>
			</td>
		</tr>	
		<tr>
			<td>
				<input type="submit" name="searchSubmit" value="submit"/>
			</td>
		</tr>
		<%
	} 
	else
	{
		//perform the search
		IProjectManager projectManager = getProjectManager(request, response, out);
		IProjectFilter projectFilter = projectManager.createProjectFilter();

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

		//set the query
		projectFilter.setNameSearchText(searchText);

		//execute the search and print out the results
		IProject[] projects = projectManager.queryProjects(projectFilter);
		if (projects.length > 0)
		{
			%>
			<tr>
				<td>
					Search Results
				</td>

			</tr>
			<tr>
				<td>
					Project Name
				</td>
				<td>
					Project ID
				</td>
			</tr>
			<%
			for (int i = 0; i < projects.length; i++)
			{
				IProject project = projects[i];
				%>
				<tr>
				<td>
			  	<%out.println(project.getName());%>
				</td>
				<td>
					<%out.println(project.getID());%>
				</td>
				</tr>
			<%	
			}
		}
		else
		{
		%>
		<tr>
			<td colspan="2">
				<%out.println("No projects found using search query of " + searchText);%>
			</td>
		</tr>
		<%
		}
	}
}
%>
</form>
</table>


