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.maven.archiva.database.Constraint; |
23 | |
import org.apache.maven.archiva.model.ArchivaArtifactModel; |
24 | |
|
25 | |
import java.util.List; |
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
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 | |
|
44 | |
|
45 | |
|
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 | |
|
68 | |
|
69 | |
|
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 | |
|
88 | |
|
89 | |
|
90 | |
|
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 | |
} |