Subversion Repositories SmartDukaan

Rev

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