test

Форк
1
/
StoreUtil.java 
82 строки · 3.2 Кб
1
package com.bittercode.util;
2

3
import java.io.PrintWriter;
4

5
import javax.servlet.http.HttpServletRequest;
6
import javax.servlet.http.HttpSession;
7

8
import com.bittercode.model.UserRole;
9

10
/*
11
 * Store UTil File To Store Commonly used methods
12
 */
13
public class StoreUtil {
14

15
    /**
16
     * Check if the User is logged in with the requested role
17
     */
18
    public static boolean isLoggedIn(UserRole role, HttpSession session) {
19

20
        return session.getAttribute(role.toString()) != null;
21
    }
22

23
    /**
24
     * Modify the active tab in the page menu bar
25
     */
26
    public static void setActiveTab(PrintWriter pw, String activeTab) {
27

28
        pw.println("<script>document.getElementById(activeTab).classList.remove(\"active\");activeTab=" + activeTab
29
                + "</script>");
30
        pw.println("<script>document.getElementById('" + activeTab + "').classList.add(\"active\");</script>");
31

32
    }
33

34
    /**
35
     * Add/Remove/Update Item in the cart using the session
36
     */
37
    public static void updateCartItems(HttpServletRequest req) {
38
        String selectedBookId = req.getParameter("selectedBookId");
39
        HttpSession session = req.getSession();
40
        if (selectedBookId != null) { // add item to the cart
41

42
            // Items will contain comma separated bookIds that needs to be added in the cart
43
            String items = (String) session.getAttribute("items");
44
            if (req.getParameter("addToCart") != null) { // add to cart
45
                if (items == null || items.length() == 0)
46
                    items = selectedBookId;
47
                else if (!items.contains(selectedBookId))
48
                    items = items + "," + selectedBookId; // if items already contains bookId, don't add it
49

50
                // set the items in the session to be used later
51
                session.setAttribute("items", items);
52

53
                /*
54
                 * Quantity of each item in the cart will be stored in the session as:
55
                 * Prefixed with qty_ following its bookId
56
                 * For example 2 no. of book with id 'myBook' in the cart will be
57
                 * added to the session as qty_myBook=2
58
                 */
59
                int itemQty = 0;
60
                if (session.getAttribute("qty_" + selectedBookId) != null)
61
                    itemQty = (int) session.getAttribute("qty_" + selectedBookId);
62
                itemQty += 1;
63
                session.setAttribute("qty_" + selectedBookId, itemQty);
64
            } else { // remove from the cart
65
                int itemQty = 0;
66
                if (session.getAttribute("qty_" + selectedBookId) != null)
67
                    itemQty = (int) session.getAttribute("qty_" + selectedBookId);
68
                if (itemQty > 1) {
69
                    itemQty--;
70
                    session.setAttribute("qty_" + selectedBookId, itemQty);
71
                } else {
72
                    session.removeAttribute("qty_" + selectedBookId);
73
                    items = items.replace(selectedBookId + ",", "");
74
                    items = items.replace("," + selectedBookId, "");
75
                    items = items.replace(selectedBookId, "");
76
                    session.setAttribute("items", items);
77
                }
78
            }
79
        }
80

81
    }
82
}
83

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.