Subversion Repositories SmartDukaan

Rev

Rev 6844 | Rev 8183 | 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.Date;
7
import java.util.HashMap;
8
import java.util.List;
9
import java.util.Map;
10
 
11
import javax.mail.MessagingException;
12
 
13
import org.apache.thrift.TException;
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 {
25
	private static final Integer OOS_STATUS_CUTOFF_HOUR = 23;
26
	private static final Integer OOS_STATUS_CUTOFF_MINUTE = 59;
27
 
28
	public static void main(String[] args) throws TTransportException {
29
		Calendar oosEvaluationDate = Calendar.getInstance();
30
		Calendar cartAdditionStartDate = Calendar.getInstance();
31
		oosEvaluationDate.add(Calendar.DATE, -1);
32
		oosEvaluationDate.set(oosEvaluationDate.get(Calendar.YEAR), 
33
				oosEvaluationDate.get(Calendar.MONTH), oosEvaluationDate.get(Calendar.DATE), 
34
				OOS_STATUS_CUTOFF_HOUR , OOS_STATUS_CUTOFF_MINUTE, 0);
6844 amar.kumar 35
		cartAdditionStartDate.set(oosEvaluationDate.get(Calendar.YEAR), 
36
				oosEvaluationDate.get(Calendar.MONTH), oosEvaluationDate.get(Calendar.DATE) -1, 
37
				OOS_STATUS_CUTOFF_HOUR , OOS_STATUS_CUTOFF_MINUTE, 0);
6821 amar.kumar 38
		Map<Long, Boolean> oosStatusMap = new HashMap<Long, Boolean>();
39
 
40
		List<Item> items = null;
41
		try {
42
			Client catalogClient = new CatalogClient().getClient();
43
			items = catalogClient.getAllAliveItems();
44
		} catch (Exception e) {
45
			try {
46
				Client catalogClient = new CatalogClient().getClient();
47
				items = catalogClient.getAllAliveItems();
48
			} catch (Exception ex) {
49
				sendMailForError("Exception while getting all items \n" + e);
50
			}
51
		}
52
 
53
		String failedItemsList = "";
54
		in.shop2020.model.v1.user.UserContextService.Client client = new UserClient().getClient();
55
		for(Item item : items) {
56
			if (item.getItemStatus() == status.ACTIVE) {
57
				oosStatusMap.put(item.getId(), false);
58
			} else {
59
				try {
60
					if(client.isProductAddedToCart(item.getId(), cartAdditionStartDate.getTimeInMillis(), oosEvaluationDate.getTimeInMillis())) {
61
						oosStatusMap.put(item.getId(), false);
62
					} else {
63
						oosStatusMap.put(item.getId(), true);
64
					}
65
				} catch (TException e) {
66
					failedItemsList = new Long(item.getId()).toString() + "\n";
67
					continue;
68
				}
69
			}
70
		}
71
 
72
		try {
73
			in.shop2020.model.v1.inventory.InventoryService.Client invClient = new InventoryClient().getClient();
74
			invClient.addOosStatusForItem(oosStatusMap, oosEvaluationDate.getTimeInMillis());
75
		} catch(Exception e) {
76
			sendMailForError("Error while adding OOS Status" + e);
77
		}
78
		sendMailForSuccess(failedItemsList);
79
	}
80
 
81
	private static void sendMailForError(String errorMessage) {
82
 
83
        GmailUtils g = new GmailUtils();
84
        try {
85
	        g.sendSSLMessage(new String[]{ "amar.kumar@shop2020.in", "sandeep.sachdeva@shop2020.in", "khushal.bhatia@shop2020.in", "rajveer.singh@shop2020.in"},
86
	        		"Error while Marking OutOfStock Statuses for Items", errorMessage, "cnc.center@shop2020.in", "5h0p2o2o", new ArrayList<File>());
87
        } catch(MessagingException mex) {
88
        	try {
89
        		g.sendSSLMessage(new String[]{ "amar.kumar@shop2020.in", "sandeep.sachdeva@shop2020.in", "khushal.bhatia@shop2020.in", "rajveer.singh@shop2020.in"},
90
    	        		"Error while Marking OutOfStock Statuses for Items", errorMessage, "cnc.center@shop2020.in", "5h0p2o2o", new ArrayList<File>());
91
        	} catch (Exception e) {
92
        		//TODO Logging
93
        	}
94
        }
95
	}
96
 
97
	private static void sendMailForSuccess(String message) {
98
 
99
        GmailUtils g = new GmailUtils();
100
        try {
101
	        g.sendSSLMessage(new String[]{ "amar.kumar@shop2020.in", "sandeep.sachdeva@shop2020.in", "khushal.bhatia@shop2020.in", "rajveer.singh@shop2020.in"},
102
	        		"Marked OutOfStock Statuses ", message, "cnc.center@shop2020.in", "5h0p2o2o", new ArrayList<File>());
103
        } catch(MessagingException mex) {
104
        		//TODO Logging
105
        }
106
	}
107
}