Subversion Repositories SmartDukaan

Rev

Rev 2307 | Rev 2426 | 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;
1258 varun.gupt 15
import java.net.URLEncoder;
375 ashish 16
import java.util.Map;
507 rajveer 17
import java.util.StringTokenizer;
317 ashish 18
 
832 rajveer 19
import org.apache.log4j.Logger;
507 rajveer 20
import org.apache.struts2.convention.annotation.Action;
21
import org.apache.struts2.convention.annotation.Actions;
974 vikas 22
import org.apache.struts2.convention.annotation.Result;
2306 vikas 23
import org.apache.struts2.convention.annotation.Results;
1364 varun.gupt 24
import org.apache.velocity.VelocityContext;
2306 vikas 25
import org.json.JSONException;
26
import org.json.JSONObject;
317 ashish 27
 
28
/**
29
 * 
650 rajveer 30
 * @author rajveer
317 ashish 31
 *
32
 */
650 rajveer 33
 
2306 vikas 34
@Results({
35
    @Result(name = "show", location = "entity-show.vm"),
36
    @Result(name = "redirect", location = "${redirectUrl}", type = "redirect")
37
})
650 rajveer 38
public class EntityController extends BaseController {
39
 
40
	private static final long serialVersionUID = 1L;
41
 
1957 vikas 42
	private static Logger log = Logger.getLogger(Class.class);
317 ashish 43
	/**
44
	 * 
45
	 */
46
	private String id;
2306 vikas 47
	private String redirectUrl;
620 rajveer 48
	private long productId;
2306 vikas 49
 
375 ashish 50
	public EntityController(){
51
		super();
52
	}
507 rajveer 53
 
974 vikas 54
	// GET /*/1000001
507 rajveer 55
	@Actions({
974 vikas 56
		@Action("/mobile-phones"),
1005 vikas 57
		@Action("/mobile-accessories"),
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 = "";
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");
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
 
1197 varun.gupt 96
    	//Extracting Product name from Page Title
2194 varun.gupt 97
        String[] items = pageTitle.split("\\|");
1269 varun.gupt 98
    	String productName = items[0] != null ? items[0].trim() : null;
1689 rajveer 99
    	String rootUrl = currentUrl.split("/")[2];
2194 varun.gupt 100
 
1689 rajveer 101
    	String productUrlEncoded = URLEncoder.encode(currentUrl, "UTF-8");
1197 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);
1364 varun.gupt 107
    	htmlSnippets.put("PRODUCT_URL_ENCODED", productUrlEncoded);
2194 varun.gupt 108
    	htmlSnippets.put("ROOT_URL", "http://" + rootUrl);
1364 varun.gupt 109
		htmlSnippets.put("SOCIAL_UTILS", getSocialUtilsHtml(productId, productName, productUrlEncoded));
620 rajveer 110
		htmlSnippets.put("SLIDE_GUIDE", pageLoader.getSlideGuideHtml(productId));
1269 varun.gupt 111
		htmlSnippets.put("PAGE_TITLE", pageTitle.trim());
2306 vikas 112
		htmlSnippets.put("PAGE_METADESC", metaDescription);
113
		htmlSnippets.put("PAGE_METAKEYWORDS", metaKeywords);
620 rajveer 114
 
650 rajveer 115
		try {
555 chandransh 116
			UserContextServiceClient userServiceClient = new UserContextServiceClient();
117
			Client client = userServiceClient.getClient();
773 rajveer 118
			long itemId = Long.parseLong(id);
119
			long userId = userinfo.getUserId();
1511 rajveer 120
			if(userId != -1){
121
				client.updateBrowseHistory(userId, itemId);
122
			}
650 rajveer 123
 
449 rajveer 124
		} catch (Exception e) {
125
			// TODO Auto-generated catch block
126
			e.printStackTrace();
127
		}
128
 
2419 vikas 129
		DataLogger.logData(EventType.PRODUCT_VIEW, session.getId(), userinfo.getUserId(), userinfo.getEmail(),
2149 vikas 130
                productName);
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
 
449 rajveer 175
	public String getSocialUtilsSnippet(){
176
		return htmlSnippets.get("SOCIAL_UTILS"); 
177
	}
178
 
179
	public String getLocatorSnippet(){
180
		return htmlSnippets.get("LOCATOR");
181
	}
182
 
183
	public String getRecommendationsSnippet(){
184
		return htmlSnippets.get("RECOMMENDATIONS");
185
	}
650 rajveer 186
 
449 rajveer 187
	public String getSimilarProductsSnippet(){
188
		return htmlSnippets.get("SIMILAR_PRODUCTS");
189
	}
650 rajveer 190
 
449 rajveer 191
	public String getAccessoriesSnippet(){
192
		return htmlSnippets.get("ACCESSORIES");
193
	}
194
 
650 rajveer 195
	public String getReviewsSnippet(){
196
		return htmlSnippets.get("REVIEWS");
449 rajveer 197
	}
198
 
1197 varun.gupt 199
	public String getProductName()	{
200
		return htmlSnippets.get("PRODUCT_NAME");
201
	}
1258 varun.gupt 202
 
203
	public String getProductId()	{
204
		return htmlSnippets.get("PRODUCT_ID");
205
	}
206
 
207
	public String getProductUrl()	{
208
		return htmlSnippets.get("PRODUCT_URL");
209
	}
1364 varun.gupt 210
 
211
	public String getRootUrl()	{
212
		return htmlSnippets.get("ROOT_URL");
213
	}
214
 
215
	private String getSocialUtilsHtml(long productId, String productName, String productEncodedUrl) {
216
		String htmlString = "";
217
		VelocityContext context = new VelocityContext();
218
 
219
		String templateFile = "templates/socialutils.vm";
220
 
221
		context.put("PRODUCT_ID", productId + "");
222
		context.put("PRODUCT_NAME", productName);
223
		context.put("PRODUCT_URL_ENCODED", productEncodedUrl);
224
 
225
		htmlString = pageLoader.getHtmlFromVelocity(templateFile, context);
226
		return htmlString;
227
	}
1197 varun.gupt 228
}