Search in sources :

Example 31 with StringUtils.isNotEmpty

use of org.apache.commons.lang3.StringUtils.isNotEmpty in project nifi by apache.

the class GenerateTableFetch method onTrigger.

@Override
public void onTrigger(final ProcessContext context, final ProcessSessionFactory sessionFactory) throws ProcessException {
    // Fetch the column/table info once (if the table name and max value columns are not dynamic). Otherwise do the setup later
    if (!isDynamicTableName && !isDynamicMaxValues && !setupComplete.get()) {
        super.setup(context);
    }
    ProcessSession session = sessionFactory.createSession();
    FlowFile fileToProcess = null;
    if (context.hasIncomingConnection()) {
        fileToProcess = session.get();
        if (fileToProcess == null) {
            // Incoming connection with no flow file available, do no work (see capability description)
            return;
        }
    }
    final ComponentLog logger = getLogger();
    final DBCPService dbcpService = context.getProperty(DBCP_SERVICE).asControllerService(DBCPService.class);
    final DatabaseAdapter dbAdapter = dbAdapters.get(context.getProperty(DB_TYPE).getValue());
    final String tableName = context.getProperty(TABLE_NAME).evaluateAttributeExpressions(fileToProcess).getValue();
    final String columnNames = context.getProperty(COLUMN_NAMES).evaluateAttributeExpressions(fileToProcess).getValue();
    final String maxValueColumnNames = context.getProperty(MAX_VALUE_COLUMN_NAMES).evaluateAttributeExpressions(fileToProcess).getValue();
    final int partitionSize = context.getProperty(PARTITION_SIZE).evaluateAttributeExpressions(fileToProcess).asInteger();
    final String customWhereClause = context.getProperty(WHERE_CLAUSE).evaluateAttributeExpressions(fileToProcess).getValue();
    final StateManager stateManager = context.getStateManager();
    final StateMap stateMap;
    FlowFile finalFileToProcess = fileToProcess;
    try {
        stateMap = stateManager.getState(Scope.CLUSTER);
    } catch (final IOException ioe) {
        logger.error("Failed to retrieve observed maximum values from the State Manager. Will not perform " + "query until this is accomplished.", ioe);
        context.yield();
        return;
    }
    try {
        // Make a mutable copy of the current state property map. This will be updated by the result row callback, and eventually
        // set as the current state map (after the session has been committed)
        final Map<String, String> statePropertyMap = new HashMap<>(stateMap.toMap());
        // If an initial max value for column(s) has been specified using properties, and this column is not in the state manager, sync them to the state property map
        for (final Map.Entry<String, String> maxProp : maxValueProperties.entrySet()) {
            String maxPropKey = maxProp.getKey().toLowerCase();
            String fullyQualifiedMaxPropKey = getStateKey(tableName, maxPropKey);
            if (!statePropertyMap.containsKey(fullyQualifiedMaxPropKey)) {
                String newMaxPropValue;
                // but store the new initial max value under the fully-qualified key.
                if (statePropertyMap.containsKey(maxPropKey)) {
                    newMaxPropValue = statePropertyMap.get(maxPropKey);
                } else {
                    newMaxPropValue = maxProp.getValue();
                }
                statePropertyMap.put(fullyQualifiedMaxPropKey, newMaxPropValue);
            }
        }
        // Build a WHERE clause with maximum-value columns (if they exist), and a list of column names that will contain MAX(<column>) aliases. The
        // executed SQL query will retrieve the count of all records after the filter(s) have been applied, as well as the new maximum values for the
        // specified columns. This allows the processor to generate the correctly partitioned SQL statements as well as to update the state with the
        // latest observed maximum values.
        String whereClause = null;
        List<String> maxValueColumnNameList = StringUtils.isEmpty(maxValueColumnNames) ? new ArrayList<>(0) : Arrays.asList(maxValueColumnNames.split("\\s*,\\s*"));
        List<String> maxValueClauses = new ArrayList<>(maxValueColumnNameList.size());
        String columnsClause = null;
        List<String> maxValueSelectColumns = new ArrayList<>(maxValueColumnNameList.size() + 1);
        maxValueSelectColumns.add("COUNT(*)");
        // For each maximum-value column, get a WHERE filter and a MAX(column) alias
        IntStream.range(0, maxValueColumnNameList.size()).forEach((index) -> {
            String colName = maxValueColumnNameList.get(index);
            maxValueSelectColumns.add("MAX(" + colName + ") " + colName);
            String maxValue = getColumnStateMaxValue(tableName, statePropertyMap, colName);
            if (!StringUtils.isEmpty(maxValue)) {
                if (columnTypeMap.isEmpty() || getColumnType(tableName, colName) == null) {
                    // This means column type cache is clean after instance reboot. We should re-cache column type
                    super.setup(context, false, finalFileToProcess);
                }
                Integer type = getColumnType(tableName, colName);
                // Add a condition for the WHERE clause
                maxValueClauses.add(colName + (index == 0 ? " > " : " >= ") + getLiteralByType(type, maxValue, dbAdapter.getName()));
            }
        });
        if (customWhereClause != null) {
            // adding the custom WHERE clause (if defined) to the list of existing clauses.
            maxValueClauses.add("(" + customWhereClause + ")");
        }
        whereClause = StringUtils.join(maxValueClauses, " AND ");
        columnsClause = StringUtils.join(maxValueSelectColumns, ", ");
        // Build a SELECT query with maximum-value columns (if present)
        final String selectQuery = dbAdapter.getSelectStatement(tableName, columnsClause, whereClause, null, null, null);
        long rowCount = 0;
        try (final Connection con = dbcpService.getConnection();
            final Statement st = con.createStatement()) {
            final Integer queryTimeout = context.getProperty(QUERY_TIMEOUT).evaluateAttributeExpressions(fileToProcess).asTimePeriod(TimeUnit.SECONDS).intValue();
            // timeout in seconds
            st.setQueryTimeout(queryTimeout);
            logger.debug("Executing {}", new Object[] { selectQuery });
            ResultSet resultSet;
            resultSet = st.executeQuery(selectQuery);
            if (resultSet.next()) {
                // Total row count is in the first column
                rowCount = resultSet.getLong(1);
                // Update the state map with the newly-observed maximum values
                ResultSetMetaData rsmd = resultSet.getMetaData();
                for (int i = 2; i <= rsmd.getColumnCount(); i++) {
                    // Some JDBC drivers consider the columns name and label to be very different things.
                    // Since this column has been aliased lets check the label first,
                    // if there is no label we'll use the column name.
                    String resultColumnName = (StringUtils.isNotEmpty(rsmd.getColumnLabel(i)) ? rsmd.getColumnLabel(i) : rsmd.getColumnName(i)).toLowerCase();
                    String fullyQualifiedStateKey = getStateKey(tableName, resultColumnName);
                    String resultColumnCurrentMax = statePropertyMap.get(fullyQualifiedStateKey);
                    if (StringUtils.isEmpty(resultColumnCurrentMax) && !isDynamicTableName) {
                        // If we can't find the value at the fully-qualified key name and the table name is static, it is possible (under a previous scheme)
                        // the value has been stored under a key that is only the column name. Fall back to check the column name; either way, when a new
                        // maximum value is observed, it will be stored under the fully-qualified key from then on.
                        resultColumnCurrentMax = statePropertyMap.get(resultColumnName);
                    }
                    int type = rsmd.getColumnType(i);
                    if (isDynamicTableName) {
                        // We haven't pre-populated the column type map if the table name is dynamic, so do it here
                        columnTypeMap.put(fullyQualifiedStateKey, type);
                    }
                    try {
                        String newMaxValue = getMaxValueFromRow(resultSet, i, type, resultColumnCurrentMax, dbAdapter.getName());
                        if (newMaxValue != null) {
                            statePropertyMap.put(fullyQualifiedStateKey, newMaxValue);
                        }
                    } catch (ParseException | IOException pie) {
                        // Fail the whole thing here before we start creating flow files and such
                        throw new ProcessException(pie);
                    }
                }
            } else {
                // Something is very wrong here, one row (even if count is zero) should be returned
                throw new SQLException("No rows returned from metadata query: " + selectQuery);
            }
            // for each maximum-value column get a right bounding WHERE condition
            IntStream.range(0, maxValueColumnNameList.size()).forEach((index) -> {
                String colName = maxValueColumnNameList.get(index);
                maxValueSelectColumns.add("MAX(" + colName + ") " + colName);
                String maxValue = getColumnStateMaxValue(tableName, statePropertyMap, colName);
                if (!StringUtils.isEmpty(maxValue)) {
                    if (columnTypeMap.isEmpty() || getColumnType(tableName, colName) == null) {
                        // This means column type cache is clean after instance reboot. We should re-cache column type
                        super.setup(context, false, finalFileToProcess);
                    }
                    Integer type = getColumnType(tableName, colName);
                    // Add a condition for the WHERE clause
                    maxValueClauses.add(colName + " <= " + getLiteralByType(type, maxValue, dbAdapter.getName()));
                }
            });
            // Update WHERE list to include new right hand boundaries
            whereClause = StringUtils.join(maxValueClauses, " AND ");
            final long numberOfFetches = (partitionSize == 0) ? 1 : (rowCount / partitionSize) + (rowCount % partitionSize == 0 ? 0 : 1);
            // Generate SQL statements to read "pages" of data
            for (long i = 0; i < numberOfFetches; i++) {
                Long limit = partitionSize == 0 ? null : (long) partitionSize;
                Long offset = partitionSize == 0 ? null : i * partitionSize;
                final String maxColumnNames = StringUtils.join(maxValueColumnNameList, ", ");
                final String query = dbAdapter.getSelectStatement(tableName, columnNames, whereClause, maxColumnNames, limit, offset);
                FlowFile sqlFlowFile = (fileToProcess == null) ? session.create() : session.create(fileToProcess);
                sqlFlowFile = session.write(sqlFlowFile, out -> out.write(query.getBytes()));
                sqlFlowFile = session.putAttribute(sqlFlowFile, "generatetablefetch.tableName", tableName);
                if (columnNames != null) {
                    sqlFlowFile = session.putAttribute(sqlFlowFile, "generatetablefetch.columnNames", columnNames);
                }
                if (StringUtils.isNotBlank(whereClause)) {
                    sqlFlowFile = session.putAttribute(sqlFlowFile, "generatetablefetch.whereClause", whereClause);
                }
                if (StringUtils.isNotBlank(maxColumnNames)) {
                    sqlFlowFile = session.putAttribute(sqlFlowFile, "generatetablefetch.maxColumnNames", maxColumnNames);
                }
                sqlFlowFile = session.putAttribute(sqlFlowFile, "generatetablefetch.limit", String.valueOf(limit));
                if (partitionSize != 0) {
                    sqlFlowFile = session.putAttribute(sqlFlowFile, "generatetablefetch.offset", String.valueOf(offset));
                }
                session.transfer(sqlFlowFile, REL_SUCCESS);
            }
            if (fileToProcess != null) {
                session.remove(fileToProcess);
            }
        } catch (SQLException e) {
            if (fileToProcess != null) {
                logger.error("Unable to execute SQL select query {} due to {}, routing {} to failure", new Object[] { selectQuery, e, fileToProcess });
                fileToProcess = session.putAttribute(fileToProcess, "generatetablefetch.sql.error", e.getMessage());
                session.transfer(fileToProcess, REL_FAILURE);
            } else {
                logger.error("Unable to execute SQL select query {} due to {}", new Object[] { selectQuery, e });
                throw new ProcessException(e);
            }
        }
        session.commit();
        try {
            // Update the state
            stateManager.setState(statePropertyMap, Scope.CLUSTER);
        } catch (IOException ioe) {
            logger.error("{} failed to update State Manager, observed maximum values will not be recorded. " + "Also, any generated SQL statements may be duplicated.", new Object[] { this, ioe });
        }
    } catch (final ProcessException pe) {
        // Log the cause of the ProcessException if it is available
        Throwable t = (pe.getCause() == null ? pe : pe.getCause());
        logger.error("Error during processing: {}", new Object[] { t.getMessage() }, t);
        session.rollback();
        context.yield();
    }
}
Also used : ProcessSession(org.apache.nifi.processor.ProcessSession) StandardValidators(org.apache.nifi.processor.util.StandardValidators) IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) Connection(java.sql.Connection) CapabilityDescription(org.apache.nifi.annotation.documentation.CapabilityDescription) ValidationContext(org.apache.nifi.components.ValidationContext) HashMap(java.util.HashMap) ComponentLog(org.apache.nifi.logging.ComponentLog) StringUtils(org.apache.commons.lang3.StringUtils) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) ProcessException(org.apache.nifi.processor.exception.ProcessException) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) SQLException(java.sql.SQLException) WritesAttributes(org.apache.nifi.annotation.behavior.WritesAttributes) Scope(org.apache.nifi.components.state.Scope) Relationship(org.apache.nifi.processor.Relationship) ResultSet(java.sql.ResultSet) Map(java.util.Map) Requirement(org.apache.nifi.annotation.behavior.InputRequirement.Requirement) ParseException(java.text.ParseException) TriggerSerially(org.apache.nifi.annotation.behavior.TriggerSerially) ValidationResult(org.apache.nifi.components.ValidationResult) DatabaseAdapter(org.apache.nifi.processors.standard.db.DatabaseAdapter) FlowFile(org.apache.nifi.flowfile.FlowFile) StateManager(org.apache.nifi.components.state.StateManager) Collection(java.util.Collection) ProcessContext(org.apache.nifi.processor.ProcessContext) Set(java.util.Set) ProcessSession(org.apache.nifi.processor.ProcessSession) IOException(java.io.IOException) WritesAttribute(org.apache.nifi.annotation.behavior.WritesAttribute) SeeAlso(org.apache.nifi.annotation.documentation.SeeAlso) ProcessSessionFactory(org.apache.nifi.processor.ProcessSessionFactory) StateMap(org.apache.nifi.components.state.StateMap) TimeUnit(java.util.concurrent.TimeUnit) InputRequirement(org.apache.nifi.annotation.behavior.InputRequirement) Stateful(org.apache.nifi.annotation.behavior.Stateful) OnScheduled(org.apache.nifi.annotation.lifecycle.OnScheduled) List(java.util.List) DynamicProperty(org.apache.nifi.annotation.behavior.DynamicProperty) Statement(java.sql.Statement) Tags(org.apache.nifi.annotation.documentation.Tags) DBCPService(org.apache.nifi.dbcp.DBCPService) Collections(java.util.Collections) ResultSetMetaData(java.sql.ResultSetMetaData) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) StateMap(org.apache.nifi.components.state.StateMap) ArrayList(java.util.ArrayList) ResultSetMetaData(java.sql.ResultSetMetaData) StateManager(org.apache.nifi.components.state.StateManager) ResultSet(java.sql.ResultSet) FlowFile(org.apache.nifi.flowfile.FlowFile) Statement(java.sql.Statement) Connection(java.sql.Connection) IOException(java.io.IOException) ComponentLog(org.apache.nifi.logging.ComponentLog) DatabaseAdapter(org.apache.nifi.processors.standard.db.DatabaseAdapter) ProcessException(org.apache.nifi.processor.exception.ProcessException) DBCPService(org.apache.nifi.dbcp.DBCPService) ParseException(java.text.ParseException) HashMap(java.util.HashMap) Map(java.util.Map) StateMap(org.apache.nifi.components.state.StateMap)

