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