Subversion Repositories SmartDukaan

Rev

Rev 10253 | Rev 12064 | 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
 
10253 manish.sha 30
	private static final String[] tomail = {"manish.sharma@shop2020.in", "sandeep.sachdeva@shop2020.in", "khushal.bhatia@shop2020.in", "rajveer.singh@shop2020.in"};
8183 amar.kumar 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();
12063 kshitij.so 36
		oosEvaluationDate.add(Calendar.DATE, -2);
6821 amar.kumar 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);
12063 kshitij.so 43
		System.out.println(cartAdditionStartDate.getTimeInMillis());
44
		System.out.println(oosEvaluationDate.getTimeInMillis());
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
 
9791 rajveer 61
		System.out.println("P1");
6821 amar.kumar 62
		String failedItemsList = "";
63
		in.shop2020.model.v1.user.UserContextService.Client client = new UserClient().getClient();
9791 rajveer 64
		try {
65
			List<Long> cartItems = client.getProductsAddedToCart(cartAdditionStartDate.getTimeInMillis(), oosEvaluationDate.getTimeInMillis());
66
			for(Item item : items) {
67
				if (item.getItemStatus() == status.ACTIVE) {
68
					oosStatusMap.put(item.getId(), false);
69
				} else {
70
					if(cartItems.contains(item.getId())) {
6821 amar.kumar 71
						oosStatusMap.put(item.getId(), false);
72
					} else {
73
						oosStatusMap.put(item.getId(), true);
74
					}
75
				}
76
			}
9791 rajveer 77
		} catch (Exception e) {
78
			log.error("Exception while getting all items list added to cart", e);
79
			sendMailForError("Exception while getting all items list added to cart \n");
6821 amar.kumar 80
		}
9791 rajveer 81
		System.out.println("P2");
6821 amar.kumar 82
		try {
83
			in.shop2020.model.v1.inventory.InventoryService.Client invClient = new InventoryClient().getClient();
84
			invClient.addOosStatusForItem(oosStatusMap, oosEvaluationDate.getTimeInMillis());
9791 rajveer 85
			System.out.println("P3");
6821 amar.kumar 86
		} catch(Exception e) {
9788 amar.kumar 87
			log.error("Error while adding OOS Status", e);
88
			sendMailForError("Error while adding OOS Status");
89
			return;
6821 amar.kumar 90
		}
91
		sendMailForSuccess(failedItemsList);
92
	}
93
 
94
	private static void sendMailForError(String errorMessage) {
95
 
96
        GmailUtils g = new GmailUtils();
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(MessagingException mex) {
100
        	try {
8183 amar.kumar 101
        		g.sendSSLMessage(tomail, "Error while Marking OutOfStock Statuses for Items", errorMessage, "cnc.center@shop2020.in", "5h0p2o2o", new ArrayList<File>());
6821 amar.kumar 102
        	} catch (Exception e) {
103
        		//TODO Logging
104
        	}
105
        }
106
	}
107
 
108
	private static void sendMailForSuccess(String message) {
109
 
110
        GmailUtils g = new GmailUtils();
111
        try {
8183 amar.kumar 112
	        g.sendSSLMessage(tomail, "Marked OutOfStock Statuses ", message, "cnc.center@shop2020.in", "5h0p2o2o", new ArrayList<File>());
6821 amar.kumar 113
        } catch(MessagingException mex) {
114
        		//TODO Logging
115
        }
116
	}
117
}