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.IOException; 023import java.util.List; 024 025import org.nuxeo.ecm.automation.AutomationService; 026import org.nuxeo.ecm.automation.OperationContext; 027import org.nuxeo.ecm.automation.OperationException; 028import org.nuxeo.ecm.automation.core.Constants; 029import org.nuxeo.ecm.automation.core.annotations.Context; 030import org.nuxeo.ecm.automation.core.annotations.Operation; 031import org.nuxeo.ecm.automation.core.annotations.OperationMethod; 032import org.nuxeo.ecm.automation.core.annotations.Param; 033import org.nuxeo.ecm.automation.core.collectors.DocumentModelCollector; 034import org.nuxeo.ecm.core.api.CoreSession; 035import org.nuxeo.ecm.core.api.DocumentModel; 036import org.nuxeo.ecm.core.api.model.Property; 037import org.nuxeo.ecm.core.api.model.impl.ListProperty; 038import org.nuxeo.ecm.core.schema.types.ListType; 039 040/** 041 * @since 8.3 042 */ 043@Operation(id = RemoveItemFromListProperty.ID, category = Constants.CAT_DOCUMENT, label = "Removes an Entry From a Multivalued Complex Property", description = "This operation removes an entry from a multivalued complex 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.", aliases = { "Document.RemoveItemFromListProperty" }) 044public class RemoveItemFromListProperty { 045 046 public static final String ID = "Document.RemoveItemFromListProperty"; 047 048 @Context 049 protected CoreSession session; 050 051 @Context 052 protected AutomationService service; 053 054 @Context 055 protected OperationContext ctx; 056 057 @Param(name = "xpath") 058 protected String xpath; 059 060 @Param(name = "index", required = false) 061 protected Integer index; 062 063 @Param(name = "save", required = false, values = { "true" }) 064 protected boolean save = true; 065 066 @OperationMethod(collector = DocumentModelCollector.class) 067 public DocumentModel run(DocumentModel doc) throws OperationException, IOException { 068 069 if (index != null) { // clear just the specific property 070 Property complexProperty = doc.getProperty(xpath); 071 ListType listType = (ListType) complexProperty.getField().getType(); 072 073 if (!listType.getFieldType().isComplexType() && !listType.isListType()) { 074 throw new OperationException("Property type is not supported by this operation"); 075 } 076 077 ListProperty listProperty = (ListProperty) complexProperty; 078 List<?> propertiesValues = (List<?>) listProperty.getValue(); 079 // remove the desired property 080 propertiesValues.remove(index.intValue()); 081 082 listProperty.clear(); 083 // set the remaining properties 084 listProperty.setValue(propertiesValues); 085 086 } else { // clear all the properties 087 Property complexProperty = doc.getProperty(xpath); 088 ListType listType = (ListType) complexProperty.getField().getType(); 089 090 if (!listType.getFieldType().isComplexType() && !listType.isListType()) { 091 throw new OperationException("Property type is not supported by this operation"); 092 } 093 094 ListProperty listProperty = (ListProperty) complexProperty; 095 listProperty.clear(); 096 } 097 098 doc = session.saveDocument(doc); 099 if (save) { 100 session.save(); 101 } 102 return doc; 103 104 } 105 106}