Example usage for org.apache.commons.collections.functors AndPredicate getInstance

List of usage examples for org.apache.commons.collections.functors AndPredicate getInstance

Introduction

In this page you can find the example usage for org.apache.commons.collections.functors AndPredicate getInstance.

Prototype

public static Predicate getInstance(Predicate predicate1, Predicate predicate2) 

Source Link

Document

Factory to create the predicate.

Usage

From source file:org.openvpms.archetype.rules.doc.DocumentTemplate.java

/**
 * Returns the printer for a given practice organisation.
 *
 * @param location an <em>party.organisationPractice</em> or <em>party.organisationLocation</em>
 * @return the corresponding printer, or {@code null} if none is defined
 *//*from  www.  jav a  2s  .c om*/
public DocumentTemplatePrinter getPrinter(Entity location) {
    Predicate predicate = AndPredicate.getInstance(IsActiveRelationship.isActiveNow(),
            RefEquals.getTargetEquals(location.getObjectReference()));
    EntityRelationship printer = bean.getNodeRelationship("printers", predicate);
    return (printer != null) ? new DocumentTemplatePrinter(printer, service) : null;
}

From source file:org.openvpms.archetype.rules.product.ProductPriceRules.java

/**
 * Returns the service ratio for a product.
 * <p/>/* w  ww.jav a 2 s. co m*/
 * This is a factor that is applied to a product's prices when the product is charged at a particular practice
 * location. It is determined by the <em>entityLink.locationProductType</em> relationship between the location and
 * the product's type.
 *
 * @param product  the product
 * @param location the practice location
 * @return the service ratio. If there is no relationship, returns {@code 1.0}
 */
public BigDecimal getServiceRatio(Product product, Party location) {
    BigDecimal ratio = BigDecimal.ONE;
    IMObjectBean bean = new IMObjectBean(product, service);
    IMObjectReference productType = bean.getNodeSourceObjectRef("type");
    if (productType != null) {
        IMObjectBean locationBean = new IMObjectBean(location, service);
        Predicate predicate = AndPredicate.getInstance(isActiveNow(), getTargetEquals(productType));
        EntityLink link = (EntityLink) locationBean.getValue("serviceRatios", predicate);
        if (link != null) {
            IMObjectBean linkBean = new IMObjectBean(link, service);
            ratio = linkBean.getBigDecimal("ratio", BigDecimal.ONE);
        }
    }
    return ratio;
}

From source file:org.openvpms.archetype.rules.product.ProductRules.java

/**
 * Returns all active <em>entityRelationship.productSupplier</em>
 * relationships for a particular product and supplier.
 *
 * @param product  the product/*from   w  w w .  j  ava2s.  co m*/
 * @param supplier the supplier
 * @return the relationships, wrapped in {@link ProductSupplier} instances
 */
public List<ProductSupplier> getProductSuppliers(Product product, Party supplier) {
    List<ProductSupplier> result = new ArrayList<ProductSupplier>();
    EntityBean bean = new EntityBean(product, service);
    Predicate predicate = AndPredicate.getInstance(isActiveNow(), RefEquals.getTargetEquals(supplier));
    List<EntityRelationship> relationships = bean.getNodeRelationships("suppliers", predicate);
    for (EntityRelationship relationship : relationships) {
        result.add(new ProductSupplier(relationship, service));
    }
    return result;
}

From source file:org.openvpms.archetype.rules.stock.StockRules.java

/**
 * Returns the <em>entityRelationship.productStockLocation</em> for the
 * specified product and stock location.
 *
 * @param product       the product/*  www  . java 2  s.c  o m*/
 * @param stockLocation the stock location
 * @return the corresponding relationship, or {@code null} if none is found
 * @throws ArchetypeServiceException for any archetype service error
 */
protected EntityRelationship getStockRelationship(Product product, Party stockLocation) {
    EntityRelationship result = null;
    EntityBean prodBean = new EntityBean(product, service);
    Predicate predicate = AndPredicate.getInstance(isActiveNow(), RefEquals.getTargetEquals(stockLocation));
    List<EntityRelationship> relationships = prodBean.getNodeRelationships("stockLocations", predicate);
    if (!relationships.isEmpty()) {
        result = relationships.get(0);
    }
    return result;
}

From source file:org.openvpms.archetype.rules.supplier.DeliveryProcessor.java

/**
 * Updates the quantity of a product at a stock location.
 * <p/>//from   w  ww.j  a  v a  2s  .c o m
 * If no <em>entityRelationship.productStockLocation</em> exists for the
 * product and stock  location, one will be created.
 *
 * @param product       the product
 * @param stockLocation the stock location
 * @param quantity      the quantity received
 * @param packageSize   the size of the package
 * @throws ArchetypeServiceException for any archetype service error
 */
