Subversion Repositories SmartDukaan

Rev

Rev 21735 | Rev 22065 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
21483 amit.gupta 1
package com.spice.profitmandi.web.controller;
2
 
3
import java.net.URL;
4
import java.time.LocalDateTime;
5
import java.util.Calendar;
6
import java.util.Collections;
7
import java.util.HashMap;
8
import java.util.Map;
9
 
10
import javax.servlet.http.HttpServletRequest;
11
 
12
import org.apache.commons.codec.binary.Base64;
13
import org.apache.commons.lang.StringUtils;
14
import org.slf4j.Logger;
15
import org.slf4j.LoggerFactory;
16
import org.slf4j.helpers.MessageFormatter;
17
import org.springframework.beans.factory.annotation.Autowired;
18
import org.springframework.http.MediaType;
19
import org.springframework.http.ResponseEntity;
20
import org.springframework.stereotype.Controller;
21
import org.springframework.web.bind.annotation.PathVariable;
22
import org.springframework.web.bind.annotation.RequestMapping;
23
import org.springframework.web.bind.annotation.RequestMethod;
24
import org.springframework.web.bind.annotation.RequestParam;
25
 
26
import com.spice.profitmandi.common.model.ProfitMandiConstants;
21740 ashik.ali 27
import com.spice.profitmandi.common.web.util.ResponseSender;
21735 ashik.ali 28
import com.spice.profitmandi.dao.entity.dtr.Click;
29
import com.spice.profitmandi.dao.repository.dtr.ClickRepository;
21483 amit.gupta 30
import com.spice.profitmandi.web.res.AddClickReponse;
31
 
32
//import in.shop2020.mobileapi.serving.services.Mysql;
33
import io.swagger.annotations.ApiImplicitParam;
34
import io.swagger.annotations.ApiImplicitParams;
35
import io.swagger.annotations.ApiOperation;
36
 
37
@Controller
38
public class ClicksController {
39
 
40
	@SuppressWarnings("serial")
41
	public static final Map<String, String> FK_MAP = Collections.unmodifiableMap(new HashMap<String, String>() {
42
		{
43
			put("id", "2");
44
			put("name", "Flipkart");
45
			put("domain", "flipkart.com");
46
			put("status", "active");
47
			put("affiliate_id", "saholic1g");
48
			put("affid_param", "affid");
49
			put("sub_tag_param", "affExtParam1");
50
			put("cashback_status", "active");
51
		}
52
	});
53
	@SuppressWarnings("serial")
54
	public static final Map<String, String> SD_MAP = Collections.unmodifiableMap(new HashMap<String, String>() {
55
		{
56
			put("id", "3");
57
			put("name", "Snapdeal");
58
			put("domain", "snapdeal.com");
59
			put("status", "active");
60
			put("affiliate_id", "33550");
61
			put("affid_param", "aff_id");
62
			put("sub_tag_param", "aff_sub");
63
			put("cashback_status", "active");
64
		}
65
	});
66
	@SuppressWarnings("serial")
67
	public static final Map<String, String> HS_18 = Collections.unmodifiableMap(new HashMap<String, String>() {
68
		{
69
			// put("id", );
70
			// put("name", );
71
			// put("domain", );
72
			// put("status",);
73
			// put("affiliate_id",);
74
			// put("affid_param",);
75
			// put("sub_tag_param",);
76
			// put("cashback_status",);
77
		}
78
	});
79
	@SuppressWarnings("serial")
80
	public static final Map<String, String> PAYTM_MAP = Collections.unmodifiableMap(new HashMap<String, String>() {
81
		{
82
			put("id", "6");
83
			put("name", "Paytm");
84
			put("domain", "paytm.com");
85
			put("status", "active");
86
			put("affiliate_id", "796881");
87
			put("affid_param", "AID");
88
			put("sub_tag_param", "UID");
89
			put("cashback_status", "active");
90
		}
91
	});
92
	@SuppressWarnings("serial")
93
	public static final Map<String, String> AMAZON_MAP = Collections.unmodifiableMap(new HashMap<String, String>() {
94
		{
95
			put("id", "1");
96
			put("name", "Amazon");
97
			put("domain", "amazon.in");
98
			put("status", "active");
99
			put("affiliate_id", "profittill2-21");
100
			put("affid_param", "tag");
101
			put("sub_tag_param", "ascsubtag");
102
			put("cashback_status", "active");
103
		}
104
	});
105
	@SuppressWarnings("serial")
106
	public static final Map<String, String> SC_MAP = Collections.unmodifiableMap(new HashMap<String, String>() {
107
		{
108
			put("id", "5");
109
			put("name", "Shopclues");
110
			put("domain", "shopclues.com");
111
			put("status", "active");
112
			put("affiliate_id", "796881");
113
			put("affid_param", "AID");
114
			put("sub_tag_param", "UID");
115
			put("cashback_status", "active");
116
		}
117
	});
118
	@SuppressWarnings("serial")
119
	public static final Map<Integer, Map<String, String>> SOURCES_MAP = Collections
120
			.unmodifiableMap(new HashMap<Integer, Map<String, String>>() {
121
				{
122
					put(1, AMAZON_MAP);
123
					put(2, FK_MAP);
124
					put(3, SD_MAP);
125
					put(5, SC_MAP);
126
					put(6, PAYTM_MAP);
127
				}
128
			});
129
 
130
	private static final Logger logger = LoggerFactory.getLogger(ClicksController.class);
131
 
132
	@Autowired
133
	ResponseSender<?> responseSender;
134
 
135
	@Autowired
136
	ClickRepository clickRepository;
137
 
138
	@RequestMapping(value = ProfitMandiConstants.URL_CLICKS_ADD, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
139
	@ApiImplicitParams({
140
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
141
	@ApiOperation(value = "Register clicks")
142
	public ResponseEntity<?> add(HttpServletRequest request, @PathVariable(value = "storeId") int sourceId,
143
			@PathVariable(value = "storeProductId") int storeProductId, @RequestParam(value = "url") String url,
144
			@RequestParam(value = "price") float price,
145
			@RequestParam(value = "callback", required = false) String callback) throws Throwable {
146
 
147
		AddClickReponse response = new AddClickReponse();
148
		int userId = (int)request.getAttribute("userId");
149
 
150
		Map<String, String> sourceMap = SOURCES_MAP.get(sourceId);
151
		String storeName = sourceMap.get("name");
152
		String prefix = "SHA" + sourceId;
153
		String tag = prefix + Calendar.getInstance().getTimeInMillis();
154
		if (sourceId == 2) {
155
			// $url = str_replace('www','m',$url);
156
			url = url.replace("www.", "");
157
			url = url.replace("http://", "");
158
			url = url.replace("flipkart.com", "http://dl.flipkart.com/dl");
159
		} else if (sourceId == 3) {
160
			URL aUrl = new URL(url);
161
			String path = aUrl.getPath();
162
			path = path.replace("viewAllSellers/", "");// quickfix for snapdeal
163
			if (!StringUtils.isEmpty(aUrl.getQuery())) {
164
				url = "http://m.snapdeal.com" + path + "?" + aUrl.getQuery()
165
						+ "&utm_source=aff_prog&utm_campaign=afts&offer_id=17";
166
			} else {
167
				url = "http://m.snapdeal.com" + path + "?utm_source=aff_prog&utm_campaign=afts&offer_id=17";
168
			}
169
		} else if (sourceId == 4) {
170
			URL aUrl = new URL(url);
171
			url = aUrl.getPath();
172
			if (!StringUtils.isEmpty(aUrl.getQuery())) {
173
				url += "?" + aUrl.getQuery();
174
			}
175
		} else if (sourceId == 5) {
176
			url = "http://clk.omgt5.com/?PID=10314&r=" + url;
177
		} else if (sourceId == 6) {
178
			url = "http://clk.omgt5.com/?PID=11365&r=" + url;
179
		} else if (sourceId == 7) {
180
			url = "http://clk.omgt5.com/?PID=9419&r=" + url;
181
		}
182
		String firstChar = "?";
183
		if (url.contains("?")) {
184
			firstChar = "&";
185
		}
186
		String redirectUrl = url + firstChar + sourceMap.get("affid_param") + "=" + sourceMap.get("affiliate_id");
187
		if (!StringUtils.isEmpty(sourceMap.get("sub_tag_param"))) {
188
			url += "&" + sourceMap.get("sub_tag_param") + "=" + tag;
189
		}
190
		String base64Url = new String(Base64.encodeBase64(url.getBytes()));
191
		switch (sourceId) {
192
			case 2:
193
			case 5:
194
			case 6:
195
			case 7: {
196
				url = "http://mobilehotindia.com/r.html?" + base64Url;
197
				break;
198
			}
199
			case 1:
200
			case 3: {
201
				url = "http://api.profittill.com/r.html?" + base64Url;
202
				break;
203
			}
204
		}
205
		Click click = new Click();
206
		//click.setBrand(brand);
207
		//click.setCategoryId(categoryId);
208
		click.setCreated(LocalDateTime.now());
209
		click.setExtras(MessageFormatter.format("{\"store\":\"{}\"}", storeName));
210
		click.setPrice(price);
211
		click.setTag(tag);
212
		click.setUrl(url);
213
		//click.setProductName(productName);
214
		click.setUserId(userId);
215
		//click.setStore
216
		click.setStoreProductId(storeProductId);
217
		clickRepository.persist(click);
218
		response.setMessage("");
219
		response.setShowMessage(false);
220
		response.setType("redirect");
221
		response.setUrl(redirectUrl);
222
		return responseSender.ok(response);
223
	}
224
}