001/*
002 * (C) Copyright 2012 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 *     Mariana Cedica
018 */
019
020package org.nuxeo.ecm.platform.routing.dm.operation;
021
022import org.apache.commons.logging.Log;
023import org.apache.commons.logging.LogFactory;
024import org.nuxeo.ecm.automation.OperationContext;
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.core.api.DocumentModel;
030import org.nuxeo.ecm.core.api.NuxeoException;
031import org.nuxeo.ecm.platform.routing.api.DocumentRoutingConstants;
032import org.nuxeo.ecm.platform.routing.dm.api.RoutingTaskConstants.EvaluationOperators;
033
034/***
035 * Evaluates the condition specified by the parameters against the input document. Supports only Integer and String
036 * parameters
037 *
038 * @author mcedica
039 * @deprecated since 5.9.2 - Use only routes of type 'graph'
040 */
041@Deprecated
042@Operation(id = EvaluateCondition.ID, category = DocumentRoutingConstants.OPERATION_CATEGORY_ROUTING_NAME, label = "Set Task Done", description = "Set the task as done.", addToStudio = false)
043public class EvaluateCondition extends AbstractTaskStepOperation {
044
045    public final static String ID = "Document.Routing.EvaluateCondition";
046
047    private Log log = LogFactory.getLog(EvaluateCondition.class);
048
049    @Context
050    protected OperationContext context;
051
052    @Param(name = "subject")
053    protected String subject;
054
055    @Param(name = "operator")
056    protected String operator;
057
058    @Param(name = "value")
059    protected String value;
060
061    @OperationMethod
062    public void evaluateCondition(DocumentModel doc) {
063        int result = 0;
064        Long longValue;
065        Object subjectValue = getPropertyValue(doc, subject);
066        if (subjectValue instanceof Long) {
067            try {
068                longValue = Long.parseLong(value);
069            } catch (NumberFormatException e) {
070                log.error("Invalid long value");
071                throw new NuxeoException(e);
072            }
073            result = ((Long) subjectValue).compareTo(longValue);
074        }
075        if (subjectValue instanceof String) {
076            result = ((String) subjectValue).compareTo(value);
077        }
078
079        if ((result == 0 && EvaluationOperators.equal.name().equals(operator))
080                || (result != 0 && EvaluationOperators.not_equal.name().equals(operator))
081                || (result < 0 && EvaluationOperators.less_than.name().equals(operator))
082                || (result > 0 && EvaluationOperators.greater_than.name().equals(operator))
083                || (result <= 0 && EvaluationOperators.less_or_equal_than.name().equals(operator))
084                || (result >= 0 && EvaluationOperators.greater_or_equal_than.name().equals(operator))) {
085            context.put("nextStepPos", "1");
086        } else {
087            context.put("nextStepPos", "2");
088        }
089    }
090
091    @SuppressWarnings("unchecked")
092    protected <T> T getPropertyValue(DocumentModel doc, String propertyName) {
093        return (T) doc.getPropertyValue(propertyName);
094    }
095
096}