Example 32 with StringUtils.isNotEmpty

use of org.apache.commons.lang3.StringUtils.isNotEmpty in project knime-core by knime.

the class GUIWorkflowLoadHelper method loadCredentials.

/**
 * {@inheritDoc}
 */
@Override
public List<Credentials> loadCredentials(final List<Credentials> credentialsList) {
    // set the ones that were already prompted for into the result list ... don't prompt them again
    final List<Credentials> newCredentialsList = new ArrayList<Credentials>();
    final List<Credentials> credentialsToBePromptedList = new ArrayList<Credentials>();
    for (Credentials c : credentialsList) {
        Credentials updatedCredentials = m_promptedCredentials.get(c.getName());
        if (updatedCredentials != null) {
            newCredentialsList.add(updatedCredentials);
        } else {
            credentialsToBePromptedList.add(c);
        }
    }
    // prompt for details for the credentials that haven't been prompted for
    if (!credentialsToBePromptedList.isEmpty()) {
        // run sync'ly in UI thread
        m_display.syncExec(new Runnable() {

            @Override
            public void run() {
                CredentialVariablesDialog dialog = new CredentialVariablesDialog(m_display.getActiveShell(), credentialsToBePromptedList, m_workflowName);
                if (dialog.open() == Window.OK) {
                    List<Credentials> updateCredentialsList = dialog.getCredentials();
                    newCredentialsList.addAll(updateCredentialsList);
                    updateCredentialsList.stream().filter(c -> StringUtils.isNotEmpty(c.getPassword())).forEach(c -> m_promptedCredentials.put(c.getName(), c));
                } else {
                    newCredentialsList.addAll(credentialsToBePromptedList);
                }
            }
        });
    }
    return newCredentialsList;
}
Also used : IPreferenceStore(org.eclipse.jface.preference.IPreferenceStore) KNIMEConstants(org.knime.core.node.KNIMEConstants) LoadVersion(org.knime.core.node.workflow.FileWorkflowPersistor.LoadVersion) WorkflowContext(org.knime.core.node.workflow.WorkflowContext) HashMap(java.util.HashMap) IDialogConstants(org.eclipse.jface.dialogs.IDialogConstants) Display(org.eclipse.swt.widgets.Display) CredentialVariablesDialog(org.knime.workbench.ui.masterkey.CredentialVariablesDialog) AtomicReference(java.util.concurrent.atomic.AtomicReference) StringUtils(org.apache.commons.lang3.StringUtils) File(java.io.File) PreferenceConstants(org.knime.workbench.ui.preferences.PreferenceConstants) ArrayList(java.util.ArrayList) Version(org.knime.core.util.Version) List(java.util.List) Window(org.eclipse.jface.window.Window) Map(java.util.Map) SWT(org.eclipse.swt.SWT) Credentials(org.knime.core.node.workflow.Credentials) MessageDialogWithToggle(org.eclipse.jface.dialogs.MessageDialogWithToggle) KNIMEUIPlugin(org.knime.workbench.ui.KNIMEUIPlugin) URI(java.net.URI) MessageDialog(org.eclipse.jface.dialogs.MessageDialog) WorkflowLoadHelper(org.knime.core.node.workflow.WorkflowLoadHelper) ArrayList(java.util.ArrayList) CredentialVariablesDialog(org.knime.workbench.ui.masterkey.CredentialVariablesDialog) ArrayList(java.util.ArrayList) List(java.util.List) Credentials(org.knime.core.node.workflow.Credentials)

