001/*
002 * (C) Copyright 2009 Nuxeo SA (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 *     Thomas Roger
018 */
019
020package org.nuxeo.ecm.webapp.bulkedit;
021
022import java.util.ArrayList;
023import java.util.Arrays;
024import java.util.List;
025
026import org.apache.commons.logging.Log;
027import org.apache.commons.logging.LogFactory;
028import org.nuxeo.ecm.core.api.DocumentModel;
029import org.nuxeo.ecm.platform.forms.layout.api.BuiltinModes;
030import org.nuxeo.ecm.platform.types.Type;
031import org.nuxeo.ecm.platform.types.TypeManager;
032
033/**
034 * Helper used for bulk edit actions
035 *
036 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
037 */
038public class BulkEditHelper {
039
040    private static final Log log = LogFactory.getLog(BulkEditHelper.class);
041
042    private BulkEditHelper() {
043        // Helper class
044    }
045
046    /**
047     * Returns the common layouts of the {@code docs} for the {@code edit} mode.
048     */
049    public static List<String> getCommonLayouts(TypeManager typeManager, List<DocumentModel> docs) {
050        return getCommonLayouts(typeManager, docs, BuiltinModes.EDIT);
051    }
052
053    /**
054     * Returns the common layouts of the {@code docs} for the given layout {@code mode}.
055     */
056    public static List<String> getCommonLayouts(TypeManager typeManager, List<DocumentModel> docs, String mode) {
057        List<String> layouts = null;
058        for (DocumentModel doc : docs) {
059            Type type = typeManager.getType(doc.getType());
060            List<String> typeLayouts = Arrays.asList(type.getLayouts(mode));
061            if (layouts == null) {
062                // first document
063                layouts = new ArrayList<String>();
064                layouts.addAll(typeLayouts);
065            } else {
066                layouts.retainAll(typeLayouts);
067            }
068        }
069        return layouts;
070    }
071
072    /**
073     * Returns the common schemas of the {@code docs}.
074     */
075    public static List<String> getCommonSchemas(List<DocumentModel> docs) {
076        List<String> schemas = null;
077        for (DocumentModel doc : docs) {
078            List<String> docSchemas = Arrays.asList(doc.getSchemas());
079            if (schemas == null) {
080                // first document
081                schemas = new ArrayList<String>();
082                schemas.addAll(docSchemas);
083            } else {
084                schemas.retainAll(docSchemas);
085            }
086        }
087        return schemas;
088    }
089
090}