001/*
002 * (C) Copyright 2006-2014 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 *     bstefanescu
018 */
019package org.nuxeo.connect.update.task.standalone.commands;
020
021import java.util.HashMap;
022import java.util.Map;
023
024import org.w3c.dom.Element;
025
026import org.nuxeo.connect.update.PackageException;
027import org.nuxeo.connect.update.PackageUpdateService;
028import org.nuxeo.connect.update.ValidationStatus;
029import org.nuxeo.connect.update.task.Command;
030import org.nuxeo.connect.update.task.Task;
031import org.nuxeo.connect.update.task.guards.Guard;
032
033/**
034 * All commands have 2 attributes: fail and ignore which are EL expressions.
035 * <p>
036 * If ignore is defined and evaluated to true then the command will be ignored (null is returned as the inverse command)
037 * If fail is defined and evaluated to true then the validation fails.
038 * <p>
039 * Commands extending this class must implement the {@link #doRun} and {@link #doValidate} methods instead of the one in
040 * the interface. These methods are first testing for ignore and fail guards and then if needed delegated to the doXXX
041 * method versions.
042 *
043 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
044 */
045public abstract class AbstractCommand implements Command {
046
047    /**
048     * List of files which must never be deleted at runtime.
049     *
050     * @since 5.5
051     */
052    protected final String id;
053
054    protected final Map<String, Object> guardVars;
055
056    protected String fail;
057
058    protected String ignore;
059
060    protected AbstractCommand(String id) {
061        this.id = id;
062        guardVars = new HashMap<>();
063    }
064
065    public AbstractCommand(AbstractCommand command) {
066        this.id = command.id;
067        guardVars = command.guardVars;
068    }
069
070    @Override
071    public void setPackageUpdateService(PackageUpdateService packageUpdateService) {
072        guardVars.put("packageUpdateService", packageUpdateService);
073    }
074
075    @Override
076    public boolean isPostInstall() {
077        return false;
078    }
079
080    /**
081     * Override to implement command actions
082     *
083     * @param task
084     * @param prefs
085     * @return Rollback command
086     * @throws PackageException
087     */
088    protected abstract Command doRun(Task task, Map<String, String> prefs) throws PackageException;
089
090    /**
091     * Override to implement validation.
092     *
093     * @param task The task being validated
094     * @param status Use {@link ValidationStatus#addError(String)} or {@link ValidationStatus#addWarning(String)} to
095     *            provide validation error/warning messages
096     * @throws PackageException
097     */
098    protected abstract void doValidate(Task task, ValidationStatus status) throws PackageException;
099
100    @Override
101    public void validate(Task task, ValidationStatus status) throws PackageException {
102        if (fail != null) {
103            if (new Guard(fail).evaluate(guardVars)) {
104                status.addError("Guard failed for command " + getId() + ": " + fail);
105            }
106        }
107        doValidate(task, status);
108    }
109
110    @Override
111    public Command run(Task task, Map<String, String> prefs) throws PackageException {
112        if (ignore()) {
113            return null;
114        }
115        return doRun(task, prefs);
116    }
117
118    @Override
119    public String getId() {
120        return id;
121    }
122
123    public void setFail(String fail) {
124        this.fail = fail;
125    }
126
127    public void setIgnore(String ignore) {
128        this.ignore = ignore;
129    }
130
131    public boolean ignore() throws PackageException {
132        if (ignore != null) {
133            return new Guard(ignore).evaluate(guardVars);
134        }
135        return false;
136    }
137
138    @Override
139    public void initialize(Element element) throws PackageException {
140        String v = element.getAttribute("fail");
141        if (v.length() > 0) {
142            fail = v;
143        }
144        v = element.getAttribute("ignore");
145        if (v.length() > 0) {
146            ignore = v;
147        }
148        v = element.getAttribute("if");
149        if (v.length() > 0) {
150            ignore = String.format("!(%s)", v);
151        }
152        readFrom(element);
153    }
154
155    /**
156     * Must be implemented to initialize the command arguments from an XML fragment.
157     */
158    public abstract void readFrom(Element element) throws PackageException;
159
160}