001/* 002 * (C) Copyright 2017 Nuxeo (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 * Gabriel Barata <[email protected]> 018 */ 019package org.nuxeo.template.automation; 020 021import org.nuxeo.ecm.automation.OperationContext; 022import org.nuxeo.ecm.automation.core.Constants; 023import org.nuxeo.ecm.automation.core.annotations.Context; 024import org.nuxeo.ecm.automation.core.annotations.Operation; 025import org.nuxeo.ecm.automation.core.annotations.OperationMethod; 026 027import org.nuxeo.ecm.core.api.DocumentModel; 028import org.nuxeo.runtime.api.Framework; 029import org.nuxeo.template.api.TemplateProcessorService; 030import org.nuxeo.template.api.adapters.TemplateBasedDocument; 031import org.nuxeo.template.api.adapters.TemplateSourceDocument; 032 033import java.util.List; 034 035/** 036 * Operation to detach an bound documents from a template. 037 * 038 * @since 9.1 039 */ 040@Operation(id = DetachTemplateOperation.ID, category = Constants.CAT_CONVERSION, 041 label = "Detach a template", 042 description = "Detach a template from all its bound documents.") 043public class DetachTemplateOperation { 044 045 public static final String ID = "TemplateProcessor.Detach"; 046 047 @Context 048 protected OperationContext ctx; 049 050 @OperationMethod 051 public void run(DocumentModel targetTemplate) { 052 TemplateSourceDocument template = targetTemplate.getAdapter(TemplateSourceDocument.class); 053 TemplateProcessorService tps = Framework.getService(TemplateProcessorService.class); 054 List<TemplateBasedDocument> documents = template.getTemplateBasedDocuments(); 055 documents.forEach(doc -> tps.detachTemplateBasedDocument(doc.getAdaptedDoc(), template.getName(), true)); 056 } 057}