|
7 | 7 | import java.util.HashMap; |
8 | 8 | import java.util.List; |
9 | 9 | import java.util.Map; |
| 10 | +import java.util.UUID; |
10 | 11 |
|
11 | 12 | import org.apache.http.HttpStatus; |
12 | 13 | import org.springframework.beans.factory.annotation.Autowired; |
| 14 | +import org.springframework.beans.factory.annotation.Value; |
13 | 15 | import org.springframework.web.bind.annotation.RequestMapping; |
14 | 16 | import org.springframework.web.bind.annotation.RequestParam; |
15 | 17 | import org.springframework.web.bind.annotation.RestController; |
|
27 | 29 | @RequestMapping("/catalog-access") |
28 | 30 | public class GetItForMeController { |
29 | 31 |
|
| 32 | + @Value("${app.publicCatalogUrl:}") |
| 33 | + private String publicCatalogUrl; |
| 34 | + |
30 | 35 | @Autowired |
31 | 36 | private GetItForMeService getItForMeService; |
32 | 37 |
|
@@ -79,45 +84,94 @@ public ApiResponse getButtonsByBibId(@RequestParam(value="catalogName",defaultVa |
79 | 84 | } |
80 | 85 | } |
81 | 86 |
|
| 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 | + |
82 | 108 | /** |
83 | 109 | * Provides the raw button data in JSON format, keyed by MFHD. |
84 | 110 | * If a specific button with matching criteria - Cushing is found, it performs a redirect. |
85 | 111 | * @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. |
88 | 113 | * @param String location Optional location parameter used for location-based redirect. |
89 | 114 | * @return ApiResponse or RedirectView based on redirectUrl. |
90 | 115 | */ |
91 | 116 | @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); |
95 | 120 | String redirectUrl = null; |
96 | 121 | 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 | + |
97 | 128 | for (Map.Entry<String, ButtonPresentation> entry : buttonData.entrySet()) { |
98 | 129 | String holdingId = entry.getKey(); |
99 | 130 | ButtonPresentation buttonPresentation = entry.getValue(); |
100 | 131 | if (holdingId != null && buttonPresentation != null && buttonPresentation.getButtons() != null) { |
| 132 | + holdingsWithButtonsCount++; |
101 | 133 | for( Map<String, String> button: buttonPresentation.getButtons()) { |
102 | | - String cssClass = button.get("cssClasses"); |
103 | 134 | 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); |
109 | 144 | break; |
110 | 145 | } |
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); |
114 | 149 | break; |
115 | 150 | } |
116 | 151 | } |
117 | 152 | } |
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); |
121 | 175 | } |
122 | 176 | throw new ResponseStatusException(HttpStatus.SC_NOT_FOUND, "No valid redirect URL found.", null); |
123 | 177 | } else { |
|
0 commit comments