Skip to content

Commit 6a543d5

Browse files
authored
Merge pull request #62 from TAMULib/multi-redirect
Redirect to Public Catalog for multi-button scenarios
2 parents adf957d + ea06ef3 commit 6a543d5

File tree

2 files changed

+75
-18
lines changed

2 files changed

+75
-18
lines changed

src/main/java/edu/tamu/app/controller/GetItForMeController.java

Lines changed: 71 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
import java.util.HashMap;
88
import java.util.List;
99
import java.util.Map;
10+
import java.util.UUID;
1011

1112
import org.apache.http.HttpStatus;
1213
import org.springframework.beans.factory.annotation.Autowired;
14+
import org.springframework.beans.factory.annotation.Value;
1315
import org.springframework.web.bind.annotation.RequestMapping;
1416
import org.springframework.web.bind.annotation.RequestParam;
1517
import org.springframework.web.bind.annotation.RestController;
@@ -27,6 +29,9 @@
2729
@RequestMapping("/catalog-access")
2830
public class GetItForMeController {
2931

32+
@Value("${app.publicCatalogUrl:}")
33+
private String publicCatalogUrl;
34+
3035
@Autowired
3136
private GetItForMeService getItForMeService;
3237

@@ -79,45 +84,94 @@ public ApiResponse getButtonsByBibId(@RequestParam(value="catalogName",defaultVa
7984
}
8085
}
8186

87+
private boolean testIsCushing(Map<String, String> button) {
88+
boolean cssClassIsCushing = button.get("cssClasses") != null && button.get("cssClasses").contains("btn_cushing");
89+
boolean linkHrefIsCushing = button.get("linkHref") != null && button.get("linkHref").contains("aeon.library.tamu.edu");
90+
return cssClassIsCushing && linkHrefIsCushing;
91+
}
92+
93+
private String buildRedirectUrl(String baseUrl) {
94+
return baseUrl.startsWith("http") ? baseUrl : "https://" + baseUrl;
95+
}
96+
97+
private String ensureBibId(String id) {
98+
String bibId = id;
99+
//if we have the prefix, we need to remove it and convert the uuid to dash format
100+
String bibIdPrefix = "tamu.";
101+
if (bibId.startsWith(bibIdPrefix)) {
102+
String[] uuidPieces = bibId.split(bibIdPrefix, 0);
103+
bibId = uuidPieces[1].replace(".", "-");
104+
}
105+
return bibId;
106+
}
107+
82108
/**
83109
* Provides the raw button data in JSON format, keyed by MFHD.
84110
* If a specific button with matching criteria - Cushing is found, it performs a redirect.
85111
* @param String catalogName Optional catalog name. Defaults to "evans" if not provided.
86-
* @param String bibId Required bibliographic ID. It used to retrieve holdings and associated button data.
87-
* @param boolean verbose.
112+
* @param String bibId Required bibliographic ID (UUID, hrid, or EBSCO format). Used to retrieve holdings and associated button data.
88113
* @param String location Optional location parameter used for location-based redirect.
89114
* @return ApiResponse or RedirectView based on redirectUrl.
90115
*/
91116
@RequestMapping("/get-buttons-redirect")
92-
public Object getButtonsRedirectByBibId(@RequestParam(value="catalogName",defaultValue="evans") String catalogName, @RequestParam("bibId") String bibId, @RequestParam(value="verbose",defaultValue="false") boolean verbose, @RequestParam(value="location", required = false, defaultValue="") String location) {
93-
94-
Map<String,ButtonPresentation> buttonData = getItForMeService.getButtonDataByBibId(catalogName, bibId, verbose);
117+
public Object getButtonsRedirectByBibId(@RequestParam(value="catalogName",defaultValue="evans") String catalogName, @RequestParam("bibId") String bibId, @RequestParam(value="location", required = false, defaultValue="") String location) {
118+
bibId = ensureBibId(bibId);
119+
Map<String,ButtonPresentation> buttonData = getItForMeService.getButtonDataByBibId(catalogName, bibId);
95120
String redirectUrl = null;
96121
if (buttonData != null) {
122+
//test for the button set having a Cushing button
123+
boolean hasCushing = false;
124+
//test for the user requesting a Cushing button
125+
boolean wantsCushing = location.equalsIgnoreCase("aeon");
126+
Integer holdingsWithButtonsCount = 0;
127+
97128
for (Map.Entry<String, ButtonPresentation> entry : buttonData.entrySet()) {
98129
String holdingId = entry.getKey();
99130
ButtonPresentation buttonPresentation = entry.getValue();
100131
if (holdingId != null && buttonPresentation != null && buttonPresentation.getButtons() != null) {
132+
holdingsWithButtonsCount++;
101133
for( Map<String, String> button: buttonPresentation.getButtons()) {
102-
String cssClass = button.get("cssClasses");
103134
String linkHref = button.get("linkHref");
104-
boolean isCushing = ( cssClass != null && cssClass.contains("btn_cushing") ) &&
105-
( linkHref != null && linkHref.contains("aeon.library.tamu.edu") );
106-
107-
if(isCushing && location.equalsIgnoreCase("aeon") && linkHref != null ) {
108-
redirectUrl = linkHref.startsWith("http") ? linkHref : "https://" + linkHref;
135+
//is the current button Cushing
136+
boolean isCushing = testIsCushing(button);
137+
//is at least one button in the set Cushing
138+
if (isCushing && !hasCushing) {
139+
hasCushing = true;
140+
}
141+
//when the user wants Cushing, grab the url of the first button that is for Cushing
142+
if(redirectUrl == null && isCushing && wantsCushing && linkHref != null ) {
143+
redirectUrl = buildRedirectUrl(linkHref);
109144
break;
110145
}
111-
112-
if(!isCushing && location.equalsIgnoreCase("illiad") && linkHref != null ) {
113-
redirectUrl = linkHref.startsWith("http") ? linkHref : "https://" + linkHref;
146+
//when the user doesn't want Cushing, grab the first button that isn't Cushing
147+
if(redirectUrl == null && !isCushing && !wantsCushing && linkHref != null ) {
148+
redirectUrl = buildRedirectUrl(linkHref);
114149
break;
115150
}
116151
}
117152
}
118-
if(redirectUrl != null) {
119-
return new RedirectView(redirectUrl);
120-
}
153+
//complete the whole button set loop even if we have a redirectUrl so we can learn if we have
154+
//a Cushing button in the set or not and our total count of holdings with buttons
155+
}
156+
//if we have a Cushing button but the user hasn't explicitly requested one, we can auto-redirect
157+
//to the other button if there are only 2 or fewer holdings
158+
Integer holdingCountThreshold = (hasCushing) ? 3:2;
159+
160+
// we can only redirect to the catalog if we have an hrid, not a uuid
161+
boolean bibIdIsUUID = true;
162+
try {
163+
bibIdIsUUID = (UUID.fromString(bibId).toString().equals(bibId));
164+
} catch (IllegalArgumentException e) {
165+
bibIdIsUUID = false;
166+
}
167+
168+
//If we have too many holdings options to choose from, redirect to the catalog so the user can choose there
169+
if (!bibIdIsUUID && (!hasCushing || !wantsCushing) && holdingsWithButtonsCount >= holdingCountThreshold && publicCatalogUrl.length() > 0) {
170+
redirectUrl = publicCatalogUrl + bibId;
171+
}
172+
173+
if (redirectUrl != null) {
174+
return new RedirectView(redirectUrl);
121175
}
122176
throw new ResponseStatusException(HttpStatus.SC_NOT_FOUND, "No valid redirect URL found.", null);
123177
} else {

src/main/resources/application.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,10 @@ app:
123123
timeout: 10000
124124

125125
# Catalog Service location *requires trailing forward slash
126-
catalogServiceUrl: https://api-dev.library.tamu.edu/catalog/3x/
126+
catalogServiceUrl: https://api-dev.library.tamu.edu/catalog/3x/catalog-access/
127+
128+
# Base URL for the public facing catalog single record view
129+
publicCatalogUrl: https://catalog-dev.library.tamu.edu/Record/
127130

128131
# Catalog Configuration file
129132
catalogsConfiguration:

0 commit comments

Comments
 (0)