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.resources;
021    
022    import org.apache.commons.lang.StringUtils;
023    import org.apache.commons.lang.builder.ReflectionToStringBuilder;
024    import org.apache.commons.lang.builder.ToStringStyle;
025    import org.sonar.api.database.BaseIdentifiable;
026    import org.sonar.api.database.model.ResourceModel;
027    
028    import javax.persistence.*;
029    
030    /**
031     * @since 1.10
032     */
033    @Entity(name = "ProjectLink")
034    @Table(name = "project_links")
035    public class ProjectLink extends BaseIdentifiable {
036    
037      public static final int NAME_COLUMN_SIZE = 128;
038      public static final int HREF_COLUMN_SIZE = 2048;
039    
040      @ManyToOne(fetch = FetchType.LAZY)
041      @JoinColumn(name = "project_id", updatable = false, nullable = false)
042      private ResourceModel resource;
043    
044      @Column(name = "link_type", updatable = true, nullable = true, length = 20)
045      private String key;
046    
047      @Column(name = "name", updatable = true, nullable = true, length = NAME_COLUMN_SIZE)
048      private String name;
049    
050      @Column(name = "href", updatable = true, nullable = false, length = HREF_COLUMN_SIZE)
051      private String href;
052    
053      public ProjectLink() {
054      }
055    
056      public ProjectLink(String key, String name, String href) {
057        this.key = key;
058        setName(name);
059        setHref(href);
060      }
061    
062      public ResourceModel getResource() {
063        return resource;
064      }
065    
066      public void setResource(ResourceModel resource) {
067        this.resource = resource;
068      }
069    
070      public String getName() {
071        return name;
072      }
073    
074      public final void setName(String name) {
075        this.name = StringUtils.abbreviate(name, NAME_COLUMN_SIZE);
076      }
077    
078      public String getHref() {
079        return href;
080      }
081    
082      public final void setHref(String href) {
083        if (href == null) {
084          throw new IllegalArgumentException("ProjectLink.href can not be null");
085        }
086        this.href = StringUtils.abbreviate(href, HREF_COLUMN_SIZE);
087      }
088    
089      public String getKey() {
090        return key;
091      }
092    
093      public void setKey(String key) {
094        this.key = key;
095      }
096    
097      @Override
098      public boolean equals(Object o) {
099        if (this == o) {
100          return true;
101        }
102        if (o == null || getClass() != o.getClass()) {
103          return false;
104        }
105    
106        ProjectLink that = (ProjectLink) o;
107        if (!key.equals(that.key)) {
108          return false;
109        }
110        return resource.equals(that.resource);
111    
112      }
113    
114      @Override
115      public int hashCode() {
116        int result = resource != null ? resource.hashCode() : 0;
117        result = 31 * result + key.hashCode();
118        return result;
119      }
120    
121      @Override
122      public String toString() {
123        return new ReflectionToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).toString();
124      }
125    
126      public void copyFieldsFrom(ProjectLink link) {
127        this.name = link.getName();
128        this.href = link.getHref();
129      }
130    }