001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2012 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.security;
021    
022    import com.google.common.base.Objects;
023    import org.apache.commons.lang.builder.ReflectionToStringBuilder;
024    import org.apache.commons.lang.builder.ToStringStyle;
025    import org.sonar.api.database.BaseIdentifiable;
026    
027    import javax.persistence.Column;
028    import javax.persistence.Entity;
029    import javax.persistence.Table;
030    
031    /**
032     * @since 1.12
033     */
034    @Entity
035    @Table(name = "group_roles")
036    public class GroupRole extends BaseIdentifiable {
037    
038      public static final Integer ANYONE_GROUP_ID = null;
039    
040      @Column(name = "group_id")
041      private Integer groupId;
042    
043      @Column(name = "role")
044      private String role;
045    
046      @Column(name = "resource_id")
047      private Integer resourceId;
048    
049      public static GroupRole buildGlobalRole(Integer groupId, String role) {
050        return new GroupRole().setGroupId(groupId).setRole(role);
051      }
052    
053      public static GroupRole buildResourceRole(Integer groupId, String role, Integer resourceId) {
054        return new GroupRole().setGroupId(groupId).setRole(role).setResourceId(resourceId);
055      }
056    
057      public Integer getGroupId() {
058        return groupId;
059      }
060    
061      public GroupRole setGroupId(Integer groupId) {
062        this.groupId = groupId;
063        return this;
064      }
065    
066      public String getRole() {
067        return role;
068      }
069    
070      public GroupRole setRole(String role) {
071        this.role = role;
072        return this;
073      }
074    
075      public Integer getResourceId() {
076        return resourceId;
077      }
078    
079      public GroupRole setResourceId(Integer resourceId) {
080        this.resourceId = resourceId;
081        return this;
082      }
083    
084      public boolean isAnyone() {
085        return Objects.equal(groupId, ANYONE_GROUP_ID);
086      }
087    
088      @Override
089      public String toString() {
090        return new ReflectionToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).toString();
091      }
092    }