Skip to content

Commit

Permalink
Introduce the Specification<T> interface for find() and findOrCreate()
Browse files Browse the repository at this point in the history
  • Loading branch information
arteymix committed Aug 25, 2023
1 parent 9ccaee3 commit 15a4d6a
Show file tree
Hide file tree
Showing 79 changed files with 491 additions and 428 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,10 @@
*/
package ubic.gemma.core.apps;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.lang3.StringUtils;

import ubic.gemma.core.analysis.report.ArrayDesignReportService;
import ubic.gemma.core.analysis.service.ArrayDesignAnnotationService;
import ubic.gemma.core.apps.GemmaCLI.CommandGroup;
Expand All @@ -49,6 +44,12 @@
import ubic.gemma.persistence.service.genome.sequenceAnalysis.AnnotationAssociationService;
import ubic.gemma.persistence.service.genome.taxon.TaxonService;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import static ubic.gemma.persistence.util.Specifications.byIdentifiable;

/**
* Create (or update) an array design based on the current set of transcripts for a taxon.
* This is used to create a 'platform' for linking non-array based data to the system, or data for which we have only
Expand Down Expand Up @@ -116,9 +117,9 @@ protected void doWork() throws Exception {
arrayDesign.setDescription( "Created by Gemma" );
arrayDesign.setTechnologyType( TechnologyType.GENELIST ); // this is key

if ( arrayDesignService.find( arrayDesign ) != null ) {
if ( arrayDesignService.find( byIdentifiable( arrayDesign ) ) != null ) {
AbstractCLI.log.info( "Platform for " + taxon + " already exists, will update" );
arrayDesign = arrayDesignService.findOrFail( arrayDesign );
arrayDesign = arrayDesignService.findOrFail( byIdentifiable( arrayDesign ) );
arrayDesignService.deleteGeneProductAnnotationAssociations( arrayDesign );
arrayDesign = arrayDesignService.loadOrFail( arrayDesign.getId() );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import ubic.gemma.model.expression.experiment.ExpressionExperiment;
import ubic.gemma.persistence.service.BaseImmutableService;
import ubic.gemma.persistence.service.BaseVoEnabledService;
import ubic.gemma.persistence.util.Specification;

import javax.annotation.ParametersAreNonnullByDefault;
import java.util.Collection;
Expand All @@ -45,18 +46,9 @@ public interface BibliographicReferenceService

List<BibliographicReference> browse( int start, int limit, String orderField, boolean descending );

/**
* check to see if the object already exists
*
* @param bibliographicReference reference
* @return reference
*/
@Override
BibliographicReference find( BibliographicReference bibliographicReference );

@Override
@Secured({ "GROUP_USER" })
BibliographicReference findOrCreate( BibliographicReference BibliographicReference );
BibliographicReference findOrCreate( Specification<BibliographicReference> spec );

@Override
@Secured({ "GROUP_USER" })
Expand All @@ -74,7 +66,7 @@ public interface BibliographicReferenceService
* @param id id
* @return reference
*/
BibliographicReference findByExternalId( java.lang.String id );
BibliographicReference findByExternalId( String id );

/**
* Retrieve a reference by identifier, qualified by the database name (such as 'pubmed').
Expand All @@ -83,7 +75,7 @@ public interface BibliographicReferenceService
* @param databaseName db name
* @return reference
*/
BibliographicReference findByExternalId( java.lang.String id, java.lang.String databaseName );
BibliographicReference findByExternalId( String id, String databaseName );

/**
* <p>
Expand All @@ -93,7 +85,7 @@ public interface BibliographicReferenceService
* @param id id
* @return reference VO
*/
BibliographicReferenceValueObject findVOByExternalId( java.lang.String id );
BibliographicReferenceValueObject findVOByExternalId( String id );

/**
* Return all the BibRefs that are linked to ExpressionExperiments.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
import java.io.IOException;
import java.util.*;

import static ubic.gemma.persistence.util.Specifications.byIdentifiable;

/**
* Update or fill in the data associated with an experiment. Cases include reprocessing data from CEL files (Affymetrix,
* GEO only), inserting data for RNA-seq data sets but also generic cases where data didn't come from GEO and we need to
Expand Down Expand Up @@ -993,7 +995,7 @@ private Collection<RawExpressionDataVector> makeNewVectors( ExpressionExperiment
assert bioAssayDimension != null;
assert !bioAssayDimension.getBioAssays().isEmpty();

bioAssayDimension = assayDimensionService.findOrCreate( bioAssayDimension );
bioAssayDimension = assayDimensionService.findOrCreate( byIdentifiable( bioAssayDimension ) );

assert !bioAssayDimension.getBioAssays().isEmpty();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.method.P;
import org.springframework.stereotype.Component;
import ubic.gemma.core.analysis.report.ArrayDesignReportService;
import ubic.gemma.core.analysis.sequence.SequenceManipulation;
Expand All @@ -47,6 +46,8 @@
import java.io.*;
import java.util.*;

import static ubic.gemma.persistence.util.Specifications.byIdentifiable;

/**
* Handles collapsing the sequences, attaching sequences to DesignElements, either from provided input or via a fetch.
*
Expand Down Expand Up @@ -650,14 +651,14 @@ private BioSequence createOrUpdateGenbankSequence( BioSequence found, boolean fo

BioSequence existing;

existing = bioSequenceService.findByAccession( sequenceDatabaseEntry );
existing = bioSequenceService.findByAccession( byIdentifiable( sequenceDatabaseEntry ) );

BioSequence result;
if ( existing == null ) {
if ( ArrayDesignSequenceProcessingServiceImpl.log.isDebugEnabled() )
ArrayDesignSequenceProcessingServiceImpl.log.debug( "Find (or creating) new sequence " + found );

result = bioSequenceService.find( found ); // there still might be a match.
result = bioSequenceService.find( byIdentifiable( found ) ); // there still might be a match.

if ( result == null ) {
result = bioSequenceService.create( found );
Expand Down Expand Up @@ -722,7 +723,7 @@ private Map<String, BioSequence> findLocalSequences( Collection<String> identifi
BioSequence template = BioSequence.Factory.newInstance();
template.setTaxon( taxon );
template.setName( id );
BioSequence seq = bioSequenceService.find( template );
BioSequence seq = bioSequenceService.find( byIdentifiable( template ) );
if ( seq != null ) {
seq = bioSequenceService.thaw( seq );
found.put( id, seq );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import ubic.basecode.dataStructure.matrix.DoubleMatrixFactory;
import ubic.basecode.io.ByteArrayConverter;
import ubic.basecode.io.reader.DoubleMatrixReader;
import ubic.gemma.core.analysis.preprocess.PreprocessingException;
import ubic.gemma.core.analysis.preprocess.PreprocessorService;
import ubic.gemma.core.loader.entrez.pubmed.PubMedXMLFetcher;
import ubic.gemma.core.loader.expression.simple.model.SimpleExpressionExperimentMetaData;
Expand All @@ -51,6 +50,8 @@
import java.io.InputStream;
import java.util.*;

import static ubic.gemma.persistence.util.Specifications.byIdentifiable;

/**
* Convert a simple matrix and some meta-data into an ExpressionExperiment. Used to handle flat file conversion.
*
Expand Down Expand Up @@ -357,7 +358,7 @@ private Taxon convertTaxon( Taxon taxon ) {
if ( taxonService == null ) {
return taxon; // for tests
}
return taxonService.findOrCreate( taxon );
return taxonService.findOrCreate( byIdentifiable( taxon ) );

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
import java.util.Collection;
import java.util.Map;

import static ubic.gemma.persistence.util.Specifications.byIdentifiable;

/**
* Persister for ubic.gemma.model.common package classes.
*
Expand Down Expand Up @@ -128,7 +130,7 @@ protected AuditTrail persistAuditTrail( AuditTrail entity ) {
}

protected Contact persistContact( Contact contact ) {
return this.contactDao.findOrCreate( contact );
return this.contactDao.findOrCreate( byIdentifiable( contact ) );
}

protected ExternalDatabase persistExternalDatabase( ExternalDatabase database, Caches caches ) {
Expand All @@ -140,7 +142,7 @@ protected ExternalDatabase persistExternalDatabase( ExternalDatabase database, C
return seenDatabases.get( name );
}

ExternalDatabase existingDatabase = externalDatabaseDao.find( database );
ExternalDatabase existingDatabase = externalDatabaseDao.find( byIdentifiable( database ) );

// don't use findOrCreate to avoid flush.
if ( existingDatabase == null ) {
Expand Down Expand Up @@ -196,16 +198,17 @@ protected QuantitationType persistQuantitationType( QuantitationType qType, Cach
}

protected Unit persistUnit( Unit unit ) {
return this.unitDao.findOrCreate( unit );
return this.unitDao.findOrCreate( byIdentifiable( unit ) );
}

private Object persistBibliographicReference( BibliographicReference reference, Caches caches ) {
this.fillInDatabaseEntry( reference.getPubAccession(), caches );
return this.bibliographicReferenceDao.findOrCreate( reference );
return this.bibliographicReferenceDao.findOrCreate( byIdentifiable( reference ) );
}

@Deprecated
private Person persistPerson( Person person ) {
return this.personDao.findOrCreate( person );
return this.personDao.findOrCreate( byIdentifiable( person ) );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
import javax.annotation.Nullable;
import java.util.*;

import static ubic.gemma.persistence.util.Specifications.byIdentifiable;

/**
* @author pavlidis
*/
Expand Down Expand Up @@ -390,7 +392,7 @@ private BioAssayDimension persistBioAssayDimension( BioAssayDimension bioAssayDi
assert persistedBioAssays.size() > 0;
bioAssayDimension.setBioAssays( persistedBioAssays );
// bioAssayDimension.setId( null ); // in case of retry.
return bioAssayDimensionDao.findOrCreate( bioAssayDimension );
return bioAssayDimensionDao.findOrCreate( byIdentifiable( bioAssayDimension ) );
}

