Subversion Repositories SmartDukaan

Rev

Rev 5017 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 5017 Rev 5084
Line 1... Line 1...
1
package in.shop2020.serving.services;
1
package in.shop2020.serving.services;
2
 
2
 
3
 
3
 
4
import in.shop2020.model.v1.catalog.InventoryService.Client;
-
 
5
import in.shop2020.model.v1.catalog.InventoryServiceException;
-
 
6
import in.shop2020.model.v1.catalog.status;
4
import in.shop2020.serving.utils.Utils;
7
import in.shop2020.model.v1.catalog.Item;
5
import java.io.File;
8
import in.shop2020.thrift.clients.CatalogClient;
-
 
9
 
-
 
10
import java.util.ArrayList;
6
import java.util.ArrayList;
11
import java.util.HashSet;
7
import java.util.HashSet;
12
import java.util.List;
8
import java.util.List;
-
 
9
import java.util.Map;
13
import java.util.Set;
10
import java.util.Set;
14
 
11
 
-
 
12
import org.apache.commons.io.FileUtils;
15
import org.apache.log4j.Logger;
13
import org.apache.log4j.Logger;
-
 
14
 
16
import org.apache.thrift.TException;
15
import com.google.gson.Gson;
17
import org.apache.thrift.transport.TTransportException;
16
import com.google.gson.reflect.TypeToken;
18
 
17
 
19
/**
18
/**
20
 * 
19
 * 
21
 * @author rajveer
20
 * @author rajveer
22
 * 
21
 * 
Line 32... Line 31...
32
		synchronized(AutoSuggestService.class){
31
		synchronized(AutoSuggestService.class){
33
			autoSuggestService = new AutoSuggestService();
32
			autoSuggestService = new AutoSuggestService();
34
			autoSuggestService.initialize();
33
			autoSuggestService.initialize();
35
		}
34
		}
36
	}
35
	}
-
 
36
	
37
	private void initialize() {
37
	private void initialize() {
38
		log.info("Starting instance of Autosuggest service");
38
		log.info("Starting instance of Autosuggest service");
39
		CatalogClient catalogServiceClient;
-
 
40
		try {
39
		try {
41
			catalogServiceClient = new CatalogClient();
40
			Set<String> titleSet = new HashSet<String>();
42
			Client client = catalogServiceClient.getClient();
41
			Gson g = new Gson();
43
	        List<Item> items = new ArrayList<Item>();
42
			java.lang.reflect.Type typeOfT = new TypeToken<Map<Long, List<String>>>(){}.getType();
44
	        items.addAll(client.getAllItemsByStatus(status.ACTIVE));
43
			Map<Long, List<String>> synonyms = g.fromJson(FileUtils.readFileToString(new File(Utils.EXPORT_JAVASCRIPT_CONTENT_PATH + "autosuggest.json")), typeOfT);
45
	        items.addAll(client.getAllItemsByStatus(status.PAUSED));
44
			if(synonyms != null){
46
	        items.addAll(client.getAllItemsByStatus(status.PAUSED_BY_RISK));
-
 
47
	        Set<Long> processedSet = new HashSet<Long>();
45
				for(List<String> entry : synonyms.values()){
48
	        for(Item item: items){
46
					for(String title : entry){
49
	        	if(!processedSet.contains(item.getCatalogItemId())){
-
 
50
	        		processedSet.add(item.getCatalogItemId());
47
						titleSet.add(title);
51
	        		addItemName(AutoSuggestService.getProductTitle(item));
-
 
52
	        	}
48
					}
53
	        }
49
				}
54
		} catch (TTransportException e) {
50
				List<String> titlelist = new ArrayList<String>(titleSet);
55
			log.error("Error while making thrift connection", e);
51
				java.util.Collections.sort(titlelist);
56
		} catch (InventoryServiceException e) {
52
				for(String title : titlelist){
57
			log.error("Inventory Service exception", e);
53
					addItemName(title);
-
 
54
				}
-
 
55
			}
58
		} catch (TException e) {
56
		} catch (Exception e) {
59
			log.error("Thrift exception", e);
57
			log.error("", e);
60
		}
58
		}
61
		log.info("Done with starting Autosuggest service");
59
		log.info("Done with starting Autosuggest service");
62
	}
60
	}
63
 
61
 
64
	public static AutoSuggestService getAutoSuggestServiceInstance(){
62
	public static AutoSuggestService getAutoSuggestServiceInstance(){
65
		return autoSuggestService;
63
		return autoSuggestService;
66
	}
64
	}
67
 
65
 
68
	/**
-
 
69
	 * Get the title of the product from catalog service.
-
 
70
	 * @param item
-
 
71
	 * @return
-
 
72
	 */
-
 
73
	private static String getProductTitle(Item item){
-
 
74
		String title = ((item.getBrand() != null) ? item.getBrand().trim() + " " : "")
-
 
75
		+ ((item.getModelName() != null) ? item.getModelName().trim() + " " : "")
-
 
76
		+ (( item.getModelNumber() != null ) ? item.getModelNumber().trim() : "" );
-
 
77
		title = title.replaceAll("  ", " ");
-
 
78
		return title;
-
 
79
	}
-
 
80
    
-
 
81
	private Node addChars(char[] s, int position, Node node, String string)
66
	private Node addChars(char[] s, int position, Node node, String string)
82
    {
67
    {
83
        if (node == null) { 
68
        if (node == null) { 
84
        	node = new Node(s[position], null); 
69
        	node = new Node(s[position], null); 
85
        }
70
        }