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

com.xlrit.gears.base.model.User Maven / Gradle / Ivy

There is a newer version: 1.17.5
Show newest version
package com.xlrit.gears.base.model;

import java.time.OffsetDateTime;
import java.util.*;
import java.util.stream.Collectors;

import com.xlrit.gears.base.function.DefaultFunctions;
import com.xlrit.gears.base.meta.*;
import com.xlrit.gears.base.util.DisplayedHelper;
import jakarta.persistence.*;
import jakarta.persistence.Id;
import lombok.Getter;
import lombok.Setter;

/**
 * 
 *     USER in USERS =
 *         username        : text // unique
 *         family_name     : text
 *         given_name      : text
 *         email_address   : text
 *         mobile_number   : text
 *         active          : boolean
 *         full_name       : text = '{given_name} {family_name}'
 *         ROLES           : multiple ROLE opposite of USERS
 * 
*/ @EntityType(typeName = "USER", collectionName = "USERS") @Attributes({ "username", "familyName", "givenName", "emailAddress", "mobileNumber", "active", "externalId", "fullName", "roles", "creator", "createdAt", "modifier", "modifiedAt", }) @Entity @Table(name = "SYS_USER") @org.hibernate.envers.Audited public class User implements Identifiable, Auditable { public static final Relation.ManyToMany ROLES = Relation.manyToMany(Role.class, User::getRoles, User::setRoles, Role::getUsers, Role::setUsers); @Getter @Id @Column(name = "id_", length = 26) private String id; @Getter @Setter @Attribute(formalName = "username", type = DataType.TEXT, label = "Username") @Basic @Column(name = "username_") private String username; @Getter @Setter @Trait("masked") @Basic @Column(name = "password_") private String password; @Getter @Setter @Attribute(formalName = "email_address", type = DataType.TEXT, label = "Email address") @Basic @Column(name = "email_") private String emailAddress; @Getter @Setter @Attribute(formalName = "family_name", type = DataType.TEXT, label = "Family name") @Basic @Column(name = "family_name_") private String familyName; @Getter @Setter @Attribute(formalName = "given_name", type = DataType.TEXT, label = "Given name") @Basic @Column(name = "given_name_") private String givenName; @Getter @Setter @Attribute(formalName = "mobile_number", type = DataType.TEXT, label = "Mobile number") @Basic @Column(name = "mobile_number_") private String mobileNumber; @Getter @Setter @Attribute(formalName = "active", type = DataType.BOOLEAN, label = "Active") @Basic @Column(name = "active_") private Boolean active; @Getter @Setter @Attribute(formalName = "external_id", type = DataType.TEXT, label = "External id") @Basic @Column(name = "external_id_") private String externalId; @Getter @Setter @Attribute(formalName = "ROLES", type = DataType.ENTITY, label = "Roles") @ManyToMany(fetch = FetchType.LAZY) @JoinTable(name = "SYS_ASSIGNMENT", joinColumns = @JoinColumn(name = "user_id_"), inverseJoinColumns = @JoinColumn(name = "role_id_")) private List roles; @Getter @Setter @Attribute(formalName = "CREATOR", type = DataType.ENTITY, label = "Creator") @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "creator_id_") private User creator; @Getter @Setter @Attribute(formalName = "created_at", type = DataType.DATETIME, label = "Created at") @Basic @Column(name = "created_at_") private OffsetDateTime createdAt; @Getter @Setter @Attribute(formalName = "MODIFIER", type = DataType.ENTITY, label = "Modifier") @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "modifier_id_") private User modifier; @Getter @Setter @Attribute(formalName = "modified_at", type = DataType.DATETIME, label = "Modified at") @Basic @Column(name = "modified_at_") private OffsetDateTime modifiedAt; public User() {} public User(String id) { this.id = id; this.roles = new ArrayList<>(); } @Displayed @Calculated @Attribute(formalName = "full_name", type = DataType.TEXT, label = "Full name") public String getFullName() { return DefaultFunctions.interpolate("{0} {1}", this.getGivenName(), this.getFamilyName()); } public void setRolesBidi(Collection roles) { ROLES.set(this, new ArrayList<>(roles)); } public void addRolesBidi(Collection extraRoles) { ROLES.add(this, extraRoles); } public String defaultDisplayed() { return new DisplayedHelper() .excludeNulls() .add("id", this.id) .add("username", this.username) .add("family name", this.familyName) .add("given name", this.givenName) .add("email address", this.emailAddress) .add("mobile number", this.mobileNumber) .add("active", this.active) .toString(); } @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof User that)) return false; return Objects.equals(id, that.getId()); } @Override public int hashCode() { return Objects.hash(id); } @Override public String toString() { return "USER[" + this.defaultDisplayed() + "]"; } public static Set getRoleNames(User user) { List roles = user.getRoles(); if (roles == null || roles.isEmpty()) return Collections.emptySet(); return roles.stream().map(Role::getName).collect(Collectors.toSet()); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy