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 */ 020package org.nuxeo.ecm.automation.core.operations.document; 021 022import org.nuxeo.ecm.automation.OperationContext; 023import org.nuxeo.ecm.automation.OperationException; 024import org.nuxeo.ecm.automation.core.Constants; 025import org.nuxeo.ecm.automation.core.annotations.Context; 026import org.nuxeo.ecm.automation.core.annotations.Operation; 027import org.nuxeo.ecm.automation.core.annotations.OperationMethod; 028import org.nuxeo.ecm.automation.core.annotations.Param; 029import org.nuxeo.ecm.automation.core.collectors.DocumentModelCollector; 030import org.nuxeo.ecm.core.api.CoreSession; 031import org.nuxeo.ecm.core.api.DocumentModel; 032import org.nuxeo.ecm.core.api.DocumentModelList; 033 034import java.util.Map; 035 036/** 037 * @since 8.3 038 */ 039@Operation(id = ResetSchema.ID, category = Constants.CAT_DOCUMENT, label = "Reset Schema", 040 description = "Reset all properties for a given schema or xpath. 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.") 041public class ResetSchema { 042 043 public static final String ID = "Document.ResetSchema"; 044 045 @Context 046 protected OperationContext context; 047 048 @Context 049 protected CoreSession session; 050 051 @Param(name = "schema", required = false) 052 protected String schema; 053 054 @Param(name = "xpath", required = false) 055 protected String xpath; 056 057 @Param(name = "save", required = false, values = { "true" }) 058 protected boolean save = true; 059 060 private void resetSchemaProperties(DocumentModel target) throws OperationException { 061 if (xpath != null) { 062 target.setPropertyValue(xpath, null); 063 } else if (schema != null) { 064 for (String key : target.getProperties(schema).keySet()) { 065 target.setProperty(schema, key, null); 066 } 067 } else { 068 throw new OperationException("No schema or xpath was provided"); 069 } 070 } 071 072 @OperationMethod(collector = DocumentModelCollector.class) 073 public DocumentModel run(DocumentModel target) throws OperationException { 074 resetSchemaProperties(target); 075 target = session.saveDocument(target); 076 if (save) { 077 session.save(); 078 } 079 return target; 080 } 081}