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 com.sixsq.slipstream.event.Event;
import com.sixsq.slipstream.exceptions.ValidationException;
import com.sixsq.slipstream.util.*;
import org.restlet.data.MediaType;
import org.restlet.data.Status;
import org.restlet.representation.Representation;
import org.restlet.representation.StringRepresentation;
import org.restlet.resource.Delete;
import org.restlet.resource.Get;
import org.restlet.resource.ResourceException;

import com.sixsq.slipstream.module.ModuleVersionView.ModuleVersionViewList;
import com.sixsq.slipstream.persistence.Module;
import com.sixsq.slipstream.resource.BaseResource;

public class ModuleVersionListResource extends BaseResource {

	private String resourceUri = null;

	@Override
	public void initialize() throws ResourceException {

		resourceUri = ResourceUriUtil.extractResourceUri(getRequest());

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

	@Override
	protected void authorize() {

		Module m = Module.loadLatest(resourceUri);

		if (getUser().isSuper()) {
			return;
		}

		if (m == null) {
			throwClientForbiddenError("Module doesn't exist. It may have been deleted");
		}else if (m.getAuthz().canGet(getUser())) {
			return;

		}

		throwClientForbiddenError("Not allowed to access: " + resourceUri);
	}

	@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, MediaType.APPLICATION_XML);
	}

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

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

		return new StringRepresentation(
				HtmlUtil.toHtml(list,
						getPageRepresentation(), getUser(), getRequest()),
				MediaType.TEXT_HTML);
	}

	@Delete
	public void deleteAllVersions() {

		List<ModuleVersionView> moduleVersions = Module.viewListAllVersions(resourceUri);

		for (ModuleVersionView moduleVersion: moduleVersions) {
			if(getUser().isSuper() || moduleVersion.getAuthz().canDelete(getUser())) {
				Module module = Module.load(moduleVersion.resourceUri);
				module.setDeleted(true);
				module.store(false);
			}
		}

		Module latest = Module.loadLatest(resourceUri);
		try {
			if (latest == null) {
				String redirectUri = ModuleUriUtil.extractParentUriFromResourceUri(resourceUri);
				redirectTo(ModuleUriUtil.extractParentUriFromResourceUri(redirectUri));
			} else {
				redirectTo(latest.getResourceUri());
			}
		} catch (ValidationException e) {
			throwClientConflicError(e.getMessage());
		}

		postEventDeleted(resourceUri);
	}

	private void redirectTo(String resourceUri) throws ValidationException {
		String absolutePath = RequestUtil.constructAbsolutePath(getRequest(), "/" + resourceUri);
		getResponse().setLocationRef(absolutePath);
		getResponse().setStatus(Status.SUCCESS_NO_CONTENT);
	}

	private void postEventDeleted(String moduleUri) {
		Event.postEvent(moduleUri, Event.Severity.high, "All versions deleted by '" + getUser().getName() + "'", getUser().getName(), Event.EventType.action);
	}

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

	protected String getPageRepresentation() {
		return "versions";
	}

}

@@ -988,16 +988,65 @@
 util.*;%0A
+import org.json.JSONObject;%0Aimport org.json.XML;%0A
 import o
@@ -2202,24 +2202,210 @@
 ceUri);%0A%09%7D%0A%0A
+%09private String toXmlString() %7B%0A%09%09ModuleVersionViewList list = new ModuleVersionViewList(%0A%09%09%09%09Module.viewListAllVersions(resourceUri));%0A%09%09return SerializationUtil.toXmlString(list);%0A%09%7D%0A%0A
 %09@Get(%22txt%22)
@@ -2438,17 +2438,16 @@
 Txt() %7B%0A
-%0A
 %09%09String
@@ -2610,149 +2610,173 @@
 ) %7B%0A
-%0A
 %09%09
-ModuleVersionViewList list = new ModuleVersionViewList(%0A%09%09%09%09Module.viewListAllVersions(resourceUri));%0A%09%09String result = SerializationUtil.
+return new StringRepresentation(toXmlString(), MediaType.APPLICATION_XML);%0A%09%7D%0A%0A%09@Get(%22json%22)%0A%09public Representation toJson() %7B%0A%09%09JSONObject obj = XML.toJSONObject(
 toXm
@@ -2779,28 +2779,25 @@
 toXmlString(
-list
+)
 );%0A%09%09return 
@@ -2825,14 +2825,22 @@
 ion(
-result
+obj.toString()
 , Me
@@ -2851,35 +2851,36 @@
 ype.APPLICATION_
-XML
+JSON
 );%0A%09%7D%0A%0A%09@Get(%22ht

Allow ModuleVersionList resource to be serialized as JSON (#1372)