using System;
using System.IO;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using Plumtree.Remote.Portlet;
using Plumtree.Remote.PRC;
using Plumtree.Remote.PRC.Collaboration.Document;
using Plumtree.Remote.PRC.Collaboration.Project;
using IDocument = Plumtree.Remote.PRC.Collaboration.Document.IDocument;
using IDocumentManager = Plumtree.Remote.PRC.Collaboration.Document.IDocumentManager;

public class ProjectCreatorCS : Page
{
	protected LinkButton OKButton;
	protected System.Web.UI.HtmlControls.HtmlInputHidden sourcePage;

	IPortletContext portletContext;

	private void Page_Load(object sender, EventArgs e)
	{
		sourcePage.Value = Request["source"];
		portletContext = PortletContextFactory.CreatePortletContext(Request, Response);

		IProject sampleProject = null;
		sampleProject = QueryForSampleProject(portletContext);
		if(sampleProject != null)
		{
			Session[SESSION_PROJECT_KEY] = sampleProject;

			if(sourcePage.Value != null)
				Response.Redirect(sourcePage.Value);
			else
				portletContext.GetResponse().ReturnToPortal();
		}
	}

	#region Web Form Designer generated code
	override protected void OnInit(EventArgs e)
	{
		//
		// CODEGEN: This call is required by the ASP.NET Web Form Designer.
		//
		InitializeComponent();
		base.OnInit(e);
	}
	
	/// <summary>
	/// Required method for Designer support - do not modify
	/// the contents of this method with the code editor.
	/// </summary>
	private void InitializeComponent()
	{    
		this.OKButton.Click += new System.EventHandler(this.OKButton_Click);
		this.Load += new System.EventHandler(this.Page_Load);

	}
	#endregion

	private void OKButton_Click(object sender, EventArgs e)
	{
		StoreProject(portletContext);

		if(sourcePage.Value != null)
			Response.Redirect(sourcePage.Value);
		else
			portletContext.GetResponse().ReturnToPortal();
	}

	public const string SESSION_PROJECT_KEY = "edk_sample_project";
	public const string PROJECT_NAME = "EDK Sample Project";

	public IProject CreateProject(IRemoteSession remoteSession)
	{
		IProjectManager projectManager = remoteSession.GetCollaborationFactory().GetProjectManager();
		IProject project = projectManager.CreateProject(PROJECT_NAME, "Project created for EDK samples");
		project.Store();

		//create sample objects in project
		IDocumentManager documentManager = remoteSession.GetCollaborationFactory().GetDocumentManager();

		IDocumentFolder rootFolder = documentManager.GetTopLevelFolder(project);

		IDocumentFolder main1 = documentManager.CreateNewFolder("main folder 1", "");
		documentManager.InsertNewFolder(rootFolder, main1, true);
		IDocumentFolder main2 = documentManager.CreateNewFolder("main folder 2", "");
		documentManager.InsertNewFolder(rootFolder, main2, true);
		IDocumentFolder main3 = documentManager.CreateNewFolder("main folder 3", "");
		documentManager.InsertNewFolder(rootFolder, main3, true);

		IDocumentFolder sub21 = documentManager.CreateNewFolder("sub folder 2.1", "");
		documentManager.InsertNewFolder(main2, sub21, true);
		IDocumentFolder sub22 = documentManager.CreateNewFolder("sub folder 2.2", "");
		documentManager.InsertNewFolder(main2, sub22, true);
		IDocumentFolder sub23 = documentManager.CreateNewFolder("sub folder 2.3", "");
		documentManager.InsertNewFolder(main2, sub23, true);

		//insert some documents
		IDocument doc1 = documentManager.CreateNewDocument("document 1", "");
		documentManager.InsertNewDocument(main1, doc1, "", GetTemporaryInputStream(), null, true);
		IDocument doc2 = documentManager.CreateNewDocument("document 2", "");
		documentManager.InsertNewDocument(main2, doc2, "", GetTemporaryInputStream(), null, true);
		IDocument doc3 = documentManager.CreateNewDocument("document 3", "");
		documentManager.InsertNewDocument(sub21, doc3, "", GetTemporaryInputStream(), null, true);
		IDocument doc4 = documentManager.CreateNewDocument("document 4", "");
		documentManager.InsertNewDocument(sub21, doc4, "", GetTemporaryInputStream(), null, true);
		IDocument doc5 = documentManager.CreateNewDocument("document 5", "");
		documentManager.InsertNewDocument(sub22, doc5, "", GetTemporaryInputStream(), null, true);
		IDocument doc6 = documentManager.CreateNewDocument("document 6", "");
		documentManager.InsertNewDocument(sub23, doc6, "", GetTemporaryInputStream(), null, true);
		IDocument doc7 = documentManager.CreateNewDocument("document 7", "");
		documentManager.InsertNewDocument(sub23, doc7, "", GetTemporaryInputStream(), null, true);
		IDocument doc8 = documentManager.CreateNewDocument("document 8", "");
		documentManager.InsertNewDocument(sub23, doc8, "", GetTemporaryInputStream(), null, true);

		return project;
	}

	public Stream GetTemporaryInputStream()
	{
		Stream rv = new MemoryStream(Encoding.Default.GetBytes("Sample document for EDK example code."));
		return rv;
	}

	public IProject QueryForSampleProject(IPortletContext portletContext)
	{
		IRemoteSession remoteSession = portletContext.GetRemotePortalSession();
		IProjectManager projectManager = remoteSession.GetCollaborationFactory().GetProjectManager();
		IProjectFilter projectFilter = projectManager.CreateProjectFilter();
		projectFilter.NameSearchText = PROJECT_NAME;
		IProject[] sampleProjects = projectManager.QueryProjects(projectFilter);
		IProject sampleProject = null;
		    
		if(sampleProjects.Length > 1)
		{
			throw new ApplicationException("Found two projects called "+PROJECT_NAME+". Please remove one.");
		}
		else if(sampleProjects.Length == 0)
		{
			return null;
		}
		else
		{
			sampleProject = sampleProjects[0];
		}

		return sampleProject;
	}


	public void StoreProject(IPortletContext portletContext)
	{
		object retrievedProject = Session[SESSION_PROJECT_KEY];

		if(retrievedProject != null && retrievedProject is IProject)
			return;

		if(QueryForSampleProject(portletContext) == null)
			Session[SESSION_PROJECT_KEY] = CreateProject(portletContext.GetRemotePortalSession());
	}
}

