001 /* 002 * Sonar, open source software quality management tool. 003 * Copyright (C) 2009 SonarSource SA 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.database.model; 021 022 import org.apache.commons.lang.StringUtils; 023 import org.apache.commons.lang.builder.EqualsBuilder; 024 import org.apache.commons.lang.builder.HashCodeBuilder; 025 import org.apache.commons.lang.builder.ToStringBuilder; 026 import org.hibernate.annotations.BatchSize; 027 import org.sonar.api.database.BaseIdentifiable; 028 import org.sonar.api.profiles.RulesProfile; 029 import org.sonar.api.resources.ProjectLink; 030 import org.sonar.api.resources.Resource; 031 032 import java.util.ArrayList; 033 import java.util.List; 034 import javax.persistence.*; 035 036 @Entity 037 @Table(name = "projects") 038 public class ResourceModel extends BaseIdentifiable implements Cloneable { 039 040 public static final String SCOPE_PROJECT = "PRJ"; 041 public static final String QUALIFIER_PROJECT_TRUNK = "TRK"; 042 043 public static final int DESCRIPTION_COLUMN_SIZE = 2000; 044 public static final int NAME_COLUMN_SIZE = 256; 045 public static final int KEY_SIZE = 400; 046 047 @Column(name = "name", updatable = true, nullable = true, length = NAME_COLUMN_SIZE) 048 private String name; 049 050 @Column(name = "description", updatable = true, nullable = true, length = DESCRIPTION_COLUMN_SIZE) 051 private String description; 052 053 @Column(name = "enabled", updatable = true, nullable = false) 054 private Boolean enabled = Boolean.TRUE; 055 056 @Column(name = "scope", updatable = true, nullable = false, length = 3) 057 private String scope; 058 059 @Column(name = "qualifier", updatable = true, nullable = false, length = 3) 060 private String qualifier; 061 062 @Column(name = "kee", updatable = false, nullable = false, length = KEY_SIZE) 063 private String key; 064 065 @Column(name = "language", updatable = true, nullable = true, length = 5) 066 private String languageKey; 067 068 @Column(name = "root_id", updatable = true, nullable = true) 069 private Integer rootId; 070 071 @Column(name = "copy_resource_id", updatable = true, nullable = true) 072 private Integer copyResourceId; 073 074 @OneToMany(mappedBy = "resource", fetch = FetchType.LAZY, cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE}) 075 @BatchSize(size = 8) 076 private List<ProjectLink> projectLinks = new ArrayList<ProjectLink>(); 077 078 @ManyToOne(fetch = FetchType.LAZY) 079 @JoinColumn(name = "profile_id", updatable = true, nullable = true) 080 private RulesProfile rulesProfile; 081 082 public ResourceModel() { 083 } 084 085 public ResourceModel(String scope, String key, String qualifier, Integer rootId, String name) { 086 this.scope = scope; 087 this.key = key; 088 this.rootId = rootId; 089 this.name = name; 090 this.qualifier = qualifier; 091 } 092 093 public List<ProjectLink> getProjectLinks() { 094 return projectLinks; 095 } 096 097 public void setProjectLinks(List<ProjectLink> projectLinks) { 098 this.projectLinks = projectLinks; 099 } 100 101 public ProjectLink getProjectLink(String key) { 102 for (ProjectLink projectLink : projectLinks) { 103 if (key.equals(projectLink.getKey())) { 104 return projectLink; 105 } 106 } 107 return null; 108 } 109 110 111 public String getDescription() { 112 return description; 113 } 114 115 public void setDescription(String description) { 116 this.description = StringUtils.abbreviate(description, DESCRIPTION_COLUMN_SIZE); 117 } 118 119 public String getName() { 120 return name; 121 } 122 123 public void setName(String name) { 124 this.name = StringUtils.abbreviate(name, NAME_COLUMN_SIZE); 125 } 126 127 public Boolean getEnabled() { 128 return enabled; 129 } 130 131 public void setEnabled(Boolean enabled) { 132 this.enabled = enabled; 133 } 134 135 public String getScope() { 136 return scope; 137 } 138 139 public void setScope(String scope) { 140 this.scope = scope; 141 } 142 143 public String getKey() { 144 return key; 145 } 146 147 public String getLanguageKey() { 148 return languageKey; 149 } 150 151 public void setLanguageKey(String lang) { 152 this.languageKey = lang; 153 } 154 155 public Integer getCopyResourceId() { 156 return copyResourceId; 157 } 158 159 public void setCopyResourceId(Integer copyResourceId) { 160 this.copyResourceId = copyResourceId; 161 } 162 163 public void setKey(String key) { 164 if (key.length() > KEY_SIZE) { 165 throw new IllegalArgumentException("Resource key is too long, max is " + KEY_SIZE + " characters. Got : " + key); 166 } 167 this.key = key; 168 } 169 170 public Integer getRootId() { 171 return rootId; 172 } 173 174 public void setRootId(Integer rootId) { 175 this.rootId = rootId; 176 } 177 178 public RulesProfile getRulesProfile() { 179 return rulesProfile; 180 } 181 182 public void setRulesProfile(RulesProfile rulesProfile) { 183 this.rulesProfile = rulesProfile; 184 } 185 186 public String getQualifier() { 187 return qualifier; 188 } 189 190 public void setQualifier(String qualifier) { 191 this.qualifier = qualifier; 192 } 193 194 @Override 195 public boolean equals(Object obj) { 196 if (!(obj instanceof ResourceModel)) { 197 return false; 198 } 199 if (this == obj) { 200 return true; 201 } 202 ResourceModel other = (ResourceModel) obj; 203 return new EqualsBuilder() 204 .append(key, other.key) 205 .append(enabled, other.enabled) 206 .append(rootId, other.rootId) 207 .isEquals(); 208 } 209 210 @Override 211 public int hashCode() { 212 return new HashCodeBuilder(17, 37) 213 .append(key) 214 .append(enabled) 215 .append(rootId) 216 .toHashCode(); 217 } 218 219 @Override 220 public String toString() { 221 return new ToStringBuilder(this) 222 .append("id", getId()) 223 .append("key", key) 224 .append("scope", scope) 225 .append("qualifier", qualifier) 226 .append("name", name) 227 .append("lang", languageKey) 228 .append("enabled", enabled) 229 .append("rootId", rootId) 230 .append("copyResourceId", copyResourceId) 231 .toString(); 232 } 233 234 @Override 235 public Object clone() { 236 ResourceModel clone = new ResourceModel(getScope(), getKey(), getQualifier(), getRootId(), getName()); 237 clone.setDescription(getDescription()); 238 clone.setEnabled(getEnabled()); 239 clone.setProjectLinks(getProjectLinks()); 240 clone.setRulesProfile(getRulesProfile()); 241 clone.setLanguageKey(getLanguageKey()); 242 clone.setCopyResourceId(getCopyResourceId()); 243 return clone; 244 } 245 246 public static ResourceModel build(Resource resource) { 247 ResourceModel model = new ResourceModel(); 248 model.setEnabled(Boolean.TRUE); 249 model.setDescription(resource.getDescription()); 250 model.setKey(resource.getKey()); 251 if (resource.getLanguage() != null) { 252 model.setLanguageKey(resource.getLanguage().getKey()); 253 } 254 model.setName(resource.getName()); 255 model.setQualifier(resource.getQualifier()); 256 model.setScope(resource.getScope()); 257 return model; 258 } 259 260 }