Subversion Repositories SmartDukaan

Rev

Rev 15033 | Rev 15185 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.dtrapi.controllers;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.List;

import org.apache.axis.encoding.Base64;
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.whalin.MemCached.MemCachedClient;
import in.shop2020.dtrapi.services.MemCache;
import in.shop2020.dtrapi.services.UserMessagePojo;

public class SnapdealProductPageParserController extends BaseController{

    private static Logger log = Logger.getLogger(Class.class);
    private static final long serialVersionUID = 1L;

    private String url;
    private String supc = "";
    private String productUrl = "";
    private String color = "";
    private static final String INDEX = "index";
    private static final String KEY = "snapdealSupcToColorMapping";
    private static MemCache memCache = new MemCache();
    private static MemCachedClient memcachedClient = memCache.getClient();
    
    

    public String getColorMessage() throws IOException, JSONException, URISyntaxException{
        try{
            parseUrl();
            if (!checkCache()){
                System.out.println(productUrl);
                Document doc = Jsoup.connect(productUrl).get();
                doc.outputSettings().charset("UTF-8");
                JSONArray jsonArray = new JSONArray(doc.getElementById("productAttributesJson").attr("value"));
                for (int element=0; element<jsonArray.length();element++){
                    if (supc.equalsIgnoreCase(jsonArray.getJSONObject(element).getString("supc"))){
                        color = jsonArray.getJSONObject(element).getString("value");
                        populateCache(supc, color);
                    }
                }
            }
        }
        catch (Exception e){
            log.error("Error while getting product details " +e);
        }
        setResultJson(getMsg());
        return INDEX;

    }
    
    private UserMessagePojo getMsg(){
        UserMessagePojo ump = new UserMessagePojo();
        if (color.isEmpty()){
                ump.setResult(false);
                ump.setMessage("");
        }
        else{
                ump.setResult(true);
                ump.setMessage("Please select "+color+" to get this price");
        }
                return ump;
        
    }
    
    private String getJavaScriptCode(){
        String jsCode = "javascript:try{var ele=document.getElementById('attribute-select-0');ele.value ="+"'"+color+"'"+";ele.onchange();}catch(error){Android.onError(error.message);}";
        return jsCode;
    }

    private void parseUrl() throws URISyntaxException{
        List<NameValuePair> params = URLEncodedUtils.parse(new URI(url), "UTF-8");
        for (NameValuePair param : params){
            if (param.getName().equalsIgnoreCase("supc")){
                supc = param.getValue();
            }
        }
        productUrl = new URI(url).getHost()+new URI(url).getPath();
        if (!productUrl.startsWith("http")){
            productUrl = "http://"+productUrl;
        }
    }
    
    private void populateCache(String supc, String color){
        System.out.println("populating cache");
        Object cacheValue = memcachedClient.get(KEY);
        HashMap<String, String> supcMap = new HashMap<String, String>();
        if (cacheValue != null) {
            supcMap=(HashMap<String, String>)cacheValue;
        }
        supcMap.put(supc, color);
        memcachedClient.set(KEY, supcMap);
    }

    private boolean checkCache(){
        if (supc!=null){
            Object cacheValue = memcachedClient.get(KEY);
            if (cacheValue != null) {
                System.out.println("cache value is not null");
                HashMap<String, String> supcMap = new HashMap<String, String>();
                supcMap=(HashMap<String, String>)cacheValue;
                System.out.println("supc map "+supcMap);
                color = supcMap.get(supc);
                System.out.println("color "+color);
                if (!(color==null || color.isEmpty())){
                    return true;
                }
            }
        }
        return false;
    }

    public void setUrl(String url) {
        byte[] decoded = Base64.decode(url);
        this.url = new String(decoded);
    }

    public String getUrl() {
        return url;
    }

    public static void main(String[] args) throws URISyntaxException, IOException, JSONException{
        SnapdealProductPageParserController s = new SnapdealProductPageParserController();
        String url = "http://m.snapdeal.com/product/samsung-galaxy-grand-max/647437791381?supc=SDL021483758&aff_id=1&bhalala=0";
        s.setUrl(Base64.encode(url.getBytes()));
        s.getColorMessage();
        System.out.println(s.getResultJson());
    }

}