001/*
002 * (C) Copyright 2018 Nuxeo (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Funsho David
018 */
019
020package org.nuxeo.ecm.automation.core.operations.services.bulk.validation;
021
022import static org.nuxeo.ecm.automation.core.operations.services.bulk.AutomationBulkAction.AutomationComputation.DOCS_INPUT_TYPE;
023import static org.nuxeo.ecm.automation.core.operations.services.bulk.AutomationBulkAction.AutomationComputation.DOC_INPUT_TYPE;
024import static org.nuxeo.ecm.automation.core.operations.services.bulk.AutomationBulkAction.OPERATION_ID;
025import static org.nuxeo.ecm.automation.core.operations.services.bulk.AutomationBulkAction.OPERATION_PARAMETERS;
026
027import java.util.Arrays;
028import java.util.List;
029
030import org.nuxeo.ecm.automation.AutomationService;
031import org.nuxeo.ecm.automation.OperationNotFoundException;
032import org.nuxeo.ecm.automation.OperationType;
033import org.nuxeo.ecm.core.bulk.AbstractBulkActionValidation;
034import org.nuxeo.ecm.core.bulk.message.BulkCommand;
035import org.nuxeo.runtime.api.Framework;
036
037/**
038 * @since 10.10
039 */
040public class AutomationBulkValidation extends AbstractBulkActionValidation {
041
042    @Override
043    protected List<String> getParametersToValidate() {
044        return Arrays.asList(OPERATION_ID, OPERATION_PARAMETERS);
045    }
046
047    @Override
048    protected void validateCommand(BulkCommand command) throws IllegalArgumentException {
049
050        // Check for operationParameters
051        validateMap(OPERATION_PARAMETERS, command);
052
053        // Check for operation id if it exists, and if its input type is compatible
054        validateString(OPERATION_ID, command);
055        String operationId = command.getParam(OPERATION_ID);
056        try {
057            OperationType op = Framework.getService(AutomationService.class).getOperation(operationId);
058            String inputType = op.getInputType();
059            if (inputType != null && !DOC_INPUT_TYPE.equals(inputType) && !DOCS_INPUT_TYPE.equals(inputType)) {
060                throw new IllegalArgumentException(
061                        "Unsupported operation input type : " + inputType + " in command: " + command);
062            }
063        } catch (OperationNotFoundException e) {
064            throw new IllegalArgumentException("Unknown operation id " + operationId + " in command: " + command);
065        }
066
067    }
068
069}