Subversion Repositories SmartDukaan

Rev

Rev 2652 | Rev 2949 | 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;
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) {
79
            e.printStackTrace();
80
            try {
81
                CatalogServiceClient catalogClientService = new CatalogServiceClient();
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) {
87
                redirectUrl = "/";
88
                e.printStackTrace();
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 {
555 chandransh 117
			UserContextServiceClient userServiceClient = new UserContextServiceClient();
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) {
126
			// TODO Auto-generated catch block
127
			e.printStackTrace();
128
		}
129
 
2419 vikas 130
		DataLogger.logData(EventType.PRODUCT_VIEW, session.getId(), userinfo.getUserId(), userinfo.getEmail(),
2149 vikas 131
                productName);
132
		return "show";
317 ashish 133
    }
134
 
135
    /**
136
     * 
137
     * @param id
138
     */
139
    public void setId(String id) {
507 rajveer 140
    	StringTokenizer tokenizer = new StringTokenizer(id,"-");
141
    	while(tokenizer.hasMoreTokens()){
142
    		this.id = tokenizer.nextToken();
143
    	}
974 vikas 144
    	this.productId = Long.parseLong(this.id);
317 ashish 145
    }
146
 
387 rajveer 147
	public Map<String,String> getHtmlSnippets(){
388 rajveer 148
		System.out.println(" getHtmlSnippets  is called");
387 rajveer 149
		return htmlSnippets;
375 ashish 150
	}
388 rajveer 151
 
449 rajveer 152
	public String getSlideGuideSnippet(){
388 rajveer 153
		return htmlSnippets.get("SLIDE_GUIDE");
154
	}
155
 
2306 vikas 156
	public String getRedirectUrl(){
157
        return redirectUrl;
158
    }
159
 
449 rajveer 160
	public String getProductSummarySnippet(){
161
		return htmlSnippets.get("PRODUCT_SUMMARY");
162
	}
163
 
974 vikas 164
	public String getPageTitleSnippet(){
165
		return htmlSnippets.get("PAGE_TITLE");
166
	}
167
 
168
	public String getPageMetaDescSnippet(){
169
		return htmlSnippets.get("PAGE_METADESC");
170
	}
171
 
172
	public String getPageMetaKeywordsSnippet(){
173
		return htmlSnippets.get("PAGE_METAKEYWORDS");
174
	}
175
 
1197 varun.gupt 176
	public String getProductName()	{
177
		return htmlSnippets.get("PRODUCT_NAME");
178
	}
1258 varun.gupt 179
 
180
	public String getProductId()	{
181
		return htmlSnippets.get("PRODUCT_ID");
182
	}
183
 
184
	public String getProductUrl()	{
185
		return htmlSnippets.get("PRODUCT_URL");
186
	}
1364 varun.gupt 187
 
188
	public String getRootUrl()	{
189
		return htmlSnippets.get("ROOT_URL");
190
	}
2867 rajveer 191
 
192
	/**
193
	 * @param isMobile the isMobile to set
194
	 */
195
	public void setMobile(boolean isMobile) {
196
		this.isMobile = isMobile;
2652 rajveer 197
	}
2867 rajveer 198
 
199
	/**
200
	 * @return the isMobile
201
	 */
202
	public boolean isMobile() {
203
		return isMobile;
204
	}
1197 varun.gupt 205
}