Subversion Repositories SmartDukaan

Rev

Rev 22498 | Rev 22501 | Go to most recent revision | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.web.controller;

import java.net.URL;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.spice.profitmandi.common.model.ProfitMandiConstants;
import com.spice.profitmandi.common.web.util.ResponseSender;
import com.spice.profitmandi.dao.entity.dtr.Click;
import com.spice.profitmandi.dao.repository.dtr.ClickRepository;
import com.spice.profitmandi.web.res.AddClickReponse;

//import in.shop2020.mobileapi.serving.services.Mysql;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Controller
@Transactional(rollbackFor = Throwable.class)
public class ClicksController {

        @SuppressWarnings("serial")
        public static final Map<String, String> FK_MAP = Collections.unmodifiableMap(new HashMap<String, String>() {
                {
                        put("id", "2");
                        put("name", "Flipkart");
                        put("domain", "flipkart.com");
                        put("status", "active");
                        put("affiliate_id", "saholic1g");
                        put("affid_param", "affid");
                        put("sub_tag_param", "affExtParam1");
                        put("cashback_status", "active");
                }
        });
        @SuppressWarnings("serial")
        public static final Map<String, String> SD_MAP = Collections.unmodifiableMap(new HashMap<String, String>() {
                {
                        put("id", "3");
                        put("name", "Snapdeal");
                        put("domain", "snapdeal.com");
                        put("status", "active");
                        put("affiliate_id", "33550");
                        put("affid_param", "aff_id");
                        put("sub_tag_param", "aff_sub");
                        put("cashback_status", "active");
                }
        });
        @SuppressWarnings("serial")
        public static final Map<String, String> HS_18 = Collections.unmodifiableMap(new HashMap<String, String>() {
                {
                        // put("id", );
                        // put("name", );
                        // put("domain", );
                        // put("status",);
                        // put("affiliate_id",);
                        // put("affid_param",);
                        // put("sub_tag_param",);
                        // put("cashback_status",);
                }
        });
        @SuppressWarnings("serial")
        public static final Map<String, String> PAYTM_MAP = Collections.unmodifiableMap(new HashMap<String, String>() {
                {
                        put("id", "6");
                        put("name", "Paytm");
                        put("domain", "paytm.com");
                        put("status", "active");
                        put("affiliate_id", "796881");
                        put("affid_param", "AID");
                        put("sub_tag_param", "UID");
                        put("cashback_status", "active");
                }
        });
        @SuppressWarnings("serial")
        public static final Map<String, String> AMAZON_MAP = Collections.unmodifiableMap(new HashMap<String, String>() {
                {
                        put("id", "1");
                        put("name", "Amazon");
                        put("domain", "amazon.in");
                        put("status", "active");
                        put("affiliate_id", "profittill2-21");
                        put("affid_param", "tag");
                        put("sub_tag_param", "ascsubtag");
                        put("cashback_status", "active");
                }
        });
        @SuppressWarnings("serial")
        public static final Map<String, String> SC_MAP = Collections.unmodifiableMap(new HashMap<String, String>() {
                {
                        put("id", "5");
                        put("name", "Shopclues");
                        put("domain", "shopclues.com");
                        put("status", "active");
                        put("affiliate_id", "796881");
                        put("affid_param", "AID");
                        put("sub_tag_param", "UID");
                        put("cashback_status", "active");
                }
        });
        @SuppressWarnings("serial")
        public static final Map<Integer, Map<String, String>> SOURCES_MAP = Collections
                        .unmodifiableMap(new HashMap<Integer, Map<String, String>>() {
                                {
                                        put(1, AMAZON_MAP);
                                        put(2, FK_MAP);
                                        put(3, SD_MAP);
                                        put(5, SC_MAP);
                                        put(6, PAYTM_MAP);
                                }
                        });

        private static final Logger logger = LoggerFactory.getLogger(ClicksController.class);

        @Autowired
        ResponseSender<?> responseSender;
        
        @Autowired
        ClickRepository clickRepository;

        @RequestMapping(value = ProfitMandiConstants.URL_CLICKS_ADD, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
        @ApiImplicitParams({
                        @ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
        @ApiOperation(value = "Register clicks")
        public ResponseEntity<?> add(HttpServletRequest request, @PathVariable(value = "storeId") int sourceId,
                        @PathVariable(value = "storeProductId") int storeProductId, @RequestParam(value = "url") String url,
                        @RequestParam(value = "price") float price,
                        @RequestParam(value = "callback", required = false) String callback) throws Throwable {

                AddClickReponse response = new AddClickReponse();
                int userId = (int)request.getAttribute("userId");

                Map<String, String> sourceMap = SOURCES_MAP.get(sourceId);
                String storeName = sourceMap.get("name");
                String prefix = "SHA" + sourceId;
                String tag = prefix + Calendar.getInstance().getTimeInMillis();
                if (sourceId == 2) {
                        // $url = str_replace('www','m',$url);
                        URL aUrl = new URL(url);
                        url = "http://dl.flipkart.com/dl/" + aUrl.getPath() + aUrl.getQuery();
                } else if (sourceId == 3) {
                        URL aUrl = new URL(url);
                        String path = aUrl.getPath();
                        path = path.replace("viewAllSellers/", "");// quickfix for snapdeal
                        if (!StringUtils.isEmpty(aUrl.getQuery())) {
                                url = "http://m.snapdeal.com" + path + "?" + aUrl.getQuery()
                                                + "&utm_source=aff_prog&utm_campaign=afts&offer_id=17";
                        } else {
                                url = "http://m.snapdeal.com" + path + "?utm_source=aff_prog&utm_campaign=afts&offer_id=17";
                        }
                } else if (sourceId == 4) {
                        URL aUrl = new URL(url);
                        url = aUrl.getPath();
                        if (!StringUtils.isEmpty(aUrl.getQuery())) {
                                url += "?" + aUrl.getQuery();
                        }
                } else if (sourceId == 5) {
                        url = "http://clk.omgt5.com/?PID=10314&r=" + url;
                } else if (sourceId == 6) {
                        url = "http://clk.omgt5.com/?PID=11365&r=" + url;
                } else if (sourceId == 7) {
                        url = "http://clk.omgt5.com/?PID=9419&r=" + url;
                }
                String firstChar = "?";
                if (url.contains("?")) {
                        firstChar = "&";
                }
                url = url + firstChar + sourceMap.get("affid_param") + "=" + sourceMap.get("affiliate_id");
                if (!StringUtils.isEmpty(sourceMap.get("sub_tag_param"))) {
                        url += "&" + sourceMap.get("sub_tag_param") + "=" + tag;
                }
                String base64Url = new String(Base64.encodeBase64(url.getBytes()));
                switch (sourceId) {
                        case 2:
                        case 5:
                        case 6:
                        case 7: {
                                url = "http://mobilehotindia.com/r.html?" + base64Url;
                                break;
                        }
                        case 1:
                        case 3: {
                                url = "http://api.profittill.com/r.html?" + base64Url;
                                break;
                        }
                }
                Click click = new Click();
                //click.setBrand(brand);
                //click.setCategoryId(categoryId);
                click.setCreated(LocalDateTime.now());
                click.setExtras(String.format("{\"store\":\"{}\"}", storeName));
                click.setPrice(price);
                click.setTag(tag);
                click.setUrl(url);
                //click.setProductName(productName);
                click.setUserId(userId);
                //click.setStore
                click.setStoreProductId(storeProductId);
                clickRepository.persist(click);
                response.setMessage("");
                response.setShowMessage(false);
                response.setType("redirect");
                response.setUrl(url);
                return responseSender.ok(response);
        }
}