Subversion Repositories SmartDukaan

Rev

Rev 9788 | Rev 10253 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
6821 amar.kumar 1
package in.shop2020.warehouse.util;
2
 
3
import java.io.File;
4
import java.util.ArrayList;
5
import java.util.Calendar;
6
import java.util.HashMap;
7
import java.util.List;
8
import java.util.Map;
9
 
10
import javax.mail.MessagingException;
11
 
9788 amar.kumar 12
import org.apache.commons.logging.Log;
13
import org.apache.commons.logging.LogFactory;
6821 amar.kumar 14
import org.apache.thrift.transport.TTransportException;
15
 
16
import in.shop2020.model.v1.catalog.CatalogService.Client;
17
import in.shop2020.model.v1.catalog.Item;
18
import in.shop2020.model.v1.catalog.status;
19
import in.shop2020.thrift.clients.CatalogClient;
20
import in.shop2020.thrift.clients.InventoryClient;
21
import in.shop2020.thrift.clients.UserClient;
22
import in.shop2020.utils.GmailUtils;
23
 
24
public class OosStatusMarker {
9788 amar.kumar 25
	private static Log log = LogFactory.getLog(OosStatusMarker.class);
26
 
6821 amar.kumar 27
	private static final Integer OOS_STATUS_CUTOFF_HOUR = 23;
28
	private static final Integer OOS_STATUS_CUTOFF_MINUTE = 59;
29
 
8183 amar.kumar 30
	private static final String[] tomail = {"amar.kumar@shop2020.in", "sandeep.sachdeva@shop2020.in", "khushal.bhatia@shop2020.in", "rajveer.singh@shop2020.in"};
31
	//private static final String[] tomail = {"amar.kumar@shop2020.in"};
32
 
6821 amar.kumar 33
	public static void main(String[] args) throws TTransportException {
34
		Calendar oosEvaluationDate = Calendar.getInstance();
35
		Calendar cartAdditionStartDate = Calendar.getInstance();
36
		oosEvaluationDate.add(Calendar.DATE, -1);
37
		oosEvaluationDate.set(oosEvaluationDate.get(Calendar.YEAR), 
38
				oosEvaluationDate.get(Calendar.MONTH), oosEvaluationDate.get(Calendar.DATE), 
39
				OOS_STATUS_CUTOFF_HOUR , OOS_STATUS_CUTOFF_MINUTE, 0);
6844 amar.kumar 40
		cartAdditionStartDate.set(oosEvaluationDate.get(Calendar.YEAR), 
41
				oosEvaluationDate.get(Calendar.MONTH), oosEvaluationDate.get(Calendar.DATE) -1, 
42
				OOS_STATUS_CUTOFF_HOUR , OOS_STATUS_CUTOFF_MINUTE, 0);
6821 amar.kumar 43
		Map<Long, Boolean> oosStatusMap = new HashMap<Long, Boolean>();
44
 
45
		List<Item> items = null;
46
		try {
47
			Client catalogClient = new CatalogClient().getClient();
48
			items = catalogClient.getAllAliveItems();
49
		} catch (Exception e) {
50
			try {
51
				Client catalogClient = new CatalogClient().getClient();
52
				items = catalogClient.getAllAliveItems();
53
			} catch (Exception ex) {
9788 amar.kumar 54
				log.error("Exception while getting all items ", ex);
55
				sendMailForError("Exception while getting all items \n" + ex);
6821 amar.kumar 56
			}
57
		}
58
 
9791 rajveer 59
		System.out.println("P1");
6821 amar.kumar 60
		String failedItemsList = "";
61
		in.shop2020.model.v1.user.UserContextService.Client client = new UserClient().getClient();
9791 rajveer 62
		try {
63
			List<Long> cartItems = client.getProductsAddedToCart(cartAdditionStartDate.getTimeInMillis(), oosEvaluationDate.getTimeInMillis());
64
			for(Item item : items) {
65
				if (item.getItemStatus() == status.ACTIVE) {
66
					oosStatusMap.put(item.getId(), false);
67
				} else {
68
					if(cartItems.contains(item.getId())) {
6821 amar.kumar 69
						oosStatusMap.put(item.getId(), false);
70
					} else {
71
						oosStatusMap.put(item.getId(), true);
72
					}
73
				}
74
			}
9791 rajveer 75
		} catch (Exception e) {
76
			log.error("Exception while getting all items list added to cart", e);
77
			sendMailForError("Exception while getting all items list added to cart \n");
6821 amar.kumar 78
		}
9791 rajveer 79
		System.out.println("P2");
6821 amar.kumar 80
		try {
81
			in.shop2020.model.v1.inventory.InventoryService.Client invClient = new InventoryClient().getClient();
82
			invClient.addOosStatusForItem(oosStatusMap, oosEvaluationDate.getTimeInMillis());
9791 rajveer 83
			System.out.println("P3");
6821 amar.kumar 84
		} catch(Exception e) {
9788 amar.kumar 85
			log.error("Error while adding OOS Status", e);
86
			sendMailForError("Error while adding OOS Status");
87
			return;
6821 amar.kumar 88
		}
89
		sendMailForSuccess(failedItemsList);
90
	}
91
 
92
	private static void sendMailForError(String errorMessage) {
93
 
94
        GmailUtils g = new GmailUtils();
95
        try {
8183 amar.kumar 96
	        g.sendSSLMessage(tomail, "Error while Marking OutOfStock Statuses for Items", errorMessage, "cnc.center@shop2020.in", "5h0p2o2o", new ArrayList<File>());
6821 amar.kumar 97
        } catch(MessagingException mex) {
98
        	try {
8183 amar.kumar 99
        		g.sendSSLMessage(tomail, "Error while Marking OutOfStock Statuses for Items", errorMessage, "cnc.center@shop2020.in", "5h0p2o2o", new ArrayList<File>());
6821 amar.kumar 100
        	} catch (Exception e) {
101
        		//TODO Logging
102
        	}
103
        }
104
	}
105
 
106
	private static void sendMailForSuccess(String message) {
107
 
108
        GmailUtils g = new GmailUtils();
109
        try {
8183 amar.kumar 110
	        g.sendSSLMessage(tomail, "Marked OutOfStock Statuses ", message, "cnc.center@shop2020.in", "5h0p2o2o", new ArrayList<File>());
6821 amar.kumar 111
        } catch(MessagingException mex) {
112
        		//TODO Logging
113
        }
114
	}
115
}