package org.xcolab.portlets.proposals.view;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;

import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.servlet.http.Cookie;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.xcolab.portlets.proposals.utils.ProposalsContext;

import com.ext.portlet.models.CollaboratoriumModelingService;
import com.ext.portlet.service.ContestLocalServiceUtil;
import com.ext.portlet.service.ModelRunnerServiceUtil;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;

@Controller
@RequestMapping("view")
public class ContestModelController extends BaseProposalsController {
    private final static String COOKIE_PREFERRED_MODEL = "cc_contests_preferredModels";
    
    @Autowired
    private ProposalsContext proposalsContext;

    @RequestMapping(params = "pageToDisplay=contestModel")
    public String showContestProposals(RenderRequest request, RenderResponse response, Model model, @RequestParam(required = false) boolean refreshModels) 
            throws PortalException, SystemException, IOException {
    	
    	if (refreshModels) {
    		ModelRunnerServiceUtil.refreshModels();
    	}
    	Long contestPK = proposalsContext.getContest(request).getContestPK();
    	Long modelId = ContestLocalServiceUtil.getDefaultModelId(contestPK);
    	

        for (Cookie cookie: request.getCookies()) {
            if (cookie.getName().equals(COOKIE_PREFERRED_MODEL)) {
            	try {
            		JsonElement element = new JsonParser().parse(URLDecoder.decode(cookie.getValue()));
            		JsonObject object = element.getAsJsonObject();
            		if (object.has(String.valueOf(proposalsContext.getContest(request).getContestPK()))) {
            			modelId = object.get(String.valueOf(contestPK)).getAsLong();
            		}
            				
            	}
            	catch (Exception e) {
            		// just ignore
            	}
            } 
        }
    	
    	
    	model.addAttribute("modelId", modelId);
    	if (modelId != null) {
        	Map<Long, String> modelIdsWithNames = ContestLocalServiceUtil.getModelIdsAndNames(proposalsContext.getContest(request).getContestPK());
        	
        	model.addAttribute("availableModels", modelIdsWithNames);
    		
    	}        
        return "contestModel";
        
    }
}

@@ -90,24 +90,50 @@
 URLDecoder;%0A
+import java.util.HashMap;%0A
 import java.
@@ -1788,16 +1788,382 @@
 estPK);%0A
+    %09Map%3CLong, String%3E modelIdsWithNames = null;%0A    %09if (modelId != null) %7B%0A        %09modelIdsWithNames = ContestLocalServiceUtil.getModelIdsAndNames(proposalsContext.getContest(request).getContestPK());%0A        %09%0A        %09model.addAttribute(%22availableModels%22, modelIdsWithNames);%0A    %09%7D    %0A    %09else %7B%0A    %09%09modelIdsWithNames = new HashMap%3CLong, String%3E();%0A    %09%7D%0A
     %09%0A%0A 
@@ -2574,17 +2574,47 @@
      %09%09%09
-m
+%0A            %09%09%09long preferredM
 odelId =
@@ -2683,26 +2683,107 @@
   %09%09
-%7D%0A            %09%09%09%09
+%09if (modelIdsWithNames.containsKey(preferredModelId)) %7B%0A            %09%09%09%09modelId = preferredModelId;
 %0A   
@@ -2784,32 +2784,34 @@
 d;%0A            %09
+%09%09
 %7D%0A            %09c
@@ -2813,58 +2813,43 @@
    %09
-catch (Exception e) %7B%0A            %09%09// just ignore
+%09%09%0A            %09%09%7D%0A            %09%09%09%09
 %0A   
@@ -2876,255 +2876,100 @@
     
-%7D %0A        %7D%0A    %09
+%09catch (Exception e) %7B
 %0A    
-%09%0A
+   
     
-%09model.addAttribute(%22modelId%22, modelId);%0A    %09if (modelId != null) %7B%0A        %09Map%3CLong, String%3E modelIdsWithNames = ContestLocalServiceUtil.getModelIdsAndNames(proposalsContext.getContest(request).getContestPK());
+ %09%09// just ignore%0A            %09%7D%0A            %7D 
 %0A    
+    %7D%0A
     
@@ -2966,32 +2966,34 @@
     %7D%0A    %09%0A    
+%09%0A
     %09model.addAt
@@ -2997,39 +2997,31 @@
 dAttribute(%22
-availableM
+m
 odel
-s
+Id
 %22, modelIdsW
@@ -3022,42 +3022,10 @@
 elId
-sWithNames);%0A    %09%09%0A    %09%7D        
+);
 %0A   

Fix to contest model selection