Subversion Repositories SmartDukaan

Rev

Rev 3074 | Rev 3185 | 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;
3126 rajveer 10
import in.shop2020.thrift.clients.CatalogClient;
11
import in.shop2020.thrift.clients.UserClient;
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;
2867 rajveer 47
	private boolean isMobile = false;
48
 
375 ashish 49
	public EntityController(){
50
		super();
51
	}
507 rajveer 52
 
974 vikas 53
	// GET /*/1000001
507 rajveer 54
	@Actions({
974 vikas 55
		@Action("/mobile-phones"),
1005 vikas 56
		@Action("/mobile-accessories"),
2460 rajveer 57
		@Action("/tablets"),
1005 vikas 58
		@Action("/entity")
507 rajveer 59
	})
2306 vikas 60
    public String show() throws SecurityException, IOException, JSONException {
317 ashish 61
    	log.info("id=" + id);
637 rajveer 62
 
2306 vikas 63
    	String entityUrl = "";
64
    	String metaKeywords = "";
65
    	String metaDescription = "";
66
    	String pageTitle = "";
2434 rajveer 67
    	String productName = "";
2652 rajveer 68
    	String displayAccessories = "FALSE";
2306 vikas 69
    	try {
70
    	    JSONObject productPropertiesInJson = new JSONObject(pageLoader.getProductPropertiesHtml(productId).trim());
71
    	    entityUrl = productPropertiesInJson.getString("entityUrl");
72
    	    metaDescription = productPropertiesInJson.getString("metaDescription");
73
    	    metaKeywords = productPropertiesInJson.getString("metaKeywords");
74
    	    pageTitle = productPropertiesInJson.getString("title");
2434 rajveer 75
    	    productName = productPropertiesInJson.getString("name");
2652 rajveer 76
    	    displayAccessories = productPropertiesInJson.getString("displayAccessories");
2306 vikas 77
    	}
78
    	catch (JSONException e) {
2949 chandransh 79
            log.error("Unable to parse product properties JSON", e);
2306 vikas 80
            try {
3126 rajveer 81
                CatalogClient catalogClientService = new CatalogClient();
2306 vikas 82
                in.shop2020.model.v1.catalog.InventoryService.Client client = catalogClientService.getClient();
83
 
84
                Item item = client.getItemsByCatalogId(productId).get(0);
2307 vikas 85
                redirectUrl = "/" + item.getBrand().toLowerCase().replace(" ", "-");
2306 vikas 86
            } catch (Exception e1) {
2949 chandransh 87
                log.error("Unable to get items by catalog id", e1);
2306 vikas 88
                redirectUrl = "/";
89
            }
90
            log.info(redirectUrl);
91
            return "redirect";
92
       }
93
    	String currentUrl = request.getRequestURL().toString();
1197 varun.gupt 94
 
2306 vikas 95
    	if (!currentUrl.contains(entityUrl)) {
96
    	    redirectUrl = entityUrl;
97
    	    return "redirect";
98
    	}
99
 
2434 rajveer 100
    	//Extracting base url
1689 rajveer 101
    	String rootUrl = currentUrl.split("/")[2];
2194 varun.gupt 102
 
650 rajveer 103
    	htmlSnippets.put("PRODUCT_SUMMARY", pageLoader.getProductSummaryHtml(productId));
1258 varun.gupt 104
    	htmlSnippets.put("PRODUCT_ID", productId + "");
105
    	htmlSnippets.put("PRODUCT_NAME", productName);
1689 rajveer 106
    	htmlSnippets.put("PRODUCT_URL", currentUrl);
2194 varun.gupt 107
    	htmlSnippets.put("ROOT_URL", "http://" + rootUrl);
620 rajveer 108
		htmlSnippets.put("SLIDE_GUIDE", pageLoader.getSlideGuideHtml(productId));
1269 varun.gupt 109
		htmlSnippets.put("PAGE_TITLE", pageTitle.trim());
2306 vikas 110
		htmlSnippets.put("PAGE_METADESC", metaDescription);
111
		htmlSnippets.put("PAGE_METAKEYWORDS", metaKeywords);
2652 rajveer 112
		if(displayAccessories.equals("TRUE")){
2867 rajveer 113
			setMobile(true);
2652 rajveer 114
		}
115
 
650 rajveer 116
		try {
3126 rajveer 117
			UserClient userServiceClient = new UserClient();
555 chandransh 118
			Client client = userServiceClient.getClient();
773 rajveer 119
			long itemId = Long.parseLong(id);
120
			long userId = userinfo.getUserId();
1511 rajveer 121
			if(userId != -1){
122
				client.updateBrowseHistory(userId, itemId);
123
			}
650 rajveer 124
 
449 rajveer 125
		} catch (Exception e) {
2949 chandransh 126
			log.warn("Unable to update the browsing history because of: ", e);
449 rajveer 127
		}
128
 
2419 vikas 129
		DataLogger.logData(EventType.PRODUCT_VIEW, session.getId(), userinfo.getUserId(), userinfo.getEmail(),
3074 vikas 130
                productName, Long.toString(productId));
2149 vikas 131
		return "show";
317 ashish 132
    }
133
 
134
    /**
135
     * 
136
     * @param id
137
     */
138
    public void setId(String id) {
507 rajveer 139
    	StringTokenizer tokenizer = new StringTokenizer(id,"-");
140
    	while(tokenizer.hasMoreTokens()){
141
    		this.id = tokenizer.nextToken();
142
    	}
974 vikas 143
    	this.productId = Long.parseLong(this.id);
317 ashish 144
    }
145
 
387 rajveer 146
	public Map<String,String> getHtmlSnippets(){
388 rajveer 147
		System.out.println(" getHtmlSnippets  is called");
387 rajveer 148
		return htmlSnippets;
375 ashish 149
	}
388 rajveer 150
 
449 rajveer 151
	public String getSlideGuideSnippet(){
388 rajveer 152
		return htmlSnippets.get("SLIDE_GUIDE");
153
	}
154
 
2306 vikas 155
	public String getRedirectUrl(){
156
        return redirectUrl;
157
    }
158
 
449 rajveer 159
	public String getProductSummarySnippet(){
160
		return htmlSnippets.get("PRODUCT_SUMMARY");
161
	}
162
 
974 vikas 163
	public String getPageTitleSnippet(){
164
		return htmlSnippets.get("PAGE_TITLE");
165
	}
166
 
167
	public String getPageMetaDescSnippet(){
168
		return htmlSnippets.get("PAGE_METADESC");
169
	}
170
 
171
	public String getPageMetaKeywordsSnippet(){
172
		return htmlSnippets.get("PAGE_METAKEYWORDS");
173
	}
174
 
1197 varun.gupt 175
	public String getProductName()	{
176
		return htmlSnippets.get("PRODUCT_NAME");
177
	}
1258 varun.gupt 178
 
179
	public String getProductId()	{
180
		return htmlSnippets.get("PRODUCT_ID");
181
	}
182
 
183
	public String getProductUrl()	{
184
		return htmlSnippets.get("PRODUCT_URL");
185
	}
1364 varun.gupt 186
 
187
	public String getRootUrl()	{
188
		return htmlSnippets.get("ROOT_URL");
189
	}
2867 rajveer 190
 
191
	/**
192
	 * @param isMobile the isMobile to set
193
	 */
194
	public void setMobile(boolean isMobile) {
195
		this.isMobile = isMobile;
2652 rajveer 196
	}
2867 rajveer 197
 
198
	/**
199
	 * @return the isMobile
200
	 */
201
	public boolean isMobile() {
202
		return isMobile;
203
	}
1197 varun.gupt 204
}