View Javadoc

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      private StringBuffer sql = new StringBuffer();
37  
38      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      {
49          appendSelect( sql, false );
50          sql.append( " WHERE " );
51          SqlBuilder.appendWhereSelectedRepositories( sql, "repositoryId", selectedRepositoryIds );
52          sql.append( " && " );
53          appendWhereSelectedGroupId( sql );
54          appendGroupBy( sql );
55  
56          countSql.append( "SELECT count(artifactId) FROM " ).append( ArchivaArtifactModel.class.getName() );
57          countSql.append( " WHERE " );
58          SqlBuilder.appendWhereSelectedRepositories( countSql, "repositoryId", selectedRepositoryIds );
59          countSql.append( " && " );
60          appendWhereSelectedGroupId( countSql );
61          appendGroupBy( countSql );
62  
63          super.params = new Object[] { groupId };
64      }
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      {
73          appendSelect( sql, false );
74          sql.append( " WHERE " );
75          appendWhereSelectedGroupId( sql );
76          appendGroupBy( sql );
77  
78          countSql.append( "SELECT count(artifactId) FROM " ).append( ArchivaArtifactModel.class.getName() );
79          countSql.append( " WHERE " );
80          appendWhereSelectedGroupId( countSql );
81          appendGroupBy( countSql );
82  
83          super.params = new Object[] { groupId };
84      }
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      {
94          appendSelect( sql, isUnique );
95          sql.append( " WHERE repositoryId == \"" + repoId + "\"" );
96  
97          if( isUnique )
98          {
99              countSql.append( "SELECT count(this) FROM " ).append( ArchivaArtifactModel.class.getName() );
100             countSql.append( " WHERE repositoryId == \"" ).append( repoId ).append( "\"" );
101             countSql.append( " GROUP BY groupId, artifactId" );
102         }
103         else
104         {
105             countSql.append( "SELECT count(artifactId) FROM " ).append( ArchivaArtifactModel.class.getName() );
106             countSql.append( " WHERE repositoryId == \"" ).append( repoId ).append( "\"" );
107         }
108 
109         resultClass = Object[].class;
110     }
111 
112     @SuppressWarnings("unchecked")
113     public Class getResultClass()
114     {
115         if( resultClass != null )
116         {
117             return resultClass;
118         }
119         
120         return String.class;
121     }
122 
123     public String getSelectSql()
124     {
125         return sql.toString();
126     }
127 
128     @Override
129     public String getCountSql()
130     {
131         return countSql.toString();
132     }
133 
134     private void appendGroupBy( StringBuffer buf )
135     {
136         buf.append( " GROUP BY artifactId ORDER BY artifactId ASCENDING" );
137     }
138 
139     private void appendSelect( StringBuffer buf, boolean isUnique )
140     {
141         if( isUnique )
142         {
143             buf.append( "SELECT DISTINCT groupId, artifactId FROM " ).append( ArchivaArtifactModel.class.getName() );
144         }
145         else
146         {
147             buf.append( "SELECT artifactId FROM " ).append( ArchivaArtifactModel.class.getName() );
148         }
149     }
150 
151     private void appendWhereSelectedGroupId( StringBuffer buf )
152     {
153         buf.append( " groupId == selectedGroupId PARAMETERS String selectedGroupId" );
154     }
155 
156 }