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 * Miguel Nixo 018 * Ricardo Dias 019 * Bertrand Chauvin 020 */ 021package org.nuxeo.ecm.automation.core.operations.document; 022 023import org.nuxeo.ecm.automation.OperationContext; 024import org.nuxeo.ecm.automation.OperationException; 025import org.nuxeo.ecm.automation.core.Constants; 026import org.nuxeo.ecm.automation.core.annotations.Context; 027import org.nuxeo.ecm.automation.core.annotations.Operation; 028import org.nuxeo.ecm.automation.core.annotations.OperationMethod; 029import org.nuxeo.ecm.automation.core.annotations.Param; 030import org.nuxeo.ecm.automation.core.collectors.DocumentModelCollector; 031import org.nuxeo.ecm.core.api.CoreSession; 032import org.nuxeo.ecm.core.api.DocumentModel; 033import org.nuxeo.ecm.core.api.DocumentModelList; 034import org.nuxeo.ecm.core.api.IdRef; 035import org.nuxeo.ecm.core.api.PathRef; 036 037/** 038 * @since 8.3 039 */ 040@Operation(id = CopySchema.ID, category = Constants.CAT_DOCUMENT, label = "Copy Schema", 041 description = "Copy all the properties from the schema of the source into the input document. Either sourceId or sourcePath parameter should be filled. When both are filled, sourceId will be used. 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.") 042public class CopySchema { 043 044 public static final String ID = "Document.CopySchema"; 045 046 @Context 047 protected OperationContext context; 048 049 @Context 050 protected CoreSession session; 051 052 @Param(name = "schema") 053 protected String schema; 054 055 @Param(name = "sourceId", required = false) 056 protected String sourceId; 057 058 @Param(name = "sourcePath", required = false) 059 protected String sourcePath; 060 061 @Param(name = "save", required = false, values = { "true" }) 062 protected boolean save = true; 063 064 private DocumentModel getDocumentFromIdOrPath() throws OperationException { 065 if (sourceId != null) { 066 return session.getDocument(new IdRef(sourceId)); 067 } else if (sourcePath != null) { 068 return session.getDocument(new PathRef(sourcePath)); 069 } else { 070 throw new OperationException("No document id or path was provided"); 071 } 072 } 073 074 private void copySchemaProperties(DocumentModel source, DocumentModel target) { 075 target.setProperties(schema, source.getProperties(schema)); 076 } 077 078 @OperationMethod(collector = DocumentModelCollector.class) 079 public DocumentModel run(DocumentModel target) throws OperationException { 080 DocumentModel source = getDocumentFromIdOrPath(); 081 copySchemaProperties(source, target); 082 083 target = session.saveDocument(target); 084 if (save) { 085 session.save(); 086 } 087 return target; 088 } 089}