Example 33 with StringUtils.isNotEmpty

use of org.apache.commons.lang3.StringUtils.isNotEmpty in project molgenis by molgenis.

the class DataExplorerController method init.

/**
 * Show the explorer page
 *
 * @return the view name
 */
@GetMapping
public String init(@RequestParam(value = "entity", required = false) String selectedEntityName, @RequestParam(value = "entityId", required = false) String selectedEntityId, Model model) {
    StringBuilder message = new StringBuilder("");
    final boolean currentUserIsSu = SecurityUtils.currentUserIsSu();
    Map<String, EntityType> entitiesMeta = dataService.getMeta().getEntityTypes().filter(entityType -> !entityType.isAbstract()).filter(entityType -> currentUserIsSu || !EntityTypeUtils.isSystemEntity(entityType)).sorted(Comparator.comparing(EntityType::getLabel)).collect(Collectors.toMap(EntityType::getId, Function.identity(), (e1, e2) -> e2, LinkedHashMap::new));
    model.addAttribute("entitiesMeta", entitiesMeta);
    if (selectedEntityId != null && selectedEntityName == null) {
        EntityType entityType = dataService.getMeta().getEntityType(selectedEntityId);
        if (entityType == null) {
            message.append("Entity does not exist or you do not have permission on this entity");
        } else {
            selectedEntityName = entityType.getId();
        }
        if (selectedEntityName != null) {
            checkExistsAndPermission(selectedEntityName, message);
        }
    }
    if (StringUtils.isNotEmpty(message.toString())) {
        model.addAttribute("warningMessage", message.toString());
    }
    model.addAttribute("selectedEntityName", selectedEntityName);
    model.addAttribute("isAdmin", currentUserIsSu);
    boolean navigatorAvailable = menuReaderService.getMenu().findMenuItemPath(NAVIGATOR) != null;
    model.addAttribute("showNavigatorLink", dataExplorerSettings.isShowNavigatorLink() && navigatorAvailable);
    model.addAttribute("hasTrackingId", null != appSettings.getGoogleAnalyticsTrackingId());
    model.addAttribute("hasMolgenisTrackingId", null != appSettings.getGoogleAnalyticsTrackingIdMolgenis());
    return "view-dataexplorer";
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) LocaleContextHolder(org.springframework.context.i18n.LocaleContextHolder) PluginController(org.molgenis.web.PluginController) URLDecoder(java.net.URLDecoder) MenuReaderService(org.molgenis.core.ui.menu.MenuReaderService) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) StringUtils(org.apache.commons.lang3.StringUtils) AnnotationJobExecutionMetaData(org.molgenis.data.annotation.web.meta.AnnotationJobExecutionMetaData) NegotiatorController(org.molgenis.dataexplorer.negotiator.NegotiatorController) JobExecutionMetaData(org.molgenis.jobs.model.JobExecutionMetaData) Model(org.springframework.ui.Model) JSONObject(org.json.JSONObject) Gson(com.google.gson.Gson) DataExplorerDownloadHandler(org.molgenis.dataexplorer.download.DataExplorerDownloadHandler) EntityTypeIdentity(org.molgenis.data.security.EntityTypeIdentity) AttributeFactory(org.molgenis.data.meta.model.AttributeFactory) org.molgenis.data(org.molgenis.data) GenomeBrowserTrack(org.molgenis.genomebrowser.GenomeBrowserTrack) EntityTypeUtils(org.molgenis.data.support.EntityTypeUtils) EntityType(org.molgenis.data.meta.model.EntityType) Collectors(java.util.stream.Collectors) UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) UserPermissionEvaluator(org.molgenis.security.core.UserPermissionEvaluator) ParseException(freemarker.core.ParseException) org.springframework.web.bind.annotation(org.springframework.web.bind.annotation) FreeMarkerConfigurer(org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer) Permission(org.molgenis.security.core.Permission) java.util(java.util) LocalDateTime(java.time.LocalDateTime) QueryImpl(org.molgenis.data.support.QueryImpl) Controller(org.springframework.stereotype.Controller) EntityTypePermission(org.molgenis.data.security.EntityTypePermission) Function(java.util.function.Function) AppSettings(org.molgenis.settings.AppSettings) DownloadType(org.molgenis.dataexplorer.controller.DataRequest.DownloadType) ServletOutputStream(javax.servlet.ServletOutputStream) EntityUtils.getTypedValue(org.molgenis.data.util.EntityUtils.getTypedValue) URI(org.molgenis.dataexplorer.controller.DataExplorerController.URI) MessageSource(org.springframework.context.MessageSource) Logger(org.slf4j.Logger) READ(org.molgenis.security.core.Permission.READ) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) GenomeBrowserService(org.molgenis.genomebrowser.service.GenomeBrowserService) SecurityUtils(org.molgenis.security.core.utils.SecurityUtils) WRITE(org.molgenis.security.core.Permission.WRITE) DateTimeFormatter(java.time.format.DateTimeFormatter) ANNOTATION_JOB_EXECUTION(org.molgenis.data.annotation.web.meta.AnnotationJobExecutionMetaData.ANNOTATION_JOB_EXECUTION) Package(org.molgenis.data.meta.model.Package) DataExplorerSettings(org.molgenis.dataexplorer.settings.DataExplorerSettings) DOWNLOAD_TYPE_CSV(org.molgenis.dataexplorer.controller.DataRequest.DownloadType.DOWNLOAD_TYPE_CSV)

