Subversion Repositories SmartDukaan

Rev

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