001    /*
002     * SonarQube, open source software quality management tool.
003     * Copyright (C) 2008-2014 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * SonarQube is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 3 of the License, or (at your option) any later version.
010     *
011     * SonarQube is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public License
017     * along with this program; if not, write to the Free Software Foundation,
018     * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019     */
020    package org.sonar.api.database.model;
021    
022    import com.google.common.base.Charsets;
023    import com.google.common.base.Throwables;
024    import org.apache.commons.lang.builder.ToStringBuilder;
025    import org.apache.commons.lang.builder.ToStringStyle;
026    import org.sonar.api.database.BaseIdentifiable;
027    
028    import javax.persistence.*;
029    import java.io.UnsupportedEncodingException;
030    
031    @Entity
032    @Table(name = "measure_data")
033    public class MeasureData extends BaseIdentifiable {
034    
035      @OneToOne(fetch = FetchType.LAZY)
036      @JoinColumn(name = "measure_id")
037      private MeasureModel measure;
038    
039      @Column(name = "snapshot_id", updatable = true, nullable = true)
040      private Integer snapshotId;
041    
042      @Column(name = "data", updatable = true, nullable = true, length = 167772150)
043      private byte[] data;
044    
045      public MeasureData(MeasureModel measure) {
046        this.measure = measure;
047      }
048    
049      public MeasureData(MeasureModel measure, byte[] data) {
050        this.measure = measure;
051        this.data = data;
052      }
053    
054      public MeasureData(MeasureModel measure, String dataString) {
055        this.measure = measure;
056        this.data = dataString.getBytes();
057      }
058    
059      public MeasureData() {
060      }
061    
062      public MeasureModel getMeasure() {
063        return measure;
064      }
065    
066      public void setMeasure(MeasureModel measure) {
067        this.measure = measure;
068      }
069    
070      public byte[] getData() {
071        return data;
072      }
073    
074      public String getText() {
075        if (data != null) {
076          try {
077            return new String(data, Charsets.UTF_8.name());
078          } catch (UnsupportedEncodingException e) {
079            // how is it possible to not support UTF-8 ?
080            Throwables.propagate(e);
081          }
082        }
083        return null;
084      }
085    
086      public void setData(byte[] data) {
087        this.data = data;
088      }
089    
090      public Integer getSnapshotId() {
091        return snapshotId;
092      }
093    
094      public void setSnapshotId(Integer snapshotId) {
095        this.snapshotId = snapshotId;
096      }
097    
098      @Override
099      public String toString() {
100        return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
101          .append("snapshotId", snapshotId)
102          .append("mesasure", measure)
103          .append("data", data)
104          .toString();
105      }
106    }
107