Search in sources :

Example 26 with BiPredicate

use of java.util.function.BiPredicate in project ArachneCentralAPI by OHDSI.

the class AnalysisFilesSavingServiceImpl method saveFiles.

@PreAuthorize("hasPermission(#analysis, " + "T(com.odysseusinc.arachne.portal.security.ArachnePermission).UPLOAD_ANALYSIS_FILES)")
protected List<AnalysisFile> saveFiles(List<MultipartFile> multipartFiles, IUser user, A analysis, DataReference dataReference, BiPredicate<String, CommonAnalysisType> checkFileExecutabilityPredicate) {
    List<MultipartFile> filteredFiles = multipartFiles.stream().filter(file -> !(CommonAnalysisType.COHORT.equals(analysis.getType()) && file.getName().endsWith(OHDSI_JSON_EXT))).filter(file -> !file.getName().startsWith(ANALYSIS_INFO_FILE_DESCRIPTION)).collect(Collectors.toList());
    List<AnalysisFile> savedFiles = new ArrayList<>();
    List<String> errorFileMessages = new ArrayList<>();
    for (MultipartFile file : filteredFiles) {
        try {
            final boolean isExecutable = checkFileExecutabilityPredicate.test(file.getOriginalFilename(), analysis.getType());
            savedFiles.add(saveFile(file, user, analysis, file.getName(), isExecutable, dataReference));
        } catch (AlreadyExistException e) {
            errorFileMessages.add(e.getMessage());
        }
    }
    if (!errorFileMessages.isEmpty()) {
        throw new ValidationRuntimeException("Failed to save files", ImmutableMap.of(dataReference.getGuid(), errorFileMessages));
    }
    return savedFiles;
}
Also used : AnalysisHelper(com.odysseusinc.arachne.portal.util.AnalysisHelper) AnalysisArchiveUtils(com.odysseusinc.arachne.commons.utils.AnalysisArchiveUtils) URL(java.net.URL) Date(java.util.Date) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) LoggerFactory(org.slf4j.LoggerFactory) SqlTranslate(org.ohdsi.sql.SqlTranslate) StringUtils(org.apache.commons.lang3.StringUtils) ValidationRuntimeException(com.odysseusinc.arachne.portal.exception.ValidationRuntimeException) OHDSI_SQL_EXT(com.odysseusinc.arachne.commons.utils.CommonFileUtils.OHDSI_SQL_EXT) ByteArrayInputStream(java.io.ByteArrayInputStream) Analysis(com.odysseusinc.arachne.portal.model.Analysis) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) Path(java.nio.file.Path) RestTemplate(org.springframework.web.client.RestTemplate) ImmutableMap(com.google.common.collect.ImmutableMap) AlreadyExistException(com.odysseusinc.arachne.portal.exception.AlreadyExistException) HttpHeaders(org.springframework.http.HttpHeaders) Collection(java.util.Collection) MediaType(org.springframework.http.MediaType) SqlRender(org.ohdsi.sql.SqlRender) CommonAnalysisType(com.odysseusinc.arachne.commons.api.v1.dto.CommonAnalysisType) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) MockMultipartFile(org.springframework.mock.web.MockMultipartFile) IUser(com.odysseusinc.arachne.portal.model.IUser) IOUtils(org.apache.commons.io.IOUtils) CommonFileUtils(com.odysseusinc.arachne.commons.utils.CommonFileUtils) List(java.util.List) HttpEntity(org.springframework.http.HttpEntity) AntivirusJob(com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJob) AnalysisFilesSavingService(com.odysseusinc.arachne.portal.service.analysis.AnalysisFilesSavingService) NotNull(org.jetbrains.annotations.NotNull) FilenameUtils(org.apache.commons.io.FilenameUtils) ZipOutputStream(java.util.zip.ZipOutputStream) ANALYSIS_INFO_FILE_DESCRIPTION(com.odysseusinc.arachne.commons.utils.CommonFileUtils.ANALYSIS_INFO_FILE_DESCRIPTION) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OHDSI_JSON_EXT(com.odysseusinc.arachne.commons.utils.CommonFileUtils.OHDSI_JSON_EXT) DataReference(com.odysseusinc.arachne.portal.model.DataReference) ArachneSystemRuntimeException(com.odysseusinc.arachne.portal.exception.ArachneSystemRuntimeException) ArrayList(java.util.ArrayList) AntivirusJobEvent(com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJobEvent) BiPredicate(java.util.function.BiPredicate) IORuntimeException(com.odysseusinc.arachne.portal.exception.IORuntimeException) ZipUtil(com.odysseusinc.arachne.portal.util.ZipUtil) ObjectUtils(org.apache.commons.lang3.ObjectUtils) Service(org.springframework.stereotype.Service) AnalysisFileRepository(com.odysseusinc.arachne.portal.repository.AnalysisFileRepository) AnalysisPreprocessorService(com.odysseusinc.arachne.portal.service.impl.AnalysisPreprocessorService) AnalysisUtils.throwAccessDeniedExceptionIfLocked(com.odysseusinc.arachne.portal.service.analysis.impl.AnalysisUtils.throwAccessDeniedExceptionIfLocked) REPLACE_EXISTING(java.nio.file.StandardCopyOption.REPLACE_EXISTING) UploadFileDTO(com.odysseusinc.arachne.portal.api.v1.dto.UploadFileDTO) Logger(org.slf4j.Logger) DBMSType(com.odysseusinc.arachne.commons.types.DBMSType) Files(java.nio.file.Files) AntivirusJobFileType(com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJobFileType) HttpMethod(org.springframework.http.HttpMethod) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) AnalysisFile(com.odysseusinc.arachne.portal.model.AnalysisFile) HttpStatus(org.springframework.http.HttpStatus) Paths(java.nio.file.Paths) MultipartFile(org.springframework.web.multipart.MultipartFile) ResponseEntity(org.springframework.http.ResponseEntity) Collections(java.util.Collections) Transactional(org.springframework.transaction.annotation.Transactional) MockMultipartFile(org.springframework.mock.web.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) AnalysisFile(com.odysseusinc.arachne.portal.model.AnalysisFile) ArrayList(java.util.ArrayList) ValidationRuntimeException(com.odysseusinc.arachne.portal.exception.ValidationRuntimeException) AlreadyExistException(com.odysseusinc.arachne.portal.exception.AlreadyExistException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 27 with BiPredicate

use of java.util.function.BiPredicate in project SpongeCommon by SpongePowered.

the class SpongeKeyBuilder method build0.

@Override
public Key<V> build0() {
    Objects.requireNonNull(this.valueType, "The value type must be set");
    Objects.requireNonNull(this.elementType, "The element type must be set");
    BiPredicate<? super E, ? super E> includesTester = this.includesTester;
    if (includesTester == null) {
        includesTester = (e, e2) -> false;
    }
    Comparator<? super E> comparator = this.comparator;
    if (comparator == null) {
        if (Comparable.class.isAssignableFrom(GenericTypeReflector.erase(this.elementType))) {
            // noinspection unchecked
            comparator = Comparator.comparing(o -> ((Comparable) o));
        } else {
            comparator = (o1, o2) -> {
                if (o1.equals(o2))
                    return 0;
                // There could be collisions, but yeah, what can you do about that..
                if (o1.hashCode() > o2.hashCode())
                    return 1;
                return -1;
            };
        }
    }
    Supplier<E> defaultValueSupplier = () -> null;
    final Class<?> rawType = GenericTypeReflector.erase(this.valueType);
    if (ListValue.class.isAssignableFrom(rawType)) {
        defaultValueSupplier = () -> (E) new ArrayList();
    } else if (SetValue.class.isAssignableFrom(rawType)) {
        defaultValueSupplier = () -> (E) new HashSet();
    } else if (WeightedCollectionValue.class.isAssignableFrom(rawType)) {
        defaultValueSupplier = () -> (E) new WeightedTable();
    } else if (MapValue.class.isAssignableFrom(rawType)) {
        defaultValueSupplier = () -> (E) new HashMap<>();
    }
    final SpongeKey<Value<E>, E> key = new SpongeKey<>(this.key, this.valueType, this.elementType, comparator, includesTester, defaultValueSupplier);
    KeyProvider.INSTANCE.register(this.key, (Key<Value<?>>) (Object) key);
    return (Key<V>) key;
}
Also used : KeyProvider(org.spongepowered.common.registry.provider.KeyProvider) HashMap(java.util.HashMap) Supplier(java.util.function.Supplier) Value(org.spongepowered.api.data.value.Value) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) BiPredicate(java.util.function.BiPredicate) WeightedCollectionValue(org.spongepowered.api.data.value.WeightedCollectionValue) AbstractResourceKeyedBuilder(org.spongepowered.common.util.AbstractResourceKeyedBuilder) Map(java.util.Map) Nullable(org.checkerframework.checker.nullness.qual.Nullable) TypeFactory(io.leangen.geantyref.TypeFactory) ListValue(org.spongepowered.api.data.value.ListValue) Set(java.util.Set) TypeToken(io.leangen.geantyref.TypeToken) SetValue(org.spongepowered.api.data.value.SetValue) Key(org.spongepowered.api.data.Key) Objects(java.util.Objects) List(java.util.List) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WeightedTable(org.spongepowered.api.util.weighted.WeightedTable) Preconditions(com.google.common.base.Preconditions) GenericTypeReflector(io.leangen.geantyref.GenericTypeReflector) MapValue(org.spongepowered.api.data.value.MapValue) Comparator(java.util.Comparator) WeightedTable(org.spongepowered.api.util.weighted.WeightedTable) ArrayList(java.util.ArrayList) MapValue(org.spongepowered.api.data.value.MapValue) Value(org.spongepowered.api.data.value.Value) WeightedCollectionValue(org.spongepowered.api.data.value.WeightedCollectionValue) ListValue(org.spongepowered.api.data.value.ListValue) SetValue(org.spongepowered.api.data.value.SetValue) MapValue(org.spongepowered.api.data.value.MapValue) SetValue(org.spongepowered.api.data.value.SetValue) Key(org.spongepowered.api.data.Key) HashSet(java.util.HashSet)

