use of org.molgenis.data.MolgenisDataException in project molgenis by molgenis.
the class AmazonBucketClientImpl method getMostRecentMatchingKey.
// in case of an key expression all matching keys are collected and the most recent file is downloaded.
private String getMostRecentMatchingKey(AmazonS3 s3Client, String bucketName, String regex) {
ObjectListing objectListing = s3Client.listObjects(bucketName);
TreeMap<Date, String> keys = new TreeMap<>();
for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
if (objectSummary.getKey().matches(regex)) {
keys.put(objectSummary.getLastModified(), objectSummary.getKey());
}
}
if (keys.size() == 0)
throw new MolgenisDataException("No key matching regular expression: " + regex);
return keys.lastEntry().getValue();
}
use of org.molgenis.data.MolgenisDataException in project molgenis by molgenis.
the class CsvWriter method add.
@Override
public void add(Entity entity) {
if (cachedAttributeNames == null)
throw new MolgenisDataException("No attribute names defined call writeAttributeNames first");
int i = 0;
String[] values = new String[cachedAttributeNames.size()];
for (String colName : cachedAttributeNames) {
values[i++] = toValue(entity.get(colName));
}
csvWriter.writeNext(values);
if (csvWriter.checkError())
throw new MolgenisDataException("An exception occured writing the csv file");
}
use of org.molgenis.data.MolgenisDataException in project molgenis by molgenis.
the class ConnectionRetryConfigTest method testInterruptFailingTries.
@Test
public void testInterruptFailingTries() throws Exception {
Future<Client> result = executorService.submit(() -> {
RetryCallback<Client, RuntimeException> fail = c -> {
throw new MolgenisDataException();
};
return retryTemplate.execute(fail);
});
result.cancel(true);
try {
result.get(100, TimeUnit.MILLISECONDS);
fail("Should throw cancellation exception!");
} catch (CancellationException ignore) {
}
assertTrue(result.isDone());
assertTrue(result.isCancelled());
}
use of org.molgenis.data.MolgenisDataException in project molgenis by molgenis.
the class ClientFactory method tryCreateClient.
private Client tryCreateClient(RetryContext retryContext) throws InterruptedException {
if (Thread.interrupted()) {
throw new InterruptedException();
}
TransportClient result = preBuiltTransportClientFactory.build(clusterName, null).addTransportAddresses(createInetTransportAddresses());
if (result.connectedNodes().isEmpty()) {
result.close();
LOG.error("Failed to connect to Elasticsearch cluster '{}' on {}. Retry count = {}", clusterName, inetAddresses, retryContext.getRetryCount());
throw new MolgenisDataException(String.format("Failed to connect to Elasticsearch cluster '%s' on %s. Is Elasticsearch running?", clusterName, inetAddresses));
}
return result;
}
use of org.molgenis.data.MolgenisDataException in project molgenis by molgenis.
the class RestService method updateMappedByEntitiesOneToMany.
/**
* For entities with the given attribute that is part of a bidirectional one-to-many relationship update the other side of the relationship.
*
* @param entity created or updated entity
* @param existingEntity existing entity
* @param attr bidirectional one-to-many attribute
*/
private void updateMappedByEntitiesOneToMany(@Nonnull Entity entity, @Nullable Entity existingEntity, @Nonnull Attribute attr) {
if (attr.getDataType() != ONE_TO_MANY || !attr.isMappedBy()) {
throw new IllegalArgumentException(format("Attribute [%s] is not of type [%s] or not mapped by another attribute", attr.getName(), attr.getDataType().toString()));
}
// update ref entities of created/updated entity
Attribute refAttr = attr.getMappedBy();
Stream<Entity> stream = stream(entity.getEntities(attr.getName()).spliterator(), false);
if (existingEntity != null) {
// filter out unchanged ref entities
Set<Object> refEntityIds = stream(existingEntity.getEntities(attr.getName()).spliterator(), false).map(Entity::getIdValue).collect(toSet());
stream = stream.filter(refEntity -> !refEntityIds.contains(refEntity.getIdValue()));
}
List<Entity> updatedRefEntities = stream.map(refEntity -> {
if (refEntity.getEntity(refAttr.getName()) != null) {
throw new MolgenisDataException(format("Updating [%s] with id [%s] not allowed: [%s] is already referred to by another [%s]", attr.getRefEntity().getId(), refEntity.getIdValue().toString(), refAttr.getName(), entity.getEntityType().getId()));
}
refEntity.set(refAttr.getName(), entity);
return refEntity;
}).collect(toList());
// update ref entities of existing entity
if (existingEntity != null) {
Set<Object> refEntityIds = stream(entity.getEntities(attr.getName()).spliterator(), false).map(Entity::getIdValue).collect(toSet());
List<Entity> updatedRefEntitiesExistingEntity = stream(existingEntity.getEntities(attr.getName()).spliterator(), false).filter(refEntity -> !refEntityIds.contains(refEntity.getIdValue())).map(refEntity -> {
refEntity.set(refAttr.getName(), null);
return refEntity;
}).collect(toList());
updatedRefEntities = Stream.concat(updatedRefEntities.stream(), updatedRefEntitiesExistingEntity.stream()).collect(toList());
}
if (!updatedRefEntities.isEmpty()) {
dataService.update(attr.getRefEntity().getId(), updatedRefEntities.stream());
}
}
Aggregations