Subversion Repositories SmartDukaan

Rev

Rev 2426 | Rev 2460 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
317 ashish 1
/**
2
 * 
3
 */
4
package in.shop2020.serving.controllers;
5
 
650 rajveer 6
 
2306 vikas 7
import in.shop2020.datalogger.EventType;
8
import in.shop2020.model.v1.catalog.Item;
9
import in.shop2020.model.v1.user.UserContextService.Client;
1957 vikas 10
import in.shop2020.serving.utils.DataLogger;
2306 vikas 11
import in.shop2020.thrift.clients.CatalogServiceClient;
555 chandransh 12
import in.shop2020.thrift.clients.UserContextServiceClient;
375 ashish 13
 
317 ashish 14
import java.io.IOException;
375 ashish 15
import java.util.Map;
507 rajveer 16
import java.util.StringTokenizer;
317 ashish 17
 
832 rajveer 18
import org.apache.log4j.Logger;
507 rajveer 19
import org.apache.struts2.convention.annotation.Action;
20
import org.apache.struts2.convention.annotation.Actions;
974 vikas 21
import org.apache.struts2.convention.annotation.Result;
2306 vikas 22
import org.apache.struts2.convention.annotation.Results;
23
import org.json.JSONException;
24
import org.json.JSONObject;
317 ashish 25
 
26
/**
27
 * 
650 rajveer 28
 * @author rajveer
317 ashish 29
 *
30
 */
650 rajveer 31
 
