Skip to content

Commit 277ef8c

Browse files
committed
Allow using any supported identifier when linting ACLs
1 parent 4d70ead commit 277ef8c

3 files changed

Lines changed: 45 additions & 3 deletions

File tree

gemma-cli/src/main/java/ubic/gemma/apps/AclLinterCli.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.apache.commons.cli.ParseException;
99
import org.springframework.beans.factory.annotation.Autowired;
1010
import ubic.gemma.cli.util.AbstractAuthenticatedCLI;
11+
import ubic.gemma.cli.util.EntityLocator;
1112
import ubic.gemma.cli.util.OptionsUtils;
1213
import ubic.gemma.core.security.authorization.acl.AclLinterConfig;
1314
import ubic.gemma.core.security.authorization.acl.AclLinterService;
@@ -68,9 +69,12 @@ private enum SecurableType {
6869
@Autowired
6970
private AclLinterService aclLinterService;
7071

72+
@Autowired
73+
private EntityLocator entityLocator;
74+
7175
private Class<? extends Securable> clazz;
7276

73-
private Long identifier;
77+
private String identifier;
7478

7579
private boolean lintPermissions;
7680

@@ -111,7 +115,9 @@ protected void doAuthenticatedWork() throws Exception {
111115
.build();
112116
Collection<AclLinterService.LintResult> results;
113117
if ( identifier != null ) {
114-
results = aclLinterService.lintAcls( clazz, identifier, config );
118+
Securable entity = entityLocator.locateEntity( clazz, identifier, true );
119+
assert entity.getId() != null;
120+
results = aclLinterService.lintAcls( clazz, entity.getId(), config );
115121
} else if ( clazz != null ) {
116122
results = aclLinterService.lintAcls( clazz, config );
117123
} else {

gemma-cli/src/main/java/ubic/gemma/cli/util/EntityLocator.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ubic.gemma.cli.util;
22

33
import ubic.gemma.model.analysis.expression.diff.DifferentialExpressionAnalysis;
4+
import ubic.gemma.model.common.Identifiable;
45
import ubic.gemma.model.common.protocol.Protocol;
56
import ubic.gemma.model.common.quantitationtype.QuantitationType;
67
import ubic.gemma.model.expression.arrayDesign.ArrayDesign;
@@ -16,11 +17,13 @@
1617
import java.util.Map;
1718

1819
/**
19-
* Locate various entities using identifiers supplied by the CLI.
20+
* Locate various entities using identifiers.
2021
* @author poirigui
2122
*/
2223
public interface EntityLocator {
2324

25+
<T extends Identifiable> T locateEntity( Class<T> clazz, String identifier, boolean useReferencesIfPossible );
26+
2427
Taxon locateTaxon( String identifier );
2528

2629
ArrayDesign locateArrayDesign( String identifier );

gemma-cli/src/main/java/ubic/gemma/cli/util/EntityLocatorImpl.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import lombok.extern.apachecommons.CommonsLog;
44
import org.apache.commons.lang3.StringUtils;
55
import org.apache.commons.lang3.tuple.Pair;
6+
import org.hibernate.SessionFactory;
67
import org.springframework.beans.factory.annotation.Autowired;
78
import org.springframework.stereotype.Component;
89
import org.springframework.util.Assert;
@@ -52,6 +53,38 @@ public class EntityLocatorImpl implements EntityLocator {
5253
private SingleCellExpressionExperimentService singleCellExpressionExperimentService;
5354
@Autowired
5455
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+
}
5588

5689
@Override
5790
public Taxon locateTaxon( String identifier ) {

0 commit comments

Comments
 (0)