From 0dbee87afa49c3c5f75f045e1ac5bf1ec171e5b7 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Mon, 15 Aug 2011 17:13:50 +0200 Subject: [PATCH] SONAR-2412 add support of project deletion from the Java web service client --- .../main/webapp/WEB-INF/app/models/project.rb | 76 ++++++++++--------- .../main/java/org/sonar/wsclient/Sonar.java | 2 +- .../sonar/wsclient/connectors/Connector.java | 2 +- .../connectors/HttpClient3Connector.java | 4 +- .../connectors/HttpClient4Connector.java | 4 +- .../sonar/wsclient/services/DeleteQuery.java | 2 +- .../services/FavouriteDeleteQuery.java | 2 +- .../services/ManualMeasureDeleteQuery.java | 2 +- .../wsclient/services/ProjectDeleteQuery.java | 49 ++++++++++++ .../services/PropertyDeleteQuery.java | 2 +- .../services/UserPropertyDeleteQuery.java | 2 +- .../services/ProjectDeleteQueryTest.java | 36 +++++++++ 12 files changed, 135 insertions(+), 48 deletions(-) create mode 100644 sonar-ws-client/src/main/java/org/sonar/wsclient/services/ProjectDeleteQuery.java create mode 100644 sonar-ws-client/src/test/java/org/sonar/wsclient/services/ProjectDeleteQueryTest.java diff --git a/sonar-server/src/main/webapp/WEB-INF/app/models/project.rb b/sonar-server/src/main/webapp/WEB-INF/app/models/project.rb index 5fd8dde7209..01b358e17ce 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/models/project.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/models/project.rb @@ -20,7 +20,7 @@ class Project < ActiveRecord::Base include Comparable include Resourceable - + has_many :snapshots has_many :processed_snapshots, :class_name => 'Snapshot', :conditions => "status='#{Snapshot::STATUS_PROCESSED}' AND qualifier<>'LIB'", :order => 'created_at asc' has_many :events, :foreign_key => 'resource_id', :order => 'event_date DESC' @@ -41,36 +41,38 @@ class Project < ActiveRecord::Base end def self.delete_project(project) + if project Snapshot.update_all(['islast=?', false], ['(root_project_id=? OR project_id=?) AND islast=?', project.id, project.id, true]) Project.delete_all(['id=? OR root_id=? or copy_resource_id=?', project.id, project.id, project.id]) + end end - + def project root||self end - + def root_project - @root_project ||= - begin - parent_module(self) - end + @root_project ||= + begin + parent_module(self) + end end - + def last_snapshot @last_snapshot ||= - begin - snapshot=Snapshot.find(:first, :conditions => {:islast => true, :project_id => id}) - if snapshot - snapshot.project=self + begin + snapshot=Snapshot.find(:first, :conditions => {:islast => true, :project_id => id}) + if snapshot + snapshot.project=self + end + snapshot end - snapshot - end end - + def events_with_snapshot - events.select{|event| !event.snapshot_id.nil?} + events.select { |event| !event.snapshot_id.nil? } end - + def key kee end @@ -78,29 +80,29 @@ class Project < ActiveRecord::Base def links project_links end - + def link(type) # to_a avoids conflicts with ActiveRecord:Base.find - links.to_a.find{ |l| l.link_type==type} - end - + links.to_a.find { |l| l.link_type==type } + end + def custom_links - links.select {|l| l.custom?} + links.select { |l| l.custom? } end - + def standard_links - links.reject {|l| l.custom?} + links.reject { |l| l.custom? } end def chart_measures(metric_id) sql = Project.send(:sanitize_sql, ['select s.created_at as created_at, m.value as value ' + - ' from project_measures m, snapshots s ' + - ' where s.id=m.snapshot_id and ' + - " s.status='%s' and " + - ' s.project_id=%s and m.metric_id=%s ', Snapshot::STATUS_PROCESSED, self.id, metric_id] ) + - ' and m.rule_id IS NULL and m.rule_priority IS NULL' + - ' order by s.created_at' - create_chart_measures( Project.connection.select_all( sql ), 'created_at', 'value' ) + ' from project_measures m, snapshots s ' + + ' where s.id=m.snapshot_id and ' + + " s.status='%s' and " + + ' s.project_id=%s and m.metric_id=%s ', Snapshot::STATUS_PROCESSED, self.id, metric_id]) + + ' and m.rule_id IS NULL and m.rule_priority IS NULL' + + ' order by s.created_at' + create_chart_measures(Project.connection.select_all(sql), 'created_at', 'value') end def <=>(other) @@ -118,13 +120,13 @@ class Project < ActiveRecord::Base def fullname name end - + def branch if project? || module? s=kee.split(':') if s.size>=3 return s[2] - end + end end nil end @@ -156,19 +158,19 @@ class Project < ActiveRecord::Base # when regular active record impl return string typed objects if results.first[date_column_name].class == Time results.each do |hash| - chart_measures << ChartMeasure.new( hash[date_column_name], hash[value_column_name] ) + chart_measures << ChartMeasure.new(hash[date_column_name], hash[value_column_name]) end else results.each do |hash| - chart_measures << ChartMeasure.new( Time.parse( hash[date_column_name] ), hash[value_column_name].to_d ) + chart_measures << ChartMeasure.new(Time.parse(hash[date_column_name]), hash[value_column_name].to_d) end end end chart_measures end - + def parent_module(current_module) current_module.root ? parent_module(current_module.root) : current_module end - + end diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/Sonar.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/Sonar.java index e6a2dc855b6..c19254963c0 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/Sonar.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/Sonar.java @@ -93,7 +93,7 @@ public class Sonar { connector.execute(query); } - public void delete(DeleteQuery query) { + public void delete(DeleteQuery query) { connector.execute(query); } diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/connectors/Connector.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/connectors/Connector.java index cf874703859..711aa9726a8 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/connectors/Connector.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/connectors/Connector.java @@ -45,7 +45,7 @@ public abstract class Connector { * @return JSON response or null if 404 NOT FOUND error * @since 2.2 */ - public abstract String execute(DeleteQuery query); + public abstract String execute(DeleteQuery query); /** * @return JSON response or null if 404 NOT FOUND error diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/connectors/HttpClient3Connector.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/connectors/HttpClient3Connector.java index cb720320d7c..7632eedd000 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/connectors/HttpClient3Connector.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/connectors/HttpClient3Connector.java @@ -81,7 +81,7 @@ public class HttpClient3Connector extends Connector { return executeRequest(newPutRequest(query)); } - public String execute(DeleteQuery query) { + public String execute(DeleteQuery query) { return executeRequest(newDeleteRequest(query)); } @@ -117,7 +117,7 @@ public class HttpClient3Connector extends Connector { return method; } - private HttpMethodBase newDeleteRequest(DeleteQuery query) { + private HttpMethodBase newDeleteRequest(DeleteQuery query) { HttpMethodBase method = new DeleteMethod(server.getHost() + query.getUrl()); initRequest(method, query); return method; diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/connectors/HttpClient4Connector.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/connectors/HttpClient4Connector.java index e1aebfd175e..cd1a31cb969 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/connectors/HttpClient4Connector.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/connectors/HttpClient4Connector.java @@ -82,7 +82,7 @@ public class HttpClient4Connector extends Connector { return executeRequest(newPutMethod(query)); } - public String execute(DeleteQuery query) { + public String execute(DeleteQuery query) { return executeRequest(newDeleteMethod(query)); } @@ -146,7 +146,7 @@ public class HttpClient4Connector extends Connector { return get; } - private HttpDelete newDeleteMethod(DeleteQuery query) { + private HttpDelete newDeleteMethod(DeleteQuery query) { HttpDelete delete = new HttpDelete(server.getHost() + query.getUrl()); initRequest(delete, query); return delete; diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/services/DeleteQuery.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/DeleteQuery.java index 5a3df918436..91249db4ae6 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/services/DeleteQuery.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/DeleteQuery.java @@ -24,6 +24,6 @@ package org.sonar.wsclient.services; * * @since 2.2 */ -public abstract class DeleteQuery extends AbstractQuery { +public abstract class DeleteQuery extends AbstractQuery { } diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/services/FavouriteDeleteQuery.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/FavouriteDeleteQuery.java index 40ac6d21e79..99065b5e3f4 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/services/FavouriteDeleteQuery.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/FavouriteDeleteQuery.java @@ -19,7 +19,7 @@ */ package org.sonar.wsclient.services; -public class FavouriteDeleteQuery extends DeleteQuery { +public class FavouriteDeleteQuery extends DeleteQuery { private String idOrKey; diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/services/ManualMeasureDeleteQuery.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/ManualMeasureDeleteQuery.java index 74b3e591136..eb7e50349b2 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/services/ManualMeasureDeleteQuery.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/ManualMeasureDeleteQuery.java @@ -22,7 +22,7 @@ package org.sonar.wsclient.services; /** * @since 2.10 */ -public final class ManualMeasureDeleteQuery extends DeleteQuery { +public final class ManualMeasureDeleteQuery extends DeleteQuery { private String resourceKey; private String metricKey; diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/services/ProjectDeleteQuery.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/ProjectDeleteQuery.java new file mode 100644 index 00000000000..47c1127aa9e --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/ProjectDeleteQuery.java @@ -0,0 +1,49 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2011 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.wsclient.services; + +/** + * @since 2.11 + */ +public final class ProjectDeleteQuery extends DeleteQuery { + + private String key; + private static final String BASE_URL = "/api/projects/"; + + private ProjectDeleteQuery(String key) { + this.key = key; + } + + public String getKey() { + return key; + } + + @Override + public String getUrl() { + StringBuilder url = new StringBuilder(); + url.append(BASE_URL); + url.append(encode(key)); + return url.toString(); + } + + public static ProjectDeleteQuery create(String projectKeyOrId) { + return new ProjectDeleteQuery(projectKeyOrId); + } +} diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/services/PropertyDeleteQuery.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/PropertyDeleteQuery.java index 8e6b7b119dd..1775bcf2360 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/services/PropertyDeleteQuery.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/PropertyDeleteQuery.java @@ -22,7 +22,7 @@ package org.sonar.wsclient.services; /** * @since 2.6 */ -public class PropertyDeleteQuery extends DeleteQuery { +public class PropertyDeleteQuery extends DeleteQuery { private String key; private String resourceKeyOrId; diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/services/UserPropertyDeleteQuery.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/UserPropertyDeleteQuery.java index 81f1e182134..cebcf9f1a8b 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/services/UserPropertyDeleteQuery.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/UserPropertyDeleteQuery.java @@ -22,7 +22,7 @@ package org.sonar.wsclient.services; /** * @since 2.2 */ -public class UserPropertyDeleteQuery extends DeleteQuery { +public class UserPropertyDeleteQuery extends DeleteQuery { private String key; diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/services/ProjectDeleteQueryTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/services/ProjectDeleteQueryTest.java new file mode 100644 index 00000000000..8957381dd5b --- /dev/null +++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/services/ProjectDeleteQueryTest.java @@ -0,0 +1,36 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2011 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.wsclient.services; + +import org.junit.Test; +import org.sonar.wsclient.Sonar; + +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertThat; + +public class ProjectDeleteQueryTest extends QueryTestCase { + + @Test + public void testUrl() { + ProjectDeleteQuery query = ProjectDeleteQuery.create("my:project"); + assertThat(query.getUrl(), is("/api/projects/my%3Aproject")); + } + +} \ No newline at end of file -- 2.39.5