Subversion Repositories SmartDukaan

Rev

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

package in.shop2020.warehouse.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.mail.MessagingException;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.thrift.transport.TTransportException;

import in.shop2020.model.v1.catalog.CatalogService.Client;
import in.shop2020.model.v1.catalog.Item;
import in.shop2020.model.v1.catalog.status;
import in.shop2020.thrift.clients.CatalogClient;
import in.shop2020.thrift.clients.InventoryClient;
import in.shop2020.thrift.clients.UserClient;
import in.shop2020.utils.GmailUtils;

public class OosStatusMarker {
        private static Log log = LogFactory.getLog(OosStatusMarker.class);
        
        private static final Integer OOS_STATUS_CUTOFF_HOUR = 23;
        private static final Integer OOS_STATUS_CUTOFF_MINUTE = 59;
        
        private static java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        
        private static final String[] tomail = {"aman.kumar@shop2020.in", "himanshu.pandey@shop2020.in", "khushal.bhatia@shop2020.in", "amit.gupta@shop2020.in","vikram.raghav@shop2020.in","kshitij.sood@shop2020.in"};
        //private static final String[] tomail = {"amar.kumar@shop2020.in"};
        
        public static void main(String[] args) throws TTransportException, IOException {
                File file = new File("/home/manish/alerts/oosmarker.txt");
                
                Calendar oosEvaluationDate = Calendar.getInstance();
                Calendar cartAdditionStartDate = Calendar.getInstance();
                oosEvaluationDate.add(Calendar.DATE, -1);
                oosEvaluationDate.set(oosEvaluationDate.get(Calendar.YEAR), 
                                oosEvaluationDate.get(Calendar.MONTH), oosEvaluationDate.get(Calendar.DATE), 
                                OOS_STATUS_CUTOFF_HOUR , OOS_STATUS_CUTOFF_MINUTE, 0);
                cartAdditionStartDate.set(oosEvaluationDate.get(Calendar.YEAR), 
                                oosEvaluationDate.get(Calendar.MONTH), oosEvaluationDate.get(Calendar.DATE) -1, 
                                OOS_STATUS_CUTOFF_HOUR , OOS_STATUS_CUTOFF_MINUTE, 0);
                Map<Long, Boolean> oosStatusMap = new HashMap<Long, Boolean>();
                
                List<Item> items = null;
                try {
                        Client catalogClient = new CatalogClient().getClient();
                        items = catalogClient.getAllAliveItems();
                } catch (Exception e) {
                        try {
                                Client catalogClient = new CatalogClient().getClient();
                                items = catalogClient.getAllAliveItems();
                        } catch (Exception ex) {
                                log.error("Exception while getting all items ", ex);
                                sendMailForError("Exception while getting all items \n" + ex);
                        }
                }
                
                System.out.println("P1");
                String failedItemsList = "";
                in.shop2020.model.v1.user.UserContextService.Client client = new UserClient().getClient();
                try {
                        List<Long> cartItems = client.getProductsAddedToCart(cartAdditionStartDate.getTimeInMillis(), oosEvaluationDate.getTimeInMillis());
                        for(Item item : items) {
                                if (item.getItemStatus() == status.ACTIVE) {
                                        oosStatusMap.put(item.getId(), false);
                                } else {
                                        if(cartItems.contains(item.getId())) {
                                                oosStatusMap.put(item.getId(), false);
                                        } else {
                                                oosStatusMap.put(item.getId(), true);
                                        }
                                }
                        }
                } catch (Exception e) {
                        log.error("Exception while getting all items list added to cart", e);
                        sendMailForError("Exception while getting all items list added to cart \n");
                }
                System.out.println("P2");
                try {
                        in.shop2020.model.v1.inventory.InventoryService.Client invClient = new InventoryClient().getClient();
                        invClient.addOosStatusForItem(oosStatusMap, oosEvaluationDate.getTimeInMillis());
                        System.out.println("P3");
                } catch(Exception e) {
                        log.error("Error while adding OOS Status", e);
                        sendMailForError("Error while adding OOS Status");
                        return;
                }
                BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
                
                bufferedWriter.write(StringUtils.join(new String[] {"OosStatusUpdationDate",sdf.format(new Date())}, '\t'));
                bufferedWriter.close();
                sendMailForSuccess(failedItemsList);
        }

        private static void sendMailForError(String errorMessage) {
                
        GmailUtils g = new GmailUtils();
        try {
                g.sendSSLMessage(tomail, "Error while Marking OutOfStock Statuses for Items", errorMessage, "cnc.center@shop2020.in", "5h0p2o2o", new ArrayList<File>());
        } catch(MessagingException mex) {
                try {
                        g.sendSSLMessage(tomail, "Error while Marking OutOfStock Statuses for Items", errorMessage, "cnc.center@shop2020.in", "5h0p2o2o", new ArrayList<File>());
                } catch (Exception e) {
                        //TODO Logging
                }
        }
        }
        
        private static void sendMailForSuccess(String message) {
                
        GmailUtils g = new GmailUtils();
        try {
                g.sendSSLMessage(tomail, "Marked OutOfStock Statuses ", message, "cnc.center@shop2020.in", "5h0p2o2o", new ArrayList<File>());
        } catch(MessagingException mex) {
                        //TODO Logging
        }
        }
}