From 5d63d85a14b4a5a6934bc178f95bcd2a894c7f63 Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Tue, 29 Apr 2014 10:45:35 +0200 Subject: SONAR-5249 Merge measure_data and project_measure tables --- .../org/sonar/api/database/model/MeasureData.java | 107 --------------------- .../sonar/api/database/model/MeasureMapper.java | 4 - .../org/sonar/api/database/model/MeasureModel.java | 72 ++++++-------- .../org/sonar/api/database/model/MeasureMapper.xml | 16 +-- .../sonar/api/database/model/MeasureDataTest.java | 39 -------- .../sonar/api/database/model/MeasureModelTest.java | 39 ++++++++ 6 files changed, 71 insertions(+), 206 deletions(-) delete mode 100644 sonar-plugin-api/src/main/java/org/sonar/api/database/model/MeasureData.java delete mode 100644 sonar-plugin-api/src/test/java/org/sonar/api/database/model/MeasureDataTest.java create mode 100644 sonar-plugin-api/src/test/java/org/sonar/api/database/model/MeasureModelTest.java (limited to 'sonar-plugin-api/src') diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/database/model/MeasureData.java b/sonar-plugin-api/src/main/java/org/sonar/api/database/model/MeasureData.java deleted file mode 100644 index b0275b64c39..00000000000 --- a/sonar-plugin-api/src/main/java/org/sonar/api/database/model/MeasureData.java +++ /dev/null @@ -1,107 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube 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. - * - * SonarQube 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 this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.api.database.model; - -import com.google.common.base.Charsets; -import com.google.common.base.Throwables; -import org.apache.commons.lang.builder.ToStringBuilder; -import org.apache.commons.lang.builder.ToStringStyle; -import org.sonar.api.database.BaseIdentifiable; - -import javax.persistence.*; -import java.io.UnsupportedEncodingException; - -@Entity -@Table(name = "measure_data") -public class MeasureData extends BaseIdentifiable { - - @OneToOne(fetch = FetchType.LAZY) - @JoinColumn(name = "measure_id") - private MeasureModel measure; - - @Column(name = "snapshot_id", updatable = true, nullable = true) - private Integer snapshotId; - - @Column(name = "data", updatable = true, nullable = true, length = 167772150) - private byte[] data; - - public MeasureData(MeasureModel measure) { - this.measure = measure; - } - - public MeasureData(MeasureModel measure, byte[] data) { - this.measure = measure; - this.data = data; - } - - public MeasureData(MeasureModel measure, String dataString) { - this.measure = measure; - this.data = dataString.getBytes(); - } - - public MeasureData() { - } - - public MeasureModel getMeasure() { - return measure; - } - - public void setMeasure(MeasureModel measure) { - this.measure = measure; - } - - public byte[] getData() { - return data; - } - - public String getText() { - if (data != null) { - try { - return new String(data, Charsets.UTF_8.name()); - } catch (UnsupportedEncodingException e) { - // how is it possible to not support UTF-8 ? - Throwables.propagate(e); - } - } - return null; - } - - public void setData(byte[] data) { - this.data = data; - } - - public Integer getSnapshotId() { - return snapshotId; - } - - public void setSnapshotId(Integer snapshotId) { - this.snapshotId = snapshotId; - } - - @Override - public String toString() { - return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE) - .append("snapshotId", snapshotId) - .append("mesasure", measure) - .append("data", data) - .toString(); - } -} - diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/database/model/MeasureMapper.java b/sonar-plugin-api/src/main/java/org/sonar/api/database/model/MeasureMapper.java index b55da95413e..42b7afc9b70 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/database/model/MeasureMapper.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/database/model/MeasureMapper.java @@ -22,9 +22,5 @@ package org.sonar.api.database.model; public interface MeasureMapper { void insert(MeasureModel measure); - void insertData(MeasureData data); - - void deleteData(MeasureModel data); - void update(MeasureModel measure); } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/database/model/MeasureModel.java b/sonar-plugin-api/src/main/java/org/sonar/api/database/model/MeasureModel.java index f2a00deccfb..95aa6b4f6bf 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/database/model/MeasureModel.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/database/model/MeasureModel.java @@ -19,17 +19,26 @@ */ package org.sonar.api.database.model; +import com.google.common.base.Charsets; +import com.google.common.base.Throwables; import org.apache.commons.lang.builder.ReflectionToStringBuilder; import org.apache.commons.lang.builder.ToStringStyle; import org.sonar.api.database.DatabaseSession; import org.sonar.api.measures.Metric; import org.sonar.api.rules.RulePriority; -import javax.persistence.*; - -import java.util.ArrayList; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.EnumType; +import javax.persistence.Enumerated; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.Temporal; +import javax.persistence.TemporalType; + +import java.io.UnsupportedEncodingException; import java.util.Date; -import java.util.List; /** * This class is the Hibernate model to store a measure in the DB @@ -78,7 +87,7 @@ public class MeasureModel implements Cloneable { */ @Deprecated @Column(name = "rules_category_id", nullable = true) - private Integer rulesCategoryId;//NOSONAR this field is kept for backward-compatiblity of API + private Integer rulesCategoryId;// NOSONAR this field is kept for backward-compatiblity of API @Column(name = "rule_priority", updatable = false, nullable = true) @Enumerated(EnumType.ORDINAL) @@ -108,15 +117,15 @@ public class MeasureModel implements Cloneable { @Column(name = "url", updatable = true, nullable = true, length = 2000) private String url; - @OneToMany(mappedBy = "measure", fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE}) - private List measureData = new ArrayList(); - @Column(name = "characteristic_id", nullable = true) private Integer characteristicId; @Column(name = "person_id", updatable = true, nullable = true) private Integer personId; + @Column(name = "measure_data", updatable = true, nullable = true, length = 167772150) + private byte[] data; + public Long getId() { return id; } @@ -357,8 +366,13 @@ public class MeasureModel implements Cloneable { if (this.textValue != null) { return this.textValue; } - if (metric.isDataType() && !measureData.isEmpty()) { - return measureData.get(0).getText(); + if (metric.isDataType() && data != null) { + try { + return new String(data, Charsets.UTF_8.name()); + } catch (UnsupportedEncodingException e) { + // how is it possible to not support UTF-8 ? + Throwables.propagate(e); + } } return null; } @@ -369,40 +383,19 @@ public class MeasureModel implements Cloneable { public final void setData(String data) { if (data == null) { this.textValue = null; - measureData.clear(); + this.data = null; } else { if (data.length() > TEXT_VALUE_LENGTH) { - measureData.clear(); - measureData.add(new MeasureData(this, data)); - + this.textValue = null; + this.data = data.getBytes(Charsets.UTF_8); } else { this.textValue = data; + this.data = null; } } } - /** - * Use getData() instead - */ - public MeasureData getMeasureData() { - if (!measureData.isEmpty()) { - return measureData.get(0); - } - return null; - } - - /** - * Use setData() instead - */ - //@Deprecated - public void setMeasureData(MeasureData data) { - measureData.clear(); - if (data != null) { - this.measureData.add(data); - } - } - /** * @return the text of the alert */ @@ -482,16 +475,7 @@ public class MeasureModel implements Cloneable { * @return the current object */ public MeasureModel save(DatabaseSession session) { - MeasureData data = getMeasureData(); - setMeasureData(null); session.save(this); - - if (data != null) { - data.setMeasure(session.getEntity(MeasureModel.class, getId())); - data.setSnapshotId(snapshotId); - session.save(data); - setMeasureData(data); - } return this; } diff --git a/sonar-plugin-api/src/main/resources/org/sonar/api/database/model/MeasureMapper.xml b/sonar-plugin-api/src/main/resources/org/sonar/api/database/model/MeasureMapper.xml index a3f83181d75..6cb43d9509b 100644 --- a/sonar-plugin-api/src/main/resources/org/sonar/api/database/model/MeasureMapper.xml +++ b/sonar-plugin-api/src/main/resources/org/sonar/api/database/model/MeasureMapper.xml @@ -7,24 +7,15 @@ INSERT INTO project_measures ( value, metric_id, snapshot_id, rule_id, text_value, tendency, measure_date, project_id, alert_status, alert_text, url, description, rule_priority, characteristic_id, variation_value_1, - variation_value_2, variation_value_3, variation_value_4, variation_value_5, person_id) + variation_value_2, variation_value_3, variation_value_4, variation_value_5, person_id, measure_data) VALUES ( #{value}, #{metricId}, #{snapshotId}, #{ruleId}, #{textValue}, #{tendency}, #{measureDate}, #{projectId}, #{alertStatus}, #{alertText}, #{url}, #{description}, #{rulePriority.ordinal}, #{characteristicId}, #{variationValue1}, - #{variationValue2}, #{variationValue3}, #{variationValue4}, #{variationValue5}, #{personId} + #{variationValue2}, #{variationValue3}, #{variationValue4}, #{variationValue5}, #{personId}, #{data} ) - - INSERT INTO measure_data (measure_id, snapshot_id, data) - VALUES (#{measure.id}, #{measure.snapshotId}, #{data}) - - - - DELETE FROM measure_data WHERE measure_id=#{id} AND snapshot_id=#{snapshotId} - - UPDATE project_measures SET @@ -44,7 +35,8 @@ variation_value_3 = #{variationValue3}, variation_value_4 = #{variationValue4}, variation_value_5 = #{variationValue5}, - person_id = #{personId} + person_id = #{personId}, + measure_data = #{data} WHERE id = #{id} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/database/model/MeasureDataTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/database/model/MeasureDataTest.java deleted file mode 100644 index 37f83197916..00000000000 --- a/sonar-plugin-api/src/test/java/org/sonar/api/database/model/MeasureDataTest.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube 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. - * - * SonarQube 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 this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.api.database.model; - -import java.io.UnsupportedEncodingException; - -import com.google.common.base.Charsets; -import org.junit.Test; - -import static org.fest.assertions.Assertions.assertThat; - -public class MeasureDataTest { - @Test - public void text_is_utf8() throws UnsupportedEncodingException { - String s = "accents éà and special characters ç€"; - - MeasureData data = new MeasureData(); - data.setData(s.getBytes(Charsets.UTF_8.name())); - - assertThat(data.getText()).isEqualTo(s); - } -} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/database/model/MeasureModelTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/database/model/MeasureModelTest.java new file mode 100644 index 00000000000..5205e9686b8 --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/database/model/MeasureModelTest.java @@ -0,0 +1,39 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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 this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.api.database.model; + +import org.junit.Test; +import org.sonar.api.measures.CoreMetrics; + +import java.io.UnsupportedEncodingException; + +import static org.fest.assertions.Assertions.assertThat; + +public class MeasureModelTest { + @Test + public void text_is_utf8() throws UnsupportedEncodingException { + String s = "accents éà and special characters ç€"; + + MeasureModel measure = new MeasureModel(); + measure.setData(s); + + assertThat(measure.getData(CoreMetrics.DUPLICATIONS_DATA)).isEqualTo(s); + } +} -- cgit v1.2.3