Subversion Repositories SmartDukaan

Rev

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