Subversion Repositories SmartDukaan

Rev

Rev 6606 | Rev 6888 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.serving.utils;

import in.shop2020.config.ConfigException;
import in.shop2020.model.v1.order.LineItem;
import in.shop2020.model.v1.order.Order;
import in.shop2020.model.v1.order.RechargeOrderStatus;
import in.shop2020.model.v1.order.RechargePlan;
import in.shop2020.model.v1.order.RechargeType;
import in.shop2020.model.v1.user.Cart;
import in.shop2020.model.v1.user.Line;
import in.shop2020.thrift.clients.TransactionClient;
import in.shop2020.thrift.clients.config.ConfigClient;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

public class Utils {
    private static Logger logger = Logger.getLogger(Utils.class);
    
        public static final String EXPORT_ENTITIES_PATH = getExportPath();
        public static final String EXPORT_JAVASCRIPT_CONTENT_PATH = "/var/lib/tomcat6/webapps/export/javascripts/";
        public static final long ROOT_CATEGORY = 10000;
        public static final long MOBILE_PHONES_CATEGORY = 10001;
        public static final long MOBILE_ACCESSORIES_CATEGORY = 10011;
        public static final long TABLETS_CATEGORY = 10010;
    public static final long LAPTOPS_CATEGORY = 10050;
    public static final long CAMERAS_CATEGORY = 11001;
    
    static Map<Long, String> providersMap;
    static Map<Long, String> mobileProvidersMap;
    static Map<Long, String> dthProvidersMap;
    static Map<Long, List<RechargePlan>> operatorPlanMap = new HashMap<Long, List<RechargePlan>>(); 
    private static Map<String, Map<String, String>> uriOperatorMap = new HashMap<String, Map<String,String>>();
    
    public static final List<RechargeOrderStatus> refundableRechargeStatusList = new ArrayList<RechargeOrderStatus>(Arrays.asList(new RechargeOrderStatus[] 
                                                                                                                                                     {RechargeOrderStatus.RECHARGE_FAILED, 
                                                                                                                                                      RechargeOrderStatus.RECHARGE_FAILED_REFUNDED,
                                                                                                                                                      RechargeOrderStatus.PAYMENT_SUCCESSFUL}));
    
    public static Map<RechargeOrderStatus, String> statusMap = new HashMap<RechargeOrderStatus, String>();
    
    public static String[] facetDefIDs = new String[] {
        "F_50010","F_50011","F_50002","F_50001",  "F_50028", "F_50006", "F_50007", "F_50012", "F_50013", "F_50014", "F_50015", "F_50017", "F_50018", "F_50019", "F_50020", "F_50021", "F_50022", "F_50023", "F_50024", "F_50025", "F_50026", "F_50027", "F_50028" };
    public static String[] facetLabels = new String[] {"Category","Sub Category","Price", "Brand", "Availability", "Data Connectivity", 
        "Camera Resolution", "Display", "Operating System", "RAM", "Storage Capacity", "Processor",
        "Capacity", "Class", "Capacity", "Capacity", "Type", "Interface", "Resolution", "Optical Zoom", "Display Size", "Effective Resolution", "Availability" };
    public static String[] rootfacetDefIDs = new String[] {"F_50010","F_50011","F_50002","F_50001","F_50028"};
    public static String[] rootfacetLabels = new String[] {"Category","Sub Category","Price", "Brand","Availability"};
    
