001/* 002 * (C) Copyright 2016 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 * Ricardo Dias 018 */ 019 020package org.nuxeo.ecm.automation.core.operations.document; 021 022import java.io.Serializable; 023import java.util.ArrayList; 024import java.util.Arrays; 025import java.util.List; 026 027import org.nuxeo.ecm.automation.AutomationService; 028import org.nuxeo.ecm.automation.OperationContext; 029import org.nuxeo.ecm.automation.OperationException; 030import org.nuxeo.ecm.automation.core.Constants; 031import org.nuxeo.ecm.automation.core.annotations.Context; 032import org.nuxeo.ecm.automation.core.annotations.Operation; 033import org.nuxeo.ecm.automation.core.annotations.OperationMethod; 034import org.nuxeo.ecm.automation.core.annotations.Param; 035import org.nuxeo.ecm.automation.core.collectors.DocumentModelCollector; 036import org.nuxeo.ecm.core.api.CoreSession; 037import org.nuxeo.ecm.core.api.DocumentModel; 038import org.nuxeo.ecm.core.api.model.Property; 039import org.nuxeo.ecm.core.api.model.impl.ListProperty; 040import org.nuxeo.ecm.core.schema.types.ListType; 041 042/** 043 * @since 8.3 044 */ 045@Operation(id = RemoveItemFromListProperty.ID, category = Constants.CAT_DOCUMENT, label = "Removes an Entry From a Multivalued Property", description = "This operation removes an entry from a multivalued property, specified using a xpath (e.g.: contract:customers). A specific entry can be removed using its index number. If the index parameter is left empty, all entries in the property are removed. Activating the save parameter forces the changes to be written in database immediately (at the cost of performance loss), otherwise changes made to the document will be written in bulk when the chain succeeds. <p>Save parameter has to be turned off when this operation is used in the context of the empty document created, about to create, before document modification, document modified events.</p>") 046public class RemoveItemFromListProperty { 047 048 public static final String ID = "Document.RemoveItemFromListProperty"; 049 050 @Context 051 protected CoreSession session; 052 053 @Context 054 protected AutomationService service; 055 056 @Context 057 protected OperationContext ctx; 058 059 @Param(name = "xpath") 060 protected String xpath; 061 062 @Param(name = "index", required = false) 063 protected Integer index; 064 065 @Param(name = "save", required = false, values = { "true" }) 066 protected boolean save = true; 067 068 @OperationMethod(collector = DocumentModelCollector.class) 069 public DocumentModel run(DocumentModel doc) throws OperationException { 070 Property property = doc.getProperty(xpath); 071 if (!property.isList()) { 072 throw new OperationException(String.format("Property: %s of type: %s is not supported by this operation", 073 xpath, property.getType().getName())); 074 } 075 076 ListType listType = (ListType) property.getType(); 077 if (listType.isArray()) { 078 removeItemFromArrayProperty(doc, property); 079 } else { 080 removeItemFromListProperty(property); 081 } 082 083 if (save) { 084 doc = session.saveDocument(doc); 085 } 086 return doc; 087 } 088 089 protected void removeItemFromArrayProperty(DocumentModel doc, Property property) { 090 if (index != null) { 091 Serializable[] value = (Serializable[]) property.getValue(); 092 List<Serializable> list = new ArrayList<>(Arrays.asList(value)); 093 list.remove(index.intValue()); 094 doc.setPropertyValue(xpath, (Serializable) list); 095 } else { 096 doc.setPropertyValue(xpath, null); 097 } 098 } 099 100 protected void removeItemFromListProperty(Property property) { 101 ListProperty listProperty = (ListProperty) property; 102 if (index != null) { 103 listProperty.remove(index.intValue()); 104 } else { 105 listProperty.clear(); 106 } 107 } 108 109}