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