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

com.github.nagyesta.lowkeyvault.service.common.impl.KeyVaultBaseEntity Maven / Gradle / Ivy

package com.github.nagyesta.lowkeyvault.service.common.impl;

import com.github.nagyesta.lowkeyvault.model.v7_2.common.constants.RecoveryLevel;
import com.github.nagyesta.lowkeyvault.service.EntityId;
import com.github.nagyesta.lowkeyvault.service.common.BaseVaultEntity;
import com.github.nagyesta.lowkeyvault.service.vault.VaultFake;
import lombok.NonNull;

import java.time.OffsetDateTime;
import java.util.Collections;
import java.util.Map;
import java.util.Optional;

@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
public abstract class KeyVaultBaseEntity extends KeyVaultLifecycleAwareEntity implements BaseVaultEntity {
    private final RecoveryLevel recoveryLevel;
    private final Integer recoverableDays;
    private Map tags;
    private Optional deletedDate;
    private Optional scheduledPurgeDate;
    private boolean managed;

    protected KeyVaultBaseEntity(@NonNull final VaultFake vault) {
        super();
        this.recoveryLevel = vault.getRecoveryLevel();
        this.recoverableDays = vault.getRecoverableDays();
        this.tags = Collections.emptyMap();
        this.deletedDate = Optional.empty();
        this.scheduledPurgeDate = Optional.empty();
    }

    @Override
    public RecoveryLevel getRecoveryLevel() {
        return recoveryLevel;
    }

    @Override
    public Integer getRecoverableDays() {
        return recoverableDays;
    }

    @Override
    public Map getTags() {
        return tags;
    }

    @Override
    public void setTags(final Map tags) {
        updatedNow();
        this.tags = Map.copyOf(tags);
    }

    @Override
    public Optional getDeletedDate() {
        return deletedDate;
    }

    @Override
    public void setDeletedDate(final OffsetDateTime deletedDate) {
        this.deletedDate = Optional.ofNullable(deletedDate);
    }

    @Override
    public Optional getScheduledPurgeDate() {
        return scheduledPurgeDate;
    }

    @Override
    public void setScheduledPurgeDate(final OffsetDateTime scheduledPurgeDate) {
        this.scheduledPurgeDate = Optional.ofNullable(scheduledPurgeDate);
    }

    @Override
    public boolean isPurgeExpired() {
        return getScheduledPurgeDate()
                .filter(date -> date.isBefore(now()))
                .isPresent();
    }

    @Override
    public boolean canPurge() {
        return getScheduledPurgeDate().isPresent() && getRecoveryLevel().isPurgeable();
    }

    @Override
    public void timeShift(final int offsetSeconds) {
        super.timeShift(offsetSeconds);
        deletedDate = deletedDate.map(offsetDateTime -> offsetDateTime.minusSeconds(offsetSeconds));
        scheduledPurgeDate = scheduledPurgeDate.map(offsetDateTime -> offsetDateTime.minusSeconds(offsetSeconds));
    }

    @Override
    public boolean isManaged() {
        return managed;
    }

    @Override
    public void setManaged(final boolean managed) {
        this.managed = managed;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy