package com.sixsq.slipstream.resource;

/*
 * +=================================================================+
 * 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 org.restlet.data.Form;
import org.restlet.data.MediaType;
import org.restlet.representation.Representation;
import org.restlet.representation.StringRepresentation;
import org.restlet.resource.Get;
import org.restlet.resource.Put;
import org.restlet.resource.ResourceException;

import com.sixsq.slipstream.exceptions.BadlyFormedElementException;
import com.sixsq.slipstream.exceptions.ConfigurationException;
import com.sixsq.slipstream.exceptions.SlipStreamClientException;
import com.sixsq.slipstream.exceptions.ValidationException;
import com.sixsq.slipstream.persistence.ServiceCatalog;
import com.sixsq.slipstream.persistence.ServiceCatalogs;
import com.sixsq.slipstream.util.HtmlUtil;
import com.sixsq.slipstream.util.SerializationUtil;

public class ServiceCatalogsResource extends SimpleResource {

	private ServiceCatalogs sc = new ServiceCatalogs();

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

		String result = null;
		try {
			result = SerializationUtil.toXmlString(retrieveServiceCatalogs());
		} catch (ValidationException e) {
			throwClientValidationError(e.getMessage());
		}
		return new StringRepresentation(result);
	}

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

		StringRepresentation result = null;

		try {
			result = new StringRepresentation(HtmlUtil.toHtml(
					retrieveServiceCatalogs(), getPageRepresentation(),
					getTransformationType(), getUser()), MediaType.TEXT_HTML);
		} catch (ValidationException e) {
			throwClientValidationError(e.getMessage());
		}
		return result;
	}

	public ServiceCatalogs retrieveServiceCatalogs() throws ValidationException {

		ServiceCatalogs scs = new ServiceCatalogs();

		for (ServiceCatalog sc : ServiceCatalog.listall()) {
			sc.populateDefinedParameters();
			scs.getList().add(sc);
		}

		return scs;

	}

	@Put("form")
	public void updateFromForm(Representation entity) throws ResourceException {

		checkIsSuper();

		if (entity == null) {
			throwClientBadRequest("Empty form");
		}

		try {
			processEntityAsForm(entity);
		} catch (ValidationException e) {
			throwClientValidationError(e.getMessage());
		}

		sc.store();

		getResponse().redirectSeeOther("/service_catalog");
	}

	public void processEntityAsForm(Representation entity)
			throws ResourceException, ConfigurationException,
			ValidationException {

		Form form = extractFormFromEntity(entity);

		for (ServiceCatalog s : sc.getList()) {

			ServiceCatalogFormProcessor processor = new ServiceCatalogFormProcessor(
					getUser(), s.getCloud());

			try {
				processor.processForm(form);
			} catch (BadlyFormedElementException e) {
				throwClientError(e);
			} catch (SlipStreamClientException e) {
				throwClientError(e);
			}

			ServiceCatalog proposedServiceCatalog = processor.getParametrized();

			sc.update(proposedServiceCatalog);
		}
	}

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

}

@@ -2745,77 +2745,8 @@
 );%0A%0A
-%09%09if (entity == null) %7B%0A%09%09%09throwClientBadRequest(%22Empty form%22);%0A%09%09%7D%0A%0A
 %09%09tr
@@ -3090,16 +3090,48 @@
 m form =
+ (entity == null) ? new Form() :
  extract

Allow empty form