SysGroup.java

/*
 * Copyright 2019 Global Crop Diversity Trust
 *
 * 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.
 */
package org.gringlobal.model;

import javax.persistence.*;

import java.time.Instant;
import java.util.Objects;

import lombok.Getter;
import lombok.Setter;
import org.genesys.blocks.security.model.AclSid;
import org.gringlobal.compatibility.LookupDisplay;
import org.gringlobal.custom.validation.javax.OneLine;
import org.springframework.security.core.GrantedAuthority;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;

/**
 * Represents a user group in GRIN-Global. The group is an ACL SID.
 *
 * @author org.apache.openjpa.jdbc.meta.ReverseMappingTool$AnnotatedCodeGenerator
 * @author mobreza
 */
@Entity
@Cacheable
@Table(name = "sys_group", uniqueConstraints = {
	@UniqueConstraint(columnNames = { "group_tag" })
})
@DiscriminatorValue(value = "5")
@JsonIdentityInfo(scope = SysGroup.class, generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
@Getter
@Setter
public class SysGroup extends AclSid implements GrantedAuthority, LazyLoading<SysGroup> {

	private static final long serialVersionUID = 8554894815800580219L;

	public static final String ADMIN_AUTHORITY = "GROUP_ADMINS";
	public static final String ADMIN_GROUPTAG = "ADMINS";

	@Basic
	@Column(name = "group_tag", nullable = false, length = 1000)
	@JsonProperty(access = JsonProperty.Access.READ_ONLY)
	@OneLine
	@LookupDisplay
	private String groupTag;

	@Basic
	@Column(name = "is_enabled", nullable = false, length = 1)
	private String isEnabled;

	public SysGroup() {
		super.setPrincipal(false);
	}

	/**
	 * Ensure authority SID.
	 */
	@PrePersist
	@PreUpdate
	protected void prePersist() {
		super.setSid(this.getAuthority());
	}

	public SysGroup(final Long id) {
		super.setId(id);
	}

	public SysGroup(final String groupTag) {
		this.groupTag = groupTag;
	}

	public void setIsEnabled(final String isEnabled) {
		this.isEnabled = isEnabled;
		super.setActive(Objects.equals(isEnabled, "Y"));
	}

	@Transient
	public boolean isEnabled() {
		return this.isEnabled.equals("Y");
	}

	@Override
	@Transient
	public String getAuthority() {
		return "GROUP_" + this.groupTag;
	}

	@Override
	public String toString() {
		return "SysGroup " + groupTag;
	}

	// Keeping JSON compatibility
	public Instant getModifiedDate() {
		return super.getLastModifiedDate();
	}
}