]> source.dussan.org Git - archiva.git/commitdiff
[MRM-1300] move search and find artifact action over to metadata repository for XMLRPC
authorBrett Porter <brett@apache.org>
Thu, 10 Dec 2009 11:36:51 +0000 (11:36 +0000)
committerBrett Porter <brett@apache.org>
Thu, 10 Dec 2009 11:36:51 +0000 (11:36 +0000)
git-svn-id: https://svn.apache.org/repos/asf/archiva/branches/MRM-1025@889203 13f79535-47bb-0310-9956-ffa450edef68

archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-services/src/main/java/org/apache/archiva/web/xmlrpc/services/SearchServiceImpl.java
archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-services/src/test/java/org/apache/archiva/web/xmlrpc/services/SearchServiceImplTest.java

index c1e81e388ce06107238bc10955c3bd9d6577917e..632821281eb47911515ded482c8ce216fff1c42a 100644 (file)
@@ -28,43 +28,36 @@ import org.apache.archiva.indexer.search.RepositorySearch;
 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")
@@ -74,21 +67,12 @@ public class SearchServiceImpl
         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 )
             {
@@ -130,47 +114,40 @@ public class SearchServiceImpl
         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;
     }
 
@@ -198,11 +175,11 @@ public class SearchServiceImpl
     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;
@@ -236,8 +213,8 @@ public class SearchServiceImpl
     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;
     }
index 34b18b48377a4d20f605718cfb4d59f49687c188..45d3f12c6732e95667096782aaa7ede9d019685f 100644 (file)
@@ -31,19 +31,16 @@ 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.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;
@@ -66,14 +63,6 @@ public class SearchServiceImplTest
 
     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";
@@ -82,6 +71,14 @@ public class SearchServiceImplTest
 
     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
@@ -89,10 +86,6 @@ public class SearchServiceImplTest
         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();
@@ -100,13 +93,14 @@ public class SearchServiceImplTest
         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
     {
@@ -133,10 +127,6 @@ public class SearchServiceImplTest
         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();
@@ -150,14 +140,14 @@ public class SearchServiceImplTest
         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() );
@@ -197,10 +187,6 @@ public class SearchServiceImplTest
         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 );
@@ -214,14 +200,14 @@ public class SearchServiceImplTest
         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() );
@@ -255,10 +241,6 @@ public class SearchServiceImplTest
 
         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 ),
@@ -276,14 +258,14 @@ public class SearchServiceImplTest
 
         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 );
@@ -327,30 +309,31 @@ public class SearchServiceImplTest
     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()
@@ -553,4 +536,18 @@ public class SearchServiceImplTest
 
         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