private BioMaterial persistBioMaterial( BioMaterial entity, Caches caches ) {
Expand All @@ -408,14 +410,14 @@ private BioMaterial persistBioMaterial( BioMaterial entity, Caches caches ) {
AbstractPersister.log.debug( "taxon done" );

AbstractPersister.log.debug( "start save" );
BioMaterial bm = bioMaterialDao.findOrCreate( entity );
BioMaterial bm = bioMaterialDao.findOrCreate( byIdentifiable( entity ) );
AbstractPersister.log.debug( "save biomaterial done" );

return bm;
}

private Compound persistCompound( Compound compound ) {
return compoundDao.findOrCreate( compound );
return compoundDao.findOrCreate( byIdentifiable( compound ) );
}

/**
Expand All @@ -434,7 +436,7 @@ private ExpressionExperimentSubSet persistExpressionExperimentSubSet( Expression
throw new IllegalArgumentException(
"Subsets are only supported for expression experiments that are already persistent" );
} else {
return expressionExperimentSubSetDao.findOrCreate( entity );
return expressionExperimentSubSetDao.findOrCreate( byIdentifiable( entity ) );
}
}

Expand All @@ -447,7 +449,7 @@ private FactorValue persistFactorValue( FactorValue factorValue, Caches caches )
"You must fill in the experimental factor before persisting a factorvalue" );
}
this.fillInFactorValueAssociations( factorValue, caches );
return factorValueDao.findOrCreate( factorValue );
return factorValueDao.findOrCreate( byIdentifiable( factorValue ) );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
import javax.annotation.Nullable;
import java.util.*;

import static ubic.gemma.persistence.util.Specifications.byIdentifiable;

/**
* @author pavlidis
*/
Expand Down Expand Up @@ -207,7 +209,7 @@ private Gene updateGene( Gene existingGene, Gene newGeneInfo, Caches caches ) {
GeneProduct existingGeneProduct = updatedGpMap.get( newGeneProductInfo.getNcbiGi() );
this.updateGeneProduct( existingGeneProduct, newGeneProductInfo, caches );
} else {
GeneProduct existingGeneProduct = geneProductDao.find( newGeneProductInfo );
GeneProduct existingGeneProduct = geneProductDao.find( byIdentifiable( newGeneProductInfo ) );
if ( existingGeneProduct == null ) {
// it is, in fact, new, so far as we can tell.
newGeneProductInfo.setGene( existingGene );
Expand Down Expand Up @@ -297,7 +299,7 @@ private Gene updateGene( Gene existingGene, Gene newGeneInfo, Caches caches ) {
}

protected BioSequence persistBioSequence( BioSequence bioSequence, Caches caches ) {
BioSequence existingBioSequence = bioSequenceDao.find( bioSequence );
BioSequence existingBioSequence = bioSequenceDao.find( byIdentifiable( bioSequence ) );

// try to avoid making the instance 'dirty' if we don't have to, to avoid updates.
if ( existingBioSequence != null ) {
Expand Down Expand Up @@ -328,7 +330,7 @@ protected Taxon persistTaxon( Taxon taxon, Caches caches ) {
} else if ( commonName != null && seenTaxa.containsKey( commonName.toLowerCase() ) ) {
return seenTaxa.get( commonName.toLowerCase() );
} else {
Taxon fTaxon = taxonDao.findOrCreate( taxon );
Taxon fTaxon = taxonDao.findOrCreate( byIdentifiable( taxon ) );
assert fTaxon != null;
assert fTaxon.getId() != null;

Expand Down Expand Up @@ -377,7 +379,7 @@ private void removeGeneProducts( Collection<GeneProduct> toRemove ) {
Collection<DatabaseEntry> accessions = gpt.getAccessions();
Collection<DatabaseEntry> toRelease = new HashSet<>();
for ( DatabaseEntry de : accessions ) {
if ( this.bioSequenceDao.findByAccession( de ) != null ) {
if ( this.bioSequenceDao.findByAccession( byIdentifiable( de ) ) != null ) {
toRelease.add( de );
}
}
Expand Down Expand Up @@ -420,7 +422,7 @@ private BioSequence2GeneProduct persistBlatAssociation( BlatAssociation associat

private Gene persistGene( Gene gene, boolean checkFirst, Caches caches ) {
if ( checkFirst ) {
Gene existingGene = geneDao.find( gene );
Gene existingGene = geneDao.find( byIdentifiable( gene ) );

if ( existingGene != null ) {
if ( AbstractPersister.log.isDebugEnabled() )
Expand Down Expand Up @@ -450,7 +452,7 @@ private Gene persistGene( Gene gene, boolean checkFirst, Caches caches ) {

Set<GeneProduct> geneProductsForNewGene = new HashSet<>();
for ( GeneProduct product : tempGeneProduct ) {
GeneProduct existingProduct = geneProductDao.find( product );
GeneProduct existingProduct = geneProductDao.find( byIdentifiable( product ) );
if ( existingProduct != null ) {
/*
* A geneProduct is being moved to a gene that didn't exist in the system already
Expand Down Expand Up @@ -494,7 +496,7 @@ private Gene persistGene( Gene gene, boolean checkFirst, Caches caches ) {
}

private GeneProduct persistGeneProduct( GeneProduct geneProduct, Caches caches ) {
GeneProduct existing = geneProductDao.find( geneProduct );
GeneProduct existing = geneProductDao.find( byIdentifiable( geneProduct ) );

if ( existing != null ) {
if ( AbstractPersister.log.isDebugEnabled() )
Expand Down Expand Up @@ -525,7 +527,7 @@ private GeneProduct persistGeneProduct( GeneProduct geneProduct, Caches caches )
private BioSequence persistOrUpdateBioSequence( BioSequence bioSequence, Caches caches ) {
// Note that this method is only really used by the ArrayDesignSequencePersister: it's for filling in
//information about probes on arrays.
BioSequence existingBioSequence = bioSequenceDao.find( bioSequence );
BioSequence existingBioSequence = bioSequenceDao.find( byIdentifiable( bioSequence ) );

if ( existingBioSequence == null ) {
if ( AbstractPersister.log.isDebugEnabled() )
Expand Down Expand Up @@ -597,7 +599,7 @@ private Gene persistOrUpdateGene( Gene gene, Caches caches ) {
if ( gene.getId() != null ) {
existingGene = geneDao.load( gene.getId() );
} else {
existingGene = geneDao.find( gene );
existingGene = geneDao.find( byIdentifiable( gene ) );
}

if ( existingGene == null ) {
Expand All @@ -615,7 +617,7 @@ private GeneProduct persistOrUpdateGeneProduct( GeneProduct geneProduct, Caches
if ( geneProduct.getId() != null ) {
existing = geneProductDao.load( geneProduct.getId() );
} else {
existing = geneProductDao.find( geneProduct );
existing = geneProductDao.find( byIdentifiable( geneProduct ) );
}

if ( existing == null ) {
Expand Down
Loading

0 comments on commit 15a4d6a

Please sign in to comment.