All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.api.database.model.ResourceModel Maven / Gradle / Ivy

There is a newer version: 5.1
Show newest version
/*
 * Sonar, open source software quality management tool.
 * Copyright (C) 2009 SonarSource SA
 * mailto:contact AT sonarsource DOT com
 *
 * Sonar is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * Sonar is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Sonar; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
 */
package org.sonar.api.database.model;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.hibernate.annotations.BatchSize;
import org.sonar.api.database.BaseIdentifiable;
import org.sonar.api.profiles.RulesProfile;
import org.sonar.api.resources.ProjectLink;
import org.sonar.api.resources.Resource;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Class to map resource with hibernate model
 */
@Entity
@Table(name = "projects")
public class ResourceModel extends BaseIdentifiable implements Cloneable {

  public static final String SCOPE_PROJECT = "PRJ";
  public static final String QUALIFIER_PROJECT_TRUNK = "TRK";

  public static final int DESCRIPTION_COLUMN_SIZE = 2000;
  public static final int NAME_COLUMN_SIZE = 256;
  public static final int KEY_SIZE = 400;

  @Column(name = "name", updatable = true, nullable = true, length = NAME_COLUMN_SIZE)
  private String name;

  @Column(name = "long_name", updatable = true, nullable = true, length = NAME_COLUMN_SIZE)
  private String longName;

  @Column(name = "description", updatable = true, nullable = true, length = DESCRIPTION_COLUMN_SIZE)
  private String description;

  @Column(name = "enabled", updatable = true, nullable = false)
  private Boolean enabled = Boolean.TRUE;

  @Column(name = "scope", updatable = true, nullable = false, length = 3)
  private String scope;

  @Column(name = "qualifier", updatable = true, nullable = false, length = 3)
  private String qualifier;

  @Column(name = "kee", updatable = false, nullable = false, length = KEY_SIZE)
  private String key;

  @Column(name = "language", updatable = true, nullable = true, length = 5)
  private String languageKey;

  @Column(name = "root_id", updatable = true, nullable = true)
  private Integer rootId;

  @Column(name = "copy_resource_id", updatable = true, nullable = true)
  private Integer copyResourceId;

  @OneToMany(mappedBy = "resource", fetch = FetchType.LAZY, cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE})
  @BatchSize(size = 8)
  private List projectLinks = new ArrayList();

  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "profile_id", updatable = true, nullable = true)
  private RulesProfile rulesProfile;

  /**
   * Default constructor
   */
  public ResourceModel() {
  }

  /**
   * 

Creates a resource model

* * @param scope the scope the rule will apply on * @param key the rule key. This is the name of the resource, including the path * @param qualifier the resource qualifier * @param rootId the rootId for the resource * @param name the short name of the resource */ public ResourceModel(String scope, String key, String qualifier, Integer rootId, String name) { this.scope = scope; this.key = key; this.rootId = rootId; this.name = name; this.qualifier = qualifier; } /** * Only available at project level. */ public List getProjectLinks() { return projectLinks; } public void setProjectLinks(List projectLinks) { this.projectLinks = projectLinks; } /** * @return a project link given its key if exists, null otherwise */ public ProjectLink getProjectLink(String key) { for (ProjectLink projectLink : projectLinks) { if (key.equals(projectLink.getKey())) { return projectLink; } } return null; } /** * Only available at project level. */ public String getDescription() { return description; } /** * Sets the resource description, truncated to DESCRIPTION_COLUMN_SIZE */ public void setDescription(String description) { this.description = StringUtils.abbreviate(description, DESCRIPTION_COLUMN_SIZE); } public String getName() { return name; } /** * Sets the resource name, truncated to NAME_COLUMN_SIZE */ public void setName(String name) { this.name = StringUtils.abbreviate(name, NAME_COLUMN_SIZE); if (this.longName==null) { this.longName = this.name; } } public String getLongName() { return longName; } /** * Sets the long name of the resource, truncated to NAME_COLUMN_SIZE */ public void setLongName(String s) { if (StringUtils.isBlank(s)) { this.longName = name; } else { this.longName = StringUtils.abbreviate(s, NAME_COLUMN_SIZE); } } public Boolean getEnabled() { return enabled; } public void setEnabled(Boolean enabled) { this.enabled = enabled; } public String getScope() { return scope; } public void setScope(String scope) { this.scope = scope; } public String getKey() { return key; } public String getLanguageKey() { return languageKey; } public void setLanguageKey(String lang) { this.languageKey = lang; } public Integer getCopyResourceId() { return copyResourceId; } public void setCopyResourceId(Integer copyResourceId) { this.copyResourceId = copyResourceId; } /** * @throws IllegalArgumentException if the key is longer than KEY_SIZE */ public void setKey(String key) { if (key.length() > KEY_SIZE) { throw new IllegalArgumentException("Resource key is too long, max is " + KEY_SIZE + " characters. Got : " + key); } this.key = key; } public Integer getRootId() { return rootId; } public void setRootId(Integer rootId) { this.rootId = rootId; } public RulesProfile getRulesProfile() { return rulesProfile; } public void setRulesProfile(RulesProfile rulesProfile) { this.rulesProfile = rulesProfile; } public String getQualifier() { return qualifier; } public void setQualifier(String qualifier) { this.qualifier = qualifier; } @Override public boolean equals(Object obj) { if (!(obj instanceof ResourceModel)) { return false; } if (this == obj) { return true; } ResourceModel other = (ResourceModel) obj; return new EqualsBuilder() .append(key, other.key) .append(enabled, other.enabled) .append(rootId, other.rootId) .isEquals(); } @Override public int hashCode() { return new HashCodeBuilder(17, 37) .append(key) .append(enabled) .append(rootId) .toHashCode(); } @Override public String toString() { return new ToStringBuilder(this) .append("id", getId()) .append("key", key) .append("scope", scope) .append("qualifier", qualifier) .append("name", name) .append("longName", longName) .append("lang", languageKey) .append("enabled", enabled) .append("rootId", rootId) .append("copyResourceId", copyResourceId) .toString(); } @Override public Object clone() { ResourceModel clone = new ResourceModel(getScope(), getKey(), getQualifier(), getRootId(), getName()); clone.setDescription(getDescription()); clone.setEnabled(getEnabled()); clone.setProjectLinks(getProjectLinks()); clone.setRulesProfile(getRulesProfile()); clone.setLanguageKey(getLanguageKey()); clone.setCopyResourceId(getCopyResourceId()); clone.setLongName(getLongName()); return clone; } /** * Maps a resource to a resource model and returns the resource */ public static ResourceModel build(Resource resource) { ResourceModel model = new ResourceModel(); model.setEnabled(Boolean.TRUE); model.setDescription(resource.getDescription()); model.setKey(resource.getKey()); if (resource.getLanguage() != null) { model.setLanguageKey(resource.getLanguage().getKey()); } if (StringUtils.isNotBlank(resource.getName())) { model.setName(resource.getName()); } else { model.setName(resource.getKey()); } model.setLongName(resource.getLongName()); model.setQualifier(resource.getQualifier()); model.setScope(resource.getScope()); return model; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy