|
3 | 3 | import org.apache.commons.logging.Log;
|
4 | 4 | import org.apache.commons.logging.LogFactory;
|
5 | 5 | import org.springframework.beans.factory.annotation.Autowired;
|
| 6 | +import org.springframework.http.HttpStatus; |
| 7 | +import org.springframework.http.ResponseEntity; |
6 | 8 | import org.springframework.stereotype.Controller;
|
7 | 9 | import org.springframework.web.bind.annotation.PathVariable;
|
8 | 10 | import org.springframework.web.bind.annotation.RequestMapping;
|
| 11 | +import org.springframework.web.bind.annotation.ResponseBody; |
9 | 12 | import org.springframework.web.bind.annotation.RequestMethod;
|
10 | 13 | import org.springframework.web.bind.annotation.RequestParam;
|
11 | 14 | import org.springframework.web.servlet.ModelAndView;
|
|
19 | 22 | import java.util.Collection;
|
20 | 23 | import java.util.Collections;
|
21 | 24 | import java.util.LinkedList;
|
| 25 | +import java.util.Map; |
| 26 | +import java.util.HashMap; |
| 27 | +import java.util.List; |
| 28 | +import java.util.ArrayList; |
| 29 | + |
| 30 | +import javax.servlet.http.Cookie; |
| 31 | +import javax.servlet.http.HttpServletRequest; |
| 32 | +import javax.servlet.http.HttpServletResponse; |
22 | 33 |
|
23 | 34 | /**
|
24 | 35 | * Created by mjacobson on 05/02/18.
|
@@ -227,15 +238,61 @@ public ModelAndView searchUsersByGeneView( @RequestParam String symbol, @Request
|
227 | 238 | modelAndView.setViewName( "fragments/error :: message" );
|
228 | 239 | modelAndView.addObject( "errorMessage", String.format( ERR_NO_ORTHOLOGS, symbol ) );
|
229 | 240 | } else {
|
230 |
| - modelAndView.addObject( "usergenes", handleGeneSearch( gene, tier, orthologs ) ); |
| 241 | + modelAndView.addObject( "usergenes", handleGeneSearch( gene, tier, orthologs ) ); |
231 | 242 | modelAndView.setViewName( "fragments/user-table :: usergenes-table" );
|
232 | 243 | }
|
233 | 244 |
|
234 | 245 | return modelAndView;
|
235 | 246 | }
|
236 | 247 |
|
237 |
| - @RequestMapping(value = "/search/view/international", method = RequestMethod.GET, params = { "symbol", "taxonId", |
238 |
| - "tier" }) |
| 248 | + @RequestMapping(value = "/search/view/orthologs", method = RequestMethod.GET, params = { "symbol", "taxonId", "tier" }) |
| 249 | + public ModelAndView searchOrthologsForGene(@RequestParam String symbol, @RequestParam Integer taxonId, |
| 250 | + @RequestParam TierType tier, |
| 251 | + @RequestParam(name = "orthologTaxonId", required = false) Integer orthologTaxonId ) { |
| 252 | + if(!searchAuthorized( userService.findCurrentUser(), false )){ |
| 253 | + return null; |
| 254 | + } |
| 255 | + |
| 256 | + // Only look for orthologs when taxon is human |
| 257 | + if(taxonId != 9606){ |
| 258 | + orthologTaxonId = null; |
| 259 | + } |
| 260 | + |
| 261 | + Taxon taxon = taxonService.findById( taxonId ); |
| 262 | + Gene gene = geneService.findBySymbolAndTaxon( symbol, taxon ); |
| 263 | + Collection<Gene> orthologs = getOrthologsIfRequested( orthologTaxonId, gene ); |
| 264 | + Map<String, List<Gene>> orthologMap = null; |
| 265 | + |
| 266 | + ModelAndView modelAndView = new ModelAndView(); |
| 267 | + |
| 268 | + if ( gene == null ) { |
| 269 | + modelAndView.setViewName( "fragments/error :: message" ); |
| 270 | + modelAndView.addObject( "errorMessage", String.format( ERR_NO_GENE, symbol ) ); |
| 271 | + } else if ( |
| 272 | + // Check if there is a ortholog request for a different taxon than the original gene |
| 273 | + ( orthologTaxonId != null && !orthologTaxonId.equals( gene.getTaxon().getId() ) ) |
| 274 | + // Check if we got some ortholog results |
| 275 | + && ( orthologs == null || orthologs.isEmpty() ) ) { |
| 276 | + modelAndView.setViewName( "fragments/error :: message" ); |
| 277 | + modelAndView.addObject( "errorMessage", String.format( ERR_NO_ORTHOLOGS, symbol ) ); |
| 278 | + } else { |
| 279 | + orthologMap = new HashMap<>(); |
| 280 | + for (Gene o : orthologs){ |
| 281 | + String name = o.getTaxon().getCommonName(); |
| 282 | + if (!orthologMap.containsKey(name)) { |
| 283 | + orthologMap.put(name, new ArrayList<Gene>()); |
| 284 | + } |
| 285 | + orthologMap.get(name).add(o); |
| 286 | + } |
| 287 | + modelAndView.addObject( "orthologs", orthologMap ); |
| 288 | + modelAndView.setViewName( "fragments/ortholog-table :: ortholog-table" ); |
| 289 | + } |
| 290 | + return modelAndView; |
| 291 | + } |
| 292 | + |
| 293 | + |
| 294 | + |
| 295 | + @RequestMapping(value = "/search/view/international", method = RequestMethod.GET, params = { "symbol", "taxonId", "tier" }) |
239 | 296 | public ModelAndView searchItlUsersByGeneView( @RequestParam String symbol, @RequestParam Integer taxonId,
|
240 | 297 | @RequestParam TierType tier,
|
241 | 298 | @RequestParam(name = "orthologTaxonId", required = false) Integer orthologTaxonId ) {
|
@@ -321,16 +378,21 @@ Collection<Gene> getOrthologsIfRequested( Integer orthologTaxonId, Gene gene ) {
|
321 | 378 | //noinspection unchecked
|
322 | 379 | return Collections.EMPTY_LIST;
|
323 | 380 | }
|
324 |
| - |
| 381 | + |
325 | 382 | private boolean searchAuthorized( User user, boolean international ) {
|
| 383 | + |
326 | 384 | if ( adminRole == null ) {
|
327 | 385 | adminRole = roleRepository.findByRole( "ROLE_ADMIN" );
|
328 | 386 | }
|
329 | 387 |
|
330 |
| - return ( applicationSettings.getPrivacy().isPublicSearch() // Search is public |
331 |
| - || ( applicationSettings.getPrivacy().isRegisteredSearch() && user != null ) // Search is registered and there is user logged |
332 |
| - || ( user != null && adminRole != null && user.getRoles().contains( adminRole ) ) ) // User is admin |
333 |
| - |
| 388 | + if ( user == null ){ |
| 389 | + log.info( "User is null in searchAuthorized(); Non-public search will not be authorized." ); |
| 390 | + } |
| 391 | + |
| 392 | + |
| 393 | + return ( applicationSettings.getPrivacy().isPublicSearch() // Search is public |
| 394 | + || ( user != null && applicationSettings.getPrivacy().isRegisteredSearch() ) // Search is registered and there is user logged |
| 395 | + || ( user != null && adminRole != null && user.getRoles().contains( adminRole ) ) ) // User is admin |
334 | 396 | && ( !international || applicationSettings.getIsearch().isEnabled() ); // International search enabled
|
335 | 397 | }
|
336 | 398 |
|
|
0 commit comments