Coverage Report - org.apache.maven.archiva.database.constraints.UniqueArtifactIdConstraint
 
Classes in this File Line Coverage Branch Coverage Complexity
UniqueArtifactIdConstraint
0%
0/52
0%
0/6
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.maven.archiva.database.Constraint;
 23  
 import org.apache.maven.archiva.model.ArchivaArtifactModel;
 24  
 
 25  
 import java.util.List;
 26  
 
 27  
 /**
 28  
  * Obtain a set of unique ArtifactIds for the specified groupId.
 29  
  *
 30  
  * @version $Id: UniqueArtifactIdConstraint.java 1043850 2010-12-09 07:58:00Z brett $
 31  
  */
 32  
 public class UniqueArtifactIdConstraint
 33  
     extends AbstractSimpleConstraint
 34  
     implements Constraint
 35  
 {
 36  0
     private StringBuffer sql = new StringBuffer();
 37  
 
 38  0
     private StringBuffer countSql = new StringBuffer();
 39  
 
 40  
     private Class<?> resultClass;
 41  
     
 42  
     /**
 43  
      * Obtain a set of unique ArtifactIds for the specified groupId.
 44  
      * 
 45  
      * @param groupId the groupId to search for artifactIds within.
 46  
      */
 47  
     public UniqueArtifactIdConstraint( List<String> selectedRepositoryIds, String groupId )
 48  0
     {
 49  0
         appendSelect( sql, false );
 50  0
         sql.append( " WHERE " );
 51  0
         SqlBuilder.appendWhereSelectedRepositories( sql, "repositoryId", selectedRepositoryIds );
 52  0
         sql.append( " && " );
 53  0
         appendWhereSelectedGroupId( sql );
 54  0
         appendGroupBy( sql );
 55  
 
 56  0
         countSql.append( "SELECT count(artifactId) FROM " ).append( ArchivaArtifactModel.class.getName() );
 57  0
         countSql.append( " WHERE " );
 58  0
         SqlBuilder.appendWhereSelectedRepositories( countSql, "repositoryId", selectedRepositoryIds );
 59  0
         countSql.append( " && " );
 60  0
         appendWhereSelectedGroupId( countSql );
 61  0
         appendGroupBy( countSql );
 62  
 
 63  0
         super.params = new Object[] { groupId };
 64  0
     }
 65  
 
 66  
     /**
 67  
      * Obtain a set of unique ArtifactIds for the specified groupId.
 68  
      * 
 69  
      * @param groupId the groupId to search for artifactIds within.
 70  
      */
 71  
     public UniqueArtifactIdConstraint( String groupId )
 72  0
     {
 73  0
         appendSelect( sql, false );
 74  0
         sql.append( " WHERE " );
 75  0
         appendWhereSelectedGroupId( sql );
 76  0
         appendGroupBy( sql );
 77  
 
 78  0
         countSql.append( "SELECT count(artifactId) FROM " ).append( ArchivaArtifactModel.class.getName() );
 79  0
         countSql.append( " WHERE " );
 80  0
         appendWhereSelectedGroupId( countSql );
 81  0
         appendGroupBy( countSql );
 82  
 
 83  0
         super.params = new Object[] { groupId };
 84  0
     }
 85  
     
 86  
     /**
 87  
      * Obtain a set of unique artifactIds with respect to their groups from the specified repository.
 88  
      * 
 89  
      * @param repoId
 90  
      * @param isUnique
 91  
      */
 92  
     public UniqueArtifactIdConstraint( String repoId, boolean isUnique )
 93  0
     {
 94  0
         appendSelect( sql, isUnique );
 95  0
         sql.append( " WHERE repositoryId == \"" + repoId + "\"" );
 96  
 
 97  0
         if( isUnique )
 98  
         {
 99  0
             countSql.append( "SELECT count(this) FROM " ).append( ArchivaArtifactModel.class.getName() );
 100  0
             countSql.append( " WHERE repositoryId == \"" ).append( repoId ).append( "\"" );
 101  0
             countSql.append( " GROUP BY groupId, artifactId" );
 102  
         }
 103  
         else
 104  
         {
 105  0
             countSql.append( "SELECT count(artifactId) FROM " ).append( ArchivaArtifactModel.class.getName() );
 106  0
             countSql.append( " WHERE repositoryId == \"" ).append( repoId ).append( "\"" );
 107  
         }
 108  
 
 109  0
         resultClass = Object[].class;
 110  0
     }
 111  
 
 112  
     @SuppressWarnings("unchecked")
 113  
     public Class getResultClass()
 114  
     {
 115  0
         if( resultClass != null )
 116  
         {
 117  0
             return resultClass;
 118  
         }
 119  
         
 120  0
         return String.class;
 121  
     }
 122  
 
 123  
     public String getSelectSql()
 124  
     {
 125  0
         return sql.toString();
 126  
     }
 127  
 
 128  
     @Override
 129  
     public String getCountSql()
 130  
     {
 131  0
         return countSql.toString();
 132  
     }
 133  
 
 134  
     private void appendGroupBy( StringBuffer buf )
 135  
     {
 136  0
         buf.append( " GROUP BY artifactId ORDER BY artifactId ASCENDING" );
 137  0
     }
 138  
 
 139  
     private void appendSelect( StringBuffer buf, boolean isUnique )
 140  
     {
 141  0
         if( isUnique )
 142  
         {
 143  0
             buf.append( "SELECT DISTINCT groupId, artifactId FROM " ).append( ArchivaArtifactModel.class.getName() );
 144  
         }
 145  
         else
 146  
         {
 147  0
             buf.append( "SELECT artifactId FROM " ).append( ArchivaArtifactModel.class.getName() );
 148  
         }
 149  0
     }
 150  
 
 151  
     private void appendWhereSelectedGroupId( StringBuffer buf )
 152  
     {
 153  0
         buf.append( " groupId == selectedGroupId PARAMETERS String selectedGroupId" );
 154  0
     }
 155  
 
 156  
 }