Example 28 with BiPredicate

use of java.util.function.BiPredicate in project sqlg by pietermartin.

the class ReplacedStep method passesLabelHasContainers.

private boolean passesLabelHasContainers(SqlgGraph sqlgGraph, boolean isVertex, String table) {
    return this.labelHasContainers.stream().allMatch(h -> {
        BiPredicate biPredicate = h.getBiPredicate();
        Object predicateValue = h.getValue();
        if (predicateValue instanceof Collection) {
            Collection<String> tableWithPrefixes = new ArrayList<>();
            Collection<String> edgeTableWithoutSchemaAndPrefixes = new ArrayList<>();
            Collection<String> predicateValues = (Collection<String>) predicateValue;
            SchemaTable schemaTableWithOutPrefix = SchemaTable.from(sqlgGraph, table).withOutPrefix();
            for (String value : predicateValues) {
                if (!isVertex && !value.contains(".")) {
                    // edges usually don't have schema, so we're matching any table with any schema if we weren't given any
                    edgeTableWithoutSchemaAndPrefixes.add(value);
                } else {
                    SchemaTable predicateValueAsSchemaTableWithPrefix = SchemaTable.from(sqlgGraph, value).withPrefix(isVertex ? VERTEX_PREFIX : EDGE_PREFIX);
                    tableWithPrefixes.add(predicateValueAsSchemaTableWithPrefix.toString());
                }
            }
            if (edgeTableWithoutSchemaAndPrefixes.isEmpty()) {
                return biPredicate.test(table, tableWithPrefixes);
            } else if (tableWithPrefixes.isEmpty()) {
                return biPredicate.test(schemaTableWithOutPrefix.getTable(), edgeTableWithoutSchemaAndPrefixes);
            } else {
                return biPredicate.test(table, tableWithPrefixes) || biPredicate.test(schemaTableWithOutPrefix.getTable(), edgeTableWithoutSchemaAndPrefixes);
            }
        } else {
            Preconditions.checkState(predicateValue instanceof String, "Label HasContainer's value must be an Collection of Strings or a String. Found " + predicateValue.getClass().toString());
            if (!isVertex && !((String) predicateValue).contains(".")) {
                // edges usually don't have schema, so we're matching any table with any schema if we weren't given any
                SchemaTable schemaTableWithOutPrefix = SchemaTable.from(sqlgGraph, table).withOutPrefix();
                return biPredicate.test(schemaTableWithOutPrefix.getTable(), predicateValue);
            } else {
                SchemaTable predicateValueAsSchemaTableWithPrefix = SchemaTable.from(sqlgGraph, (String) predicateValue).withPrefix(isVertex ? VERTEX_PREFIX : EDGE_PREFIX);
                return biPredicate.test(table, predicateValueAsSchemaTableWithPrefix.toString());
            }
        }
    });
}
Also used : BiPredicate(java.util.function.BiPredicate)

