package com.fielo.deploy.controller;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.codehaus.jackson.map.ObjectMapper;
import org.json.simple.parser.JSONParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fielo.deploy.utils.GithubUtil;
import com.force.sdk.connector.ForceServiceConnector;
import com.force.sdk.oauth.context.ForceSecurityContextHolder;
import com.force.sdk.oauth.context.SecurityContext;



@Controller
@RequestMapping("/home")
public class HomeController {
	
	@Autowired
    ServletContext context;
	
	HttpServletRequest servletRequest;
	ForceServiceConnector forceConnector;
	
		
	@RequestMapping(method = RequestMethod.GET, value="")
	public String confirm(HttpSession session, Map<String, Object> map) throws Exception {
		
		String reposList = "";
		File file = new File(context.getRealPath("/") + "Fielo-ProgramTypes.txt");
		if(!file.exists() || ((System.currentTimeMillis() - file.lastModified())/3600000) > 0){
			//llamo github
			reposList = new ObjectMapper().writeValueAsString(GithubUtil.getJsonFromGithub("Fielo-ProgramTypes"));
			
			if(!file.exists()){
				//creo arquivo
				file.createNewFile();
			}
			
			FileWriter fileWriter = new FileWriter(file, false);
            fileWriter.write(reposList);
            fileWriter.close();   
		}else{
			//get arquivo
			JSONParser parser = new JSONParser();
			try{
				reposList = parser.parse(new FileReader(file.getAbsolutePath())).toString().replace("\\","");
			}catch(java.io.FileNotFoundException e){
				System.out.println(e.getMessage());
			}
		}
		
		System.out.println(reposList);
		
		map.put("items", reposList);
		
		return "home";
	}
	
	


	@ResponseBody
	@RequestMapping(method = { RequestMethod.POST }, value="/checkcommunity")
	public String checkCommunity(HttpServletRequest request, HttpSession session) throws Exception
	{

		String flag = "false";
		
		 return flag;
	}

}

@@ -851,16 +851,79 @@
 ontext;%0A
+import com.force.api.ApiSession;%0Aimport com.force.api.ForceApi;
 %0A%0A%0A@Cont
@@ -2149,228 +2149,812 @@
 %0A%09%0A%09
-%0A%0A%0A%09@ResponseBody%0A%09@RequestMapping(method = %7B RequestMethod.POST %7D, value=%22/checkcommunity%22)%0A%09public String checkCommunity(HttpServletRequest request, HttpSession session) throws Exception%0A%09%7B%0A%0A%09%09String flag = %22false%22;%0A%09%09
+ private ForceApi getForceApi() %7B%0A%09%0A         SecurityContext sc = ForceSecurityContextHolder.get();%0A         ApiSession s = new ApiSession();%0A%0A         s.setAccessToken(sc.getSessionId());%0A         s.setApiEndpoint(sc.getEndPointHost());%0A%0A         return new ForceApi(s);%0A     %7D%0A%0A%0A%09@ResponseBody%0A%09@RequestMapping(method = %7B RequestMethod.POST %7D, value=%22/checkcommunity%22)%0A%09public String checkCommunity(HttpServletRequest request, HttpSession session) throws Exception%0A%09%7B%0A%0A%09%09String flag = %22false%22;%0A%09%09try%0A%09%09%7B%0A%09%09%09%0A%09%09%09String queryS = %22Select Id from Network%22;%0A%09%09%09com.force.api.QueryResult%3CMap%3E  result = getForceApi().query(queryS);%0A%09%09%09%0A%09%09%09if(result.getTotalSize()%3E0)%0A%09%09%09%7B%0A%09%09%09%09flag = %22true%22;%0A%09%09%09%7D%09%09%09%0A%09%09%09%0A%09%09%7D%0A%09%09catch(Exception ex)%0A%09%09%7B%0A%0A%09%09%09flag =%22false%22;%0A%09%09%09System.out.println(%22Exception in main : %22 + ex);%0A%09%09%7D%0A
 %0A%09%09 

check communities