Subversion Repositories SmartDukaan

Rev

Rev 2460 | Rev 2652 | 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;
10
import in.shop2020.thrift.clients.CatalogServiceClient;
555 chandransh 11
import in.shop2020.thrift.clients.UserContextServiceClient;
2511 vikas 12
import in.shop2020.utils.DataLogger;
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"),
2460 rajveer 56
		@Action("/tablets"),
1005 vikas 57
		@Action("/entity")
507 rajveer 58
	})
2306 vikas 59
    public String show() throws SecurityException, IOException, JSONException {
317 ashish 60
    	log.info("id=" + id);
637 rajveer 61
 
2306 vikas 62
    	String entityUrl = "";
63
    	String metaKeywords = "";
64
    	String metaDescription = "";
65
    	String pageTitle = "";
2434 rajveer 66
    	String productName = "";
2306 vikas 67
    	try {
68
    	    JSONObject productPropertiesInJson = new JSONObject(pageLoader.getProductPropertiesHtml(productId).trim());
69
    	    entityUrl = productPropertiesInJson.getString("entityUrl");
70
    	    metaDescription = productPropertiesInJson.getString("metaDescription");
71
    	    metaKeywords = productPropertiesInJson.getString("metaKeywords");
72
    	    pageTitle = productPropertiesInJson.getString("title");
2434 rajveer 73
    	    productName = productPropertiesInJson.getString("name");
2306 vikas 74
    	}
75
    	catch (JSONException e) {
76
            e.printStackTrace();
77
            try {
78
                CatalogServiceClient catalogClientService = new CatalogServiceClient();
79
                in.shop2020.model.v1.catalog.InventoryService.Client client = catalogClientService.getClient();
80
 
81
                Item item = client.getItemsByCatalogId(productId).get(0);
2307 vikas 82
                redirectUrl = "/" + item.getBrand().toLowerCase().replace(" ", "-");
2306 vikas 83
            } catch (Exception e1) {
84
                redirectUrl = "/";
85
                e.printStackTrace();
86
            }
87
            log.info(redirectUrl);
88
            return "redirect";
89
       }
90
    	String currentUrl = request.getRequestURL().toString();
1197 varun.gupt 91
 
2306 vikas 92
    	if (!currentUrl.contains(entityUrl)) {
93
    	    redirectUrl = entityUrl;
94
    	    return "redirect";
95
    	}
96
 
2434 rajveer 97
    	//Extracting base url
1689 rajveer 98
    	String rootUrl = currentUrl.split("/")[2];
2194 varun.gupt 99
 
650 rajveer 100
    	htmlSnippets.put("PRODUCT_SUMMARY", pageLoader.getProductSummaryHtml(productId));
1258 varun.gupt 101
    	htmlSnippets.put("PRODUCT_ID", productId + "");
102
    	htmlSnippets.put("PRODUCT_NAME", productName);
1689 rajveer 103
    	htmlSnippets.put("PRODUCT_URL", currentUrl);
2194 varun.gupt 104
    	htmlSnippets.put("ROOT_URL", "http://" + rootUrl);
620 rajveer 105
		htmlSnippets.put("SLIDE_GUIDE", pageLoader.getSlideGuideHtml(productId));
1269 varun.gupt 106
		htmlSnippets.put("PAGE_TITLE", pageTitle.trim());
2306 vikas 107
		htmlSnippets.put("PAGE_METADESC", metaDescription);
108
		htmlSnippets.put("PAGE_METAKEYWORDS", metaKeywords);
620 rajveer 109
 
650 rajveer 110
		try {
555 chandransh 111
			UserContextServiceClient userServiceClient = new UserContextServiceClient();
112
			Client client = userServiceClient.getClient();
773 rajveer 113
			long itemId = Long.parseLong(id);
114
			long userId = userinfo.getUserId();
1511 rajveer 115
			if(userId != -1){
116
				client.updateBrowseHistory(userId, itemId);
117
			}
650 rajveer 118
 
449 rajveer 119
		} catch (Exception e) {
120
			// TODO Auto-generated catch block
121
			e.printStackTrace();
122
		}
123
 
2419 vikas 124
		DataLogger.logData(EventType.PRODUCT_VIEW, session.getId(), userinfo.getUserId(), userinfo.getEmail(),
2149 vikas 125
                productName);
126
		return "show";
317 ashish 127
    }
128
 
129
    /**
130
     * 
131
     * @param id
132
     */
133
    public void setId(String id) {
507 rajveer 134
    	StringTokenizer tokenizer = new StringTokenizer(id,"-");
135
    	while(tokenizer.hasMoreTokens()){
136
    		this.id = tokenizer.nextToken();
137
    	}
974 vikas 138
    	this.productId = Long.parseLong(this.id);
317 ashish 139
    }
140
 
387 rajveer 141
	public Map<String,String> getHtmlSnippets(){
388 rajveer 142
		System.out.println(" getHtmlSnippets  is called");
387 rajveer 143
		return htmlSnippets;
375 ashish 144
	}
388 rajveer 145
 
449 rajveer 146
	public String getSlideGuideSnippet(){
388 rajveer 147
		return htmlSnippets.get("SLIDE_GUIDE");
148
	}
149
 
2306 vikas 150
	public String getRedirectUrl(){
151
        return redirectUrl;
152
    }
153
 
449 rajveer 154
	public String getProductSummarySnippet(){
155
		return htmlSnippets.get("PRODUCT_SUMMARY");
156
	}
157
 
974 vikas 158
	public String getPageTitleSnippet(){
159
		return htmlSnippets.get("PAGE_TITLE");
160
	}
161
 
162
	public String getPageMetaDescSnippet(){
163
		return htmlSnippets.get("PAGE_METADESC");
164
	}
165
 
166
	public String getPageMetaKeywordsSnippet(){
167
		return htmlSnippets.get("PAGE_METAKEYWORDS");
168
	}
169
 
1197 varun.gupt 170
	public String getProductName()	{
171
		return htmlSnippets.get("PRODUCT_NAME");
172
	}
1258 varun.gupt 173
 
174
	public String getProductId()	{
175
		return htmlSnippets.get("PRODUCT_ID");
176
	}
177
 
178
	public String getProductUrl()	{
179
		return htmlSnippets.get("PRODUCT_URL");
180
	}
1364 varun.gupt 181
 
182
	public String getRootUrl()	{
183
		return htmlSnippets.get("ROOT_URL");
184
	}
1197 varun.gupt 185
}