Example 29 with BiPredicate

use of java.util.function.BiPredicate in project sqlg by pietermartin.

the class ReplacedStep method groupIdsBySchemaTable.

/**
 * Groups the idHasContainers by SchemaTable.
 * Each SchemaTable has a list representing the idHasContainers with the relevant BiPredicate and RecordId
 *
 * @return
 */
private Map<SchemaTable, List<Multimap<BiPredicate, RecordId>>> groupIdsBySchemaTable() {
    Map<SchemaTable, List<Multimap<BiPredicate, RecordId>>> result = new HashMap<>();
    for (HasContainer idHasContainer : this.idHasContainers) {
        Map<SchemaTable, Boolean> newHasContainerMap = new HashMap<>();
        P<Object> idPredicate = (P<Object>) idHasContainer.getPredicate();
        BiPredicate biPredicate = idHasContainer.getBiPredicate();
        // This is statement is for g.V().hasId(Collection) where the logic is actually P.within not P.eq
        if (biPredicate == Compare.eq && idPredicate.getValue() instanceof Collection && ((Collection) idPredicate.getValue()).size() > 1) {
            biPredicate = Contains.within;
        }
        Multimap<BiPredicate, RecordId> biPredicateRecordIdMultimap;
        if (idPredicate.getValue() instanceof Collection) {
            Collection<Object> ids = (Collection<Object>) idPredicate.getValue();
            for (Object id : ids) {
                RecordId recordId = RecordId.from(id);
                List<Multimap<BiPredicate, RecordId>> biPredicateRecordIdList = result.get(recordId.getSchemaTable());
                Boolean newHasContainer = newHasContainerMap.get(recordId.getSchemaTable());
                if (biPredicateRecordIdList == null) {
                    biPredicateRecordIdList = new ArrayList<>();
                    biPredicateRecordIdMultimap = LinkedListMultimap.create();
                    biPredicateRecordIdList.add(biPredicateRecordIdMultimap);
                    result.put(recordId.getSchemaTable(), biPredicateRecordIdList);
                    newHasContainerMap.put(recordId.getSchemaTable(), false);
                } else if (newHasContainer == null) {
                    biPredicateRecordIdMultimap = LinkedListMultimap.create();
                    biPredicateRecordIdList.add(biPredicateRecordIdMultimap);
                    newHasContainerMap.put(recordId.getSchemaTable(), false);
                }
                biPredicateRecordIdMultimap = biPredicateRecordIdList.get(biPredicateRecordIdList.size() - 1);
                biPredicateRecordIdMultimap.put(biPredicate, recordId);
            }
        } else {
            Object id = idPredicate.getValue();
            RecordId recordId = RecordId.from(id);
            List<Multimap<BiPredicate, RecordId>> biPredicateRecordIdList = result.computeIfAbsent(recordId.getSchemaTable(), k -> new ArrayList<>());
            biPredicateRecordIdMultimap = LinkedListMultimap.create();
            biPredicateRecordIdList.add(biPredicateRecordIdMultimap);
            biPredicateRecordIdMultimap.put(biPredicate, recordId);
        }
    }
    return result;
}
Also used : HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer) P(org.apache.tinkerpop.gremlin.process.traversal.P) Multimap(com.google.common.collect.Multimap) LinkedListMultimap(com.google.common.collect.LinkedListMultimap) BiPredicate(java.util.function.BiPredicate)

