org.italiangrid.voms.persistence.model.Mapping.java Source code

Java tutorial

Introduction

Here is the source code for org.italiangrid.voms.persistence.model.Mapping.java

Source

/**
 * Copyright (c) Members of the EGEE Collaboration. 2006-2009.
 * See http://www.eu-egee.org/partners/ for details on the copyright holders.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Authors:
 *    Andrea Ceccanti (INFN)
 */
package org.italiangrid.voms.persistence.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import org.hibernate.annotations.ForeignKey;
import org.hibernate.annotations.NaturalId;
import org.springframework.data.domain.Persistable;

@Entity
@Table(name = "m")
public class Mapping implements Persistable<Long>, Comparable<Mapping> {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "VOMS_M_SEQ")
    @SequenceGenerator(name = "VOMS_M_SEQ", sequenceName = "VOMS_M_SEQ")
    private Long id;

    @NaturalId
    @ManyToOne
    @JoinColumn(name = "userid", nullable = false)
    @ForeignKey(name = "fk_m_usr")
    private User user;

    @NaturalId
    @ManyToOne
    @JoinColumn(name = "gid", nullable = false)
    @ForeignKey(name = "fk_m_groups")
    private Group group;

    @NaturalId
    @ManyToOne
    @JoinColumn(name = "rid", nullable = true)
    @ForeignKey(name = "fk_m_roles")
    private Role role;

    public Mapping() {

    }

    public Mapping(User u, Group g, Role r) {

        user = u;
        group = g;
        role = r;
    }

    public Mapping(User u, Group g) {
        this(u, g, null);
    }

    public Group getGroup() {

        return group;
    }

    public void setGroup(Group group) {

        this.group = group;
    }

    public Role getRole() {

        return role;
    }

    public void setRole(Role role) {

        this.role = role;
    }

    public User getUser() {

        return user;
    }

    public void setUser(User user) {

        this.user = user;
    }

    public Long getId() {

        return id;
    }

    public void setId(Long id) {

        this.id = id;
    }

    public boolean equals(Object other) {

        if (this == other)
            return true;

        if (!(other instanceof Mapping))
            return false;

        Mapping that = (Mapping) other;

        // User is different
        if (!getUser().equals(that.getUser()))
            return false;

        // Both group mappings
        if (this.isGroupMapping() && that.isGroupMapping())
            return getGroup().equals(that.getGroup());

        // Both role mappings
        if (this.isRoleMapping() && that.isRoleMapping())
            return (getGroup().equals(that.getGroup()) && getRole().equals(that.getRole()));

        // one group, one role, mappings cannot be equal
        return false;

    }

    public boolean isGroupMapping() {

        if (getUser() == null)
            throw new IllegalStateException("This mapping has not been initialized (user== null)!");

        if (getGroup() == null)
            throw new IllegalStateException("This mapping has not been initialized (group == null)!");

        return (getRole() == null);
    }

    public boolean isRoleMapping() {

        if (getUser() == null)
            throw new IllegalStateException("This mapping has not been initialized (user== null)!");

        if (getGroup() == null)
            throw new IllegalStateException("This mapping has not been initialized (group == null)!");

        return (getRole() != null);
    }

    public int hashCode() {

        int result = 14;

        result = 29 * result + getUser().hashCode();

        result = 29 * result + getGroup().hashCode();

        if (getRole() != null)
            result = 29 * result + getRole().hashCode();

        return result;
    }

    public String toString() {

        StringBuffer buf = new StringBuffer();

        if (getUser() != null)
            buf.append(getUser() + ",");

        buf.append(getFQAN());

        return buf.toString();
    }

    public String getFQAN() {

        StringBuffer buf = new StringBuffer();

        if (getGroup() != null)
            buf.append(getGroup());

        if (getRole() != null)
            buf.append("/" + getRole());

        return buf.toString();

    }

    public int compareTo(Mapping that) {

        if (this.equals(that))
            return 0;

        if (!getUser().equals(that.getUser()))
            return getUser().compareTo(that.getUser());

        if (isGroupMapping() && that.isGroupMapping())
            return getGroup().compareTo(that.getGroup());

        if (isRoleMapping() && that.isRoleMapping()) {
            int groupResult = getGroup().compareTo(that.getGroup());

            if (groupResult == 0)
                return getRole().compareTo(that.getRole());
            else
                return groupResult;
        }

        // One role, one group, will sort against groups.
        if (this.isGroupMapping() && that.isRoleMapping()) {

            int groupResult = getGroup().compareTo(that.getGroup());
            // Same group, group mappings come first.
            if (groupResult == 0)
                return -1;
            else
                return groupResult;
        }

        if (this.isRoleMapping() && that.isGroupMapping()) {
            int groupResult = getGroup().compareTo(that.getGroup());
            if (groupResult == 0)
                return 1;
            else
                return groupResult;
        }

        throw new IllegalStateException("BUG in Mappings compare logic!");
    }

    @Override
    public boolean isNew() {
        return getId() == null;
    }

}