Subversion Repositories SmartDukaan

Rev

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