001/*
002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Mariana Cedica
016 */
017
018package org.nuxeo.ecm.platform.routing.dm.operation;
019
020import org.apache.commons.logging.Log;
021import org.apache.commons.logging.LogFactory;
022import org.nuxeo.ecm.automation.OperationContext;
023import org.nuxeo.ecm.automation.core.annotations.Context;
024import org.nuxeo.ecm.automation.core.annotations.Operation;
025import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
026import org.nuxeo.ecm.automation.core.annotations.Param;
027import org.nuxeo.ecm.core.api.DocumentModel;
028import org.nuxeo.ecm.core.api.NuxeoException;
029import org.nuxeo.ecm.platform.routing.api.DocumentRoutingConstants;
030import org.nuxeo.ecm.platform.routing.dm.api.RoutingTaskConstants.EvaluationOperators;
031
032/***
033 * Evaluates the condition specified by the parameters against the input document. Supports only Integer and String
034 * parameters
035 *
036 * @author mcedica
037 * @deprecated since 5.9.2 - Use only routes of type 'graph'
038 */
039@Deprecated
040@Operation(id = EvaluateCondition.ID, category = DocumentRoutingConstants.OPERATION_CATEGORY_ROUTING_NAME, label = "Set Task Done", description = "Set the task as done.", addToStudio = false)
041public class EvaluateCondition extends AbstractTaskStepOperation {
042
043    public final static String ID = "Document.Routing.EvaluateCondition";
044
045    private Log log = LogFactory.getLog(EvaluateCondition.class);
046
047    @Context
048    protected OperationContext context;
049
050    @Param(name = "subject")
051    protected String subject;
052
053    @Param(name = "operator")
054    protected String operator;
055
056    @Param(name = "value")
057    protected String value;
058
059    @OperationMethod
060    public void evaluateCondition(DocumentModel doc) {
061        int result = 0;
062        Long longValue;
063        Object subjectValue = getPropertyValue(doc, subject);
064        if (subjectValue instanceof Long) {
065            try {
066                longValue = Long.parseLong(value);
067            } catch (NumberFormatException e) {
068                log.error("Invalid long value");
069                throw new NuxeoException(e);
070            }
071            result = ((Long) subjectValue).compareTo(longValue);
072        }
073        if (subjectValue instanceof String) {
074            result = ((String) subjectValue).compareTo(value);
075        }
076
077        if ((result == 0 && EvaluationOperators.equal.name().equals(operator))
078                || (result != 0 && EvaluationOperators.not_equal.name().equals(operator))
079                || (result < 0 && EvaluationOperators.less_than.name().equals(operator))
080                || (result > 0 && EvaluationOperators.greater_than.name().equals(operator))
081                || (result <= 0 && EvaluationOperators.less_or_equal_than.name().equals(operator))
082                || (result >= 0 && EvaluationOperators.greater_or_equal_than.name().equals(operator))) {
083            context.put("nextStepPos", "1");
084        } else {
085            context.put("nextStepPos", "2");
086        }
087    }
088
089    @SuppressWarnings("unchecked")
090    protected <T> T getPropertyValue(DocumentModel doc, String propertyName) {
091        return (T) doc.getPropertyValue(propertyName);
092    }
093
094}