private void updateStockQuantity(Product product, Party stockLocation, BigDecimal quantity, int packageSize) {
    EntityBean bean = new EntityBean(product, service);
    if (bean.hasNode("stockLocations")) {
        Predicate predicate = AndPredicate.getInstance(isActiveNow(), RefEquals.getTargetEquals(stockLocation));
        IMObjectRelationship relationship = bean.getNodeRelationship("stockLocations", predicate);
        if (relationship == null) {
            relationship = bean.addRelationship("entityRelationship.productStockLocation", stockLocation);
            toSave.add(product);
            toSave.add(stockLocation);
        } else {
            toSave.add(relationship);
        }
        BigDecimal units = quantity.multiply(BigDecimal.valueOf(packageSize));
        IMObjectBean relBean = new IMObjectBean(relationship, service);
        BigDecimal stockQuantity = relBean.getBigDecimal("quantity");
        stockQuantity = stockQuantity.add(units);
        if (stockQuantity.compareTo(BigDecimal.ZERO) < 0) {
            stockQuantity = BigDecimal.ZERO;
        }
        relBean.setValue("quantity", stockQuantity);
    }
}

From source file:org.openvpms.archetype.rules.supplier.SupplierRules.java

/**
 * Determines if a supplier supplies a particular product.
 *
 * @param supplier the supplier//from  www. j a  va  2s .co m
 * @param product  the product
 * @return {@code true} if {@code supplier} supplies {@code product}; otherwise {@code false}
 */
public boolean isSuppliedBy(Party supplier, Product product) {
    EntityBean bean = new EntityBean(supplier, service);
    Predicate predicate = AndPredicate.getInstance(isActiveNow(), RefEquals.getSourceEquals(product));
    return bean.getNodeRelationship("products", predicate) != null;
}

From source file:org.openvpms.archetype.rules.supplier.SupplierRules.java

/**
 * Returns the <em>entityRelationship.supplierStockLocation*</em> between a supplier and stock location.
 *
 * @param supplier      a <em>party.supplier*</em>
 * @param stockLocation a <em>party.organisationStockLocation</em>
 * @return the supplier's <em>entityRelationship.supplierStockLocation*</em> or {@code null} if none is found
 *///from  w  w w .j ava2s . c  om
public EntityRelationship getSupplierStockLocation(Party supplier, Party stockLocation) {
    EntityBean bean = new EntityBean(supplier, service);
    Predicate active = AndPredicate.getInstance(isActiveNow(), RefEquals.getTargetEquals(stockLocation));
    List<EntityRelationship> relationships = bean.getValues("stockLocations", active, EntityRelationship.class);
    return (!relationships.isEmpty()) ? relationships.get(0) : null;
}

From source file:org.springmodules.validation.valang.parser.ValangParser.java

final public Predicate predicates(Function fieldFunction) throws ParseException {
    Predicate predicate1 = null;//ww  w  .  ja v a  2 s . c  om
    Predicate predicate2 = null;
    boolean andTest = false;
    boolean orTest = false;
    predicate1 = expression(fieldFunction);
    label_3: while (true) {
        switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
        case AND:
        case OR:
            ;
            break;
        default:
            jj_la1[4] = jj_gen;
            break label_3;
        }
        switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
        case AND:
            jj_consume_token(AND);
            andTest = true;
            break;
        case OR:
            jj_consume_token(OR);
            orTest = true;
            break;
        default:
            jj_la1[5] = jj_gen;
            jj_consume_token(-1);
            throw new ParseException();
        }
        predicate2 = expression(fieldFunction);
        if (andTest) {
            predicate1 = AndPredicate.getInstance(predicate1, predicate2);
            andTest = false;
        } else if (orTest) {
            predicate1 = OrPredicate.getInstance(predicate1, predicate2);
            orTest = false;
        }
    }
    {
        if (true)
            return predicate1;
    }
    throw new Error("Missing return statement in function");
}

From source file:org.springmodules.validation.valang.predicates.BetweenTestPredicate.java

/**
 * <p>The evaluate method takes the result of both functions and tests with the operator.
 *
 * @param   target      The target bean.
 * @return  boolean      Whether or not the test passed.
 *///from   w  ww . j  a  v a  2 s. c  om