    static {
        statusMap.put(RechargeOrderStatus.PAYMENT_FAILED, "Payment Unsuccessful");
        statusMap.put(RechargeOrderStatus.PAYMENT_SUCCESSFUL, "Processing Recharge");
        statusMap.put(RechargeOrderStatus.RECHARGE_FAILED, "Recharge Failed");
        statusMap.put(RechargeOrderStatus.RECHARGE_FAILED_REFUNDED, "Recharge Failed");
        statusMap.put(RechargeOrderStatus.RECHARGE_SUCCESSFUL, "Recharge Successful");
        statusMap.put(RechargeOrderStatus.REFUNDED, "Amount Refunded");
        statusMap.put(RechargeOrderStatus.PARTIALLY_REFUNDED, "Amount Refunded");
        
        TransactionClient tcl;
        try {
            tcl = new TransactionClient();
            mobileProvidersMap = tcl.getClient().getServiceProviders(RechargeType.MOBILE, true);
            dthProvidersMap = tcl.getClient().getServiceProviders(RechargeType.DTH, true);
//            providersMap.putAll(mobileProvidersMap);
//            providersMap.putAll(dthProvidersMap);
            for (Long operatorId : mobileProvidersMap.keySet()) {
                List<RechargePlan> plans = tcl.getClient().getPlansForOperator(operatorId);
                if (!plans.isEmpty()) {
                    operatorPlanMap.put(operatorId, plans);
                }
            }
            for (Long operatorId : mobileProvidersMap.keySet()) {
                Map<String,String> valueMap = new HashMap<String,String>();
                valueMap.put("Id", operatorId+"");
                valueMap.put("Name", mobileProvidersMap.get(operatorId)+"");
                valueMap.put("ServiceType", "1");
                valueMap.put("ServiceName", "Prepaid Mobile");
                uriOperatorMap.put(mobileProvidersMap.get(operatorId).toLowerCase().trim().replace(" ", "-")+"-online-recharge", valueMap);
            }
            for (Long operatorId : dthProvidersMap.keySet()) {
                Map<String,String> valueMap = new HashMap<String,String>();
                valueMap.put("Id", operatorId+"");
                valueMap.put("Name", dthProvidersMap.get(operatorId)+"");
                valueMap.put("ServiceType", "2");
                valueMap.put("ServiceName", "DTH TV");
                uriOperatorMap.put(dthProvidersMap.get(operatorId).toLowerCase().trim().replace(" ", "-")+"-online-recharge", valueMap);
            }
            
            Map<String,String> valueMap = new HashMap<String,String>();
            valueMap.put("Id", "0");
                valueMap.put("Name", "Mobile and DTH");
                valueMap.put("ServiceType", "1");
                valueMap.put("ServiceName", "");
                uriOperatorMap.put("recharge", valueMap);
//            providersMap.putAll(tcl.getClient().getServiceProviders(RechargeType.DTH));
                
                
        } catch (Exception e) {
            logger.error("Could not get providers", e);
        }
    }
    
    
    
        private static String getExportPath(){
                String exportPath=null;
                ConfigClient client = ConfigClient.getClient();
                try{
                        exportPath = client.get("export_entities_path");
                }catch(ConfigException ce){
                        logger.error("Unable to read export path from the config client: ", ce);
                        logger.warn("Setting the default export path");
                        exportPath = "/var/lib/tomcat6/webapps/export/html/entities/";
                }
                return exportPath;
        }
        
        public static boolean validatePin(String pincode) {
                int pin;
                try {
                        pin = Integer.parseInt(pincode);
                }
                catch (NumberFormatException e) {
                        return false;
                }
                
                if (pin < 100000 || pin > 999999) {
                        return false;
                }
                return true;
        }
        
        public static boolean isValidEmail(String emailAddress) {
                
                if(emailAddress == null || emailAddress.isEmpty())      {
                        return false;
                }
                try {
                        InternetAddress address = new InternetAddress(emailAddress, true);
                        address.validate();
                        return true;
                        
                } catch (AddressException e) {
                        logger.error(emailAddress + " is not valid email address ", e);
                        return false;
                }
        }

        public static boolean validatePhone(String phone) {
                long iPhone;
                try {
                iPhone = Long.parseLong(phone);
                }
                catch (NumberFormatException e) {
                        return false;
                }
                
                if (iPhone < 1000000000l || iPhone > 9999999999l) {
                        return false;
                }
                return true;
        }
        
        public static String getItemIdStringInCart(Cart cart) {
            List<String> itemIds = new ArrayList<String>();
        for (Line line : cart.getLines()) {
            itemIds.add(Long.toString(line.getItemId()));
        }
        return StringUtils.join(itemIds, '_');
    }
        
        public static String getItemIdStringFromOrders(List<Order> orders) {
        List<String> itemIds = new ArrayList<String>();
        if (orders != null) {
            for (Order order : orders) {
                for (LineItem lItem : order.getLineitems()) {
                    itemIds.add(Long.toString(lItem.getItem_id()));
                }
            }
        }
        return StringUtils.join(itemIds, '_');
    }
        
        
        
        public static String getRechargeProviderName(long providerId) {
            return providersMap.get(providerId);
        }

    public static Map<Long, String> getMobileProvidersMap() {
        return mobileProvidersMap;
    }

    public static Map<Long, String> getDthProvidersMap() {
        return dthProvidersMap;
    }

    public static Map<Long, List<RechargePlan>> getOperatorPlanMap() {
        return operatorPlanMap;
    }
    
    public static Map<String, Map<String, String>> getUriOperatorMap(){
        return uriOperatorMap;
        }
    
    public static Map<String, String> getOperatorByUri(String uri){
        return uriOperatorMap.get(uri);
        }
}