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.core.bulk.validation;
021
022import static org.nuxeo.ecm.core.bulk.action.SetPropertiesAction.PARAM_DISABLE_AUDIT;
023import static org.nuxeo.ecm.core.bulk.action.SetPropertiesAction.PARAM_VERSIONING_OPTION;
024
025import java.util.Arrays;
026import java.util.HashSet;
027import java.util.List;
028import java.util.Set;
029import java.util.stream.Stream;
030
031import org.nuxeo.ecm.core.api.VersioningOption;
032import org.nuxeo.ecm.core.bulk.AbstractBulkActionValidation;
033import org.nuxeo.ecm.core.bulk.message.BulkCommand;
034
035/**
036 * @since 10.10
037 */
038public class SetPropertiesValidation extends AbstractBulkActionValidation {
039
040    @Override
041    public void validate(BulkCommand command) throws IllegalArgumentException {
042
043        Set<String> params = new HashSet<>(command.getParams().keySet());
044        params.removeAll(getParametersToValidate());
045        // Check for unknown xpath
046        for (String param : params) {
047            validateXpath(null, param, command);
048        }
049        validateCommand(command);
050
051    }
052
053    @Override
054    protected List<String> getParametersToValidate() {
055        return Arrays.asList(PARAM_DISABLE_AUDIT, PARAM_VERSIONING_OPTION);
056    }
057
058    @Override
059    protected void validateCommand(BulkCommand command) throws IllegalArgumentException {
060
061        // Check for disableAudit parameter
062        validateBoolean(PARAM_DISABLE_AUDIT, command);
063
064        // Check for versioningOption parameter
065        validateString(PARAM_VERSIONING_OPTION, command);
066        String versioningOption = command.getParam(PARAM_VERSIONING_OPTION);
067        if (versioningOption != null
068                && Stream.of(VersioningOption.values()).noneMatch(op -> versioningOption.equals(op.name()))) {
069            throw new IllegalArgumentException(invalidParameterMessage(PARAM_VERSIONING_OPTION, command));
070        }
071    }
072}