Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions gemma-cli/src/main/config/fish/completions/gemma-cli.fish

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 12 additions & 6 deletions gemma-cli/src/main/java/ubic/gemma/apps/AclLinterCli.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.springframework.beans.factory.annotation.Autowired;
import ubic.gemma.cli.util.AbstractAuthenticatedCLI;
import ubic.gemma.cli.util.EntityLocator;
import ubic.gemma.cli.util.EntityOptionsUtils;
import ubic.gemma.cli.util.OptionsUtils;
import ubic.gemma.core.security.authorization.acl.AclLinterConfig;
import ubic.gemma.core.security.authorization.acl.AclLinterService;
Expand Down Expand Up @@ -68,9 +69,12 @@ private enum SecurableType {
@Autowired
private AclLinterService aclLinterService;

@Autowired
private EntityLocator entityLocator;

private Class<? extends Securable> clazz;

private Long identifier;
private String identifier;

private boolean lintPermissions;

Expand All @@ -82,8 +86,8 @@ private enum SecurableType {
@Override
protected void buildOptions( Options options ) {
OptionsUtils.addEnumOption( options, "type", "type", "Type of securable entities to lint.", SecurableType.class );
options.addOption( Option.builder( "identifier" ).longOpt( "identifier" ).hasArg().type( Long.class )
.desc( "Identifier of the securable entity to lint. Requires the -type,--type option to be set." ).get() );
EntityOptionsUtils.addEntityOption( options, "identifier", "identifier",
"Identifier of the securable entity to lint. Requires the -type,--type option to be set." );
options.addOption( "lintPermissions", "lint-permissions", false, "Lint permissions." );
options.addOption( "applyFixes", "apply-fixes", false, "Apply fixes to ACLs" );
}
Expand All @@ -92,7 +96,7 @@ protected void buildOptions( Options options ) {
protected void processOptions( CommandLine commandLine ) throws ParseException {
SecurableType st = OptionsUtils.getEnumOptionValue( commandLine, "type" );
this.clazz = st != null ? st.getClazz() : null;
this.identifier = getParsedOptionValue( commandLine, "identifier",
this.identifier = getOptionValue( commandLine, "identifier",
requires( toBeSet( "type" ) ) );
this.lintPermissions = commandLine.hasOption( "lintPermissions" );
this.applyFixes = commandLine.hasOption( "applyFixes" );
Expand All @@ -111,7 +115,9 @@ protected void doAuthenticatedWork() throws Exception {
.build();
Collection<AclLinterService.LintResult> results;
if ( identifier != null ) {
results = aclLinterService.lintAcls( clazz, identifier, config );
Securable entity = entityLocator.locateEntity( clazz, identifier, true );
assert entity.getId() != null;
results = aclLinterService.lintAcls( clazz, entity.getId(), config );
} else if ( clazz != null ) {
results = aclLinterService.lintAcls( clazz, config );
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ubic.gemma.cli.util;

import ubic.gemma.model.analysis.expression.diff.DifferentialExpressionAnalysis;
import ubic.gemma.model.common.Identifiable;
import ubic.gemma.model.common.protocol.Protocol;
import ubic.gemma.model.common.quantitationtype.QuantitationType;
import ubic.gemma.model.expression.arrayDesign.ArrayDesign;
Expand All @@ -16,11 +17,13 @@
import java.util.Map;

/**
* Locate various entities using identifiers supplied by the CLI.
* Locate various entities using identifiers.
* @author poirigui
*/
public interface EntityLocator {

<T extends Identifiable> T locateEntity( Class<T> clazz, String identifier, boolean useReferencesIfPossible );

Taxon locateTaxon( String identifier );

ArrayDesign locateArrayDesign( String identifier );
Expand Down
33 changes: 33 additions & 0 deletions gemma-cli/src/main/java/ubic/gemma/cli/util/EntityLocatorImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import lombok.extern.apachecommons.CommonsLog;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
Expand Down Expand Up @@ -52,6 +53,38 @@ public class EntityLocatorImpl implements EntityLocator {
private SingleCellExpressionExperimentService singleCellExpressionExperimentService;
@Autowired
private DifferentialExpressionAnalysisService differentialExpressionAnalysisService;
@Autowired
private SessionFactory sessionFactory;

@Override
@SuppressWarnings("unchecked")
public <T extends Identifiable> T locateEntity( Class<T> clazz, String identifier, boolean useReferencesIfPossible ) {
// if a match by ID is possible, prefer it so we can apply useReferencesIfPossible for anything
try {
Long id = Long.parseLong( identifier );
if ( useReferencesIfPossible ) {
return ( T ) sessionFactory.getCurrentSession().load( clazz, id );
} else {
return ( T ) requireNonNull( sessionFactory.getCurrentSession().get( clazz, id ),
"No " + clazz.getSimpleName() + " found with ID " + id + "." );
}
} catch ( NumberFormatException e ) {
// ignore
}
// this is a non-ID lookup, so delegate to the appropriate locator
if ( Taxon.class.isAssignableFrom( clazz ) ) {
return ( T ) locateTaxon( identifier );
} else if ( Protocol.class.isAssignableFrom( clazz ) ) {
return ( T ) locateProtocol( identifier );
} else if ( ArrayDesign.class.isAssignableFrom( clazz ) ) {
return ( T ) locateArrayDesign( identifier );
} else if ( ExpressionExperiment.class.isAssignableFrom( clazz ) ) {
return ( T ) locateExpressionExperiment( identifier, useReferencesIfPossible );
} else {
// all the other locator in this class require an experiment context, so we cannot support them here.
throw new UnsupportedOperationException( "Cannot locate an entity of type " + clazz.getSimpleName() + "." );
}
}

@Override
public Taxon locateTaxon( String identifier ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.lang3.Strings;
import ubic.gemma.cli.completion.CompletionType;
import ubic.gemma.cli.completion.CompletionUtils;

Expand All @@ -11,6 +12,19 @@
*/
public class EntityOptionsUtils {


/**
* @see EntityLocator#locateEntity(Class, String, boolean)
*/
public static void addEntityOption( Options options, String optionName, String longOpt, String description ) {
options.addOption( Option.builder( optionName )
.longOpt( longOpt )
.hasArg()
.argName( "ID, short name, name, NCBI ID, common name, scientific name, alternate name" )
.desc( Strings.CS.appendIfMissing( description, "." ) + " Entity ID, short name, name, NCBI ID, common name, scientific name, alternate name" )
.get() );
}

/**
* Add an option for supplying a dataset.
*/
Expand Down