| 419 |
rajveer |
1 |
package in.shop2020.serving.utils;
|
|
|
2 |
|
| 17646 |
amit.gupta |
3 |
import java.io.BufferedReader;
|
| 20180 |
aman.kumar |
4 |
import java.io.DataOutputStream;
|
| 17646 |
amit.gupta |
5 |
import java.io.InputStreamReader;
|
| 20180 |
aman.kumar |
6 |
import java.net.HttpURLConnection;
|
|
|
7 |
import java.net.URL;
|
| 3209 |
vikas |
8 |
import java.util.ArrayList;
|
| 6603 |
anupam.sin |
9 |
import java.util.Arrays;
|
| 6998 |
amit.gupta |
10 |
import java.util.Collections;
|
| 12620 |
amit.gupta |
11 |
import java.util.Date;
|
| 6178 |
anupam.sin |
12 |
import java.util.HashMap;
|
| 3209 |
vikas |
13 |
import java.util.List;
|
| 6135 |
anupam.sin |
14 |
import java.util.Map;
|
| 20180 |
aman.kumar |
15 |
import java.util.Map.Entry;
|
| 3209 |
vikas |
16 |
|
| 4891 |
varun.gupt |
17 |
import javax.mail.internet.AddressException;
|
|
|
18 |
import javax.mail.internet.InternetAddress;
|
| 20180 |
aman.kumar |
19 |
import javax.net.ssl.HttpsURLConnection;
|
| 4891 |
varun.gupt |
20 |
|
| 3209 |
vikas |
21 |
import org.apache.commons.lang.StringUtils;
|
| 17646 |
amit.gupta |
22 |
import org.apache.http.HttpResponse;
|
|
|
23 |
import org.apache.http.client.HttpClient;
|
|
|
24 |
import org.apache.http.client.methods.HttpGet;
|
|
|
25 |
import org.apache.http.impl.client.DefaultHttpClient;
|
| 3209 |
vikas |
26 |
import org.apache.log4j.Logger;
|
|
|
27 |
|
| 20180 |
aman.kumar |
28 |
import in.shop2020.config.ConfigException;
|
|
|
29 |
import in.shop2020.model.v1.catalog.ExclusiveAffiliateItemInfo;
|
|
|
30 |
import in.shop2020.model.v1.order.LineItem;
|
|
|
31 |
import in.shop2020.model.v1.order.Order;
|
|
|
32 |
import in.shop2020.model.v1.order.RechargeOrderStatus;
|
|
|
33 |
import in.shop2020.model.v1.order.RechargePlan;
|
|
|
34 |
import in.shop2020.model.v1.order.RechargeType;
|
|
|
35 |
import in.shop2020.model.v1.user.Cart;
|
|
|
36 |
import in.shop2020.model.v1.user.Line;
|
|
|
37 |
import in.shop2020.thrift.clients.CatalogClient;
|
|
|
38 |
import in.shop2020.thrift.clients.TransactionClient;
|
|
|
39 |
import in.shop2020.thrift.clients.config.ConfigClient;
|
|
|
40 |
|
| 419 |
rajveer |
41 |
public class Utils {
|
| 2942 |
chandransh |
42 |
private static Logger logger = Logger.getLogger(Utils.class);
|
|
|
43 |
|
| 4892 |
varun.gupt |
44 |
public static final String EXPORT_ENTITIES_PATH = getExportPath();
|
| 5084 |
phani.kuma |
45 |
public static final String EXPORT_JAVASCRIPT_CONTENT_PATH = "/var/lib/tomcat6/webapps/export/javascripts/";
|
| 2306 |
vikas |
46 |
public static final long ROOT_CATEGORY = 10000;
|
|
|
47 |
public static final long MOBILE_PHONES_CATEGORY = 10001;
|
|
|
48 |
public static final long MOBILE_ACCESSORIES_CATEGORY = 10011;
|
| 3656 |
mandeep.dh |
49 |
public static final long TABLETS_CATEGORY = 10010;
|
|
|
50 |
public static final long LAPTOPS_CATEGORY = 10050;
|
| 5873 |
amit.gupta |
51 |
public static final long CAMERAS_CATEGORY = 11001;
|
| 6135 |
anupam.sin |
52 |
|
|
|
53 |
static Map<Long, String> providersMap;
|
| 6178 |
anupam.sin |
54 |
static Map<Long, String> mobileProvidersMap;
|
|
|
55 |
static Map<Long, String> dthProvidersMap;
|
| 12620 |
amit.gupta |
56 |
static Map<Long, ExclusiveAffiliateItemInfo> exclusiveAffiliateItems = null;
|
| 6178 |
anupam.sin |
57 |
static Map<Long, List<RechargePlan>> operatorPlanMap = new HashMap<Long, List<RechargePlan>>();
|
| 6204 |
rajveer |
58 |
private static Map<String, Map<String, String>> uriOperatorMap = new HashMap<String, Map<String,String>>();
|
| 12620 |
amit.gupta |
59 |
static long fifteenMinsCounter = 0l;
|
| 6204 |
rajveer |
60 |
|
| 6603 |
anupam.sin |
61 |
public static final List<RechargeOrderStatus> refundableRechargeStatusList = new ArrayList<RechargeOrderStatus>(Arrays.asList(new RechargeOrderStatus[]
|
|
|
62 |
{RechargeOrderStatus.RECHARGE_FAILED,
|
| 8932 |
anupam.sin |
63 |
RechargeOrderStatus.RECHARGE_FAILED_REFUNDED}));
|
| 6603 |
anupam.sin |
64 |
|
|
|
65 |
public static Map<RechargeOrderStatus, String> statusMap = new HashMap<RechargeOrderStatus, String>();
|
|
|
66 |
|
| 6998 |
amit.gupta |
67 |
//public static final List<String> multiValuedFacet = Arrays.asList("F_50006");
|
| 6887 |
amit.gupta |
68 |
|
| 6998 |
amit.gupta |
69 |
public static final List<String>rootfacetDefIDs = Arrays.asList("F_50010","F_50011","F_50002", "F_50001", "F_50028");
|
| 8878 |
amit.gupta |
70 |
public static final String availabilityFacet = "F_50028";
|
| 6998 |
amit.gupta |
71 |
public static final Map<String, String> FACET_LABEL_MAP = Collections.unmodifiableMap(
|
|
|
72 |
new HashMap<String, String>(){
|
|
|
73 |
/**
|
|
|
74 |
*
|
|
|
75 |
*/
|
|
|
76 |
private static final long serialVersionUID = 1L;
|
|
|
77 |
|
|
|
78 |
{
|
|
|
79 |
put("F_50010","Category");
|
|
|
80 |
put("F_50011","Sub Category");
|
|
|
81 |
put("F_50002","Price");
|
|
|
82 |
put("F_50001","Brand");
|
|
|
83 |
put("F_50006","Data Connectivity");
|
|
|
84 |
put("F_50007","Camera Resolution");
|
| 12111 |
amit.gupta |
85 |
put("F_50039","");
|
| 6998 |
amit.gupta |
86 |
put("F_50012","Display");
|
|
|
87 |
put("F_50013","Operating System");
|
|
|
88 |
put("F_50014","RAM");
|
|
|
89 |
put("F_50015","Storage Capacity");
|
|
|
90 |
put("F_50017","Processor");
|
|
|
91 |
put("F_50018","Capacity");
|
|
|
92 |
put("F_50019","Class");
|
|
|
93 |
put("F_50020","Capacity");
|
|
|
94 |
put("F_50021","Capacity");
|
|
|
95 |
put("F_50022","Type");
|
|
|
96 |
put("F_50023","Interface");
|
|
|
97 |
//Resolution for Composite
|
|
|
98 |
put("F_50024","Resolution");
|
|
|
99 |
put("F_50025","Optical Zoom");
|
|
|
100 |
put("F_50026","Display Size");
|
|
|
101 |
//Resolution for DSLR
|
|
|
102 |
put("F_50027","Resolution");
|
|
|
103 |
put("F_50028","Availability");
|
|
|
104 |
put("F_50031","Operating System");
|
|
|
105 |
put("F_50032","Screen Size"); //Screen size for mobiles
|
|
|
106 |
put("F_50033","Screen Size"); //Screen size for laptop
|
|
|
107 |
put("F_50034","Screen Size"); //Screen size for tablets
|
| 7132 |
amit.gupta |
108 |
put("F_50035","Operating System"); //Screen size for tablets
|
|
|
109 |
put("F_50036","Camera Resolution"); //Screen size for tablets
|
| 8880 |
amit.gupta |
110 |
put("F_50037","Voice Call Facility"); //Screen size for tablets
|
| 9472 |
amit.gupta |
111 |
put("F_50038","Mobile Type"); //Mobile Type
|
| 8880 |
amit.gupta |
112 |
put("F_50028","Availability");
|
| 6998 |
amit.gupta |
113 |
}
|
|
|
114 |
});
|
| 6135 |
anupam.sin |
115 |
|
|
|
116 |
static {
|
| 6603 |
anupam.sin |
117 |
statusMap.put(RechargeOrderStatus.PAYMENT_FAILED, "Payment Unsuccessful");
|
|
|
118 |
statusMap.put(RechargeOrderStatus.PAYMENT_SUCCESSFUL, "Processing Recharge");
|
|
|
119 |
statusMap.put(RechargeOrderStatus.RECHARGE_FAILED, "Recharge Failed");
|
|
|
120 |
statusMap.put(RechargeOrderStatus.RECHARGE_FAILED_REFUNDED, "Recharge Failed");
|
|
|
121 |
statusMap.put(RechargeOrderStatus.RECHARGE_SUCCESSFUL, "Recharge Successful");
|
|
|
122 |
statusMap.put(RechargeOrderStatus.REFUNDED, "Amount Refunded");
|
|
|
123 |
statusMap.put(RechargeOrderStatus.PARTIALLY_REFUNDED, "Amount Refunded");
|
|
|
124 |
|
| 6135 |
anupam.sin |
125 |
TransactionClient tcl;
|
|
|
126 |
try {
|
|
|
127 |
tcl = new TransactionClient();
|
| 6206 |
rajveer |
128 |
mobileProvidersMap = tcl.getClient().getServiceProviders(RechargeType.MOBILE, true);
|
|
|
129 |
dthProvidersMap = tcl.getClient().getServiceProviders(RechargeType.DTH, true);
|
| 6178 |
anupam.sin |
130 |
// providersMap.putAll(mobileProvidersMap);
|
|
|
131 |
// providersMap.putAll(dthProvidersMap);
|
|
|
132 |
for (Long operatorId : mobileProvidersMap.keySet()) {
|
|
|
133 |
List<RechargePlan> plans = tcl.getClient().getPlansForOperator(operatorId);
|
|
|
134 |
if (!plans.isEmpty()) {
|
|
|
135 |
operatorPlanMap.put(operatorId, plans);
|
|
|
136 |
}
|
|
|
137 |
}
|
| 6204 |
rajveer |
138 |
for (Long operatorId : mobileProvidersMap.keySet()) {
|
|
|
139 |
Map<String,String> valueMap = new HashMap<String,String>();
|
|
|
140 |
valueMap.put("Id", operatorId+"");
|
|
|
141 |
valueMap.put("Name", mobileProvidersMap.get(operatorId)+"");
|
|
|
142 |
valueMap.put("ServiceType", "1");
|
|
|
143 |
valueMap.put("ServiceName", "Prepaid Mobile");
|
|
|
144 |
uriOperatorMap.put(mobileProvidersMap.get(operatorId).toLowerCase().trim().replace(" ", "-")+"-online-recharge", valueMap);
|
|
|
145 |
}
|
|
|
146 |
for (Long operatorId : dthProvidersMap.keySet()) {
|
|
|
147 |
Map<String,String> valueMap = new HashMap<String,String>();
|
|
|
148 |
valueMap.put("Id", operatorId+"");
|
|
|
149 |
valueMap.put("Name", dthProvidersMap.get(operatorId)+"");
|
|
|
150 |
valueMap.put("ServiceType", "2");
|
|
|
151 |
valueMap.put("ServiceName", "DTH TV");
|
|
|
152 |
uriOperatorMap.put(dthProvidersMap.get(operatorId).toLowerCase().trim().replace(" ", "-")+"-online-recharge", valueMap);
|
|
|
153 |
}
|
| 6603 |
anupam.sin |
154 |
|
| 6204 |
rajveer |
155 |
Map<String,String> valueMap = new HashMap<String,String>();
|
|
|
156 |
valueMap.put("Id", "0");
|
|
|
157 |
valueMap.put("Name", "Mobile and DTH");
|
|
|
158 |
valueMap.put("ServiceType", "1");
|
|
|
159 |
valueMap.put("ServiceName", "");
|
|
|
160 |
uriOperatorMap.put("recharge", valueMap);
|
| 6178 |
anupam.sin |
161 |
// providersMap.putAll(tcl.getClient().getServiceProviders(RechargeType.DTH));
|
| 6603 |
anupam.sin |
162 |
|
|
|
163 |
|
| 6135 |
anupam.sin |
164 |
} catch (Exception e) {
|
|
|
165 |
logger.error("Could not get providers", e);
|
|
|
166 |
}
|
|
|
167 |
}
|
|
|
168 |
|
|
|
169 |
|
|
|
170 |
|
| 1453 |
chandransh |
171 |
private static String getExportPath(){
|
| 1457 |
chandransh |
172 |
String exportPath=null;
|
| 1453 |
chandransh |
173 |
ConfigClient client = ConfigClient.getClient();
|
|
|
174 |
try{
|
|
|
175 |
exportPath = client.get("export_entities_path");
|
|
|
176 |
}catch(ConfigException ce){
|
| 2942 |
chandransh |
177 |
logger.error("Unable to read export path from the config client: ", ce);
|
|
|
178 |
logger.warn("Setting the default export path");
|
| 1475 |
chandransh |
179 |
exportPath = "/var/lib/tomcat6/webapps/export/html/entities/";
|
| 1453 |
chandransh |
180 |
}
|
|
|
181 |
return exportPath;
|
|
|
182 |
}
|
|
|
183 |
|
| 12620 |
amit.gupta |
184 |
|
|
|
185 |
public static Map<Long, ExclusiveAffiliateItemInfo> getExclusiveAffiliateItems(){
|
|
|
186 |
try{
|
|
|
187 |
CatalogClient client = new CatalogClient();
|
| 12626 |
amit.gupta |
188 |
Long currentTime = new Date().getTime();
|
|
|
189 |
if(exclusiveAffiliateItems == null || fifteenMinsCounter < currentTime) {
|
| 12620 |
amit.gupta |
190 |
exclusiveAffiliateItems = client.getClient().getExAffiliateItemInfo();
|
|
|
191 |
fifteenMinsCounter = new Date().getTime() + 15*60000;
|
|
|
192 |
}
|
|
|
193 |
}catch(Exception ce){
|
|
|
194 |
logger.error("Unable to fetch exclusive items from catalog", ce);
|
|
|
195 |
}
|
|
|
196 |
return exclusiveAffiliateItems;
|
|
|
197 |
}
|
|
|
198 |
|
| 822 |
vikas |
199 |
public static boolean validatePin(String pincode) {
|
|
|
200 |
int pin;
|
|
|
201 |
try {
|
|
|
202 |
pin = Integer.parseInt(pincode);
|
|
|
203 |
}
|
|
|
204 |
catch (NumberFormatException e) {
|
|
|
205 |
return false;
|
|
|
206 |
}
|
|
|
207 |
|
|
|
208 |
if (pin < 100000 || pin > 999999) {
|
|
|
209 |
return false;
|
|
|
210 |
}
|
|
|
211 |
return true;
|
|
|
212 |
}
|
| 4891 |
varun.gupt |
213 |
|
|
|
214 |
public static boolean isValidEmail(String emailAddress) {
|
|
|
215 |
|
|
|
216 |
if(emailAddress == null || emailAddress.isEmpty()) {
|
|
|
217 |
return false;
|
|
|
218 |
}
|
|
|
219 |
try {
|
|
|
220 |
InternetAddress address = new InternetAddress(emailAddress, true);
|
|
|
221 |
address.validate();
|
|
|
222 |
return true;
|
|
|
223 |
|
|
|
224 |
} catch (AddressException e) {
|
|
|
225 |
logger.error(emailAddress + " is not valid email address ", e);
|
|
|
226 |
return false;
|
|
|
227 |
}
|
|
|
228 |
}
|
| 822 |
vikas |
229 |
|
|
|
230 |
public static boolean validatePhone(String phone) {
|
| 839 |
vikas |
231 |
long iPhone;
|
| 822 |
vikas |
232 |
try {
|
| 839 |
vikas |
233 |
iPhone = Long.parseLong(phone);
|
| 822 |
vikas |
234 |
}
|
|
|
235 |
catch (NumberFormatException e) {
|
|
|
236 |
return false;
|
|
|
237 |
}
|
| 839 |
vikas |
238 |
|
|
|
239 |
if (iPhone < 1000000000l || iPhone > 9999999999l) {
|
|
|
240 |
return false;
|
|
|
241 |
}
|
| 822 |
vikas |
242 |
return true;
|
|
|
243 |
}
|
| 3209 |
vikas |
244 |
|
|
|
245 |
public static String getItemIdStringInCart(Cart cart) {
|
|
|
246 |
List<String> itemIds = new ArrayList<String>();
|
|
|
247 |
for (Line line : cart.getLines()) {
|
|
|
248 |
itemIds.add(Long.toString(line.getItemId()));
|
|
|
249 |
}
|
|
|
250 |
return StringUtils.join(itemIds, '_');
|
|
|
251 |
}
|
|
|
252 |
|
| 17646 |
amit.gupta |
253 |
public static String fetchUrl(String url) throws Exception{
|
|
|
254 |
HttpClient client = new DefaultHttpClient();
|
|
|
255 |
HttpGet request = new HttpGet(url);
|
|
|
256 |
HttpResponse response = client.execute(request);
|
|
|
257 |
StringBuffer textView = new StringBuffer();
|
|
|
258 |
|
|
|
259 |
// Get the response
|
|
|
260 |
BufferedReader rd = new BufferedReader
|
|
|
261 |
(new InputStreamReader(response.getEntity().getContent()));
|
|
|
262 |
|
|
|
263 |
String line = "";
|
|
|
264 |
while ((line = rd.readLine()) != null) {
|
|
|
265 |
textView.append(line);
|
|
|
266 |
}
|
|
|
267 |
return textView.toString();
|
|
|
268 |
}
|
|
|
269 |
|
| 3209 |
vikas |
270 |
public static String getItemIdStringFromOrders(List<Order> orders) {
|
|
|
271 |
List<String> itemIds = new ArrayList<String>();
|
|
|
272 |
if (orders != null) {
|
|
|
273 |
for (Order order : orders) {
|
|
|
274 |
for (LineItem lItem : order.getLineitems()) {
|
|
|
275 |
itemIds.add(Long.toString(lItem.getItem_id()));
|
|
|
276 |
}
|
|
|
277 |
}
|
|
|
278 |
}
|
|
|
279 |
return StringUtils.join(itemIds, '_');
|
| 6135 |
anupam.sin |
280 |
}
|
|
|
281 |
|
|
|
282 |
|
|
|
283 |
|
|
|
284 |
public static String getRechargeProviderName(long providerId) {
|
|
|
285 |
return providersMap.get(providerId);
|
|
|
286 |
}
|
| 6178 |
anupam.sin |
287 |
|
|
|
288 |
public static Map<Long, String> getMobileProvidersMap() {
|
|
|
289 |
return mobileProvidersMap;
|
|
|
290 |
}
|
|
|
291 |
|
|
|
292 |
public static Map<Long, String> getDthProvidersMap() {
|
|
|
293 |
return dthProvidersMap;
|
|
|
294 |
}
|
|
|
295 |
|
|
|
296 |
public static Map<Long, List<RechargePlan>> getOperatorPlanMap() {
|
|
|
297 |
return operatorPlanMap;
|
|
|
298 |
}
|
| 6204 |
rajveer |
299 |
|
| 6208 |
rajveer |
300 |
public static Map<String, Map<String, String>> getUriOperatorMap(){
|
|
|
301 |
return uriOperatorMap;
|
|
|
302 |
}
|
|
|
303 |
|
| 6204 |
rajveer |
304 |
public static Map<String, String> getOperatorByUri(String uri){
|
|
|
305 |
return uriOperatorMap.get(uri);
|
|
|
306 |
}
|
| 20180 |
aman.kumar |
307 |
|
|
|
308 |
private final String USER_AGENT = "Mozilla/5.0";
|
|
|
309 |
public final static String METHOD_POST = "POST";
|
|
|
310 |
public final static String METHOD_GET = "GET";
|
|
|
311 |
|
|
|
312 |
|
|
|
313 |
public String callRestApi(String apiUrl,String method,Map<String, String> paramsMap, Map<String, String> requestProperty){
|
|
|
314 |
StringBuffer response = new StringBuffer();
|
| 20185 |
aman.kumar |
315 |
StringBuffer urlParameters= new StringBuffer("");
|
| 20180 |
aman.kumar |
316 |
try{
|
|
|
317 |
|
|
|
318 |
if (paramsMap != null) {
|
|
|
319 |
for (Entry<String, String> entry : paramsMap.entrySet()) {
|
|
|
320 |
urlParameters.append(entry.getKey() + "=" + entry.getValue() + "&");
|
|
|
321 |
}
|
|
|
322 |
}
|
|
|
323 |
if (METHOD_GET.equalsIgnoreCase(method)) {
|
|
|
324 |
apiUrl += "?"+urlParameters.toString();
|
|
|
325 |
}
|
|
|
326 |
URL obj = new URL(apiUrl);
|
|
|
327 |
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
|
|
|
328 |
if (apiUrl.startsWith("https")) {
|
|
|
329 |
con = (HttpsURLConnection) obj.openConnection();
|
|
|
330 |
}
|
|
|
331 |
|
|
|
332 |
// add reuqest header
|
|
|
333 |
con.setRequestMethod(method);
|
|
|
334 |
con.setRequestProperty("User-Agent", USER_AGENT);
|
|
|
335 |
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
|
|
|
336 |
|
|
|
337 |
if (requestProperty != null) {
|
|
|
338 |
for (Entry<String, String> entry : requestProperty.entrySet()) {
|
|
|
339 |
con.setRequestProperty(entry.getKey(), entry.getValue());
|
|
|
340 |
}
|
|
|
341 |
}
|
|
|
342 |
// Send Post request
|
|
|
343 |
if (METHOD_POST.equalsIgnoreCase(method)) {
|
|
|
344 |
con.setDoOutput(true);
|
|
|
345 |
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
|
|
|
346 |
wr.writeBytes(urlParameters.toString());
|
|
|
347 |
wr.flush();
|
|
|
348 |
wr.close();
|
|
|
349 |
}
|
|
|
350 |
|
|
|
351 |
int responseCode = con.getResponseCode();
|
|
|
352 |
System.out.println("\nSending request to URL : " + apiUrl);
|
|
|
353 |
System.out.println("Post parameters : " + urlParameters);
|
|
|
354 |
System.out.println("Response Code : " + responseCode);
|
|
|
355 |
|
|
|
356 |
if(responseCode==200){
|
|
|
357 |
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
|
|
|
358 |
String inputLine;
|
|
|
359 |
|
|
|
360 |
while ((inputLine = in.readLine()) != null) {
|
|
|
361 |
response.append(inputLine);
|
|
|
362 |
}
|
|
|
363 |
in.close();
|
|
|
364 |
|
|
|
365 |
}
|
|
|
366 |
|
|
|
367 |
// print result
|
|
|
368 |
System.out.println(response.toString());
|
|
|
369 |
}catch(Exception e){
|
|
|
370 |
|
|
|
371 |
}
|
|
|
372 |
return response.toString();
|
|
|
373 |
}
|
|
|
374 |
|
|
|
375 |
public static void main(String args[]){
|
|
|
376 |
|
|
|
377 |
//new Utils().callRestApi("https://graph.facebook.com/debug_token", Utils.METHOD_GET, paramsMap, null);
|
|
|
378 |
}
|
| 4891 |
varun.gupt |
379 |
}
|