/**********************************************************************************
 * $URL$
 * $Id$
 ***********************************************************************************
 *
 * Copyright (c) 2003, 2004, 2005, 2006 The Sakai Foundation.
 *
 * Licensed under the Educational Community License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.opensource.org/licenses/ecl1.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 **********************************************************************************/

package uk.ac.cam.caret.sakai.rwiki.component.radeox.service.impl;

import org.radeox.engine.context.BaseRenderContext;
import org.sakaiproject.entity.api.Reference;
import org.sakaiproject.site.api.Site;
import org.sakaiproject.site.api.SiteService;

import uk.ac.cam.caret.sakai.rwiki.service.api.RWikiObjectService;
import uk.ac.cam.caret.sakai.rwiki.service.api.RWikiSecurityService;
import uk.ac.cam.caret.sakai.rwiki.service.api.model.RWikiObject;
import uk.ac.cam.caret.sakai.rwiki.service.api.radeox.CachableRenderContext;

/**
 * This class acts as a container for the Render context, making the RWiki
 * Object, the Security Service and the RWikiObject service available to the
 * Render Engine. If the operation is cachable, the RenderContext should return
 * true
 * 
 * @author andrew
 */
// FIXME: Component
public class SpecializedRenderContext extends BaseRenderContext implements
		CachableRenderContext
{

	/**
	 * Monitors cachable status
	 */
	private boolean cachable = true;

	private RWikiObjectService objectService;

	private RWikiObject rwikiObject;

	private RWikiSecurityService securityService;

	private SiteService siteService;

	public SpecializedRenderContext(RWikiObject rwikiObject,
			RWikiObjectService objectService,
			RWikiSecurityService securityService,
			SiteService siteService)
	{
		this.rwikiObject = rwikiObject;

		this.objectService = objectService;
		this.securityService = securityService;
		this.siteService = siteService;
	}

	public RWikiObject getRWikiObject()
	{
		return rwikiObject;
	}

	public void setRWikiObject(RWikiObject rwikiObject)
	{
		this.rwikiObject = rwikiObject;
	}

	public RWikiObjectService getObjectService()
	{
		return objectService;
	}

	public void setObjectService(RWikiObjectService objectService)
	{
		this.objectService = objectService;
	}

	public RWikiSecurityService getSecurityService()
	{
		return securityService;
	}

	public void setSecurityService(RWikiSecurityService securityService)
	{
		this.securityService = securityService;
	}

	/*
	 * public String getUser() { cachable = false; return user; }
	 */
	/**
	 * @return true if the render operation is cachable (after rendering)
	 */
	public boolean isCachable()
	{
		return cachable;
	}

	public Site getSite()
	{

		RWikiObjectService rwobjService = getObjectService();
		RWikiObject rwobj = getRWikiObject();
		Reference ref = rwobjService.getReference(rwobj);

		Site s = null;
		String siteId = getSiteId();
		if (siteId != null)
		{
			try
			{
				s = siteService.getSite(siteId);
			}
			catch (Exception ex)
			{
			}
		}
		return s;

	}

	public String getSiteId()
	{
		RWikiObjectService rwobjService = getObjectService();
		RWikiObject rwobj = getRWikiObject();
		Reference ref = rwobjService.getReference(rwobj);

		String siteContext = ref.getContext();
		if (siteContext.startsWith("/site/"))
		{
			String siteId = siteContext.substring(6);

			int slash = siteId.indexOf("/");
			if (slash != -1)
			{
				siteId = siteId.substring(0, slash - 1);
			}
			return siteId;
		}
		return null;
	}

}

@@ -1070,24 +1070,77 @@
 .Reference;%0A
+import org.sakaiproject.exception.IdUnusedException;%0A
 import org.s
@@ -2429,32 +2429,353 @@
  = siteService;%0A
+%09%09%0A%09%09this.set(RWikiObject.class.getName(), rwikiObject);%0A%09%09this.set(RWikiObject.class.getName().concat(%22.name%22), rwikiObject.getName());%0A%09%09this.set(RWikiObjectService.class.getName(), objectService);%0A%09%09this.set(RWikiSecurityService.class.getName(), securityService);%0A%09%09this.set(SiteService.class.getName(), siteService);%0A
 %09%7D%0A%0A%09public RWik
@@ -3547,175 +3547,8 @@
 )%0A%09%7B
-%0A%0A%09%09RWikiObjectService rwobjService = getObjectService();%0A%09%09RWikiObject rwobj = getRWikiObject();%0A%09%09Reference ref = rwobjService.getReference(rwobj);%0A%0A%09%09Site s = null;
 %0A%09%09S
@@ -3588,17 +3588,17 @@
 (siteId 
-!
+=
 = null)%0A
@@ -3606,26 +3606,49 @@
 %09%7B%0A%09
+%09%09return null;%0A%09%09%7D%0A%09%09%0A
 %09%09try%0A
-%09
 %09%09%7B%0A%09%09%09
-%09s =
+return
  sit
@@ -3675,20 +3675,18 @@
 eId);%0A%09%09
-%09
 %7D%0A
-%09
 %09%09catch 
@@ -3686,16 +3686,24 @@
 %09catch (
+IdUnused
 Exceptio
@@ -3714,32 +3714,29 @@
 )%0A%09%09
-%09
 %7B%0A%09%09%09
-%7D%0A%09%09%7D%0A%09%09return s;%0A
+return null;%0A%09%09%7D
 %0A%09%7D%0A

Clean up the code for getting the site id etc.