1 /*
2 * This file is part of *** M y C o R e ***
3 * See http://www.mycore.de/ for details.
4 *
5 * MyCoRe is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * MyCoRe is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with MyCoRe. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 package org.mycore.frontend.basket;
20
21 import org.mycore.common.MCRSessionMgr;
22
23 /**
24 * Manages basket objects in the user's current MCRSession.
25 * A session may store multiple baskets with different type IDs,
26 * for example a basket for documents and another for an other type of entry.
27 *
28 * @author Frank L\u00FCtzenkirchen
29 */
30 public class MCRBasketManager {
31
32 /**
33 * Convenience method to get a basket of the given type.
34 * When there already is a basket in the session, that basket is returned.
35 * Otherwise a new basket is created and saved in the session.
36 */
37 public static MCRBasket getOrCreateBasketInSession(String type) {
38 MCRBasket basket = getBasketFromSession(type);
39 if (basket == null) {
40 basket = new MCRBasket(type);
41 setBasketInSession(basket);
42 }
43 return basket;
44 }
45
46 /**
47 * Returns the basket of the given type from the current session, if there is any.
48 */
49 public static MCRBasket getBasketFromSession(String type) {
50 String key = getBasketKey(type);
51 return (MCRBasket) (MCRSessionMgr.getCurrentSession().get(key));
52 }
53
54 /**
55 * Stores the given basket in the current user's session
56 */
57 public static void setBasketInSession(MCRBasket basket) {
58 String key = getBasketKey(basket.getType());
59 MCRSessionMgr.getCurrentSession().put(key, basket);
60 }
61
62 /**
63 * Returns the key to be used to store a basket in the current user's MCRSession
64 */
65 private static String getBasketKey(String type) {
66 return "basket." + type;
67 }
68
69 /**
70 * Checks if a basket entry is present in the current basket
71 * @param type basket type
72 * @param id basket entry id
73 * @return true if a basket of this type exist and contains basket entry with the given id
74 */
75 public static boolean contains(String type, String id) {
76 MCRBasket basket = getBasketFromSession(type);
77 if (basket == null) {
78 return false;
79 }
80 return (basket.get(id) != null);
81 }
82 }