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.io.Serializable;
023import java.util.ArrayList;
024import java.util.Arrays;
025import java.util.List;
026import java.util.Map;
027
028import org.apache.commons.logging.Log;
029import org.apache.commons.logging.LogFactory;
030import org.nuxeo.ecm.core.api.CoreSession;
031import org.nuxeo.ecm.core.api.DocumentModel;
032import org.nuxeo.ecm.platform.forms.layout.api.BuiltinModes;
033import org.nuxeo.ecm.platform.types.Type;
034import org.nuxeo.ecm.platform.types.TypeManager;
035import org.nuxeo.runtime.api.Framework;
036
037/**
038 * Helper used for bulk edit actions
039 *
040 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
041 */
042public class BulkEditHelper {
043
044    private static final Log log = LogFactory.getLog(BulkEditHelper.class);
045
046    /**
047     * @deprecated since 5.7.3. Now in {@link BulkEditService}.
048     */
049    @Deprecated
050    public static final String BULK_EDIT_PREFIX = "bulkEdit/";
051
052    /**
053     * @deprecated since 5.7.3. Now in {@link BulkEditService}.
054     */
055    @Deprecated
056    public static final String CONTEXT_DATA = "contextData";
057
058    private BulkEditHelper() {
059        // Helper class
060    }
061
062    /**
063     * Returns the common layouts of the {@code docs} for the {@code edit} mode.
064     */
065    public static List<String> getCommonLayouts(TypeManager typeManager, List<DocumentModel> docs) {
066        return getCommonLayouts(typeManager, docs, BuiltinModes.EDIT);
067    }
068
069    /**
070     * Returns the common layouts of the {@code docs} for the given layout {@code mode}.
071     */
072    public static List<String> getCommonLayouts(TypeManager typeManager, List<DocumentModel> docs, String mode) {
073        List<String> layouts = null;
074        for (DocumentModel doc : docs) {
075            Type type = typeManager.getType(doc.getType());
076            List<String> typeLayouts = Arrays.asList(type.getLayouts(mode));
077            if (layouts == null) {
078                // first document
079                layouts = new ArrayList<String>();
080                layouts.addAll(typeLayouts);
081            } else {
082                layouts.retainAll(typeLayouts);
083            }
084        }
085        return layouts;
086    }
087
088    /**
089     * Returns the common schemas of the {@code docs}.
090     */
091    public static List<String> getCommonSchemas(List<DocumentModel> docs) {
092        List<String> schemas = null;
093        for (DocumentModel doc : docs) {
094            List<String> docSchemas = Arrays.asList(doc.getSchemas());
095            if (schemas == null) {
096                // first document
097                schemas = new ArrayList<String>();
098                schemas.addAll(docSchemas);
099            } else {
100                schemas.retainAll(docSchemas);
101            }
102        }
103        return schemas;
104    }
105
106    /**
107     * Copy all the marked properties (stored in the ContextData of {@code sourceDoc}) from {@code sourceDoc} to all the
108     * {@code targetDocs}.
109     *
110     * @param session the {@code CoreSession} to use
111     * @param sourceDoc the doc where to get the metadata to copy
112     * @param targetDocs the docs where to set the metadata
113     * @deprecated since 5.7.3. Now in {@link BulkEditService}.
114     */
115    @Deprecated
116    public static void copyMetadata(CoreSession session, DocumentModel sourceDoc, List<DocumentModel> targetDocs)
117            {
118        Framework.getLocalService(BulkEditService.class).updateDocuments(session, sourceDoc, targetDocs);
119    }
120
121    /**
122     * Extracts the properties to be copied from {@code sourceDoc}. The properties are stored in the ContextData of
123     * {@code sourceDoc}: the key is the xpath property, the value is {@code true} if the property has to be copied,
124     * {@code false otherwise}.
125     *
126     * @deprecated since 5.7.3. Now in {@link BulkEditServiceImpl}.
127     */
128    @Deprecated
129    protected static List<String> getPropertiesToCopy(DocumentModel sourceDoc) {
130        List<String> propertiesToCopy = new ArrayList<String>();
131        for (Map.Entry<String, Serializable> entry : sourceDoc.getContextData().entrySet()) {
132            String key = entry.getKey();
133            if (key.startsWith(BULK_EDIT_PREFIX)) {
134                String[] properties = key.replace(BULK_EDIT_PREFIX, "").split(" ");
135                Serializable value = entry.getValue();
136                if (value instanceof Boolean && (Boolean) value) {
137                    for (String property : properties) {
138                        if (!property.startsWith(CONTEXT_DATA)) {
139                            propertiesToCopy.add(property);
140                        }
141                    }
142                }
143            }
144        }
145        return propertiesToCopy;
146    }
147
148}