Search in sources :

Example 1 with Actions

use of org.talend.dataprep.api.preparation.Actions in project data-prep by Talend.

the class ActionMetadataTestUtils method parseParameters.

/**
 * Parse the given input stream into a parameter map.
 *
 * @param input the parameters input stream.
 * @return the parsed parameters.
 * @throws IOException if an error occurs.
 */
public static Map<String, String> parseParameters(InputStream input) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new MixedContentMapModule());
    mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    List<Action> parsedAction = ((Actions) mapper.reader(Actions.class).readValue(input)).getActions();
    return parsedAction.get(0).getParameters();
}
Also used : Action(org.talend.dataprep.api.preparation.Action) MixedContentMapModule(org.talend.dataprep.api.preparation.json.MixedContentMapModule) Actions(org.talend.dataprep.api.preparation.Actions) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with Actions

use of org.talend.dataprep.api.preparation.Actions in project data-prep by Talend.

the class ActionParser method parse.

/**
 * Return the parsed actions ready to be run.
 *
 * @param actions the actions to be parsed as string.
 * @return the parsed actions.
 * @throws IllegalArgumentException if <code>actions</code> is null.
 */
public List<RunnableAction> parse(String actions) {
    if (actions == null) {
        // Actions cannot be null (but can be empty string for no op actions).
        throw new IllegalArgumentException("Actions parameter can not be null.");
    }
    if (StringUtils.isEmpty(actions)) {
        return Collections.emptyList();
    }
    try {
        // Parse action JSON
        final Actions parsedActions = mapper.reader(Actions.class).readValue(actions);
        final List<Action> allActions = parsedActions.getActions();
        // Create closures from parsed actions
        final List<RunnableAction> builtActions = new ArrayList<>(allActions.size() + 1);
        // 
        allActions.stream().filter(// 
        parsedAction -> parsedAction != null && parsedAction.getName() != null).forEach(parsedAction -> {
            String actionNameLowerCase = parsedAction.getName().toLowerCase();
            final ActionDefinition metadata = actionRegistry.get(actionNameLowerCase);
            builtActions.add(factory.create(metadata, parsedAction.getParameters()));
        });
        // all set: wraps everything and return to caller
        return builtActions;
    } catch (TalendRuntimeException tpe) {
        // leave TDPException as is
        throw tpe;
    } catch (Exception e) {
        throw new TalendRuntimeException(BaseErrorCodes.UNABLE_TO_PARSE_JSON, e);
    }
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) ActionRegistry(org.talend.dataprep.transformation.pipeline.ActionRegistry) ActionFactory(org.talend.dataprep.transformation.actions.common.ActionFactory) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Actions(org.talend.dataprep.api.preparation.Actions) ArrayList(java.util.ArrayList) List(java.util.List) ActionDefinition(org.talend.dataprep.api.action.ActionDefinition) BaseErrorCodes(org.talend.dataprep.BaseErrorCodes) Action(org.talend.dataprep.api.preparation.Action) Collections(java.util.Collections) TalendRuntimeException(org.talend.daikon.exception.TalendRuntimeException) RunnableAction(org.talend.dataprep.transformation.actions.common.RunnableAction) TalendRuntimeException(org.talend.daikon.exception.TalendRuntimeException) Action(org.talend.dataprep.api.preparation.Action) RunnableAction(org.talend.dataprep.transformation.actions.common.RunnableAction) Actions(org.talend.dataprep.api.preparation.Actions) RunnableAction(org.talend.dataprep.transformation.actions.common.RunnableAction) ArrayList(java.util.ArrayList) ActionDefinition(org.talend.dataprep.api.action.ActionDefinition) TalendRuntimeException(org.talend.daikon.exception.TalendRuntimeException)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Action (org.talend.dataprep.api.preparation.Action)2 Actions (org.talend.dataprep.api.preparation.Actions)2 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 List (java.util.List)1 StringUtils (org.apache.commons.lang.StringUtils)1 TalendRuntimeException (org.talend.daikon.exception.TalendRuntimeException)1 BaseErrorCodes (org.talend.dataprep.BaseErrorCodes)1 ActionDefinition (org.talend.dataprep.api.action.ActionDefinition)1 MixedContentMapModule (org.talend.dataprep.api.preparation.json.MixedContentMapModule)1 ActionFactory (org.talend.dataprep.transformation.actions.common.ActionFactory)1 RunnableAction (org.talend.dataprep.transformation.actions.common.RunnableAction)1 ActionRegistry (org.talend.dataprep.transformation.pipeline.ActionRegistry)1