2306 vikas 32
@Results({
33
    @Result(name = "show", location = "entity-show.vm"),
34
    @Result(name = "redirect", location = "${redirectUrl}", type = "redirect")
35
})
650 rajveer 36
public class EntityController extends BaseController {
37
 
38
	private static final long serialVersionUID = 1L;
39
 
1957 vikas 40
	private static Logger log = Logger.getLogger(Class.class);
317 ashish 41
	/**
42
	 * 
43
	 */
44
	private String id;
2306 vikas 45
	private String redirectUrl;
620 rajveer 46
	private long productId;
2306 vikas 47
 
375 ashish 48
	public EntityController(){
49
		super();
50
	}
507 rajveer 51
 
974 vikas 52
	// GET /*/1000001
507 rajveer 53
	@Actions({
974 vikas 54
		@Action("/mobile-phones"),
1005 vikas 55
		@Action("/mobile-accessories"),
56
		@Action("/entity")
507 rajveer 57
	})
2306 vikas 58
    public String show() throws SecurityException, IOException, JSONException {
317 ashish 59
    	log.info("id=" + id);
637 rajveer 60
 
2306 vikas 61
    	String entityUrl = "";
62
    	String metaKeywords = "";
63
    	String metaDescription = "";
64
    	String pageTitle = "";
2434 rajveer 65
    	String productName = "";
2306 vikas 66
    	try {
67
    	    JSONObject productPropertiesInJson = new JSONObject(pageLoader.getProductPropertiesHtml(productId).trim());
68
    	    entityUrl = productPropertiesInJson.getString("entityUrl");
69
    	    metaDescription = productPropertiesInJson.getString("metaDescription");
70
    	    metaKeywords = productPropertiesInJson.getString("metaKeywords");
71
    	    pageTitle = productPropertiesInJson.getString("title");
2434 rajveer 72
    	    productName = productPropertiesInJson.getString("name");
2306 vikas 73
    	}
74
    	catch (JSONException e) {
75
            e.printStackTrace();
76
            try {
77
                CatalogServiceClient catalogClientService = new CatalogServiceClient();
78
                in.shop2020.model.v1.catalog.InventoryService.Client client = catalogClientService.getClient();
79
 
80
                Item item = client.getItemsByCatalogId(productId).get(0);
2307 vikas 81
                redirectUrl = "/" + item.getBrand().toLowerCase().replace(" ", "-");
2306 vikas 82
            } catch (Exception e1) {
83
                redirectUrl = "/";
84
                e.printStackTrace();
85
            }
86
            log.info(redirectUrl);
87
            return "redirect";
88
       }
89
    	String currentUrl = request.getRequestURL().toString();
1197 varun.gupt 90
 
2306 vikas 91
    	if (!currentUrl.contains(entityUrl)) {
92
    	    redirectUrl = entityUrl;
93
    	    return "redirect";
94
    	}
95
 
2434 rajveer 96
    	//Extracting base url
1689 rajveer 97
    	String rootUrl = currentUrl.split("/")[2];
2194 varun.gupt 98
 
650 rajveer 99
    	htmlSnippets.put("PRODUCT_SUMMARY", pageLoader.getProductSummaryHtml(productId));
1258 varun.gupt 100
    	htmlSnippets.put("PRODUCT_ID", productId + "");
101
    	htmlSnippets.put("PRODUCT_NAME", productName);
1689 rajveer 102
    	htmlSnippets.put("PRODUCT_URL", currentUrl);
2194 varun.gupt 103
    	htmlSnippets.put("ROOT_URL", "http://" + rootUrl);
620 rajveer 104
		htmlSnippets.put("SLIDE_GUIDE", pageLoader.getSlideGuideHtml(productId));
1269 varun.gupt 105
		htmlSnippets.put("PAGE_TITLE", pageTitle.trim());
2306 vikas 106
		htmlSnippets.put("PAGE_METADESC", metaDescription);
107
		htmlSnippets.put("PAGE_METAKEYWORDS", metaKeywords);
620 rajveer 108
 
650 rajveer 109
		try {
555 chandransh 110
			UserContextServiceClient userServiceClient = new UserContextServiceClient();
111
			Client client = userServiceClient.getClient();
773 rajveer 112
			long itemId = Long.parseLong(id);
113
			long userId = userinfo.getUserId();
1511 rajveer 114
			if(userId != -1){
115
				client.updateBrowseHistory(userId, itemId);
116
			}
650 rajveer 117
 
449 rajveer 118
		} catch (Exception e) {
119
			// TODO Auto-generated catch block
120
			e.printStackTrace();
121
		}
122
 
2419 vikas 123
		DataLogger.logData(EventType.PRODUCT_VIEW, session.getId(), userinfo.getUserId(), userinfo.getEmail(),
2149 vikas 124
                productName);
125
		return "show";
317 ashish 126
    }
127
 
128
    /**
129
     * 
130
     * @param id
131
     */
132
    public void setId(String id) {
507 rajveer 133
    	StringTokenizer tokenizer = new StringTokenizer(id,"-");
134
    	while(tokenizer.hasMoreTokens()){
135
    		this.id = tokenizer.nextToken();
136
    	}
974 vikas 137
    	this.productId = Long.parseLong(this.id);
317 ashish 138
    }
139
 
387 rajveer 140
	public Map<String,String> getHtmlSnippets(){
388 rajveer 141
		System.out.println(" getHtmlSnippets  is called");
387 rajveer 142
		return htmlSnippets;
375 ashish 143
	}
388 rajveer 144
 
449 rajveer 145
	public String getSlideGuideSnippet(){
388 rajveer 146
		return htmlSnippets.get("SLIDE_GUIDE");
147
	}
148
 
2306 vikas 149
	public String getRedirectUrl(){
150
        return redirectUrl;
151
    }
152
 
449 rajveer 153
	public String getProductSummarySnippet(){
154
		return htmlSnippets.get("PRODUCT_SUMMARY");
155
	}
156
 
974 vikas 157
	public String getPageTitleSnippet(){
158
		return htmlSnippets.get("PAGE_TITLE");
159
	}
160
 
161
	public String getPageMetaDescSnippet(){
162
		return htmlSnippets.get("PAGE_METADESC");
163
	}
164
 
165
	public String getPageMetaKeywordsSnippet(){
166
		return htmlSnippets.get("PAGE_METAKEYWORDS");
167
	}
168
 
1197 varun.gupt 169
	public String getProductName()	{
170
		return htmlSnippets.get("PRODUCT_NAME");
171
	}
1258 varun.gupt 172
 
173
	public String getProductId()	{
174
		return htmlSnippets.get("PRODUCT_ID");
175
	}
176
 
177
	public String getProductUrl()	{
178
		return htmlSnippets.get("PRODUCT_URL");
179
	}
1364 varun.gupt 180
 
181
	public String getRootUrl()	{
182
		return htmlSnippets.get("ROOT_URL");
183
	}
1197 varun.gupt 184
}