package com.iqser.red.service.persistence.management.v1.processor.acl;

import java.io.Serializable;

import org.springframework.security.acls.domain.ObjectIdentityImpl;
import org.springframework.security.acls.model.MutableAcl;
import org.springframework.security.acls.model.MutableAclService;
import org.springframework.security.acls.model.NotFoundException;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public abstract class AbstractACLService<ID extends Serializable> {

    protected final MutableAclService mutableAclService;


    public MutableAcl getOrCreateACL(Serializable id) {

        var objectIdentity = new ObjectIdentityImpl(getIdentifier(), id);
        try {
            return (MutableAcl) mutableAclService.readAclById(objectIdentity);
        } catch (NotFoundException e) {
            return mutableAclService.createAcl(objectIdentity);
        }
    }


    public abstract String getIdentifier();

}
