001/* 002 * (C) Copyright 2009 Nuxeo SA (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Thomas Roger 016 */ 017 018package org.nuxeo.ecm.webapp.bulkedit; 019 020import java.io.Serializable; 021import java.util.ArrayList; 022import java.util.Arrays; 023import java.util.List; 024import java.util.Map; 025 026import org.apache.commons.logging.Log; 027import org.apache.commons.logging.LogFactory; 028import org.nuxeo.ecm.core.api.CoreSession; 029import org.nuxeo.ecm.core.api.DocumentModel; 030import org.nuxeo.ecm.platform.forms.layout.api.BuiltinModes; 031import org.nuxeo.ecm.platform.types.Type; 032import org.nuxeo.ecm.platform.types.TypeManager; 033import org.nuxeo.runtime.api.Framework; 034 035/** 036 * Helper used for bulk edit actions 037 * 038 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a> 039 */ 040public class BulkEditHelper { 041 042 private static final Log log = LogFactory.getLog(BulkEditHelper.class); 043 044 /** 045 * @deprecated since 5.7.3. Now in {@link BulkEditService}. 046 */ 047 @Deprecated 048 public static final String BULK_EDIT_PREFIX = "bulkEdit/"; 049 050 /** 051 * @deprecated since 5.7.3. Now in {@link BulkEditService}. 052 */ 053 @Deprecated 054 public static final String CONTEXT_DATA = "contextData"; 055 056 private BulkEditHelper() { 057 // Helper class 058 } 059 060 /** 061 * Returns the common layouts of the {@code docs} for the {@code edit} mode. 062 */ 063 public static List<String> getCommonLayouts(TypeManager typeManager, List<DocumentModel> docs) { 064 return getCommonLayouts(typeManager, docs, BuiltinModes.EDIT); 065 } 066 067 /** 068 * Returns the common layouts of the {@code docs} for the given layout {@code mode}. 069 */ 070 public static List<String> getCommonLayouts(TypeManager typeManager, List<DocumentModel> docs, String mode) { 071 List<String> layouts = null; 072 for (DocumentModel doc : docs) { 073 Type type = typeManager.getType(doc.getType()); 074 List<String> typeLayouts = Arrays.asList(type.getLayouts(mode)); 075 if (layouts == null) { 076 // first document 077 layouts = new ArrayList<String>(); 078 layouts.addAll(typeLayouts); 079 } else { 080 layouts.retainAll(typeLayouts); 081 } 082 } 083 return layouts; 084 } 085 086 /** 087 * Returns the common schemas of the {@code docs}. 088 */ 089 public static List<String> getCommonSchemas(List<DocumentModel> docs) { 090 List<String> schemas = null; 091 for (DocumentModel doc : docs) { 092 List<String> docSchemas = Arrays.asList(doc.getSchemas()); 093 if (schemas == null) { 094 // first document 095 schemas = new ArrayList<String>(); 096 schemas.addAll(docSchemas); 097 } else { 098 schemas.retainAll(docSchemas); 099 } 100 } 101 return schemas; 102 } 103 104 /** 105 * Copy all the marked properties (stored in the ContextData of {@code sourceDoc}) from {@code sourceDoc} to all the 106 * {@code targetDocs}. 107 * 108 * @param session the {@code CoreSession} to use 109 * @param sourceDoc the doc where to get the metadata to copy 110 * @param targetDocs the docs where to set the metadata 111 * @deprecated since 5.7.3. Now in {@link BulkEditService}. 112 */ 113 @Deprecated 114 public static void copyMetadata(CoreSession session, DocumentModel sourceDoc, List<DocumentModel> targetDocs) 115 { 116 Framework.getLocalService(BulkEditService.class).updateDocuments(session, sourceDoc, targetDocs); 117 } 118 119 /** 120 * Extracts the properties to be copied from {@code sourceDoc}. The properties are stored in the ContextData of 121 * {@code sourceDoc}: the key is the xpath property, the value is {@code true} if the property has to be copied, 122 * {@code false otherwise}. 123 * 124 * @deprecated since 5.7.3. Now in {@link BulkEditServiceImpl}. 125 */ 126 @Deprecated 127 protected static List<String> getPropertiesToCopy(DocumentModel sourceDoc) { 128 List<String> propertiesToCopy = new ArrayList<String>(); 129 for (Map.Entry<String, Serializable> entry : sourceDoc.getContextData().entrySet()) { 130 String key = entry.getKey(); 131 if (key.startsWith(BULK_EDIT_PREFIX)) { 132 String[] properties = key.replace(BULK_EDIT_PREFIX, "").split(" "); 133 Serializable value = entry.getValue(); 134 if (value instanceof Boolean && (Boolean) value) { 135 for (String property : properties) { 136 if (!property.startsWith(CONTEXT_DATA)) { 137 propertiesToCopy.add(property); 138 } 139 } 140 } 141 } 142 } 143 return propertiesToCopy; 144 } 145 146}