Subversion Repositories SmartDukaan

Rev

Rev 22501 | 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);
22500 amit.gupta 157
			URL aUrl = new URL(url);
22501 amit.gupta 158
			url = "http://dl.flipkart.com/dl/" + aUrl.getPath() + "?" + aUrl.getQuery();
21483 amit.gupta 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
		}
22498 amit.gupta 186
		url = url + firstChar + sourceMap.get("affid_param") + "=" + sourceMap.get("affiliate_id");
21483 amit.gupta 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());
22554 amit.gupta 209
		click.setExtras(String.format("{\"store\":\"%s\"}", storeName));
21483 amit.gupta 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");
22498 amit.gupta 221
		response.setUrl(url);
21483 amit.gupta 222
		return responseSender.ok(response);
223
	}
224
}