Example 30 with BiPredicate

use of java.util.function.BiPredicate in project sqlg by pietermartin.

the class ReplacedStep method getRootSchemaTableTrees.

/**
 * Calculates the root labels from which to start the query construction.
 * <p>
 * The hasContainers at this stage contains the {@link TopologyStrategy} from or without hasContainer.
 * After doing the filtering it must be removed from the hasContainers as it must not partake in sql generation.
 *
 * @return A set of SchemaTableTree. A SchemaTableTree for each root label.
 */
Set<SchemaTableTree> getRootSchemaTableTrees(SqlgGraph sqlgGraph, int replacedStepDepth) {
    Preconditions.checkState(this.isGraphStep(), "ReplacedStep must be for a GraphStep!");
    Set<SchemaTableTree> result = new HashSet<>();
    final GraphStep graphStep = (GraphStep) this.step;
    final boolean isVertex = graphStep.getReturnClass().isAssignableFrom(Vertex.class);
    final boolean isEdge = !isVertex;
    // RecordIds grouped by SchemaTable
    Map<SchemaTable, List<Multimap<BiPredicate, RecordId>>> groupedIds = null;
    if (!this.idHasContainers.isEmpty()) {
        groupedIds = groupIdsBySchemaTable();
    }
    // All tables depending on the strategy, topology tables only or the rest.
    Map<String, Map<String, PropertyType>> filteredAllTables = SqlgUtil.filterSqlgSchemaHasContainers(this.topology, this.hasContainers, false);
    // Optimization for the simple case of only one label specified.
    if (isVertex && this.labelHasContainers.size() == 1 && this.labelHasContainers.get(0).getBiPredicate() == Compare.eq) {
        HasContainer labelHasContainer = this.labelHasContainers.get(0);
        String table = (String) labelHasContainer.getValue();
        SchemaTable schemaTableWithPrefix = SchemaTable.from(sqlgGraph, table).withPrefix(isVertex ? VERTEX_PREFIX : EDGE_PREFIX);
        if (filteredAllTables.containsKey(schemaTableWithPrefix.toString())) {
            collectSchemaTableTrees(sqlgGraph, replacedStepDepth, result, groupedIds, schemaTableWithPrefix.toString());
        }
    } else {
        for (String table : filteredAllTables.keySet()) {
            // if graphStep's return class is Vertex ignore all edges and vice versa.
            if ((isVertex && table.substring(table.indexOf(".") + 1).startsWith(VERTEX_PREFIX)) || (isEdge && table.substring(table.indexOf(".") + 1).startsWith(EDGE_PREFIX))) {
                if (passesLabelHasContainers(sqlgGraph, isVertex, table)) {
                    collectSchemaTableTrees(sqlgGraph, replacedStepDepth, result, groupedIds, table);
                }
            }
        }
    }
    return result;
}
Also used : GraphStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.GraphStep) HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer) BiPredicate(java.util.function.BiPredicate)

Aggregations

BiPredicate (java.util.function.BiPredicate)36 List (java.util.List)17 Collectors (java.util.stream.Collectors)16 ArrayList (java.util.ArrayList)14 IOException (java.io.IOException)12 Collections (java.util.Collections)12 Map (java.util.Map)11 Files (java.nio.file.Files)10 Objects (java.util.Objects)8 Set (java.util.Set)8 HashMap (java.util.HashMap)7 Paths (java.nio.file.Paths)6 Arrays (java.util.Arrays)6 HashSet (java.util.HashSet)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 HasContainer (org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer)6 Path (java.nio.file.Path)5 java.util (java.util)5 Collection (java.util.Collection)5 Function (java.util.function.Function)5