Example 34 with StringUtils.isNotEmpty

use of org.apache.commons.lang3.StringUtils.isNotEmpty in project molgenis by molgenis.

the class RestController method retrieveEntityCollection.

/**
 * Does a rsql/fiql query, returns the result as csv
 * <p>
 * Parameters:
 * <p>
 * q: the query
 * <p>
 * attributes: the attributes to return, if not specified returns all attributes
 * <p>
 * start: the index of the first row, default 0
 * <p>
 * num: the number of results to return, default 100, max 10000
 * <p>
 * <p>
 * Example: /api/v1/csv/person?q=firstName==Piet&attributes=firstName,lastName&start=10&num=100
 */
@GetMapping(value = "/csv/{entityTypeId}", produces = "text/csv")
@ResponseBody
public EntityCollection retrieveEntityCollection(@PathVariable("entityTypeId") String entityTypeId, @RequestParam(value = "attributes", required = false) String[] attributes, HttpServletRequest req, HttpServletResponse resp) throws IOException {
    final Set<String> attributesSet = toAttributeSet(attributes);
    EntityType meta;
    Iterable<Entity> entities;
    try {
        meta = dataService.getEntityType(entityTypeId);
        Query<Entity> q = new QueryStringParser(meta, molgenisRSQL).parseQueryString(req.getParameterMap());
        String[] sortAttributeArray = req.getParameterMap().get("sortColumn");
        if (sortAttributeArray != null && sortAttributeArray.length == 1 && StringUtils.isNotEmpty(sortAttributeArray[0])) {
            String sortAttribute = sortAttributeArray[0];
            String[] sortOrderArray = req.getParameterMap().get("sortOrder");
            Sort.Direction order = Sort.Direction.ASC;
            if (sortOrderArray != null && sortOrderArray.length == 1 && StringUtils.isNotEmpty(sortOrderArray[0])) {
                String sortOrder = sortOrderArray[0];
                switch(sortOrder) {
                    case "ASC":
                        order = Sort.Direction.ASC;
                        break;
                    case "DESC":
                        order = Sort.Direction.DESC;
                        break;
                    default:
                        throw new RuntimeException("unknown sort order");
                }
            }
            q.sort().on(sortAttribute, order);
        }
        if (q.getPageSize() == 0) {
            q.pageSize(EntityCollectionRequest.DEFAULT_ROW_COUNT);
        }
        if (q.getPageSize() > EntityCollectionRequest.MAX_ROWS) {
            resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Num exceeded the maximum of " + EntityCollectionRequest.MAX_ROWS + " rows");
            return null;
        }
        entities = () -> dataService.findAll(entityTypeId, q).iterator();
    } catch (ConversionFailedException | RSQLParserException | UnknownAttributeException | IllegalArgumentException | UnsupportedOperationException | UnknownEntityException e) {
        resp.sendError(HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
        return null;
    } catch (MolgenisDataAccessException e) {
        resp.sendError(HttpServletResponse.SC_UNAUTHORIZED);
        return null;
    }
    // Check attribute names
    Iterable<String> attributesIterable = Iterables.transform(meta.getAtomicAttributes(), attribute -> attribute.getName().toLowerCase());
    if (attributesSet != null) {
        SetView<String> diff = Sets.difference(attributesSet, Sets.newHashSet(attributesIterable));
        if (!diff.isEmpty()) {
            resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Unknown attributes " + diff);
            return null;
        }
    }
    attributesIterable = Iterables.transform(meta.getAtomicAttributes(), Attribute::getName);
    if (attributesSet != null) {
        attributesIterable = Iterables.filter(attributesIterable, attribute -> attributesSet.contains(attribute.toLowerCase()));
    }
    return new DefaultEntityCollection(entities, attributesIterable);
}
Also used : UnknownTokenException(org.molgenis.security.core.token.UnknownTokenException) WebAuthenticationDetails(org.springframework.security.web.authentication.WebAuthenticationDetails) ConversionFailedException(org.springframework.core.convert.ConversionFailedException) RestService(org.molgenis.data.rest.service.RestService) ErrorMessageResponse(org.molgenis.web.ErrorMessageResponse) LoggerFactory(org.slf4j.LoggerFactory) MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest) RunAsSystemAspect.runAsSystem(org.molgenis.security.core.runas.RunAsSystemAspect.runAsSystem) ENFORCED(org.molgenis.security.twofactor.auth.TwoFactorAuthenticationSetting.ENFORCED) StringUtils(org.apache.commons.lang3.StringUtils) Attribute(org.molgenis.data.meta.model.Attribute) AuthenticationSettings(org.molgenis.security.settings.AuthenticationSettings) Valid(javax.validation.Valid) User(org.molgenis.data.security.auth.User) Matcher(java.util.regex.Matcher) ObjectError(org.springframework.validation.ObjectError) USER(org.molgenis.data.security.auth.UserMetaData.USER) AuthenticationException(org.springframework.security.core.AuthenticationException) SecurityContextHolder(org.springframework.security.core.context.SecurityContextHolder) DefaultEntityCollection(org.molgenis.data.support.DefaultEntityCollection) org.molgenis.data(org.molgenis.data) MolgenisValidationException(org.molgenis.data.validation.MolgenisValidationException) UserAccountService(org.molgenis.security.user.UserAccountService) ConversionException(org.springframework.core.convert.ConversionException) Instant(java.time.Instant) EntityType(org.molgenis.data.meta.model.EntityType) Sets(com.google.common.collect.Sets) UserPermissionEvaluator(org.molgenis.security.core.UserPermissionEvaluator) LocalDate(java.time.LocalDate) MolgenisRSQL(org.molgenis.core.ui.data.rsql.MolgenisRSQL) org.springframework.web.bind.annotation(org.springframework.web.bind.annotation) Entry(java.util.Map.Entry) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) Pattern(java.util.regex.Pattern) Authentication(org.springframework.security.core.Authentication) UserMetaData(org.molgenis.data.security.auth.UserMetaData) Iterables(com.google.common.collect.Iterables) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) java.util(java.util) ENABLED(org.molgenis.security.twofactor.auth.TwoFactorAuthenticationSetting.ENABLED) TokenParam(org.molgenis.security.token.TokenParam) ConstraintViolation(org.molgenis.data.validation.ConstraintViolation) QueryImpl(org.molgenis.data.support.QueryImpl) RSQLParserException(cz.jirutka.rsql.parser.RSQLParserException) HttpServletRequest(javax.servlet.http.HttpServletRequest) Lists(com.google.common.collect.Lists) EntityUtils.getTypedValue(org.molgenis.data.util.EntityUtils.getTypedValue) Objects.requireNonNull(java.util.Objects.requireNonNull) Href(org.molgenis.core.ui.data.support.Href) ATTRIBUTE_META_DATA(org.molgenis.data.meta.model.AttributeMetadata.ATTRIBUTE_META_DATA) BASE_URI(org.molgenis.data.rest.RestController.BASE_URI) AttributeType(org.molgenis.data.meta.AttributeType) Logger(org.slf4j.Logger) AuthenticationManager(org.springframework.security.authentication.AuthenticationManager) HttpServletResponse(javax.servlet.http.HttpServletResponse) SetView(com.google.common.collect.Sets.SetView) IOException(java.io.IOException) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException) APPLICATION_JSON_VALUE(org.springframework.http.MediaType.APPLICATION_JSON_VALUE) HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) ErrorMessage(org.molgenis.web.ErrorMessageResponse.ErrorMessage) HttpStatus(org.springframework.http.HttpStatus) TokenService(org.molgenis.security.core.token.TokenService) MultipartFile(org.springframework.web.multipart.MultipartFile) Transactional(org.springframework.transaction.annotation.Transactional) DefaultEntityCollection(org.molgenis.data.support.DefaultEntityCollection) RSQLParserException(cz.jirutka.rsql.parser.RSQLParserException) EntityType(org.molgenis.data.meta.model.EntityType) ConversionFailedException(org.springframework.core.convert.ConversionFailedException)

