Subversion Repositories SmartDukaan

Rev

Blame | Last modification | View Log | RSS feed

package in.shop2020.recharge.controllers;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import in.shop2020.model.v1.order.HotspotStore;
import in.shop2020.thrift.clients.HelperClient;
import in.shop2020.thrift.clients.TransactionClient;
import in.shop2020.utils.Mail;

public class ManageStoreController extends BaseController{

    /**
     * 
     */
    private static final String chars = "0123456789";
    private static final int LENGTH = 4;
    private static final Random random = new Random();
    private static final long serialVersionUID = 1L;
    private String storecode = "";
    private HotspotStore store = null;
    private String message = "";
    private String passwordGeneration = "";
    private String password;
    private long storeid;

    public String index() {
        return "index";
    }
    
    public String searchStore() {
        try{
            TransactionClient tcl = new TransactionClient();
            store = tcl.getClient().getHotspotStore(0, storecode);
            if(store == null) {
                setMessage("No store found for this code");
            }
        } catch (Exception e) {
            log.error("Unable to get store for hotspotId : " + storecode, e);
            setMessage("Internal error. Please try again");
        }
        return index();
    }
    
    public String reset() throws Exception {
        
        char[] buf = new char[LENGTH];
        for (int i = 0; i < buf.length; i++) {
            buf[i] = chars.charAt(random.nextInt(chars.length()));
        }
        password = new String(buf);
        
        try {
            TransactionClient tcl = new TransactionClient(); 
            HotspotStore hotSpotStore = tcl.getClient().getHotspotStore(storeid, "");
            boolean wasPasswordSet = tcl.getClient().updateHotspotStorePassword(storeid, password);
            if(!wasPasswordSet) {
                setPasswordGeneration("FAIL");
                return index();
            }
            List<String> toList = new ArrayList<String>();
            toList.add(hotSpotStore.getEmail());
            HelperClient helperServiceClient = null;
            helperServiceClient = new HelperClient();
            in.shop2020.utils.HelperService.Client client = helperServiceClient.getClient();
            
            
            Mail mail = new Mail();
            mail.setSubject("New Password for Saholic Recharge");
            mail.setTo(toList);
            mail.setData("Your new password is : " + password);
            client.sendMail(mail);
        } catch(Exception e) {
            setPasswordGeneration("FAIL");
            log.error("Password generation/sending failed for storeId : " + storeId, e);
        }
        setPasswordGeneration("SUCCESS");
        return index();
    }

    public void setStore(HotspotStore store) {
        this.store = store;
    }

    public HotspotStore getStore() {
        return store;
    }

    public void setStorecode(String storecode) {
        this.storecode = storecode;
    }

    public String getStorecode() {
        return storecode;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setPasswordGeneration(String passwordGeneration) {
        this.passwordGeneration = passwordGeneration;
    }

    public String getPasswordGeneration() {
        return passwordGeneration;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public long getStoreid() {
        return storeid;
    }

    public void setStoreid(long storeid) {
        this.storeid = storeid;
    }

}