Coverage Report - org.apache.maven.archiva.database.constraints.UniqueVersionConstraint
 
Classes in this File Line Coverage Branch Coverage Complexity
UniqueVersionConstraint
0%
0/34
0%
0/8
0
 
 1  
 package org.apache.maven.archiva.database.constraints;
 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 org.apache.commons.lang.StringUtils;
 23  
 import org.apache.maven.archiva.database.Constraint;
 24  
 import org.apache.maven.archiva.model.ArchivaArtifactModel;
 25  
 
 26  
 import java.util.List;
 27  
 
 28  
 /**
 29  
  * Obtain the list of version's for specific GroupId and ArtifactId.
 30  
  *
 31  
  * @version $Id: UniqueVersionConstraint.java 718864 2008-11-19 06:33:35Z brett $
 32  
  */
 33  
 public class UniqueVersionConstraint
 34  
     extends AbstractSimpleConstraint
 35  
     implements Constraint
 36  
 {
 37  0
     private StringBuffer sql = new StringBuffer();
 38  
 
 39  
     /**
 40  
      * Obtain the list of version's for specific GroupId and ArtifactId.
 41  
      * 
 42  
      * @param selectedRepositoryIds the selected repository ids.
 43  
      * @param groupId the selected groupId.
 44  
      * @param artifactId the selected artifactId.
 45  
      */
 46  
     public UniqueVersionConstraint( List<String> selectedRepositoryIds, String groupId, String artifactId )
 47  0
     {
 48  0
         if ( StringUtils.isBlank( groupId ) )
 49  
         {
 50  0
             throw new IllegalArgumentException( "A blank groupId is not allowed." );
 51  
         }
 52  
 
 53  0
         if ( StringUtils.isBlank( artifactId ) )
 54  
         {
 55  0
             throw new IllegalArgumentException( "A blank artifactId is not allowed." );
 56  
         }
 57  
 
 58  0
         appendSelect( sql );
 59  0
         sql.append( " WHERE " );
 60  0
         SqlBuilder.appendWhereSelectedRepositories( sql, "repositoryId", selectedRepositoryIds );
 61  0
         sql.append( " && " );
 62  0
         appendWhereSelectedGroupIdArtifactId( sql );
 63  0
         appendGroupBy( sql );
 64  
 
 65  0
         super.params = new Object[] { groupId, artifactId };
 66  0
     }
 67  
 
 68  
     /**
 69  
      * Obtain the list of version's for specific GroupId and ArtifactId.
 70  
      * 
 71  
      * @param groupId the selected groupId.
 72  
      * @param artifactId the selected artifactId.
 73  
      */
 74  
     public UniqueVersionConstraint( String groupId, String artifactId )
 75  0
     {
 76  0
         if ( StringUtils.isBlank( groupId ) )
 77  
         {
 78  0
             throw new IllegalArgumentException( "A blank groupId is not allowed." );
 79  
         }
 80  
 
 81  0
         if ( StringUtils.isBlank( artifactId ) )
 82  
         {
 83  0
             throw new IllegalArgumentException( "A blank artifactId is not allowed." );
 84  
         }
 85  
 
 86  0
         appendSelect( sql );
 87  0
         sql.append( " WHERE " );
 88  0
         appendWhereSelectedGroupIdArtifactId( sql );
 89  0
         appendGroupBy( sql );
 90  
 
 91  0
         super.params = new Object[] { groupId, artifactId };
 92  0
     }
 93  
 
 94  
     @SuppressWarnings("unchecked")
 95  
     public Class getResultClass()
 96  
     {
 97  0
         return String.class;
 98  
     }
 99  
 
 100  
     public String getSelectSql()
 101  
     {
 102  0
         return sql.toString();
 103  
     }
 104  
 
 105  
     private void appendGroupBy( StringBuffer buf )
 106  
     {
 107  0
         buf.append( " GROUP BY version ORDER BY version ASCENDING" );
 108  0
     }
 109  
 
 110  
     private void appendSelect( StringBuffer buf )
 111  
     {
 112  0
         buf.append( "SELECT version FROM " ).append( ArchivaArtifactModel.class.getName() );
 113  0
     }
 114  
 
 115  
     private void appendWhereSelectedGroupIdArtifactId( StringBuffer buf )
 116  
     {
 117  0
         buf.append( " groupId == selectedGroupId && artifactId == selectedArtifactId" );
 118  0
         buf.append( " PARAMETERS String selectedGroupId, String selectedArtifactId" );
 119  0
     }
 120  
 }