| 419 |
rajveer |
1 |
package in.shop2020.serving.utils;
|
|
|
2 |
|
| 637 |
rajveer |
3 |
import java.io.ByteArrayInputStream;
|
|
|
4 |
import java.io.ByteArrayOutputStream;
|
|
|
5 |
import java.io.ObjectInputStream;
|
|
|
6 |
import java.io.ObjectOutputStream;
|
| 421 |
rajveer |
7 |
import java.util.List;
|
|
|
8 |
|
| 424 |
rajveer |
9 |
import in.shop2020.model.v1.catalog.Item;
|
|
|
10 |
import in.shop2020.model.v1.catalog.InventoryService.Client;
|
| 741 |
rajveer |
11 |
import in.shop2020.model.v1.order.LineItem;
|
|
|
12 |
import in.shop2020.model.v1.order.Order;
|
|
|
13 |
import in.shop2020.model.v1.order.Transaction;
|
| 555 |
chandransh |
14 |
import in.shop2020.model.v1.user.Cart;
|
|
|
15 |
|
|
|
16 |
import in.shop2020.model.v1.user.Line;
|
| 595 |
rajveer |
17 |
import in.shop2020.model.v1.user.Sex;
|
| 555 |
chandransh |
18 |
import in.shop2020.model.v1.user.ShoppingCartException;
|
|
|
19 |
import in.shop2020.model.v1.user.User;
|
| 419 |
rajveer |
20 |
import in.shop2020.model.v1.user.UserContextException;
|
| 681 |
rajveer |
21 |
import in.shop2020.model.v1.user.WidgetException;
|
| 421 |
rajveer |
22 |
import in.shop2020.thrift.clients.CatalogServiceClient;
|
| 741 |
rajveer |
23 |
import in.shop2020.thrift.clients.TransactionServiceClient;
|
| 419 |
rajveer |
24 |
import in.shop2020.thrift.clients.UserContextServiceClient;
|
|
|
25 |
|
|
|
26 |
import org.apache.thrift.TException;
|
|
|
27 |
|
|
|
28 |
public class Utils {
|
| 649 |
chandransh |
29 |
//FIXME: Read this path from the config server
|
| 637 |
rajveer |
30 |
public static final String EXPORT_ENTITIES_PATH = "/var/lib/tomcat6/webapps/export/html/entities/";
|
| 419 |
rajveer |
31 |
|
| 424 |
rajveer |
32 |
public static double getPaymentAmount(long cartId){
|
|
|
33 |
double totalAmount = 0;
|
|
|
34 |
|
|
|
35 |
Cart cart;
|
|
|
36 |
try {
|
| 555 |
chandransh |
37 |
UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
|
|
|
38 |
in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
|
| 536 |
rajveer |
39 |
|
| 555 |
chandransh |
40 |
cart = userClient.getCart(cartId);
|
| 424 |
rajveer |
41 |
|
|
|
42 |
List<Line> lineItems = cart.getLines();
|
|
|
43 |
|
|
|
44 |
for (Line line : lineItems) {
|
|
|
45 |
long productId = line.getItemId();
|
| 637 |
rajveer |
46 |
totalAmount = totalAmount + line.getQuantity() * Utils.getItemPrice(productId);
|
| 424 |
rajveer |
47 |
}
|
|
|
48 |
|
|
|
49 |
} catch (ShoppingCartException e) {
|
|
|
50 |
e.printStackTrace();
|
|
|
51 |
} catch (TException e) {
|
|
|
52 |
e.printStackTrace();
|
| 536 |
rajveer |
53 |
} catch (Exception e) {
|
|
|
54 |
e.printStackTrace();
|
| 424 |
rajveer |
55 |
}
|
|
|
56 |
|
|
|
57 |
return totalAmount;
|
|
|
58 |
}
|
|
|
59 |
|
| 637 |
rajveer |
60 |
public static double getItemPrice(long itemId){
|
| 424 |
rajveer |
61 |
CatalogServiceClient catalogServiceClient = null;
|
|
|
62 |
Client client = null;
|
|
|
63 |
Double itemPrice = 0.0;
|
|
|
64 |
try {
|
|
|
65 |
catalogServiceClient = new CatalogServiceClient();
|
|
|
66 |
client = catalogServiceClient.getClient();
|
| 637 |
rajveer |
67 |
Item item = client.getItem(itemId);
|
| 517 |
rajveer |
68 |
itemPrice = item.getSellingPrice();
|
| 536 |
rajveer |
69 |
|
| 424 |
rajveer |
70 |
}
|
|
|
71 |
catch(Exception e){
|
|
|
72 |
e.printStackTrace();
|
|
|
73 |
}
|
|
|
74 |
return itemPrice;
|
|
|
75 |
}
|
|
|
76 |
|
| 449 |
rajveer |
77 |
|
| 507 |
rajveer |
78 |
|
| 637 |
rajveer |
79 |
public static void storeCategories(Object obj) throws Exception {
|
|
|
80 |
|
|
|
81 |
ByteArrayOutputStream bStream = new ByteArrayOutputStream();
|
|
|
82 |
ObjectOutputStream oStream = new ObjectOutputStream( bStream );
|
|
|
83 |
oStream.writeObject ( obj );
|
|
|
84 |
|
|
|
85 |
byte[] byteVal = bStream. toByteArray();
|
|
|
86 |
|
|
|
87 |
CatalogServiceClient catalogServiceClient = new CatalogServiceClient();
|
|
|
88 |
Client client = catalogServiceClient.getClient();
|
|
|
89 |
|
|
|
90 |
client.putCategoryObject(byteVal);
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
public static Object getCategories() throws Exception {
|
|
|
94 |
CatalogServiceClient catalogServiceClient = new CatalogServiceClient();
|
|
|
95 |
Client client = catalogServiceClient.getClient();
|
|
|
96 |
|
|
|
97 |
byte[] byteVal = client.getCategoryObject();
|
|
|
98 |
|
|
|
99 |
Object obj = null;
|
|
|
100 |
ObjectInputStream in = null;
|
|
|
101 |
try {
|
|
|
102 |
ByteArrayInputStream bStream = new ByteArrayInputStream(byteVal);
|
|
|
103 |
in = new ObjectInputStream(bStream);
|
|
|
104 |
obj = in.readObject();
|
|
|
105 |
}
|
|
|
106 |
finally {
|
|
|
107 |
if(in != null) {
|
|
|
108 |
in.close();
|
|
|
109 |
}
|
|
|
110 |
}
|
|
|
111 |
return obj;
|
|
|
112 |
}
|
| 649 |
chandransh |
113 |
|
|
|
114 |
// public static void main(String args[]) throws Exception{
|
|
|
115 |
// // to store categories
|
|
|
116 |
// Map<Long, Category> categories = new HashMap<Long, Category>();
|
|
|
117 |
// Map<Long, in.shop2020.metamodel.definitions.Category> cmsCategories = Catalog.getInstance().getDefinitionsContainer().getCategories();
|
|
|
118 |
// for(in.shop2020.metamodel.definitions.Category cmsCategory: cmsCategories.values()){
|
|
|
119 |
// Category category = new Category(cmsCategory.getID());
|
|
|
120 |
// category.setLabel(cmsCategory.getLabel());
|
|
|
121 |
// if(cmsCategory.getParentCategory()!=null){
|
|
|
122 |
// category.setParentCategoryId(cmsCategory.getParentCategory().getID());
|
|
|
123 |
// }
|
|
|
124 |
// if(cmsCategory.getChildrenCategory()!=null){
|
|
|
125 |
// for(in.shop2020.metamodel.definitions.Category childCategory: cmsCategory.getChildrenCategory()){
|
|
|
126 |
// category.addChild(childCategory.getID());
|
|
|
127 |
// }
|
|
|
128 |
// }
|
|
|
129 |
// categories.put(cmsCategory.getID(), category);
|
|
|
130 |
// }
|
|
|
131 |
// Utils.storeCategories(categories);
|
|
|
132 |
// // to get categories
|
|
|
133 |
// //Map<Long, Category>
|
|
|
134 |
// categories = (Map<Long, Category>)Utils.getCategories();
|
|
|
135 |
// System.out.println("hello");
|
|
|
136 |
// }
|
|
|
137 |
|
| 419 |
rajveer |
138 |
}
|
| 745 |
chandransh |
139 |
|