001/* 002 * (C) Copyright 2006-2013 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 * Benjamin JALON <bjalon@nuxeo.com> 018 */ 019package org.nuxeo.ecm.automation.core.operations.document; 020 021import java.io.Serializable; 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.automation.OperationException; 029import org.nuxeo.ecm.automation.core.Constants; 030import org.nuxeo.ecm.automation.core.annotations.Context; 031import org.nuxeo.ecm.automation.core.annotations.Operation; 032import org.nuxeo.ecm.automation.core.annotations.OperationMethod; 033import org.nuxeo.ecm.automation.core.annotations.Param; 034import org.nuxeo.ecm.automation.core.collectors.DocumentModelCollector; 035import org.nuxeo.ecm.core.api.CoreSession; 036import org.nuxeo.ecm.core.api.DocumentModel; 037import org.nuxeo.ecm.core.api.model.Property; 038import org.nuxeo.ecm.core.schema.types.Type; 039 040/** 041 * @author <a href="mailto:bjalon@nuxeo.com">Benjamin JALON</a> 042 * @since 5.7 043 */ 044@Operation(id = RemoveEntryOfMultiValuedProperty.ID, category = Constants.CAT_DOCUMENT, label = "Remove Entry Of Multivalued Property", description = "Remove the first entry of the giving value in the multivalued xpath, does nothing if does not exist: <ul<li>if 'is Remove All' is check, all entry instance in the list.</li><li>if not will remove just the first one found</li><ul>", aliases = { "RemoveEntryOfMultivaluedProperty" }) 045public class RemoveEntryOfMultiValuedProperty extends AbstractOperationMultiValuedProperty { 046 047 public static final String ID = "Document.RemoveEntryOfMultivaluedProperty"; 048 049 public static final Log log = LogFactory.getLog(RemoveEntryOfMultiValuedProperty.class); 050 051 @Context 052 protected CoreSession session; 053 054 @Param(name = "xpath") 055 protected String xpath; 056 057 @Param(name = "value") 058 protected Serializable value; 059 060 @Param(name = "save", required = false, values = { "true" }) 061 protected boolean save = true; 062 063 @Param(name = "is Remove All", required = false, values = { "true" }) 064 protected boolean isRemoveAll = true; 065 066 @OperationMethod(collector = DocumentModelCollector.class) 067 public DocumentModel run(DocumentModel doc) throws OperationException { 068 069 Property p = doc.getProperty(xpath); 070 Type type = p.getType(); 071 checkFieldType(type, value); 072 073 List<Serializable> array = Arrays.asList((Serializable[]) p.getValue()); 074 075 if (array == null) { 076 log.info(String.format("Value \"%s\" not found in %s, can't remove it", value, doc.getPathAsString())); 077 return doc; 078 } 079 List<Serializable> list = new ArrayList<Serializable>(array); 080 081 if (!list.contains(value)) { 082 log.info(String.format("Value \"%s\" not found in %s, can't remove it", value, doc.getPathAsString())); 083 return doc; 084 } 085 086 do { 087 list.remove(value); 088 p.setValue(list); 089 } while (list.contains(value) && isRemoveAll); 090 091 if (save) { 092 doc = session.saveDocument(doc); 093 session.save(); 094 } 095 096 return doc; 097 } 098 099}