Subversion Repositories SmartDukaan

Rev

Rev 13244 | 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
 
13244 manish.sha 3
import java.io.BufferedWriter;
6821 amar.kumar 4
import java.io.File;
13244 manish.sha 5
import java.io.FileWriter;
6
import java.io.IOException;
6821 amar.kumar 7
import java.util.ArrayList;
8
import java.util.Calendar;
13244 manish.sha 9
import java.util.Date;
6821 amar.kumar 10
import java.util.HashMap;
11
import java.util.List;
12
import java.util.Map;
13
 
14
import javax.mail.MessagingException;
15
 
13244 manish.sha 16
import org.apache.commons.lang.StringUtils;
9788 amar.kumar 17
import org.apache.commons.logging.Log;
18
import org.apache.commons.logging.LogFactory;
6821 amar.kumar 19
import org.apache.thrift.transport.TTransportException;
20
 
21
import in.shop2020.model.v1.catalog.CatalogService.Client;
22
import in.shop2020.model.v1.catalog.Item;
23
import in.shop2020.model.v1.catalog.status;
24
import in.shop2020.thrift.clients.CatalogClient;
25
import in.shop2020.thrift.clients.InventoryClient;
26
import in.shop2020.thrift.clients.UserClient;
27
import in.shop2020.utils.GmailUtils;
28
 
29
public class OosStatusMarker {
9788 amar.kumar 30
	private static Log log = LogFactory.getLog(OosStatusMarker.class);
31
 
6821 amar.kumar 32
	private static final Integer OOS_STATUS_CUTOFF_HOUR = 23;
33
	private static final Integer OOS_STATUS_CUTOFF_MINUTE = 59;
34
 
13244 manish.sha 35
	private static java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
36
 
20003 aman.kumar 37
	private static final String[] tomail = {"aman.kumar@shop2020.in", "himanshu.pandey@shop2020.in", "khushal.bhatia@shop2020.in", "amit.gupta@shop2020.in","vikram.raghav@shop2020.in","kshitij.sood@shop2020.in"};
8183 amar.kumar 38
	//private static final String[] tomail = {"amar.kumar@shop2020.in"};
39
 
13244 manish.sha 40
	public static void main(String[] args) throws TTransportException, IOException {
41
		File file = new File("/home/manish/alerts/oosmarker.txt");
42
 
6821 amar.kumar 43
		Calendar oosEvaluationDate = Calendar.getInstance();
44
		Calendar cartAdditionStartDate = Calendar.getInstance();
12064 kshitij.so 45
		oosEvaluationDate.add(Calendar.DATE, -1);
6821 amar.kumar 46
		oosEvaluationDate.set(oosEvaluationDate.get(Calendar.YEAR), 
47
				oosEvaluationDate.get(Calendar.MONTH), oosEvaluationDate.get(Calendar.DATE), 
48
				OOS_STATUS_CUTOFF_HOUR , OOS_STATUS_CUTOFF_MINUTE, 0);
6844 amar.kumar 49
		cartAdditionStartDate.set(oosEvaluationDate.get(Calendar.YEAR), 
50
				oosEvaluationDate.get(Calendar.MONTH), oosEvaluationDate.get(Calendar.DATE) -1, 
51
				OOS_STATUS_CUTOFF_HOUR , OOS_STATUS_CUTOFF_MINUTE, 0);
6821 amar.kumar 52
		Map<Long, Boolean> oosStatusMap = new HashMap<Long, Boolean>();
53
 
54
		List<Item> items = null;
55
		try {
56
			Client catalogClient = new CatalogClient().getClient();
57
			items = catalogClient.getAllAliveItems();
58
		} catch (Exception e) {
59
			try {
60
				Client catalogClient = new CatalogClient().getClient();
61
				items = catalogClient.getAllAliveItems();
62
			} catch (Exception ex) {
9788 amar.kumar 63
				log.error("Exception while getting all items ", ex);
64
				sendMailForError("Exception while getting all items \n" + ex);
6821 amar.kumar 65
			}
66
		}
67
 
9791 rajveer 68
		System.out.println("P1");
6821 amar.kumar 69
		String failedItemsList = "";
70
		in.shop2020.model.v1.user.UserContextService.Client client = new UserClient().getClient();
9791 rajveer 71
		try {
72
			List<Long> cartItems = client.getProductsAddedToCart(cartAdditionStartDate.getTimeInMillis(), oosEvaluationDate.getTimeInMillis());
73
			for(Item item : items) {
74
				if (item.getItemStatus() == status.ACTIVE) {
75
					oosStatusMap.put(item.getId(), false);
76
				} else {
77
					if(cartItems.contains(item.getId())) {
6821 amar.kumar 78
						oosStatusMap.put(item.getId(), false);
79
					} else {
80
						oosStatusMap.put(item.getId(), true);
81
					}
82
				}
83
			}
9791 rajveer 84
		} catch (Exception e) {
85
			log.error("Exception while getting all items list added to cart", e);
86
			sendMailForError("Exception while getting all items list added to cart \n");
6821 amar.kumar 87
		}
9791 rajveer 88
		System.out.println("P2");
6821 amar.kumar 89
		try {
90
			in.shop2020.model.v1.inventory.InventoryService.Client invClient = new InventoryClient().getClient();
91
			invClient.addOosStatusForItem(oosStatusMap, oosEvaluationDate.getTimeInMillis());
9791 rajveer 92
			System.out.println("P3");
6821 amar.kumar 93
		} catch(Exception e) {
9788 amar.kumar 94
			log.error("Error while adding OOS Status", e);
95
			sendMailForError("Error while adding OOS Status");
96
			return;
6821 amar.kumar 97
		}
13244 manish.sha 98
		BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
99
 
100
		bufferedWriter.write(StringUtils.join(new String[] {"OosStatusUpdationDate",sdf.format(new Date())}, '\t'));
101
		bufferedWriter.close();
6821 amar.kumar 102
		sendMailForSuccess(failedItemsList);
103
	}
104
 
105
	private static void sendMailForError(String errorMessage) {
106
 
107
        GmailUtils g = new GmailUtils();
108
        try {
8183 amar.kumar 109
	        g.sendSSLMessage(tomail, "Error while Marking OutOfStock Statuses for Items", errorMessage, "cnc.center@shop2020.in", "5h0p2o2o", new ArrayList<File>());
6821 amar.kumar 110
        } catch(MessagingException mex) {
111
        	try {
8183 amar.kumar 112
        		g.sendSSLMessage(tomail, "Error while Marking OutOfStock Statuses for Items", errorMessage, "cnc.center@shop2020.in", "5h0p2o2o", new ArrayList<File>());
6821 amar.kumar 113
        	} catch (Exception e) {
114
        		//TODO Logging
115
        	}
116
        }
117
	}
118
 
119
	private static void sendMailForSuccess(String message) {
120
 
121
        GmailUtils g = new GmailUtils();
122
        try {
8183 amar.kumar 123
	        g.sendSSLMessage(tomail, "Marked OutOfStock Statuses ", message, "cnc.center@shop2020.in", "5h0p2o2o", new ArrayList<File>());
6821 amar.kumar 124
        } catch(MessagingException mex) {
125
        		//TODO Logging
126
        }
127
	}
128
}