1 | |
package org.apache.maven.archiva.database.constraints; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
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 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
public class UniqueVersionConstraint |
34 | |
extends AbstractSimpleConstraint |
35 | |
implements Constraint |
36 | |
{ |
37 | 0 | private StringBuffer sql = new StringBuffer(); |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
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 | |
|
70 | |
|
71 | |
|
72 | |
|
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 | |
} |