package org.fiware.apps.marketplace.webcontrollers;

/*
 * #%L
 * FiwareMarketplace
 * %%
 * Copyright (C) 2015 CoNWeT Lab, Universidad Politécnica de Madrid
 * %%
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 3. Neither the name of copyright holders nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 * #L%
 */

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.core.Response.Status;

import org.fiware.apps.marketplace.bo.OfferingBo;
import org.fiware.apps.marketplace.exceptions.DescriptionNotFoundException;
import org.fiware.apps.marketplace.exceptions.NotAuthorizedException;
import org.fiware.apps.marketplace.exceptions.OfferingNotFoundException;
import org.fiware.apps.marketplace.exceptions.StoreNotFoundException;
import org.fiware.apps.marketplace.exceptions.UserNotFoundException;
import org.fiware.apps.marketplace.model.Offering;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.ui.ModelMap;
import org.springframework.web.servlet.ModelAndView;


@Component
@Path("offerings")
public class OfferingController extends AbstractController {

    @Autowired private OfferingBo offeringBo;

    private static Logger logger = LoggerFactory.getLogger(OfferingController.class);

    @GET
    @Produces(MediaType.TEXT_HTML)
    public Response listView() {

        ModelAndView view;
        ModelMap model = new ModelMap();
        ResponseBuilder builder;

        try {
            model.addAttribute("user", getCurrentUser());
            model.addAttribute("title", "Catalogue - " + getContextName());

            view = new ModelAndView("offering.list", model);
            builder = Response.ok();
        } catch (UserNotFoundException e) {
            logger.warn("User not found", e);

            view = buildErrorView(Status.INTERNAL_SERVER_ERROR, e.getMessage());
            builder = Response.serverError();
        }

        return builder.entity(view).build();
    }

    @GET
    @Produces(MediaType.TEXT_HTML)
    @Path("{storeName}/{descriptionName}/{offeringName}")
    public Response detailView(
            @PathParam("storeName") String storeName,
            @PathParam("descriptionName") String descriptionName,
            @PathParam("offeringName") String offeringName) {

        ModelAndView view;
        ModelMap model = new ModelMap();
        Offering offering;
        ResponseBuilder builder;

        try {
            model.addAttribute("user", getCurrentUser());

            offering = offeringBo.findOfferingByNameStoreAndDescription(
                    storeName, descriptionName, offeringName);

            model.addAttribute("offering", offering);
            model.addAttribute("title", offering.getDisplayName() + " - " + getContextName());

            view = new ModelAndView("offering.detail", model);
            builder = Response.ok();
        } catch (UserNotFoundException e) {
            logger.warn("User not found", e);

            view = buildErrorView(Status.INTERNAL_SERVER_ERROR, e.getMessage());
            builder = Response.serverError();
        } catch (NotAuthorizedException e) {
            logger.info("User unauthorized", e);

            view = buildErrorView(Status.UNAUTHORIZED, e.getMessage());
            builder = Response.status(Status.UNAUTHORIZED);
        } catch (OfferingNotFoundException e) {
            logger.info("Offering not found", e);

            view = buildErrorView(Status.NOT_FOUND, e.getMessage());
            builder = Response.status(Status.NOT_FOUND);
        } catch (StoreNotFoundException e) {
            logger.info("Store not found", e);

            view = buildErrorView(Status.NOT_FOUND, e.getMessage());
            builder = Response.status(Status.NOT_FOUND);
        } catch (DescriptionNotFoundException e) {
            logger.info("Description not found", e);

            view = buildErrorView(Status.NOT_FOUND, e.getMessage());
            builder = Response.status(Status.NOT_FOUND);
        }

        return builder.entity(view).build();
    }
}

@@ -1675,16 +1675,101 @@
 L%25%0A */%0A%0A
+import javax.servlet.http.HttpServletRequest;%0Aimport javax.servlet.http.HttpSession;%0A
 import j
@@ -1868,16 +1868,49 @@
 oduces;%0A
+import javax.ws.rs.core.Context;%0A
 import j
@@ -2521,16 +2521,63 @@
 fering;%0A
+import org.fiware.apps.marketplace.model.User;%0A
 import o
@@ -3131,20 +3131,125 @@
 istView(
-) %7B%0A
+%0A            @Context HttpServletRequest request) %7B%0A%0A        Boolean wasCreated;%0A        HttpSession session;
 %0A       
@@ -3333,32 +3333,51 @@
 Builder builder;
+%0A        User user;
 %0A%0A        try %7B%0A
@@ -3368,32 +3368,70 @@
 %0A%0A        try %7B%0A
+            user = getCurrentUser();%0A%0A
             mode
@@ -3453,32 +3453,20 @@
 %22user%22, 
-getCurrentU
+u
 ser
-()
 );%0A     
@@ -3529,32 +3529,447 @@
 ontextName());%0A%0A
+            session = request.getSession();%0A%0A            synchronized (session) %7B%0A                wasCreated = (Boolean) session.getAttribute(%22created%22);%0A%0A                if (wasCreated != null) %7B%0A                    model.addAttribute(%22message%22, %22Hi '%22 + user.getDisplayName() + %22', your account was created successfully.%22);%0A                    session.removeAttribute(%22created%22);%0A                %7D%0A            %7D%0A%0A
             view

Add in OfferingListView the access to user created prop from requestSession