public boolean evaluate(Object target) {
    // constructor checks that left function is never null
    Object leftValue = getLeftFunction().getResult(target);
    Object rightValue = (getRightFunction() != null ? getRightFunction().getResult(target) : null);

    Object[] array = getArray(rightValue);

    Predicate predicate1 = new GreaterThanOrEqualTestPredicate(new LiteralFunction(leftValue),
            Operator.GREATER_THAN_OR_EQUAL, (Function) array[0], getLine(), getColumn());
    Predicate predicate2 = new LessThanOrEqualTestPredicate(new LiteralFunction(leftValue),
            Operator.LESS_THAN_OR_EQUAL, (Function) array[1], getLine(), getColumn());

    return AndPredicate.getInstance(predicate1, predicate2).evaluate(target);
}

From source file:org.springmodules.validation.valang.predicates.GenericTestPredicate.java

/**
 * <p>The evaluate method takes the result of both functions and tests with the operator.
 *
 * @param target the target bean/*from  w  w w.  ja v  a2s  . c  o  m*/
 * @return the result of the test
 */
public boolean evaluate(Object target) {
    Object leftValue = getLeftFunction().getResult(target);
    Object rightValue = null;
    boolean dates = false;
    boolean numbers = false;

    if (getRightFunction() != null) {
        rightValue = getRightFunction().getResult(target);
    }

    if (leftValue instanceof Number) {
        leftValue = new BigDecimal(leftValue.toString());
    }
    if (rightValue instanceof Number) {
        rightValue = new BigDecimal(rightValue.toString());
    }

    dates = leftValue instanceof Date && rightValue instanceof Date;
    numbers = leftValue instanceof BigDecimal && rightValue instanceof BigDecimal;

    if (getOperator() instanceof Operator.NullOperator) {
        return leftValue == null;
    } else if (getOperator() instanceof Operator.NotNullOperator) {
        return leftValue != null;
    } else if (getOperator() instanceof Operator.EqualsOperator) {
        if (leftValue instanceof BigDecimal && rightValue instanceof BigDecimal) {
            return ((BigDecimal) leftValue).compareTo((BigDecimal) rightValue) == 0;
        } else if (dates) {
            return ((Date) leftValue).getTime() == ((Date) rightValue).getTime();
        } else {
            return leftValue.equals(rightValue);
        }
    } else if (getOperator() instanceof Operator.NotEqualsOperator) {
        if (leftValue instanceof BigDecimal && rightValue instanceof BigDecimal) {
            return ((BigDecimal) leftValue).compareTo((BigDecimal) rightValue) != 0;
        } else if (dates) {
            return ((Date) leftValue).getTime() != ((Date) rightValue).getTime();
        } else {
            return !leftValue.equals(rightValue);
        }
    } else if (getOperator() instanceof Operator.LessThanOperator) {
        if (dates) {
            return ((Date) leftValue).getTime() < ((Date) rightValue).getTime();
        } else if (numbers) {
            return ((BigDecimal) leftValue).compareTo((BigDecimal) rightValue) < 0;
        } else {
            throw new ValangException("< operator only supports two date or two number values!", getLine(),
                    getColumn());
        }
    } else if (getOperator() instanceof Operator.LessThanOrEqualOperator) {
        if (dates) {
            return ((Date) leftValue).getTime() <= ((Date) rightValue).getTime();
        } else if (numbers) {
            return ((BigDecimal) leftValue).compareTo((BigDecimal) rightValue) <= 0;
        } else {
            throw new ValangException("<= operator only supports two date or two number values!", getLine(),
                    getColumn());
        }
    } else if (getOperator() instanceof Operator.MoreThanOperator) {
        if (dates) {
            return ((Date) leftValue).getTime() > ((Date) rightValue).getTime();
        } else if (numbers) {
            return ((BigDecimal) leftValue).compareTo((BigDecimal) rightValue) > 0;
        } else {
            throw new ValangException("> operator only supports two date or two number values!", getLine(),
                    getColumn());
        }
    } else if (getOperator() instanceof Operator.MoreThanOrEqualOperator) {
        if (dates) {
            return ((Date) leftValue).getTime() >= ((Date) rightValue).getTime();
        } else if (numbers) {
            return ((BigDecimal) leftValue).compareTo((BigDecimal) rightValue) >= 0;
        } else {
            throw new IllegalArgumentException(">= operator only supports two date or two number values!");
        }
    } else if (getOperator() instanceof Operator.InOperator) {
        Collection predicates = new ArrayList();
        for (Iterator iter = getIterator(rightValue); iter.hasNext();) {
            Object o = iter.next();
            if (o instanceof Function) {
                predicates.add(getPredicate(new LiteralFunction(leftValue), OperatorConstants.EQUALS_OPERATOR,
                        (Function) o, getLine(), getColumn()));
            } else {
                predicates.add(getPredicate(new LiteralFunction(leftValue), OperatorConstants.EQUALS_OPERATOR,
                        new LiteralFunction(o), getLine(), getColumn()));
            }
        }
        if (predicates.isEmpty()) {
            throw new IllegalStateException("IN expression contains no elements!");
        } else if (predicates.size() == 1) {
            predicates.add(FalsePredicate.getInstance());
        }
        return AnyPredicate.getInstance(predicates).evaluate(target);
    } else if (getOperator() instanceof Operator.NotInOperator) {
        Collection predicates = new ArrayList();
        for (Iterator iter = getIterator(rightValue); iter.hasNext();) {
            Object o = iter.next();
            if (o instanceof Function) {
                predicates.add(getPredicate(new LiteralFunction(leftValue), OperatorConstants.EQUALS_OPERATOR,
                        (Function) o, getLine(), getColumn()));
            } else {
                predicates.add(getPredicate(new LiteralFunction(leftValue), OperatorConstants.EQUALS_OPERATOR,
                        new LiteralFunction(o), getLine(), getColumn()));
            }
        }
        if (predicates.isEmpty()) {
            throw new IllegalStateException("NOT IN expression contains no elements!");
        } else if (predicates.size() == 1) {
            predicates.add(FalsePredicate.getInstance());
        }
        return !AnyPredicate.getInstance(predicates).evaluate(target);
    } else if (getOperator() instanceof Operator.BetweenOperator) {
        Object[] array = getArray(rightValue);
        Predicate predicate1 = getPredicate(new LiteralFunction(leftValue),
                OperatorConstants.MORE_THAN_OR_EQUAL_OPERATOR, (Function) array[0], getLine(), getColumn());
        Predicate predicate2 = getPredicate(new LiteralFunction(leftValue),
                OperatorConstants.LESS_THAN_OR_EQUAL_OPERATOR, (Function) array[1], getLine(), getColumn());
        return AndPredicate.getInstance(predicate1, predicate2).evaluate(target);
    } else if (getOperator() instanceof Operator.NotBetweenOperator) {
        Object[] array = getArray(rightValue);
        Predicate predicate1 = getPredicate(new LiteralFunction(leftValue),
                OperatorConstants.MORE_THAN_OR_EQUAL_OPERATOR, (Function) array[0], getLine(), getColumn());
        Predicate predicate2 = getPredicate(new LiteralFunction(leftValue),
                OperatorConstants.LESS_THAN_OR_EQUAL_OPERATOR, (Function) array[1], getLine(), getColumn());
        return !AndPredicate.getInstance(predicate1, predicate2).evaluate(target);
    } else if (getOperator() instanceof Operator.HasLengthOperator) {
        return StringUtils.hasLength(leftValue != null ? leftValue.toString() : null);
    } else if (getOperator() instanceof Operator.HasNoLengthOperator) {
        return !StringUtils.hasLength(leftValue != null ? leftValue.toString() : null);
    } else if (getOperator() instanceof Operator.HasTextOperator) {
        return StringUtils.hasText(leftValue != null ? leftValue.toString() : null);
    } else if (getOperator() instanceof Operator.HasNoTextOperator) {
        return !StringUtils.hasText(leftValue != null ? leftValue.toString() : null);
    } else if (getOperator() instanceof Operator.IsBlankOperator) {
        return isBlank(leftValue != null ? leftValue.toString() : null);
    } else if (getOperator() instanceof Operator.IsNotBlankOperator) {
        return !isBlank(leftValue != null ? leftValue.toString() : null);
    } else if (getOperator() instanceof Operator.IsWordOperator) {
        return isWord(leftValue != null ? leftValue.toString() : null);
    } else if (getOperator() instanceof Operator.IsNotWordOperator) {
        return !isWord(leftValue != null ? leftValue.toString() : null);
    } else if (getOperator() instanceof Operator.IsLowerCaseOperator) {
        return isLowerCase(leftValue != null ? leftValue.toString() : null);
    } else if (getOperator() instanceof Operator.IsNotLowerCaseOperator) {
        return !isLowerCase(leftValue != null ? leftValue.toString() : null);
    } else if (getOperator() instanceof Operator.IsUpperCaseOperator) {
        return isUpperCase(leftValue != null ? leftValue.toString() : null);
    } else if (getOperator() instanceof Operator.IsNotUpperCaseOperator) {
        return !isUpperCase(leftValue != null ? leftValue.toString() : null);
    }

    throw new IllegalStateException(
            "Operator class [" + getOperator().getClass().getName() + "] not supported!");
}