Subversion Repositories SmartDukaan

Rev

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

package in.shop2020.util;

import in.shop2020.metamodel.util.CreationUtils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.net.URL;
import java.net.URLConnection;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class MostFrequentlySearchedKeywords implements Serializable{
        
        public static final String GOOGLE_SEARCH_DATA_URL = "http://saholic-datastore.appspot.com/jsp/export-search-activity.jsp";
        /**
         * 
         */
        private static final long serialVersionUID = 8355859683013334832L;
        private SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
                //removeLastDate process the data and store it for further computations

        private void fetchAndSave(){
                Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("IST"));
                cal.add(Calendar.DATE, -1);
                Date lastDate = cal.getTime();
                //iterate through map for each date and generate computations
                String data = sendGetRequest(GOOGLE_SEARCH_DATA_URL, null);
                try {
                        JSONArray jsonArray = new JSONArray(data);
                        List<String> searchStats = new ArrayList<String>();
                        for(int i=0; i<jsonArray.length(); i++){
                                JSONObject json = jsonArray.getJSONObject(i);
                                String queryString = json.getString("queryString");
                                Double searchConvRate = Double.parseDouble(json.getString("searchConvRate"));
                                //filter all numberic strings
                                if(!isDoubleNumber(queryString)  && searchConvRate.compareTo(0d) > 0){
                                        searchStats.add(queryString);
                                }
                        }
                        CreationUtils.storeSearchStats(sdf.format(lastDate), searchStats);
                        cal.add(Calendar.DATE, -30);
                        //Remove data for last date
                        CreationUtils.deleteSearchStats(sdf.format(cal.getTime()));
                } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
        }
                
                //Get all the search terms used in last 30 days and compute them
        private List<String> getAllSearchedTerms(){
                Set<String> set = new HashSet<String>();
                Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("IST"));
                List<String> searchTerms=null;
                int i=1;
                while(i<=30){
                        cal.add(Calendar.DATE, -(i++));
                        try {
                                searchTerms = CreationUtils.getSearchStats(sdf.format(cal.getTime()));
                        } catch (Exception e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                                continue;
                        }
                        if(searchTerms != null){
                                set.addAll(searchTerms);
                        } else continue;
                }
                
                return new ArrayList<String>(set);
        }
        
        public void generate(){
                fetchAndSave();
                String alphabeticalTemplate = "Search keywords starting with <b>{0}</b>";
                String levelOneTitle = "Most Frequently Searched";
                String levelTwoTitle = "";
                List<String> list = this.getAllSearchedTerms();
                if(list != null && !list.isEmpty()){
                        Map<String, List<List<String>>> map = new HashMap<String, List<List<String>>>();
                        for(String keyword : list){
                                String startsWith = keyword.substring(0,1).toLowerCase();
                                List<List<String>> map1;
                                if(map.containsKey(startsWith)){
                                        map1 = map.get(startsWith);
                                }else {
                                        map1 = new ArrayList<List<String>>();
                                        map.put(startsWith, map1);
                                }
                                List<String> l1 = new ArrayList<String>();
                                l1.add(keyword);
                                l1.add("/search?q=" + keyword.replaceAll(" +", "+") + "&category=10000");
                                map1.add(l1);
                        }
                        LevelHierarchy lh = new LevelHierarchy(alphabeticalTemplate, levelOneTitle, levelTwoTitle, map);
                        lh.generatePages();
                }
        }
        
        public boolean isDoubleNumber(String num) {
            try {
                Double.parseDouble(num);
            } catch(NumberFormatException nfe) {
                return false;
                }
            return true;
    }
        
        /**
         * 
         * @param endpoint
         * @param requestParameters
         * @return
         */
         private String sendGetRequest(String endpoint, String requestParameters)
     {
             String result = null;
             try
             {
                     String urlStr = endpoint;
                     if (requestParameters != null && requestParameters.length () > 0){
                             urlStr += "?" + requestParameters;
                     }
                     URL url = new URL(urlStr);
                     URLConnection conn = url.openConnection ();

                     // Get the response
                     BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                     StringBuffer sb = new StringBuffer();
                     String line;
                     while ((line = rd.readLine()) != null)
                     {
                             sb.append(line);
                     }
                     rd.close();
                     result = sb.toString();
             } catch (Exception e){
                     e.printStackTrace();
             }

             return result;
     }
         
         public static void main(String [] args) throws ParseException {
                 MostFrequentlySearchedKeywords mfsk = new MostFrequentlySearchedKeywords();
                 mfsk.generate();
/*               
                    
                    
                        SimpleDateFormat iSdf = new SimpleDateFormat("yyyyMMdd");
                        iSdf.setTimeZone(TimeZone.getTimeZone("IST"));
                        Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("IST"));
                        String dateStr = "20120101";
                        Date date = iSdf.parse(dateStr);
                        System.out.println(date);
                        
                        cal.add(Calendar.DATE, -1);
                            date = iSdf.parse(iSdf.format(cal.getTime()));
                            System.out.println(date);*/
                        
         }
}