package com.sixsq.slipstream.module;

/*
 * +=================================================================+
 * SlipStream Server (WAR)
 * =====
 * Copyright (C) 2013 SixSq Sarl (sixsq.com)
 * =====
 * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
 * 
 * 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.
 * -=================================================================-
 */

import java.util.List;

import org.restlet.Request;
import org.restlet.data.Cookie;
import org.restlet.data.Status;
import org.restlet.representation.Representation;
import org.restlet.representation.StringRepresentation;
import org.restlet.resource.Get;
import org.restlet.resource.ResourceException;

import com.sixsq.slipstream.configuration.Configuration;
import com.sixsq.slipstream.cookie.CookieUtils;
import com.sixsq.slipstream.exceptions.SlipStreamInternalException;
import com.sixsq.slipstream.module.ModuleVersionView.ModuleVersionViewList;
import com.sixsq.slipstream.persistence.Module;
import com.sixsq.slipstream.persistence.User;
import com.sixsq.slipstream.util.HtmlUtil;
import com.sixsq.slipstream.util.RequestUtil;
import com.sixsq.slipstream.util.SerializationUtil;

public class ModuleVersionListResource extends ModuleListResourceBase {

	private Configuration configuration = null;

	private User user = null;

	private String baseUrlSlash = null;

	private String resourceUri = null;

	@Override
	public void doInit() throws ResourceException {

		Request request = getRequest();

		Cookie cookie = CookieUtils.extractAuthnCookie(request);
		String username = CookieUtils.getCookieUsername(cookie);

		user = User.loadByName(username);

		// FIXME: Add authorization.

		configuration = RequestUtil.getConfigurationFromRequest(request);
		if (configuration == null) {
			throw new SlipStreamInternalException("configuration is null");
		}

		baseUrlSlash = RequestUtil.getBaseUrlSlash(request);

		resourceUri = RequestUtil.extractResourceUri(getRequest());

		if (resourceUri == null) {
			throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST);
		}
	}

	@Get("txt")
	public Representation toTxt() {

		String viewList = serialized(Module.viewListAllVersions(resourceUri));
		return new StringRepresentation(viewList);
	}

	@Get("xml")
	public Representation toXml() {

		ModuleVersionViewList list = new ModuleVersionViewList(
				Module.viewListAllVersions(resourceUri));
		String result = SerializationUtil.toXmlString(list);
		return new StringRepresentation(result);
	}

	@Get("html")
	public Representation toHtml() {

		ModuleVersionViewList list = new ModuleVersionViewList(
				Module.viewListAllVersions(resourceUri));

		return HtmlUtil.transformToHtml(baseUrlSlash, resourceUri,
				configuration.version, getViewStylesheet(), user, list,
				getChooser());
	}

	private String serialized(List<ModuleVersionView> viewList) {
		ModuleVersionViewList moduleViewList = new ModuleVersionViewList(
				viewList);
		return SerializationUtil.toXmlString(moduleViewList);
	}

	protected String getViewStylesheet() {
		return "module-list.xsl";
	}

}

@@ -913,16 +913,51 @@
 Cookie;%0A
+import org.restlet.data.MediaType;%0A
 import o
@@ -980,16 +980,16 @@
 Status;%0A
-
 import o
@@ -1165,16 +1165,45 @@
 ception;
+%0Aimport org.w3c.dom.Document;
 %0A%0Aimport
@@ -1543,51 +1543,8 @@
 er;%0A
-import com.sixsq.slipstream.util.HtmlUtil;%0A
 impo
@@ -1636,16 +1636,58 @@
 ionUtil;
+%0Aimport com.sixsq.slipstream.util.XmlUtil;
 %0A%0Apublic
@@ -1831,46 +1831,8 @@
 l;%0A%0A
-%09private String baseUrlSlash = null;%0A%0A
 %09pri
@@ -2323,64 +2323,8 @@
 %09%7D%0A%0A
-%09%09baseUrlSlash = RequestUtil.getBaseUrlSlash(request);%0A%0A
 %09%09re
@@ -3066,143 +3066,327 @@
 %0A%0A%09%09
-return HtmlUtil.transformToHtml(baseUrlSlash, resourceUri,%0A%09%09%09%09configuration.version, getViewStylesheet(), user, list,%0A%09%09%09%09getChooser()
+Document doc = SerializationUtil.toXmlDocument(list);%0A%0A%09%09XmlUtil.addUser(doc, user);%0A%0A%09%09String metadata = SerializationUtil.documentToString(doc);%0A%0A%09%09return new StringRepresentation(%0A%09%09%09%09slipstream.ui.views.Representation.toHtml(metadata,%0A%09%09%09%09%09%09getPageRepresentation(), getTransformationType()),%0A%09%09%09%09MediaType.TEXT_HTML
 );%0A%09
@@ -3619,52 +3619,49 @@
  get
-ViewStylesheet() %7B%0A%09%09return %22module-list.xsl
+PageRepresentation() %7B%0A%09%09return %22versions
 %22;%0A%09

Connect html generation to new Enlive class