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 * Thibaud Arguillere 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; 033 034/** 035 * @since 8.3 036 */ 037@Operation(id = ResetSchema.ID, category = Constants.CAT_DOCUMENT, label = "Reset Schema", 038 description = "Reset all properties for a given schema or xpath. If saveDocument is true, the document is saved. If save is true, the session is saved (setting save to true and saveDocument to false has no effect, the session will not be saved).<p>WARNING: Default values are true for both saveDocument and save, which means the document is saved by default. saveDocument must be set to false when the operation is used in the context of an event that will fail if the document is saved (empty document created, about to create, before modification, ...).</p>") 039public class ResetSchema { 040 041 public static final String ID = "Document.ResetSchema"; 042 043 @Context 044 protected OperationContext context; 045 046 @Context 047 protected CoreSession session; 048 049 @Param(name = "schema", required = false) 050 protected String schema; 051 052 @Param(name = "xpath", required = false) 053 protected String xpath; 054 055 @Param(name = "save", required = false, values = { "true" }) 056 protected boolean save = true; 057 058 @Param(name = "saveDocument", required = false, values = { "true" }) 059 protected boolean saveDocument = true; 060 061 private void resetSchemaProperties(DocumentModel target) throws OperationException { 062 if (xpath != null) { 063 target.setPropertyValue(xpath, null); 064 } else if (schema != null) { 065 for (String key : target.getProperties(schema).keySet()) { 066 target.setProperty(schema, key, null); 067 } 068 } else { 069 throw new OperationException("No schema or xpath was provided"); 070 } 071 } 072 073 @OperationMethod(collector = DocumentModelCollector.class) 074 public DocumentModel run(DocumentModel target) throws OperationException { 075 resetSchemaProperties(target); 076 if (saveDocument) { 077 target = session.saveDocument(target); 078 if (save) { 079 session.save(); 080 } 081 } 082 return target; 083 } 084}