Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
DARIAH-DE
TextGridRep Portal
Commits
ce10b588
Commit
ce10b588
authored
May 15, 2020
by
Ubbo Veentjer
Browse files
rewrite basket to use spring SessionAttributes
parent
e26a6699
Changes
4
Hide whitespace changes
Inline
Side-by-side
src/main/java/info/textgrid/rep/basket/Basket.java
0 → 100644
View file @
ce10b588
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
);
}
}
src/main/java/info/textgrid/rep/basket/BasketAjaxController.java
View file @
ce10b588
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
();
}
}
src/main/java/info/textgrid/rep/basket/BasketController.java
View file @
ce10b588
...
...
@@ -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
.
getB
asketItemsAsString
(
session
));
model
.
addAttribute
(
"basketItemString"
,
b
asket
.
get
ItemsAsString
());
model
.
addAttribute
(
"textgridHost"
,
this
.
tgrepConfig
.
getTextgridHost
());
// translation array
...
...
src/main/java/info/textgrid/rep/shared/Utils.java
View file @
ce10b588
...
...
@@ -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"
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment