Commit ce10b588 authored by Ubbo Veentjer's avatar Ubbo Veentjer
Browse files

rewrite basket to use spring SessionAttributes

parent e26a6699
package info.textgrid.rep.basket;
import java.util.ArrayList;
import java.util.List;
public class Basket {
private List<String> items = new ArrayList<String>();
public List<String> getItems() {
return items;
}
public int addItem(String item) {
items.add(item);
return items.size();
}
public long remove(String textgridUri) {
items.remove(textgridUri);
return items.size();
}
public String getItemsAsString() {
return String.join(",", items);
}
public long clear() {
items.clear();
return items.size();
}
public long size() {
return items.size();
}
public boolean contains(String textgridUri) {
return items.contains(textgridUri);
}
}
package info.textgrid.rep.basket;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttributes;
@RestController
@SessionAttributes("basket")
@RequestMapping("/service/shelf")
public class BasketAjaxController {
@ModelAttribute("basket")
public Basket getBasket() {
return new Basket();
}
@PostMapping("/add")
public long addToBasket(
HttpSession session,
public int addToBasket(
@ModelAttribute Basket basket,
@RequestParam("uri") String textgridUri
) throws IOException {
List<String> basket;
return basket.addItem(textgridUri);
if(session.getAttribute("basket") == null) {
basket = new ArrayList<String>();
} else {
basket = (List<String>) session.getAttribute("basket");
}
basket.add(textgridUri);
session.setAttribute("basket", basket);
return basket.size();
}
@PostMapping("/remove")
public long removeFromBasket(
HttpSession session,
@ModelAttribute Basket basket,
@RequestParam("uri") String textgridUri
) throws IOException {
List<String> basket = (List<String>) session.getAttribute("basket");
basket.remove(textgridUri);
session.setAttribute("basket", basket);
return basket.size();
return basket.remove(textgridUri);
}
@PostMapping("/clear")
public long clearBasket(
HttpSession session
) throws IOException {
session.setAttribute("basket", new ArrayList<String>());
return basketCount(session);
public long clearBasket(@ModelAttribute Basket basket) {
return basket.clear();
}
@GetMapping("/count")
public long basketCount(HttpSession session) {
long count;
if (session.getAttribute("basket")== null) {
count = 0;
} else {
count = ((List<String>) session.getAttribute("basket")).size();
}
return count;
public long basketCount(@ModelAttribute Basket basket) {
return basket.size();
}
}
......@@ -3,44 +3,47 @@ package info.textgrid.rep.basket;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import info.textgrid.namespaces.middleware.tgsearch.ResultType;
import info.textgrid.rep.i18n.I18N;
import info.textgrid.rep.i18n.I18NProvider;
import info.textgrid.rep.service.tgsearch.TgrepConfigurationService;
import info.textgrid.rep.service.tgsearch.TgsearchClientService;
import info.textgrid.rep.shared.Utils;
import info.textgrid.rep.shared.ViewMode;
@Controller
@SessionAttributes("basket")
public class BasketController {
@Autowired
private TgsearchClientService tgsearchClientService;
@Autowired
private TgrepConfigurationService tgrepConfig;
@Autowired
private I18NProvider i18nProvider;
private static final Log log = LogFactory.getLog(BasketController.class);
@Autowired
public BasketController(TgsearchClientService tgsearchClientService, TgrepConfigurationService tgrepConfig, I18NProvider i18nProvider) {
this.tgsearchClientService = tgsearchClientService;
this.tgrepConfig = tgrepConfig;
this.i18nProvider = i18nProvider;
@ModelAttribute("basket")
public Basket getBasket() {
return new Basket();
}
@GetMapping("/shelf")
public String render(
Model model,
Locale locale,
HttpSession session,
@ModelAttribute Basket basket,
@RequestParam(value="mode", required=false) String mode) {
I18N i18n = i18nProvider.getI18N(locale);
......@@ -49,10 +52,8 @@ public class BasketController {
List<ResultType> results = new ArrayList<ResultType>();
List<String> basket = (List<String>) session.getAttribute("basket");
if(basket != null) {
for(String textgridUri : basket) {
for(String textgridUri : basket.getItems()) {
ResultType res = tgsearchClientService.getMetadata(textgridUri);
results.add(res);
}
......@@ -66,7 +67,7 @@ public class BasketController {
model.addAttribute("results", results);
model.addAttribute("aggregatorUrl", tgrepConfig.getTextgridHost()+"/1.0/aggregator");
model.addAttribute("voyantUrl", tgrepConfig.getToolVoyantHost());
model.addAttribute("basketItemString", Utils.getBasketItemsAsString(session));
model.addAttribute("basketItemString", basket.getItemsAsString());
model.addAttribute("textgridHost", this.tgrepConfig.getTextgridHost());
// translation array
......
......@@ -3,8 +3,6 @@ package info.textgrid.rep.shared;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3._1999._02._22_rdf_syntax_ns_.RdfType;
......@@ -12,9 +10,6 @@ import org.w3c.dom.Element;
import info.textgrid.namespaces.metadata.core._2010.RelationType;
import info.textgrid.namespaces.middleware.tgsearch.ResultType;
public class Utils {
private final static int THUMBSIZE = 250;
......@@ -92,19 +87,6 @@ public class Utils {
return sb.toString();
}
public static List<String> getBasketItems(HttpSession session) {
return (List<String>) session.getAttribute("basket");
}
public static String getBasketItemsAsString(HttpSession session) {
String itemstring = "";
if(getBasketItems(session) != null) {
itemstring = String.join(",", getBasketItems(session));
}
return itemstring;
}
public static String urlencode(String string) {
try {
string = URLEncoder.encode(string, "UTF-8");
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment