Subversion Repositories SmartDukaan

Rev

Rev 15192 | Rev 15272 | 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 UserMessagePojo getColorMessage() throws IOException, JSONException, URISyntaxException{
                try{
                        parseUrl();
                        if (!checkCache()){
                                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 (!color.isEmpty()){
                                                break;
                                        }
                                        try{
                                                JSONArray subAttributeArray = jsonArray.getJSONObject(element).getJSONArray("subAttributes");
                                                for (int innerElement=0; innerElement< subAttributeArray.length(); innerElement++){
                                                        if (supc.equalsIgnoreCase(subAttributeArray.getJSONObject(innerElement).getString("supc"))){
                                                                color = jsonArray.getJSONObject(element).getString("value");
                                                        }
                                                }
                                        }
                                        catch(Exception e1){
                                                e1.printStackTrace();
                                        }
                                        if (supc.equalsIgnoreCase(jsonArray.getJSONObject(element).getString("supc"))){
                                                color = jsonArray.getJSONObject(element).getString("value");
                                        }
                                }
                        }
                }
                catch (Exception e){
                        log.error("Error while getting product details " +e);
                }
                return getMsg();
        }

        private UserMessagePojo getMsg(){
                UserMessagePojo ump = new UserMessagePojo();
                if (color.isEmpty()){
                        ump.setResult(false);
                        ump.setMessage("");
                }
                else{
                        ump.setResult(true);
                        ump.setMessage("Please select "+color+" 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 = "aHR0cDovL20uc25hcGRlYWwuY29tL3Byb2R1Y3QvaHRjLWRlc2lyZS01MjYtZy82ODE1MDk5MjQyNDg/c3VwYz1TREw1MjU0MDI1OTYmdXRtX3NvdXJjZT1hZmZfcHJvZyZ1dG1fY2FtcGFpZ249YWZ0cyZvZmZlcl9pZD0xNyZhZmZfaWQ9MzM1NTAmYWZmX3N1Yj1TSEEzMTQzMjEwODk3MA==";
                String url = "aHR0cDovL20uc25hcGRlYWwuY29tL3Byb2R1Y3QvaHRjLWRlc2lyZS02MjYtZy82NDAwMzA4NDA3MTU/c3VwYz1TREw5MzU5MzI1NzgmdXRtX3NvdXJjZT1hZmZfcHJvZyZ1dG1fY2FtcGFpZ249YWZ0cyZvZmZlcl9pZD0xNyZhZmZfaWQ9MzM1NTAmYWZmX3N1Yj1TSEEzMTQzMjExMzkxNQ==";
                byte[] decoded = Base64.decode(url);
                System.out.println(new String(decoded));
                s.setUrl(url);
                System.out.println(s.getColorMessage().getMessage());
        }

}