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

dk.grinn.keycloak.migration.entities.RealmHistoryLock Maven / Gradle / Ivy

Go to download

Keycloak migrate extending the keycloak jpa model to include migrate versioning. And addtional REST end-points for migration purposes.

There is a newer version: 15.0.0.1
Show newest version
package dk.grinn.keycloak.migration.entities;

/*-
 * #%L
 * Keycloak : Migrate : Spi
 * %%
 * Copyright (C) 2019 Jonas Grann & Kim Jersin
 * %%
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 * #L%
 */
import java.io.Serializable;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.UUID;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

/**
 *
 * @author @author Kim Jersin <[email protected]>
 */
@Entity
@Table(name = "gkcadm_realm_history_lock")
@NamedQueries({
    @NamedQuery(name = "RealmHistoryLock.findAll",
            query = "SELECT rh FROM RealmHistoryLock rh"
    ),
    @NamedQuery(name = "RealmHistoryLock.findSessions",
            query = "SELECT new dk.grinn.keycloak.migration.entities.Session("
            + "     rh.realm, rh.session, rh.lockGranted, rh.lockedBy"
            + ") FROM RealmHistoryLock rh "
            + "where :onlyActive = false or rh.session is not null"
    ),
    @NamedQuery(name = "RealmHistoryLock.findByRealm",
            query = "SELECT rh "
            + "FROM RealmHistoryLock rh "
            + "where rh.realm = :realm"
    ),
    @NamedQuery(name = "RealmHistoryLock.findBySession",
            query = "SELECT rh "
            + "FROM RealmHistoryLock rh "
            + "where rh.session = :session"
    ),
    @NamedQuery(name = "RealmHistoryLock.releaseSession",
            query = "UPDATE RealmHistoryLock rh "
            + "SET rh.session = null, rh.lockGranted = null, rh.lockedBy = null "
            + "where rh.session = :session"
    ),
    @NamedQuery(name = "RealmHistoryLock.releaseSessionByRealm",
            query = "UPDATE RealmHistoryLock rh "
            + "SET rh.session = null, rh.lockGranted = null, rh.lockedBy = null "
            + "where rh.realm = :realm and rh.session is not null"
    )
})
public class RealmHistoryLock implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", insertable = false, updatable = false)
    private Integer id;

    @Basic(optional = false)
    @Column(name = "realm")
    private String realm;

    @Column(name = "session")
    private byte[] session;

    @Column(name = "lock_granted")
    @Temporal(TemporalType.TIMESTAMP)
    private Date lockGranted;

    @Column(name = "locked_by")
    private String lockedBy;

    @Basic(optional = false)
    @Column(name = "current_rank")
    private int currentRank;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "historyLock", fetch = FetchType.LAZY)
    @Column(name = "history")
    private Collection history;

    public RealmHistoryLock() {
    }

    public RealmHistoryLock(String realm, String lockedBy) {
        this.realm = realm;
        this.lockedBy = lockedBy;
        this.lockGranted = new Date();
        this.session = new byte[16];
        this.history = new ArrayList<>();

        UUID uuId = UUID.randomUUID();
        ByteBuffer bb = ByteBuffer.wrap(this.session);
        bb.putLong(uuId.getMostSignificantBits());
        bb.putLong(uuId.getLeastSignificantBits());
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getRealm() {
        return realm;
    }

    public void setRealm(String realm) {
        this.realm = realm;
    }

    public UUID getSession() {
        if (session != null) {
            ByteBuffer bb = ByteBuffer.wrap(session);
            return new UUID(bb.getLong(), bb.getLong());
        }
        return null;
    }

    public void setSession(String user) {
        setSession(UUID.randomUUID());
        setLockedBy(user);
        setLockGranted(new Date());
    }

    public void setSession(UUID session) {
        this.session = new byte[16];
        ByteBuffer bb = ByteBuffer.wrap(this.session);
        bb.putLong(session.getMostSignificantBits());
        bb.putLong(session.getLeastSignificantBits());
    }

    public Date getLockGranted() {
        return lockGranted;
    }

    public void setLockGranted(Date lockGranted) {
        this.lockGranted = lockGranted;
    }

    public String getLockedBy() {
        return lockedBy;
    }

    public void setLockedBy(String lockedBy) {
        this.lockedBy = lockedBy;
    }

    public int getCurrentRank() {
        return currentRank;
    }

    public void setCurrentRank(int currentRank) {
        this.currentRank = currentRank;
    }

    public int incrementAndGetCurrentRank() {
        return ++currentRank;
    }

    public Collection getHistory() {
        return history;
    }

    public void setHistory(Collection history) {
        this.history = history;
    }

    public Session toSession() {
        return new Session(realm, session, lockGranted, lockedBy);
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof RealmHistoryLock)) {
            return false;
        }
        RealmHistoryLock other = (RealmHistoryLock) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "dk.grinn.keycloak.migration.entities.RealmHistoryLock[ id=" + id + " ]";
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy