Subversion Repositories SmartDukaan

Rev

Rev 3128 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3128 Rev 4142
Line 1... Line 1...
1
package in.shop2020.serving.controllers;
1
package in.shop2020.serving.controllers;
2
 
2
 
-
 
3
import in.shop2020.model.v1.catalog.InventoryServiceException;
3
import in.shop2020.model.v1.catalog.Item;
4
import in.shop2020.model.v1.catalog.Item;
4
import in.shop2020.model.v1.user.Line;
5
import in.shop2020.model.v1.user.Line;
-
 
6
import in.shop2020.model.v1.user.ShoppingCartException;
-
 
7
import in.shop2020.model.v1.user.UserContextException;
5
import in.shop2020.thrift.clients.CatalogClient;
8
import in.shop2020.thrift.clients.CatalogClient;
6
import in.shop2020.thrift.clients.UserClient;
9
import in.shop2020.thrift.clients.UserClient;
-
 
10
import in.shop2020.utils.ModelUtils;
7
 
11
 
8
import java.text.SimpleDateFormat;
-
 
9
import java.util.ArrayList;
-
 
10
import java.util.Date;
-
 
11
import java.util.HashMap;
-
 
12
import java.util.List;
12
import java.util.List;
13
import java.util.Map;
-
 
14
import java.util.TimeZone;
-
 
15
 
13
 
16
import org.apache.log4j.Logger;
14
import org.apache.log4j.Logger;
-
 
15
import org.apache.thrift.TException;
17
 
16
 
18
/**
17
/**
19
 * @author vikas
18
 * @author vikas
20
 *
19
 *
21
 */
20
 */
22
@SuppressWarnings("serial")
21
@SuppressWarnings("serial")
23
public class UserCartController extends BaseController {
22
public class UserCartController extends BaseController {
24
    private static Logger log = Logger.getLogger(Class.class);
23
    private static Logger log = Logger.getLogger(Class.class);
25
    private long userId;
24
    private long userId;
26
    private long cartId;
25
    private long cartId;
27
    private List<Map<String, String>> userLines;
26
    private List<Line> lines;
28
 
27
 
29
    public UserCartController(){
28
    public String index() {
30
        super();
29
        try {
-
 
30
            UserClient userServiceClient = new UserClient();
-
 
31
            in.shop2020.model.v1.user.UserContextService.Client userClient = userServiceClient.getClient();
-
 
32
 
-
 
33
            if (userClient.getUserById(userId).getActiveCartId() == cartId) {
-
 
34
                lines = userClient.getCart(cartId).getLines();
-
 
35
            }
-
 
36
        } catch (UserContextException e) {
-
 
37
            addActionError("Error fetching cart details");
-
 
38
        } catch (TException e) {
-
 
39
            addActionError("Error fetching cart details");
-
 
40
        } catch (ShoppingCartException e) {
-
 
41
            addActionError("Error fetching cart details");
-
 
42
        }
-
 
43
 
-
 
44
        return INDEX;
31
    }
45
    }
32
 
46
 
33
    public String index() throws Exception {
47
    public String getProductName(Line line) {
34
    	UserClient userServiceClient = new UserClient();
-
 
35
        in.shop2020.model.v1.user.UserContextService.Client userClient = userServiceClient.getClient();
-
 
36
        
-
 
37
        CatalogClient catalogServiceClient = new CatalogClient();
-
 
38
        in.shop2020.model.v1.catalog.InventoryService.Client catalogClient = catalogServiceClient.getClient();
-
 
39
        
-
 
40
        List<Line> lines = userClient.getCart(cartId).getLines();
-
 
41
        
-
 
42
        userLines = new ArrayList<Map<String,String>>();
-
 
43
        if (userClient.getUserById(userId).getActiveCartId() != cartId) {
-
 
44
            return "index";
48
        String productName = "";
45
        }
-
 
46
 
49
 
47
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
-
 
48
        sdf.setTimeZone(TimeZone.getTimeZone("IST"));
-
 
49
        
50
        try {
50
        for (Line line : lines) {
51
            CatalogClient catalogServiceClient = new CatalogClient();
51
            Map<String, String> lineMap = new HashMap<String, String>();
52
            in.shop2020.model.v1.catalog.InventoryService.Client catalogClient = catalogServiceClient
52
            lineMap.put("itemid", Long.toString(line.getItemId()));
53
                    .getClient();
53
            Item item = catalogClient.getItem(line.getItemId());
54
            Item item = catalogClient.getItem(line.getItemId());
54
            
-
 
55
            StringBuilder productName = new StringBuilder();
-
 
56
            if (item.getBrand() != null && !item.getBrand().isEmpty()) {
-
 
57
                productName.append(item.getBrand() + " ");
-
 
58
            }
-
 
59
            if (item.getModelName() != null && !item.getModelName().isEmpty()) {
-
 
60
                productName.append(item.getModelName()  + " ");
55
            productName = ModelUtils.extractProductNameFromItem(item);
61
            }
56
        }
62
            if (item.getModelNumber() != null && !item.getModelNumber().isEmpty()) {
-
 
63
                productName.append(item.getModelNumber()  + " ");
-
 
64
            }
57
        catch (TException e) {
65
            lineMap.put("itemname", productName.toString());
-
 
66
            lineMap.put("quantity", Double.toString(line.getQuantity()));
-
 
67
            lineMap.put("actualprice", Double.toString(line.getActualPrice()));
-
 
68
            lineMap.put("discountedprice", Double.toString(line.getDiscountedPrice()));
-
 
69
            lineMap.put("estimate", Integer.toString(line.getEstimate()));
58
            log.error("Could not fetch Item id: " + line.getItemId(), e);
70
            lineMap.put("createdon", sdf.format((new Date(line.getCreatedOn()))));
-
 
71
            
-
 
72
            userLines.add(lineMap);
-
 
73
        }
59
        }
-
 
60
        catch (InventoryServiceException e) {
-
 
61
            log.error("Could not fetch Item id: " + line.getItemId(), e);
-
 
62
        }
-
 
63
 
-
 
64
        return productName;
-
 
65
    }
-
 
66
 
-
 
67
    public int convertDouble(double value) {
74
        return "index";
68
        return (int)value;
75
    }
69
    }
76
 
70
 
77
    public void setUserId(String userId) {
71
    public void setUserId(String userId) {
78
        try {
72
        try {
79
            this.userId = Long.parseLong(userId);
73
            this.userId = Long.parseLong(userId);
Line 94... Line 88...
94
 
88
 
95
    public Long getUserId() {
89
    public Long getUserId() {
96
        return userId;
90
        return userId;
97
    }
91
    }
98
 
92
 
99
    public List<Map<String, String>> getUserLines() {
93
    public List<Line> getLines() {
100
        return userLines;
94
        return lines;
-
 
95
    }
-
 
96
 
-
 
97
    public void setLines(List<Line> lines) {
-
 
98
        this.lines = lines;
101
    }
99
    }
102
}
100
}
103
101