Search in sources :

Example 46 with MolgenisDataException

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();
}
Also used : MolgenisDataException(org.molgenis.data.MolgenisDataException) ObjectListing(com.amazonaws.services.s3.model.ObjectListing) S3ObjectSummary(com.amazonaws.services.s3.model.S3ObjectSummary) TreeMap(java.util.TreeMap) Date(java.util.Date)

Example 47 with MolgenisDataException

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");
}
Also used : MolgenisDataException(org.molgenis.data.MolgenisDataException)

Example 48 with MolgenisDataException

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());
}
Also used : BackOffContext(org.springframework.retry.backoff.BackOffContext) Arrays(java.util.Arrays) ExponentialBackOffPolicy(org.springframework.retry.backoff.ExponentialBackOffPolicy) java.util.concurrent(java.util.concurrent) Client(org.elasticsearch.client.Client) Autowired(org.springframework.beans.factory.annotation.Autowired) Test(org.testng.annotations.Test) RetryPolicy(org.springframework.retry.RetryPolicy) List(java.util.List) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) RetryContext(org.springframework.retry.RetryContext) Assert(org.testng.Assert) RetryCallback(org.springframework.retry.RetryCallback) SleepingBackOffPolicy(org.springframework.retry.backoff.SleepingBackOffPolicy) ContextConfiguration(org.springframework.test.context.ContextConfiguration) RetryTemplate(org.springframework.retry.support.RetryTemplate) MolgenisDataException(org.molgenis.data.MolgenisDataException) AbstractTestNGSpringContextTests(org.springframework.test.context.testng.AbstractTestNGSpringContextTests) MolgenisDataException(org.molgenis.data.MolgenisDataException) Client(org.elasticsearch.client.Client) Test(org.testng.annotations.Test)

Example 49 with MolgenisDataException

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;
}
Also used : MolgenisDataException(org.molgenis.data.MolgenisDataException) TransportClient(org.elasticsearch.client.transport.TransportClient)

Example 50 with MolgenisDataException

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());
    }
}
Also used : IdGenerator(org.molgenis.data.populate.IdGenerator) ONE_TO_MANY(org.molgenis.data.meta.AttributeType.ONE_TO_MANY) StringUtils(org.apache.commons.lang3.StringUtils) Attribute(org.molgenis.data.meta.model.Attribute) FileStore(org.molgenis.data.file.FileStore) FileDownloadController(org.molgenis.core.ui.file.FileDownloadController) FileMetaFactory(org.molgenis.data.file.model.FileMetaFactory) Service(org.springframework.stereotype.Service) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) Collectors.toSet(java.util.stream.Collectors.toSet) POPULATE(org.molgenis.data.EntityManager.CreationMode.POPULATE) AttributeType(org.molgenis.data.meta.AttributeType) ServletUriComponentsBuilder(org.springframework.web.servlet.support.ServletUriComponentsBuilder) Collections.emptyList(java.util.Collections.emptyList) Set(java.util.Set) IOException(java.io.IOException) Instant(java.time.Instant) MolgenisDateFormat(org.molgenis.data.util.MolgenisDateFormat) EntityType(org.molgenis.data.meta.model.EntityType) FILENAME(org.molgenis.data.file.model.FileMetaMetaData.FILENAME) String.format(java.lang.String.format) UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) FileMeta(org.molgenis.data.file.model.FileMeta) FILE_META(org.molgenis.data.file.model.FileMetaMetaData.FILE_META) DateTimeParseException(java.time.format.DateTimeParseException) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Stream(java.util.stream.Stream) StreamSupport.stream(java.util.stream.StreamSupport.stream) LocalDate(java.time.LocalDate) DataService(org.molgenis.data.DataService) MultipartFile(org.springframework.web.multipart.MultipartFile) MolgenisDataException(org.molgenis.data.MolgenisDataException) EntityManager(org.molgenis.data.EntityManager) Entity(org.molgenis.data.Entity) UriComponents(org.springframework.web.util.UriComponents) Entity(org.molgenis.data.Entity) MolgenisDataException(org.molgenis.data.MolgenisDataException) Attribute(org.molgenis.data.meta.model.Attribute)

Aggregations

MolgenisDataException (org.molgenis.data.MolgenisDataException)51 Entity (org.molgenis.data.Entity)16 Attribute (org.molgenis.data.meta.model.Attribute)11 EntityType (org.molgenis.data.meta.model.EntityType)11 Test (org.testng.annotations.Test)7 IOException (java.io.IOException)6 List (java.util.List)6 DynamicEntity (org.molgenis.data.support.DynamicEntity)6 File (java.io.File)5 AttributeType (org.molgenis.data.meta.AttributeType)5 UnexpectedEnumException (org.molgenis.util.UnexpectedEnumException)5 Instant (java.time.Instant)4 LocalDate (java.time.LocalDate)4 Collectors.toList (java.util.stream.Collectors.toList)4 DataService (org.molgenis.data.DataService)4 RepositoryCollection (org.molgenis.data.RepositoryCollection)4 String.format (java.lang.String.format)3 DateTimeParseException (java.time.format.DateTimeParseException)3 Map (java.util.Map)3 Stream (java.util.stream.Stream)3