|
3 | 3 | import lombok.extern.apachecommons.CommonsLog; |
4 | 4 | import org.apache.commons.lang3.StringUtils; |
5 | 5 | import org.apache.commons.lang3.tuple.Pair; |
| 6 | +import org.hibernate.SessionFactory; |
6 | 7 | import org.springframework.beans.factory.annotation.Autowired; |
7 | 8 | import org.springframework.stereotype.Component; |
8 | 9 | import org.springframework.util.Assert; |
@@ -52,6 +53,38 @@ public class EntityLocatorImpl implements EntityLocator { |
52 | 53 | private SingleCellExpressionExperimentService singleCellExpressionExperimentService; |
53 | 54 | @Autowired |
54 | 55 | private DifferentialExpressionAnalysisService differentialExpressionAnalysisService; |
| 56 | + @Autowired |
| 57 | + private SessionFactory sessionFactory; |
| 58 | + |
| 59 | + @Override |
| 60 | + @SuppressWarnings("unchecked") |
| 61 | + public <T extends Identifiable> T locateEntity( Class<T> clazz, String identifier, boolean useReferencesIfPossible ) { |
| 62 | + // if a match by ID is possible, prefer it so we can apply useReferencesIfPossible for anything |
| 63 | + try { |
| 64 | + Long id = Long.parseLong( identifier ); |
| 65 | + if ( useReferencesIfPossible ) { |
| 66 | + return ( T ) sessionFactory.getCurrentSession().load( clazz, id ); |
| 67 | + } else { |
| 68 | + return ( T ) requireNonNull( sessionFactory.getCurrentSession().get( clazz, id ), |
| 69 | + "No " + clazz.getSimpleName() + " found with ID " + id + "." ); |
| 70 | + } |
| 71 | + } catch ( NumberFormatException e ) { |
| 72 | + // ignore |
| 73 | + } |
| 74 | + // this is a non-ID lookup, so delegate to the appropriate locator |
| 75 | + if ( Taxon.class.isAssignableFrom( clazz ) ) { |
| 76 | + return ( T ) locateTaxon( identifier ); |
| 77 | + } else if ( Protocol.class.isAssignableFrom( clazz ) ) { |
| 78 | + return ( T ) locateProtocol( identifier ); |
| 79 | + } else if ( ArrayDesign.class.isAssignableFrom( clazz ) ) { |
| 80 | + return ( T ) locateArrayDesign( identifier ); |
| 81 | + } else if ( ExpressionExperiment.class.isAssignableFrom( clazz ) ) { |
| 82 | + return ( T ) locateExpressionExperiment( identifier, useReferencesIfPossible ); |
| 83 | + } else { |
| 84 | + // all the other locator in this class require an experiment context, so we cannot support them here. |
| 85 | + throw new UnsupportedOperationException( "Cannot locate an entity of type " + clazz.getSimpleName() + "." ); |
| 86 | + } |
| 87 | + } |
55 | 88 |
|
56 | 89 | @Override |
57 | 90 | public Taxon locateTaxon( String identifier ) { |
|
0 commit comments