Subversion Repositories SmartDukaan

Rev

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

package in.shop2020.warehouse.util;

import java.io.File;
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.thrift.TException;
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 final Integer OOS_STATUS_CUTOFF_HOUR = 23;
        private static final Integer OOS_STATUS_CUTOFF_MINUTE = 59;
        
        private static final String[] tomail = {"amar.kumar@shop2020.in", "sandeep.sachdeva@shop2020.in", "khushal.bhatia@shop2020.in", "rajveer.singh@shop2020.in"};
        //private static final String[] tomail = {"amar.kumar@shop2020.in"};
        
        public static void main(String[] args) throws TTransportException {
                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) {
                                sendMailForError("Exception while getting all items \n" + e);
                        }
                }
                
                String failedItemsList = "";
                in.shop2020.model.v1.user.UserContextService.Client client = new UserClient().getClient();
                for(Item item : items) {
                        if (item.getItemStatus() == status.ACTIVE) {
                                oosStatusMap.put(item.getId(), false);
                        } else {
                                try {
                                        if(client.isProductAddedToCart(item.getId(), cartAdditionStartDate.getTimeInMillis(), oosEvaluationDate.getTimeInMillis())) {
                                                oosStatusMap.put(item.getId(), false);
                                        } else {
                                                oosStatusMap.put(item.getId(), true);
                                        }
                                } catch (TException e) {
                                        failedItemsList = new Long(item.getId()).toString() + "\n";
                                        continue;
                                }
                        }
                }
                
                try {
                        in.shop2020.model.v1.inventory.InventoryService.Client invClient = new InventoryClient().getClient();
                        invClient.addOosStatusForItem(oosStatusMap, oosEvaluationDate.getTimeInMillis());
                } catch(Exception e) {
                        sendMailForError("Error while adding OOS Status" + e);
                }
                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
        }
        }
}