Subversion Repositories SmartDukaan

Rev

Rev 23809 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.support.controllers;

import in.shop2020.model.v1.order.UserWallet;
import in.shop2020.model.v1.order.UserWalletHistory;
import in.shop2020.model.v1.order.WalletReferenceType;
import in.shop2020.model.v1.user.User;
import in.shop2020.model.v1.user.UserContextService.Client;
import in.shop2020.support.utils.ReportsUtils;
import in.shop2020.thrift.clients.TransactionClient;
import in.shop2020.thrift.clients.UserClient;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.util.ServletContextAware;
import org.apache.thrift.TException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.ValidationAwareSupport;

@SuppressWarnings("serial")

@InterceptorRefs({
        @InterceptorRef("defaultStack"),
        @InterceptorRef("login")
})
@Results({
        @Result(name = "redirect", location = "${url}", type = "redirect"),
        @Result(name="authsuccess", type="redirectAction", params = {"actionName" , "reports"})
})
public class UserWalletCreditController extends ValidationAwareSupport implements ServletRequestAware ,ServletResponseAware, ServletContextAware{

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

        private HttpServletRequest request;
        private HttpSession session;
        private HttpServletResponse response;
        private String id;
        private String email;
        private User user;

        private String amount;
        private String url;
        private String result;
        private String cashback;
        private String description;
        private WalletReferenceType transactionType;

        public String getCashback() {
                return cashback;
        }

        public void setCashback(String cashback) {
                this.cashback = cashback;
        }

        public String getResult() {
                return result;
        }

        public void setResult(String result) {
                this.result = result;
        }

        public String getUrl() {
                return url;
        }

        public void setUrl(String url) {
                this.url = url;
        }

        public WalletReferenceType getTransactionType() {
                return transactionType;
        }
        
        public void setTransactionType(WalletReferenceType transactionType) {
                this.transactionType = transactionType;
        }
        
        public void setTransactionType(String transactionType) {
                this.transactionType = WalletReferenceType.valueOf(transactionType);
        }
        
        public String getAmount() {
                return amount;
        }

        public void setAmount(String amount) {
                this.amount = amount;
        }

        public User getUser() {
                return user;
        }

        public void setUser(User user) {
                this.user = user;
        }

        private UserWallet userWallet;
        private List<UserWalletHistory> userWalletHistory;


        public UserWallet getUserWallet() {
                return userWallet;
        }
        
        public List<UserWalletHistory> getUserWalletHistory() {
                return this.userWalletHistory;
        }

        public void setUserWallet(UserWallet userWallet) {
                this.userWallet = userWallet;
        }

        public String getEmail() {
                return email;
        }

        public void setEmail(String email) {
                this.email = email;
        }

        public String index() throws TException {
                if (!ReportsUtils.canAccessReport((Long) session.getAttribute(ReportsUtils.ROLE),request.getServletPath())) {
                        return "authfail";
                }
                return "index";
        }

        public String getWallet(){
                try{
                        Client uc = new UserClient().getClient();
                        user = uc.getUserByEmail(email);
                        if (user.getUserId()==-1){
                                addActionError("User email not valid");
                                return "index";
                        }
                }
                catch(Exception e){
                        e.printStackTrace();
                        addActionError("Service exception.");
                        return "index";
                }
                try{
                        Client uc = new UserClient().getClient();
                        user = uc.getUserByEmail(email);
                        in.shop2020.model.v1.order.TransactionService.Client tc = new TransactionClient().getClient();
                        userWallet = tc.getUserWallet(user.getUserId());
                        setUserWalletHistory(tc.getLatestUserWalletHistory(user.getUserId(), 0, 20));
                }
                catch(Exception e){
                        e.printStackTrace();
                        addActionError("Service exception.");
                        return "index";
                }
                return "user-wallet-credit-edit";
        }

        public String creditWallet(){
                long credit_amount;
                double cash_back;
                try{
                        credit_amount = Long.valueOf(amount);
                        if (credit_amount <=0){
                                setResult("Amount cant be negative");
                                return "pmsa-result";
                        }
                }
                catch(Exception e){
                        setResult("Invalid amount");
                        return "pmsa-result";
                }
                
                try{
                        cash_back = Double.valueOf(cashback);
                }
                catch(Exception e){
                        setResult("Invalid cashback");
                        e.printStackTrace();
                        return "pmsa-result";
                }
                try{
                        Client uc = new UserClient().getClient();
                        user = uc.getUserByEmail(email);
                }
                catch(Exception e){
                        setResult("Service error");
                        return "pmsa-result";
                }

                if (user.getUserId()==-1){
                        setResult("User email not valid");
                        return "pmsa-result";
                }
                if(StringUtils.isEmpty(description)) {
                        setResult("Short Description should not be empty");
                        return "pmsa-result";
                }
                if(this.transactionType.equals(WalletReferenceType.ADVANCE_REVERSAL)) {
                        credit_amount = -credit_amount;
                }
                try {
                        in.shop2020.model.v1.order.TransactionService.Client tc = new TransactionClient().getClient();
                        tc.creditUserWallet(user.getUserId(), credit_amount, cash_back, description);
                } catch (Exception e) {
                        e.printStackTrace();
                        setResult("Service error");
                        return "pmsa-result";
                }
                setResult("Wallet updated successfully");
                return "pmsa-result";
        }


        public String getId() {
                return id;
        }

        public void setId(String id) {
                this.id = id;
        }



        public void setServletRequest(HttpServletRequest req) {
                this.request = req;
                this.session = req.getSession();        
        }

        @Override
        public void setServletContext(ServletContext arg0) {
                // TODO Auto-generated method stub

        }

        public void setServletResponse(HttpServletResponse response) {
                this.response = response;
        }
        
        public void setDescription(String description) {
                this.description = description;
        }

        public void setUserWalletHistory(List<UserWalletHistory> userWalletHistory) {
                this.userWalletHistory = userWalletHistory;
        }
        
        public String getDate(long timestamp) {
                return new SimpleDateFormat().format(new Date(timestamp));
        }


}