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 * Frédéric Vadon 018 * Ricardo Dias 019 */ 020 021package org.nuxeo.ecm.automation.core.operations.document; 022 023import java.io.IOException; 024import java.util.List; 025 026import org.nuxeo.ecm.automation.AutomationService; 027import org.nuxeo.ecm.automation.OperationContext; 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.automation.core.util.ComplexTypeJSONDecoder; 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.schema.types.ListType; 040 041/** 042 * @since 8.3 043 */ 044@Operation(id = AddItemToListProperty.ID, category = Constants.CAT_DOCUMENT, label = "Adds an Entry Into a Multivalued Complex Property", description = "This operation can add new entries to a multivalued complex property. The xpath parameter is the property that should be updated (e.g.: contract:customers). The value parameter is a String containing the JSON-formatted list of entries to add. E.g.: assuming a Contract document type holding customers, each having a firstName and lastName property: [{\"lastName\":\"Norris\", \"firstName\": \"Chuck\"}, {\"lastName\":\"Lee\", \"firstName\": \"Bruce\"}] . 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 = { 045 "Document.AddItemToListProperty" }) 046public class AddItemToListProperty { 047 048 public static final String ID = "Document.AddItemToListProperty"; 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 = "complexJsonProperties") 063 protected String complexJsonProperties; 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, IOException { 070 Property complexProperty = doc.getProperty(xpath); 071 ListType listType = (ListType) complexProperty.getField().getType(); 072 073 if (!listType.getFieldType().isComplexType()) { 074 throw new OperationException("Property type " + listType.getFieldType().getClass().getName() 075 + " is not supported by this operation"); 076 } 077 078 List<Object> newVals = ComplexTypeJSONDecoder.decodeList(listType, complexJsonProperties); 079 for (Object newVal : newVals) { 080 complexProperty.addValue(newVal); 081 } 082 083 doc = session.saveDocument(doc); 084 if (save) { 085 session.save(); 086 } 087 return doc; 088 } 089 090}