Subversion Repositories SmartDukaan

Rev

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