Coverage Report - org.apache.archiva.web.xmlrpc.services.SearchServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
SearchServiceImpl
0%
0/82
0%
0/24
0
 
 1  
 package org.apache.archiva.web.xmlrpc.services;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *  http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import java.util.ArrayList;
 23  
 import java.util.Date;
 24  
 import java.util.List;
 25  
 
 26  
 import org.apache.archiva.indexer.search.RepositorySearch;
 27  
 import org.apache.archiva.indexer.search.SearchResultHit;
 28  
 import org.apache.archiva.indexer.search.SearchResultLimits;
 29  
 import org.apache.archiva.indexer.search.SearchResults;
 30  
 import org.apache.archiva.web.xmlrpc.api.SearchService;
 31  
 import org.apache.archiva.web.xmlrpc.api.beans.Artifact;
 32  
 import org.apache.archiva.web.xmlrpc.api.beans.Dependency;
 33  
 import org.apache.archiva.web.xmlrpc.security.XmlRpcUserRepositories;
 34  
 import org.apache.maven.archiva.common.utils.VersionUtil;
 35  
 import org.apache.maven.archiva.database.ArchivaDAO;
 36  
 import org.apache.maven.archiva.database.ArchivaDatabaseException;
 37  
 import org.apache.maven.archiva.database.ArtifactDAO;
 38  
 import org.apache.maven.archiva.database.ObjectNotFoundException;
 39  
 import org.apache.maven.archiva.database.browsing.BrowsingResults;
 40  
 import org.apache.maven.archiva.database.browsing.RepositoryBrowsing;
 41  
 import org.apache.maven.archiva.database.constraints.ArtifactsByChecksumConstraint;
 42  
 import org.apache.maven.archiva.database.constraints.UniqueVersionConstraint;
 43  
 import org.apache.maven.archiva.model.ArchivaArtifact;
 44  
 import org.apache.maven.archiva.model.ArchivaProjectModel;
 45  
 import org.slf4j.Logger;
 46  
 import org.slf4j.LoggerFactory;
 47  
 
 48  
 public class SearchServiceImpl
 49  
     implements SearchService
 50  
 { 
 51  0
     private Logger log = LoggerFactory.getLogger( SearchServiceImpl.class );
 52  
                                                  
 53  
     private RepositorySearch search;
 54  
     
 55  
     private XmlRpcUserRepositories xmlRpcUserRepositories;
 56  
     
 57  
     private ArchivaDAO archivaDAO;
 58  
     
 59  
     private RepositoryBrowsing repoBrowsing;
 60  
     
 61  
     public SearchServiceImpl( XmlRpcUserRepositories xmlRpcUserRepositories, ArchivaDAO archivaDAO,
 62  
                               RepositoryBrowsing repoBrowsing, RepositorySearch search )
 63  0
     {
 64  0
         this.xmlRpcUserRepositories = xmlRpcUserRepositories;
 65  0
         this.archivaDAO = archivaDAO;        
 66  0
         this.repoBrowsing = repoBrowsing;
 67  0
         this.search = search;
 68  0
     }
 69  
       
 70  
     @SuppressWarnings( "unchecked" )
 71  
     public List<Artifact> quickSearch( String queryString )
 72  
         throws Exception
 73  
     {   
 74  0
         List<Artifact> artifacts = new ArrayList<Artifact>();
 75  0
         List<String> observableRepos = xmlRpcUserRepositories.getObservableRepositories();
 76  0
         SearchResultLimits limits = new SearchResultLimits( SearchResultLimits.ALL_PAGES );
 77  0
         SearchResults results = null;
 78  
         
 79  0
         results = search.search( "", observableRepos, queryString, limits, null );
 80  
         
 81  0
         for ( SearchResultHit resultHit : results.getHits() )
 82  
         {
 83  
             // double-check all versions as done in SearchAction
 84  0
             final List<String> versions =
 85  
                 (List<String>) archivaDAO.query( new UniqueVersionConstraint( observableRepos, resultHit.getGroupId(),
 86  
                                                     resultHit.getArtifactId() ) );
 87  0
             if ( versions != null && !versions.isEmpty() )
 88  
             {
 89  0
                 resultHit.setVersion( null );
 90  0
                 resultHit.setVersions( filterTimestampedSnapshots( versions ) );
 91  
             }
 92  
                         
 93  0
             List<String> resultHitVersions = resultHit.getVersions();
 94  0
             if( resultHitVersions != null )
 95  
             {
 96  0
                 for( String version : resultHitVersions )
 97  
                 {   
 98  
                     try
 99  
                     {
 100  0
                         ArchivaProjectModel model = repoBrowsing.selectVersion( "", observableRepos, resultHit.getGroupId(), resultHit.getArtifactId(), version );
 101  
                         
 102  0
                         String repoId = repoBrowsing.getRepositoryId( "", observableRepos, resultHit.getGroupId(), resultHit.getArtifactId(), version );
 103  
                         
 104  0
                         Artifact artifact = null;
 105  0
                         if( model == null )
 106  
                         {
 107  0
                            artifact = new Artifact( repoId, resultHit.getGroupId(), resultHit.getArtifactId(), version, "jar" );                           
 108  
                         }
 109  
                         else
 110  
                         {                       
 111  0
                             artifact = new Artifact( repoId, model.getGroupId(), model.getArtifactId(), version, model.getPackaging() );
 112  
                         }
 113  0
                         artifacts.add( artifact );
 114  
                     }
 115  0
                     catch( ObjectNotFoundException e )
 116  
                     {                          
 117  0
                         log.debug( "Unable to find pom artifact : " + e.getMessage() );                        
 118  
                     }
 119  0
                     catch( ArchivaDatabaseException e )
 120  
                     {                           
 121  0
                         log.debug( "Error occurred while getting pom artifact from database : " + e.getMessage() );
 122  0
                     }
 123  
                 }
 124  
             }
 125  0
         }    
 126  
         
 127  0
         return artifacts;
 128  
     }
 129  
     
 130  
     /**
 131  
      * Remove timestamped snapshots from versions
 132  
      */
 133  
     private static List<String> filterTimestampedSnapshots(List<String> versions)
 134  
     {
 135  0
         final List<String> filtered = new ArrayList<String>();
 136  0
         for (final String version : versions)
 137  
         {
 138  0
             final String baseVersion = VersionUtil.getBaseVersion(version);
 139  0
             if (!filtered.contains(baseVersion))
 140  
             {
 141  0
                 filtered.add(baseVersion);
 142  
             }
 143  0
         }
 144  0
         return filtered;
 145  
     }
 146  
     
 147  
     public List<Artifact> getArtifactByChecksum( String checksum ) 
 148  
         throws Exception
 149  
     {
 150  
         // 1. get ArtifactDAO from ArchivaDAO
 151  
         // 2. create ArtifactsByChecksumConstraint( "queryTerm" )
 152  
         // 3. query artifacts using constraint
 153  
         // 4. convert results to list of Artifact objects
 154  
         
 155  0
         List<Artifact> results = new ArrayList<Artifact>();
 156  0
         ArtifactDAO artifactDAO = archivaDAO.getArtifactDAO();
 157  
         
 158  0
         ArtifactsByChecksumConstraint constraint = new ArtifactsByChecksumConstraint( checksum );
 159  0
         List<ArchivaArtifact> artifacts = artifactDAO.queryArtifacts( constraint );
 160  
         
 161  0
         for( ArchivaArtifact archivaArtifact : artifacts )
 162  
         {
 163  0
             Artifact artifact = new Artifact( archivaArtifact.getModel().getRepositoryId(), archivaArtifact.getModel().getGroupId(),
 164  
                           archivaArtifact.getModel().getArtifactId(), archivaArtifact.getModel().getVersion(), archivaArtifact.getType() ); 
 165  
                           //archivaArtifact.getModel().getWhenGathered() );
 166  0
             results.add( artifact );
 167  0
         }
 168  
         
 169  0
         return results;
 170  
     }
 171  
     
 172  
     public List<Artifact> getArtifactVersions( String groupId, String artifactId ) 
 173  
         throws Exception
 174  
     {
 175  0
         final List<Artifact> artifacts = new ArrayList<Artifact>();        
 176  0
         final List<String> observableRepos = xmlRpcUserRepositories.getObservableRepositories();
 177  
         
 178  0
         final BrowsingResults results = repoBrowsing.selectArtifactId( "", observableRepos, groupId, artifactId );
 179  
         
 180  0
         for( final String version : results.getVersions() )
 181  
         {
 182  0
             final Artifact artifact = new Artifact( "", groupId, artifactId, version, "pom" ); 
 183  
             //ArchivaArtifact pomArtifact = artifactDAO.getArtifact( groupId, artifactId, version, "", "pom",  );
 184  
             //Artifact artifact = new Artifact( "", groupId, artifactId, version, pomArtifact.getType() ); 
 185  
                           //pomArtifact.getModel().getWhenGathered() );
 186  
             
 187  0
             artifacts.add( artifact );
 188  0
         }
 189  
         
 190  
         // 1. get observable repositories
 191  
         // 2. use RepositoryBrowsing method to query uniqueVersions?
 192  0
         return artifacts;
 193  
     }
 194  
     
 195  
     public List<Artifact> getArtifactVersionsByDate( String groupId, String artifactId, String version, Date since )
 196  
         throws Exception
 197  
     {
 198  0
         List<Artifact> artifacts = new ArrayList<Artifact>();
 199  
         
 200  
         // 1. get observable repositories
 201  
         // 2. use RepositoryBrowsing method to query uniqueVersions? (but with date)
 202  
         
 203  0
         throw new UnsupportedOperationException( "getArtifactVersionsByDate not yet implemented" );
 204  
 
 205  
 //        return artifacts;
 206  
     }
 207  
     
 208  
     public List<Dependency> getDependencies( String groupId, String artifactId, String version ) 
 209  
         throws Exception
 210  
     {  
 211  0
         List<Dependency> dependencies = new ArrayList<Dependency>();        
 212  0
         List<String> observableRepos = xmlRpcUserRepositories.getObservableRepositories();
 213  
         
 214  
         try
 215  
         {
 216  0
             ArchivaProjectModel model = repoBrowsing.selectVersion( "", observableRepos, groupId, artifactId, version );
 217  0
             List<org.apache.maven.archiva.model.Dependency> modelDeps = model.getDependencies();
 218  0
             for( org.apache.maven.archiva.model.Dependency dep : modelDeps )
 219  
             {
 220  0
                 Dependency dependency = new Dependency( 
 221  
                     dep.getGroupId(), dep.getArtifactId(), dep.getVersion(), dep.getClassifier(), dep.getType(), dep.getScope() );
 222  0
                 dependencies.add( dependency );
 223  0
             }
 224  
         }
 225  0
         catch ( ObjectNotFoundException oe )
 226  
         {
 227  0
             throw new Exception( "Artifact does not exist." );
 228  0
         }
 229  
         
 230  0
         return dependencies;
 231  
     }
 232  
     
 233  
     public List<Artifact> getDependencyTree( String groupId, String artifactId, String version ) 
 234  
         throws Exception
 235  
     {
 236  0
         List<Artifact> a = new ArrayList<Artifact>();
 237  
         
 238  0
         throw new UnsupportedOperationException( "getDependencyTree not yet implemented" );
 239  
 //        return a;
 240  
     }
 241  
     
 242  
   //get artifacts that depend on a given artifact
 243  
     public List<Artifact> getDependees( String groupId, String artifactId, String version )
 244  
         throws Exception
 245  
     {
 246  0
         List<Artifact> artifacts = new ArrayList<Artifact>();
 247  0
         List<String> observableRepos = xmlRpcUserRepositories.getObservableRepositories();
 248  
         
 249  0
         List<ArchivaProjectModel> dependees = repoBrowsing.getUsedBy( "", observableRepos, groupId, artifactId, version );
 250  0
         for( ArchivaProjectModel model : dependees )
 251  
         {
 252  0
             Artifact artifact =
 253  
                 new Artifact( "", model.getGroupId(), model.getArtifactId(), model.getVersion(), "" );
 254  
                               //model.getWhenIndexed() );
 255  0
             artifacts.add( artifact );
 256  0
         }
 257  
         
 258  0
         return artifacts;
 259  
     }
 260  
 }