package org.springcommerce.controller;

import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springcommerce.catalog.domain.CatalogItem;
import org.springcommerce.catalog.domain.ItemAttribute;
import org.springcommerce.catalog.domain.SellableItem;
import org.springcommerce.catalog.service.CatalogService;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class SellableItemFormController extends SimpleFormController {
    protected final Log logger = LogFactory.getLog(getClass());
    private CatalogService catalogService;

    public void setCatalogService(CatalogService catalogService) {
        this.catalogService = catalogService;
    }

    @Override
	protected Object formBackingObject(HttpServletRequest request)
                                throws ServletException {
        CatalogItem createCatalogItem = new CatalogItem();
        SellableItem sellableItem = new SellableItem();

        if (request.getParameter("catalogItemId") != null) {
            createCatalogItem = catalogService.readCatalogItemById(Long.valueOf(request.getParameter("catalogItemId")));
            sellableItem.setCatalogItem(createCatalogItem);
        }
        
        if (request.getParameter("sellableItemId") != null){
        	sellableItem = catalogService.readSellableItemById(new Long(request.getParameter("sellableItemId")));        	
        	Set<ItemAttribute> attributes = sellableItem.getItemAttributes();
        }

        return sellableItem;
    }

    @Override
    protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)
                             throws Exception {
        SellableItem sellableItem = (SellableItem) command;

        ModelAndView mav = new ModelAndView(getSuccessView(), errors.getModel());

        if (errors.hasErrors()) {
            logger.debug("Error returning back to the form");

            return showForm(request, response, errors);
        }

        
        
        catalogService.saveSellableItem(sellableItem);
        mav.addObject("saved", true);

        return mav;
    }
}

@@ -39,33 +39,8 @@
 %0D%0A%0D%0A
-import java.util.Set;%0D%0A%0D%0A
 impo
@@ -318,65 +318,8 @@
 m;%0D%0A
-import org.springcommerce.catalog.domain.ItemAttribute;%0D%0A
 impo
@@ -927,17 +927,20 @@
 erride%0D%0A
-%09
+    
 protecte
@@ -997,41 +997,8 @@
 est)
-%0D%0A                               
  thr
@@ -1396,24 +1396,16 @@
      %7D%0D%0A
-        
 %0D%0A      
@@ -1457,16 +1457,17 @@
 != null)
+ 
 %7B%0D%0A     
@@ -1469,17 +1469,20 @@
         
-%09
+    
 sellable
@@ -1574,25 +1574,16 @@
 mId%22)));
-        %09
 %0D%0A      
@@ -1588,9 +1588,15 @@
     
-%09
+    // 
 Set%3C
@@ -1860,38 +1860,8 @@
 ors)
-%0D%0A                            
  thr
@@ -2199,28 +2199,8 @@
 %0D%0A%0D%0A
-        %0D%0A        %0D%0A
     

fix warnings