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