package jnpf.limsService.provider;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
import jnpf.database.util.TenantDataSourceUtil;
|
import jnpf.exception.DataException;
|
import jnpf.lims.api.model.ElnSourceActorRequest;
|
import jnpf.lims.api.model.ElnSourceSnapshot;
|
import org.springframework.stereotype.Service;
|
|
import java.util.List;
|
import java.util.stream.Collectors;
|
|
/** Only exposed through the SameToken-protected internal endpoint. */
|
@Service
|
public class LimsElnSourceActorService {
|
private final LimsElnReadonlyResolver resolver;
|
private final ObjectMapper json = new ObjectMapper();
|
|
public LimsElnSourceActorService(LimsElnReadonlyResolver resolver) {
|
this.resolver = resolver;
|
}
|
|
public List<ElnSourceSnapshot> resolve(ElnSourceActorRequest request) {
|
if (request == null) throw new DataException("Source request is required");
|
LimsElnSourceCatalog.Actor actor = LimsElnSourceCatalog.requireActor(
|
new LimsElnSourceCatalog.Actor(request.getTenantId(), request.getUserId(), false));
|
if (request.getProperties() == null || request.getProperties().size() > 50
|
|| request.getProperties().stream().anyMatch(ref -> ref == null || ref.getRevision() == null)) {
|
throw new DataException("Invalid source references");
|
}
|
try {
|
TenantDataSourceUtil.switchTenant(actor.getTenantId());
|
return resolver.resolve(request.getTestWorkItemIds(), request.getProperties().stream()
|
.map(ref -> new LimsElnReadonlyResolver.PropertyRef(ref.getPropertyCode(), ref.getRevision()))
|
.collect(Collectors.toList()), actor).stream()
|
.map(value -> json.convertValue(value, ElnSourceSnapshot.class)).collect(Collectors.toList());
|
} finally {
|
TenantDataSourceUtil.clearLocalTenantInfo();
|
}
|
}
|
}
|