123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493 |
- /*
- * 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.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.annotation.CheckForNull;
- 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 java.io.UnsupportedEncodingException;
- import java.nio.charset.StandardCharsets;
-
- /**
- * This class is the Hibernate model to store a measure in the DB
- */
- @Entity
- @Table(name = "project_measures")
- public class MeasureModel implements Cloneable {
-
- public static final int TEXT_VALUE_LENGTH = 4000;
-
- @Id
- @Column(name = "id")
- @GeneratedValue
- private Long id;
-
- @Column(name = "value", updatable = true, nullable = true, precision = 30, scale = 20)
- private Double value = 0.0;
-
- @Column(name = "text_value", updatable = true, nullable = true, length = TEXT_VALUE_LENGTH)
- private String textValue;
-
- @Column(name = "metric_id", updatable = false, nullable = false)
- private Integer metricId;
-
- @Column(name = "snapshot_id", updatable = true, nullable = true)
- private Integer snapshotId;
-
- @Column(name = "project_id", updatable = true, nullable = true)
- private Integer projectId;
-
- @Column(name = "description", updatable = true, nullable = true, length = 4000)
- private String description;
-
- @Column(name = "rule_id", updatable = true, nullable = true)
- private Integer ruleId;
-
- @Column(name = "rule_priority", updatable = false, nullable = true)
- @Enumerated(EnumType.ORDINAL)
- private RulePriority rulePriority;
-
- @Column(name = "alert_status", updatable = true, nullable = true, length = 5)
- private String alertStatus;
-
- @Column(name = "alert_text", updatable = true, nullable = true, length = 4000)
- private String alertText;
-
- @Column(name = "variation_value_1", updatable = true, nullable = true)
- private Double variationValue1;
-
- @Column(name = "variation_value_2", updatable = true, nullable = true)
- private Double variationValue2;
-
- @Column(name = "variation_value_3", updatable = true, nullable = true)
- private Double variationValue3;
-
- @Column(name = "variation_value_4", updatable = true, nullable = true)
- private Double variationValue4;
-
- @Column(name = "variation_value_5", updatable = true, nullable = true)
- private Double variationValue5;
-
- @Column(name = "url", updatable = true, nullable = true, length = 2000)
- private String url;
-
- @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;
-
- /**
- * Creates a measure based on a metric and a double value
- */
- public MeasureModel(int metricId, Double val) {
- if (val.isNaN() || val.isInfinite()) {
- throw new IllegalArgumentException("Measure value is NaN. Metric=" + metricId);
- }
- this.metricId = metricId;
- this.value = val;
- }
-
- /**
- * Creates a measure based on a metric and an alert level
- */
- public MeasureModel(int metricId, Metric.Level level) {
- this.metricId = metricId;
- if (level != null) {
- this.textValue = level.toString();
- }
- }
-
- /**
- * Creates a measure based on a metric and a string value
- */
- public MeasureModel(int metricId, String val) {
- this.metricId = metricId;
- setData(val);
- }
-
- /**
- * Creates an empty measure
- */
- public MeasureModel() {
- }
-
- public Long getId() {
- return id;
- }
-
- public void setId(Long id) {
- this.id = id;
- }
-
- /**
- * @return the measure double value
- */
- public Double getValue() {
- return value;
- }
-
- /**
- * Sets the measure value
- *
- * @throws IllegalArgumentException in case value is not a valid double
- */
- public MeasureModel setValue(Double value) {
- if (value != null && (value.isNaN() || value.isInfinite())) {
- throw new IllegalArgumentException();
- }
- this.value = value;
- return this;
- }
-
- /**
- * @return the measure description
- */
- public String getDescription() {
- return description;
- }
-
- /**
- * Sets the measure description
- */
- public void setDescription(String description) {
- this.description = description;
- }
-
- /**
- * @return the measure alert level
- */
- public Metric.Level getLevelValue() {
- if (textValue != null) {
- return Metric.Level.valueOf(textValue);
- }
- return null;
- }
-
- /**
- * Use getData() instead
- */
- public String getTextValue() {
- return textValue;
- }
-
- /**
- * Use setData() instead
- */
- public void setTextValue(String textValue) {
- this.textValue = textValue;
- }
-
- /**
- * Concept of measure trend is dropped. This method always returns {@code null} since version 5.2.
- * @deprecated since 5.2. See https://jira.sonarsource.com/browse/SONAR-6392
- * @return null
- */
- @CheckForNull
- @Deprecated
- public Integer getTendency() {
- return null;
- }
-
- /**
- * Concept of measure trend is dropped. This method does nothing.
- * @deprecated since 5.2. See https://jira.sonarsource.com/browse/SONAR-6392
- */
- @Deprecated
- public MeasureModel setTendency(Integer tendency) {
- return this;
- }
-
- /**
- * @return whether the measure is about rule
- */
- public boolean isRuleMeasure() {
- return ruleId != null || rulePriority != null;
- }
-
- public Integer getMetricId() {
- return metricId;
- }
-
- public void setMetricId(Integer metricId) {
- this.metricId = metricId;
- }
-
- /**
- * @return the snapshot id the measure is attached to
- */
- public Integer getSnapshotId() {
- return snapshotId;
- }
-
- /**
- * Sets the snapshot id
- *
- * @return the current object
- */
- public MeasureModel setSnapshotId(Integer snapshotId) {
- this.snapshotId = snapshotId;
- return this;
- }
-
- public Integer getRuleId() {
- return ruleId;
- }
-
- /**
- * Sets the rule for the measure
- *
- * @return the current object
- */
- public MeasureModel setRuleId(Integer ruleId) {
- this.ruleId = ruleId;
- return this;
- }
-
- /**
- * @return the rule priority
- */
- public RulePriority getRulePriority() {
- return rulePriority;
- }
-
- /**
- * Sets the rule priority
- */
- public void setRulePriority(RulePriority rulePriority) {
- this.rulePriority = rulePriority;
- }
-
- /**
- * @return the project id
- */
- public Integer getProjectId() {
- return projectId;
- }
-
- /**
- * Sets the project id
- */
- public void setProjectId(Integer projectId) {
- this.projectId = projectId;
- }
-
- /**
- * @return the alert status if there is one, null otherwise
- */
- public Metric.Level getAlertStatus() {
- if (alertStatus == null) {
- return null;
- }
- return Metric.Level.valueOf(alertStatus);
- }
-
- /**
- * Sets the measure alert status
- *
- * @return the current object
- */
- public MeasureModel setAlertStatus(Metric.Level level) {
- if (level != null) {
- this.alertStatus = level.toString();
- } else {
- this.alertStatus = null;
- }
- return this;
- }
-
- /**
- * @return the measure data
- */
- public String getData(Metric metric) {
- if (this.textValue != null) {
- return this.textValue;
- }
- if (metric.isDataType() && data != null) {
- try {
- return new String(data, StandardCharsets.UTF_8.name());
- } catch (UnsupportedEncodingException e) {
- // how is it possible to not support UTF-8 ?
- Throwables.propagate(e);
- }
- }
- return null;
- }
-
- /**
- * Sets the measure data
- */
- public final void setData(String data) {
- if (data == null) {
- this.textValue = null;
- this.data = null;
-
- } else {
- if (data.length() > TEXT_VALUE_LENGTH) {
- this.textValue = null;
- this.data = data.getBytes(StandardCharsets.UTF_8);
- } else {
- this.textValue = data;
- this.data = null;
- }
- }
- }
-
- /**
- * @return the text of the alert
- */
- public String getAlertText() {
- return alertText;
- }
-
- /**
- * Sets the text for the alert
- */
- public void setAlertText(String alertText) {
- this.alertText = alertText;
- }
-
- /**
- * @return the measure URL
- */
- public String getUrl() {
- return url;
- }
-
- /**
- * Sets the measure URL
- */
- public void setUrl(String url) {
- this.url = url;
- }
-
- @Override
- public String toString() {
- return new ReflectionToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).toString();
- }
-
- public Double getVariationValue1() {
- return variationValue1;
- }
-
- public void setVariationValue1(Double d) {
- this.variationValue1 = d;
- }
-
- public Double getVariationValue2() {
- return variationValue2;
- }
-
- public void setVariationValue2(Double d) {
- this.variationValue2 = d;
- }
-
- public Double getVariationValue3() {
- return variationValue3;
- }
-
- public void setVariationValue3(Double d) {
- this.variationValue3 = d;
- }
-
- public Double getVariationValue4() {
- return variationValue4;
- }
-
- public void setVariationValue4(Double d) {
- this.variationValue4 = d;
- }
-
- public Double getVariationValue5() {
- return variationValue5;
- }
-
- public void setVariationValue5(Double d) {
- this.variationValue5 = d;
- }
-
- /**
- * Saves the current object to database
- *
- * @return the current object
- */
- public MeasureModel save(DatabaseSession session) {
- session.save(this);
- return this;
- }
-
- public Integer getCharacteristicId() {
- return characteristicId;
- }
-
- public MeasureModel setCharacteristicId(Integer characteristicId) {
- this.characteristicId = characteristicId;
- return this;
- }
-
- public Integer getPersonId() {
- return personId;
- }
-
- public MeasureModel setPersonId(Integer i) {
- this.personId = i;
- return this;
- }
-
- @Override
- public Object clone() {
- MeasureModel clone = new MeasureModel();
- clone.setMetricId(getMetricId());
- clone.setDescription(getDescription());
- clone.setTextValue(getTextValue());
- clone.setAlertStatus(getAlertStatus());
- clone.setAlertText(getAlertText());
- clone.setVariationValue1(getVariationValue1());
- clone.setVariationValue2(getVariationValue2());
- clone.setVariationValue3(getVariationValue3());
- clone.setVariationValue4(getVariationValue4());
- clone.setVariationValue5(getVariationValue5());
- clone.setValue(getValue());
- clone.setRulePriority(getRulePriority());
- clone.setRuleId(getRuleId());
- clone.setSnapshotId(getSnapshotId());
- clone.setUrl(getUrl());
- clone.setCharacteristicId(getCharacteristicId());
- clone.setPersonId(getPersonId());
- return clone;
- }
-
- }
|