com.github.nagyesta.lowkeyvault.mapper.common.BaseRecoveryAwareConverter Maven / Gradle / Ivy
package com.github.nagyesta.lowkeyvault.mapper.common;
import com.github.nagyesta.lowkeyvault.model.common.DeletedModel;
import com.github.nagyesta.lowkeyvault.service.EntityId;
import com.github.nagyesta.lowkeyvault.service.key.ReadOnlyDeletedEntity;
import org.springframework.util.Assert;
import java.net.URI;
import java.util.function.Supplier;
/**
* Base converter supporting both active and deleted entities.
*
* @param The entityId type.
* @param The source type.
* @param The active target type.
* @param The deleted target type.
*/
public abstract class BaseRecoveryAwareConverter, T, DT extends T>
implements RecoveryAwareConverter {
private final Supplier modelSupplier;
private final Supplier deletedModelSupplier;
protected BaseRecoveryAwareConverter(final Supplier modelSupplier, final Supplier deletedModelSupplier) {
this.modelSupplier = modelSupplier;
this.deletedModelSupplier = deletedModelSupplier;
Assert.isInstanceOf(DeletedModel.class, deletedModelSupplier.get());
}
protected void mapDeletedFields(final S source, final DeletedModel model, final URI vaultUri) {
model.setRecoveryId(source.getId().asRecoveryUri(vaultUri).toString());
model.setDeletedDate(source.getDeletedDate().orElseThrow());
model.setScheduledPurgeDate(source.getScheduledPurgeDate().orElseThrow());
}
@Override
@org.springframework.lang.NonNull
public T convert(@org.springframework.lang.NonNull final S source, @org.springframework.lang.NonNull final URI vaultUri) {
return mapActiveFields(source, modelSupplier.get(), vaultUri);
}
@Override
@org.springframework.lang.NonNull
public DT convertDeleted(@org.springframework.lang.NonNull final S source, @org.springframework.lang.NonNull final URI vaultUri) {
final DT model = deletedModelSupplier.get();
mapDeletedFields(source, (DeletedModel) model, vaultUri);
return mapActiveFields(source, model, vaultUri);
}
protected abstract M mapActiveFields(S source, M model, URI vaultUri);
}