Example 35 with StringUtils.isNotEmpty

use of org.apache.commons.lang3.StringUtils.isNotEmpty in project georocket by georocket.

the class StoreEndpoint method onPut.

/**
 * Handles the HTTP PUT request
 * @param context the routing context
 */
private void onPut(RoutingContext context) {
    String path = getEndpointPath(context);
    HttpServerResponse response = context.response();
    HttpServerRequest request = context.request();
    String search = request.getParam("search");
    String properties = request.getParam("properties");
    String tags = request.getParam("tags");
    if (StringUtils.isNotEmpty(properties) || StringUtils.isNotEmpty(tags)) {
        Single<Void> single = Single.just(null);
        if (StringUtils.isNotEmpty(properties)) {
            single = single.flatMap(v -> setProperties(search, path, properties));
        }
        if (StringUtils.isNotEmpty(tags)) {
            single = single.flatMap(v -> appendTags(search, path, tags));
        }
        single.subscribe(v -> response.setStatusCode(204).end(), err -> fail(response, err));
    } else {
        response.setStatusCode(405).end("Only properties and tags can be modified");
    }
}
Also used : Arrays(java.util.Arrays) Router(io.vertx.ext.web.Router) RxStoreCursor(io.georocket.storage.RxStoreCursor) RoutingContext(io.vertx.ext.web.RoutingContext) StringUtils(org.apache.commons.lang3.StringUtils) ChunkMeta(io.georocket.storage.ChunkMeta) RxStore(io.georocket.storage.RxStore) StoreCursor(io.georocket.storage.StoreCursor) Single(rx.Single) Pair(org.apache.commons.lang3.tuple.Pair) Map(java.util.Map) Pump(io.vertx.core.streams.Pump) JsonObject(io.vertx.core.json.JsonObject) Logger(io.vertx.core.logging.Logger) Splitter(com.google.common.base.Splitter) OpenOptions(io.vertx.core.file.OpenOptions) ContentType(org.apache.http.entity.ContentType) UUID(java.util.UUID) Future(io.vertx.core.Future) FileNotFoundException(java.io.FileNotFoundException) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) HttpServerResponse(io.vertx.core.http.HttpServerResponse) FileSystem(io.vertx.core.file.FileSystem) RxHelper(io.vertx.rx.java.RxHelper) MultiMerger(io.georocket.output.MultiMerger) Pattern(java.util.regex.Pattern) AddressConstants(io.georocket.constants.AddressConstants) AsyncFile(io.vertx.core.file.AsyncFile) HttpServerRequest(io.vertx.core.http.HttpServerRequest) MimeTypeUtils(io.georocket.util.MimeTypeUtils) HashMap(java.util.HashMap) LoggerFactory(io.vertx.core.logging.LoggerFactory) Observable(rx.Observable) ServerAPIException(io.georocket.ServerAPIException) WriteStream(io.vertx.core.streams.WriteStream) StoreFactory(io.georocket.storage.StoreFactory) AsyncResult(io.vertx.core.AsyncResult) HttpException(io.georocket.util.HttpException) ParseException(org.apache.http.ParseException) ObservableFuture(io.vertx.rx.java.ObservableFuture) Vertx(io.vertx.core.Vertx) IOException(java.io.IOException) StringEscapeUtils(org.apache.commons.text.StringEscapeUtils) RxAsyncCursor(io.georocket.storage.RxAsyncCursor) File(java.io.File) JsonArray(io.vertx.core.json.JsonArray) ObjectId(org.bson.types.ObjectId) Merger(io.georocket.output.Merger) Handler(io.vertx.core.Handler) ConfigConstants(io.georocket.constants.ConfigConstants) HttpServerResponse(io.vertx.core.http.HttpServerResponse) HttpServerRequest(io.vertx.core.http.HttpServerRequest)

Aggregations

StringUtils (org.apache.commons.lang3.StringUtils)42 List (java.util.List)31 Map (java.util.Map)28 Collectors (java.util.stream.Collectors)21 ArrayList (java.util.ArrayList)20 HashMap (java.util.HashMap)19 IOException (java.io.IOException)17 LoggerFactory (org.slf4j.LoggerFactory)15 Set (java.util.Set)14 Autowired (org.springframework.beans.factory.annotation.Autowired)14 Logger (org.slf4j.Logger)13 Collections (java.util.Collections)12 HashSet (java.util.HashSet)12 Optional (java.util.Optional)12 Arrays (java.util.Arrays)11 File (java.io.File)9 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)7 java.util (java.util)7 Collection (java.util.Collection)7 Pattern (java.util.regex.Pattern)7