package com.sixsq.slipstream.stats;

/*
 * +=================================================================+
 * 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.Arrays;
import java.util.List;

import org.restlet.data.MediaType;
import org.restlet.representation.Representation;
import org.restlet.representation.StringRepresentation;
import org.restlet.resource.Get;

import com.sixsq.slipstream.exceptions.AbortException;
import com.sixsq.slipstream.exceptions.ConfigurationException;
import com.sixsq.slipstream.exceptions.SlipStreamClientException;
import com.sixsq.slipstream.persistence.User;
import com.sixsq.slipstream.resource.BaseResource;
import com.sixsq.slipstream.util.SerializationUtil;

public class StatsResource extends BaseResource {

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

		String metadata = SerializationUtil.toXmlString(compute());
		return new StringRepresentation(metadata, MediaType.APPLICATION_XML);

	}

	private Measurements compute() {

		Measurements measurements = new Measurements();

		List<User> users = getUser().isSuper() ? User.list() : Arrays
				.asList(getUser());

		for (User user : users) {

			try {
				measurements.populate(user);
			} catch (SlipStreamClientException e) {
				throwClientConflicError(e.getMessage());
			} catch (ConfigurationException e) {
				throwServerError(e);
			} catch (AbortException e) {
				throwServerError(e);
			}

		}
		return measurements;
	}

	@Override
	protected String getPageRepresentation() {
		return "runs";
	}

}

@@ -1228,32 +1228,92 @@
 lientException;%0A
+import com.sixsq.slipstream.exceptions.ValidationException;%0A
 import com.sixsq
@@ -1342,16 +1342,16 @@
 e.User;%0A
-
 import c
@@ -1393,16 +1393,59 @@
 source;%0A
+import com.sixsq.slipstream.util.HtmlUtil;%0A
 import c
@@ -1489,16 +1489,16 @@
 nUtil;%0A%0A
-
 public c
@@ -1540,16 +1540,338 @@
 urce %7B%0A%0A
+%09@Get(%22html%22)%0A%09public Representation toHtml() throws ConfigurationException,%0A%09%09%09ValidationException %7B%0A%0A%09%09String metadata = SerializationUtil.toXmlString(compute());%0A%09%09return new StringRepresentation(HtmlUtil.toHtml(metadata,%0A%09%09%09%09getPageRepresentation(), getTransformationType(), getUser()),%0A%09%09%09%09MediaType.TEXT_HTML);%0A%0A%09%7D%0A%0A
 %09@Get(%22x

Remove support for html content-type