import org.apache.archiva.indexer.search.SearchResultHit;
import org.apache.archiva.indexer.search.SearchResultLimits;
import org.apache.archiva.indexer.search.SearchResults;
+import org.apache.archiva.metadata.model.ArtifactMetadata;
import org.apache.archiva.metadata.model.ProjectVersionMetadata;
import org.apache.archiva.metadata.model.ProjectVersionReference;
+import org.apache.archiva.metadata.repository.MetadataRepository;
import org.apache.archiva.metadata.repository.MetadataResolver;
import org.apache.archiva.metadata.repository.storage.maven2.MavenProjectFacet;
import org.apache.archiva.web.xmlrpc.api.SearchService;
import org.apache.archiva.web.xmlrpc.api.beans.Artifact;
import org.apache.archiva.web.xmlrpc.api.beans.Dependency;
import org.apache.archiva.web.xmlrpc.security.XmlRpcUserRepositories;
-import org.apache.maven.archiva.common.utils.VersionUtil;
-import org.apache.maven.archiva.database.ArchivaDAO;
-import org.apache.maven.archiva.database.ArtifactDAO;
-import org.apache.maven.archiva.database.constraints.ArtifactsByChecksumConstraint;
-import org.apache.maven.archiva.database.constraints.UniqueVersionConstraint;
-import org.apache.maven.archiva.model.ArchivaArtifact;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import org.apache.maven.archiva.repository.content.ArtifactExtensionMapping;
public class SearchServiceImpl
implements SearchService
{
- private Logger log = LoggerFactory.getLogger( SearchServiceImpl.class );
-
private RepositorySearch search;
private XmlRpcUserRepositories xmlRpcUserRepositories;
- private ArchivaDAO archivaDAO;
-
private MetadataResolver metadataResolver;
- public SearchServiceImpl( XmlRpcUserRepositories xmlRpcUserRepositories, ArchivaDAO archivaDAO,
- MetadataResolver metadataResolver, RepositorySearch search )
+ private MetadataRepository metadataRepository;
+
+ public SearchServiceImpl( XmlRpcUserRepositories xmlRpcUserRepositories, MetadataResolver metadataResolver,
+ MetadataRepository metadataRepository, RepositorySearch search )
{
this.xmlRpcUserRepositories = xmlRpcUserRepositories;
- this.archivaDAO = archivaDAO;
this.search = search;
this.metadataResolver = metadataResolver;
+ this.metadataRepository = metadataRepository;
}
@SuppressWarnings("unchecked")
List<Artifact> artifacts = new ArrayList<Artifact>();
List<String> observableRepos = xmlRpcUserRepositories.getObservableRepositories();
SearchResultLimits limits = new SearchResultLimits( SearchResultLimits.ALL_PAGES );
- SearchResults results = null;
+ SearchResults results;
results = search.search( "", observableRepos, queryString, limits, null );
for ( SearchResultHit resultHit : results.getHits() )
{
- // double-check all versions as done in SearchAction
- final List<String> versions = (List<String>) archivaDAO.query(
- new UniqueVersionConstraint( observableRepos, resultHit.getGroupId(), resultHit.getArtifactId() ) );
- if ( versions != null && !versions.isEmpty() )
- {
- resultHit.setVersion( null );
- resultHit.setVersions( filterTimestampedSnapshots( versions ) );
- }
-
List<String> resultHitVersions = resultHit.getVersions();
if ( resultHitVersions != null )
{
return artifacts;
}
- /**
- * Remove timestamped snapshots from versions
- */
- private static List<String> filterTimestampedSnapshots( List<String> versions )
- {
- final List<String> filtered = new ArrayList<String>();
- for ( final String version : versions )
- {
- final String baseVersion = VersionUtil.getBaseVersion( version );
- if ( !filtered.contains( baseVersion ) )
- {
- filtered.add( baseVersion );
- }
- }
- return filtered;
- }
-
public List<Artifact> getArtifactByChecksum( String checksum )
throws Exception
{
- // 1. get ArtifactDAO from ArchivaDAO
- // 2. create ArtifactsByChecksumConstraint( "queryTerm" )
- // 3. query artifacts using constraint
- // 4. convert results to list of Artifact objects
+ List<String> observableRepos = xmlRpcUserRepositories.getObservableRepositories();
List<Artifact> results = new ArrayList<Artifact>();
- ArtifactDAO artifactDAO = archivaDAO.getArtifactDAO();
-
- ArtifactsByChecksumConstraint constraint = new ArtifactsByChecksumConstraint( checksum );
- List<ArchivaArtifact> artifacts = artifactDAO.queryArtifacts( constraint );
-
- for ( ArchivaArtifact archivaArtifact : artifacts )
+ for ( String repoId : observableRepos )
{
- Artifact artifact =
- new Artifact( archivaArtifact.getModel().getRepositoryId(), archivaArtifact.getModel().getGroupId(),
- archivaArtifact.getModel().getArtifactId(), archivaArtifact.getModel().getVersion(),
- archivaArtifact.getType() );
- //archivaArtifact.getModel().getWhenGathered() );
- results.add( artifact );
+ for ( ArtifactMetadata artifact : metadataRepository.getArtifactsByChecksum( repoId, checksum ) )
+ {
+ // TODO: use a maven facet instead, for now just using the extension and classifier
+ String type = null;
+ String key = artifact.getProject() + "-" + artifact.getVersion();
+ String filename = artifact.getId();
+ int extIndex = filename.lastIndexOf( "." );
+ if ( filename.startsWith( key ) )
+ {
+ int i = key.length();
+ char nextToken = filename.charAt( i );
+ if ( nextToken == '-' )
+ {
+ String classifier = filename.substring( i + 1, extIndex );
+ String extension = filename.substring( extIndex + 1 );
+ type = ArtifactExtensionMapping.mapExtensionAndClassifierToType( classifier, extension );
+ }
+ else if ( nextToken == '.' )
+ {
+ type = ArtifactExtensionMapping.mapExtensionToType( filename.substring( i + 1 ) );
+ }
+ }
+ results.add( new Artifact( artifact.getRepositoryId(), artifact.getNamespace(), artifact.getProject(),
+ artifact.getVersion(), type ) );
+ }
}
-
return results;
}
public List<Artifact> getArtifactVersionsByDate( String groupId, String artifactId, String version, Date since )
throws Exception
{
- List<Artifact> artifacts = new ArrayList<Artifact>();
+// List<Artifact> artifacts = new ArrayList<Artifact>();
// 1. get observable repositories
// 2. use RepositoryBrowsing method to query uniqueVersions? (but with date)
-
+
throw new UnsupportedOperationException( "getArtifactVersionsByDate not yet implemented" );
// return artifacts;
public List<Artifact> getDependencyTree( String groupId, String artifactId, String version )
throws Exception
{
- List<Artifact> a = new ArrayList<Artifact>();
-
+// List<Artifact> a = new ArrayList<Artifact>();
+
throw new UnsupportedOperationException( "getDependencyTree not yet implemented" );
// return a;
}
import org.apache.archiva.indexer.search.SearchResultLimits;
import org.apache.archiva.indexer.search.SearchResults;
import org.apache.archiva.indexer.util.SearchUtil;
+import org.apache.archiva.metadata.model.ArtifactMetadata;
import org.apache.archiva.metadata.model.ProjectVersionMetadata;
import org.apache.archiva.metadata.model.ProjectVersionReference;
+import org.apache.archiva.metadata.repository.MetadataRepository;
import org.apache.archiva.metadata.repository.MetadataResolver;
import org.apache.archiva.metadata.repository.storage.maven2.MavenProjectFacet;
import org.apache.archiva.web.xmlrpc.api.SearchService;
import org.apache.archiva.web.xmlrpc.api.beans.Artifact;
import org.apache.archiva.web.xmlrpc.api.beans.Dependency;
import org.apache.archiva.web.xmlrpc.security.XmlRpcUserRepositories;
-import org.apache.maven.archiva.database.ArchivaDAO;
-import org.apache.maven.archiva.database.ArtifactDAO;
-import org.apache.maven.archiva.database.constraints.ArtifactsByChecksumConstraint;
-import org.apache.maven.archiva.database.constraints.UniqueVersionConstraint;
-import org.apache.maven.archiva.model.ArchivaArtifact;
import org.codehaus.plexus.spring.PlexusInSpringTestCase;
import org.easymock.MockControl;
import org.easymock.classextension.MockClassControl;
private RepositorySearch search;
- private MockControl archivaDAOControl;
-
- private ArchivaDAO archivaDAO;
-
- private MockControl artifactDAOControl;
-
- private ArtifactDAO artifactDAO;
-
private static final String ARCHIVA_TEST_ARTIFACT_ID = "archiva-xmlrpc-test";
private static final String ARCHIVA_TEST_GROUP_ID = "org.apache.archiva";
private MetadataResolver metadataResolver;
+ private MockControl metadataRepositoryControl;
+
+ private MetadataRepository metadataRepository;
+
+ private static final String CHECKSUM = "a1b2c3aksjhdasfkdasasd";
+
+ private static final String TEST_REPO = "test-repo";
+
@Override
public void setUp()
throws Exception
userReposControl = MockClassControl.createControl( XmlRpcUserRepositories.class );
userRepos = (XmlRpcUserRepositories) userReposControl.getMock();
- archivaDAOControl = MockControl.createControl( ArchivaDAO.class );
- archivaDAOControl.setDefaultMatcher( MockControl.ALWAYS_MATCHER );
- archivaDAO = (ArchivaDAO) archivaDAOControl.getMock();
-
searchControl = MockControl.createControl( RepositorySearch.class );
searchControl.setDefaultMatcher( MockControl.ALWAYS_MATCHER );
search = (RepositorySearch) searchControl.getMock();
metadataResolverControl = MockControl.createControl( MetadataResolver.class );
metadataResolver = (MetadataResolver) metadataResolverControl.getMock();
- artifactDAOControl = MockControl.createControl( ArtifactDAO.class );
- artifactDAO = (ArtifactDAO) artifactDAOControl.getMock();
+ metadataRepositoryControl = MockControl.createControl( MetadataRepository.class );
+ metadataRepository = (MetadataRepository) metadataRepositoryControl.getMock();
- searchService = new SearchServiceImpl( userRepos, archivaDAO, metadataResolver, search );
+ searchService = new SearchServiceImpl( userRepos, metadataResolver, metadataRepository, search );
}
// MRM-1230
+
public void testQuickSearchModelPackagingIsUsed()
throws Exception
{
searchControl.expectAndDefaultReturn( search.search( "", observableRepoIds, "archiva", limits, null ),
results );
- archivaDAOControl.expectAndReturn( archivaDAO.query(
- new UniqueVersionConstraint( observableRepoIds, resultHit.getGroupId(), resultHit.getArtifactId() ) ),
- null );
-
ProjectVersionMetadata model = new ProjectVersionMetadata();
model.setId( "1.0" );
MavenProjectFacet facet = new MavenProjectFacet();
userReposControl.replay();
searchControl.replay();
metadataResolverControl.replay();
- archivaDAOControl.replay();
+ metadataRepositoryControl.replay();
List<Artifact> artifacts = searchService.quickSearch( "archiva" );
userReposControl.verify();
searchControl.verify();
metadataResolverControl.verify();
- archivaDAOControl.verify();
+ metadataRepositoryControl.verify();
assertNotNull( artifacts );
assertEquals( 1, artifacts.size() );
searchControl.expectAndDefaultReturn( search.search( "", observableRepoIds, "archiva", limits, null ),
results );
- archivaDAOControl.expectAndReturn( archivaDAO.query(
- new UniqueVersionConstraint( observableRepoIds, resultHit.getGroupId(), resultHit.getArtifactId() ) ),
- null );
-
metadataResolverControl.expectAndReturn(
metadataResolver.getProjectVersion( "repo1.mirror", ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID,
"1.0" ), null );
userReposControl.replay();
searchControl.replay();
metadataResolverControl.replay();
- archivaDAOControl.replay();
+ metadataRepositoryControl.replay();
List<Artifact> artifacts = searchService.quickSearch( "archiva" );
userReposControl.verify();
searchControl.verify();
metadataResolverControl.verify();
- archivaDAOControl.verify();
+ metadataRepositoryControl.verify();
assertNotNull( artifacts );
assertEquals( 1, artifacts.size() );
results.addHit( SearchUtil.getHitId( resultHit.getGroupId(), resultHit.getArtifactId() ), resultHit );
- archivaDAOControl.expectAndReturn( archivaDAO.query(
- new UniqueVersionConstraint( observableRepoIds, resultHit.getGroupId(), resultHit.getArtifactId() ) ),
- null );
-
SearchResultLimits limits = new SearchResultLimits( SearchResultLimits.ALL_PAGES );
searchControl.expectAndDefaultReturn( search.search( "", observableRepoIds, "archiva", limits, null ),
userReposControl.replay();
searchControl.replay();
- archivaDAOControl.replay();
+ metadataRepositoryControl.replay();
metadataResolverControl.replay();
List<Artifact> artifacts = searchService.quickSearch( "archiva" );
userReposControl.verify();
searchControl.verify();
- archivaDAOControl.verify();
+ metadataRepositoryControl.verify();
metadataResolverControl.verify();
assertNotNull( artifacts );
public void testGetArtifactByChecksum()
throws Exception
{
- Date whenGathered = new Date();
+ userReposControl.expectAndReturn( userRepos.getObservableRepositories(),
+ Collections.singletonList( TEST_REPO ) );
- ArtifactsByChecksumConstraint constraint = new ArtifactsByChecksumConstraint( "a1b2c3aksjhdasfkdasasd" );
- List<ArchivaArtifact> artifacts = new ArrayList<ArchivaArtifact>();
- ArchivaArtifact artifact =
- new ArchivaArtifact( ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.0", "", "jar", "test-repo" );
- artifact.getModel().setWhenGathered( whenGathered );
- artifacts.add( artifact );
+ Date whenGathered = new Date();
- archivaDAOControl.expectAndReturn( archivaDAO.getArtifactDAO(), artifactDAO );
- artifactDAO.queryArtifacts( constraint );
- artifactDAOControl.setMatcher( MockControl.ALWAYS_MATCHER );
- artifactDAOControl.setReturnValue( artifacts );
+ ArtifactMetadata artifact = createArtifact( whenGathered );
+ metadataRepositoryControl.expectAndReturn( metadataRepository.getArtifactsByChecksum( TEST_REPO, CHECKSUM ),
+ Collections.singletonList( artifact ) );
- archivaDAOControl.replay();
- artifactDAOControl.replay();
+ metadataRepositoryControl.replay();
+ userReposControl.replay();
- List<Artifact> results = searchService.getArtifactByChecksum( "a1b2c3aksjhdasfkdasasd" );
+ List<Artifact> results = searchService.getArtifactByChecksum( CHECKSUM );
- archivaDAOControl.verify();
- artifactDAOControl.verify();
+ metadataRepositoryControl.verify();
+ userReposControl.verify();
assertNotNull( results );
assertEquals( 1, results.size() );
+ Artifact result = results.get( 0 );
+ assertEquals( ARCHIVA_TEST_GROUP_ID, result.getGroupId() );
+ assertEquals( ARCHIVA_TEST_ARTIFACT_ID, result.getArtifactId() );
+ assertEquals( "1.0", result.getVersion() );
+ assertEquals( "jar", result.getType() );
+ assertEquals( TEST_REPO, result.getRepositoryId() );
}
public void testGetArtifactVersionsArtifactExists()
assertTrue( searchService.getDependees( ARCHIVA_TEST_GROUP_ID, ARCHIVA_TEST_ARTIFACT_ID, "1.0" ).isEmpty() );
}
+
+ private ArtifactMetadata createArtifact( Date whenGathered )
+ {
+ String version = "1.0";
+ ArtifactMetadata artifactMetadata = new ArtifactMetadata();
+ artifactMetadata.setVersion( version );
+ artifactMetadata.setId( ARCHIVA_TEST_ARTIFACT_ID + "-" + version + ".jar" );
+ artifactMetadata.setProject( ARCHIVA_TEST_ARTIFACT_ID );
+ artifactMetadata.setNamespace( ARCHIVA_TEST_GROUP_ID );
+ artifactMetadata.setMd5( CHECKSUM );
+ artifactMetadata.setWhenGathered( whenGathered );
+ artifactMetadata.setRepositoryId( TEST_REPO );
+ return artifactMetadata;
+ }
}
\ No newline at end of file