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.platform.csv.export.validation;
021
022import static org.nuxeo.ecm.core.bulk.action.computation.SortBlob.SORT_PARAMETER;
023import static org.nuxeo.ecm.core.bulk.action.computation.ZipBlob.ZIP_PARAMETER;
024import static org.nuxeo.ecm.platform.csv.export.computation.CSVProjectionComputation.PARAM_LANG;
025import static org.nuxeo.ecm.platform.csv.export.computation.CSVProjectionComputation.PARAM_SCHEMAS;
026import static org.nuxeo.ecm.platform.csv.export.computation.CSVProjectionComputation.PARAM_XPATHS;
027
028import java.io.Serializable;
029import java.util.Arrays;
030import java.util.List;
031import java.util.Locale;
032
033import org.nuxeo.ecm.core.bulk.AbstractBulkActionValidation;
034import org.nuxeo.ecm.core.bulk.message.BulkCommand;
035
036/**
037 * @since 10.10
038 */
039public class CSVExportValidation extends AbstractBulkActionValidation {
040
041    @Override
042    protected List<String> getParametersToValidate() {
043        return Arrays.asList(SORT_PARAMETER, ZIP_PARAMETER, PARAM_LANG, PARAM_SCHEMAS, PARAM_XPATHS);
044    }
045
046    @Override
047    protected void validateCommand(BulkCommand command) throws IllegalArgumentException {
048
049        // Check sort and zip parameters
050        validateBoolean(SORT_PARAMETER, command);
051        validateBoolean(ZIP_PARAMETER, command);
052
053        // Check for lang parameter and if it is a known language
054        validateString(PARAM_LANG, command);
055        String lang = command.getParam(PARAM_LANG);
056        if (lang != null && !Arrays.asList(Locale.getAvailableLocales()).contains(Locale.forLanguageTag(lang))) {
057            throw new IllegalArgumentException(invalidParameterMessage(PARAM_LANG, command));
058        }
059
060        // Check schemas parameter and if they exists
061        validateList(PARAM_SCHEMAS, command);
062        List<?> schemas = command.getParam(PARAM_SCHEMAS);
063        if (schemas != null) {
064            for (Object schema : schemas) {
065                validateSchema(PARAM_SCHEMAS, (Serializable) schema, command);
066            }
067        }
068
069        // Check xpaths parameter and if they exists
070        validateList(PARAM_XPATHS, command);
071        List<?> xpaths = command.getParam(PARAM_XPATHS);
072        if (xpaths != null) {
073            for (Object xpath : xpaths) {
074                validateXpath(PARAM_XPATHS, (Serializable) xpath, command);
075            }
076        }
077    }
078}