Working with Constraint Classes

Constraint classes are used by Constraint definitions. Constraint definitions let you veto the submission of a Process Definition or Chain Definition if it contains Parameters values that violate certain rules.

Note: For more information, see Working with Constraints.

Constraint classes implement Constraint and ConstraintFactory.

Syntax

Copy
package com.redwood.scheduler.custom.constraint;

import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

import com.redwood.scheduler.api.constraint.ConstraintViolatedException;
import com.redwood.scheduler.api.constraint.LOVCollection;
import com.redwood.scheduler.api.constraint.LOVSupport;
import com.redwood.scheduler.api.model.EntityDefinitionConstraintParameterMapping;
import com.redwood.scheduler.api.model.Job;
import com.redwood.scheduler.api.model.JobParameter;
import com.redwood.scheduler.infrastructure.logging.LoggerFactory;
import com.redwood.scheduler.infrastructure.logging.api.Logger;
import com.redwood.scheduler.spi.constraint.Constraint;
import com.redwood.scheduler.api.constraint.ConstraintContext;
import com.redwood.scheduler.spi.constraint.ConstraintFactory;

import com.redwood.scheduler.spi.constraint.ConstraintFactory;

public class SampleConstraint
  implements Constraint, ConstraintFactory
{
  private static final Logger log = LoggerFactory.getRegisteredLogger(RadioConstraint.class,
                                                                      "(C) Copyright 2011-2022 Redwood Technology B.V., Houten, The Netherlands",
                                                                      "$Id$");

  private static final long serialVersionUID = -2920424304798489040L;
  private Map<String, String> parameterMappingsMap;

  @Override
  public Constraint create()
  {
    return new SampleConstraint();
  }

  @Override
  public void execute(ConstraintContext context)
    throws ConstraintViolatedException
  {
    //Logic to validate the constraint
  }

  @Override
  public LOVCollection getLOV(ConstraintContext context, String parameterName, String query, int startAt, int fetchSize)
  {
    //Logic for LOV constraint
  }

  @Override
  public LOVSupport getLOVSupport(ConstraintContext context, String parameterName)
  {
    //Will this contraint return a list of values (LOV)
    //return LOVSupport.NoGetLOV;
    //return
  }

  @Override
  public void validate()
  {
    // nothing to do
  }

  @Override
  public void freeze()
  {
    // TODO Constraint.freeze is an auto-generated method stub

  }

  public Map<String, String> getParameterMappingsMap()
  {
    //Logic for retrieving the parameter mappings
    //return Collections.unmodifiableMap(parameterMappingsMap);
  }

  @Override
  public void setParameterMappings(Iterator<? extends com.redwood.scheduler.api.model.EntityDefinitionConstraintParameterMapping> newParameterMappings)
  {
    parameterMappingsMap = new LinkedHashMap<>();
    if (newParameterMappings == null)
    {
      return;
    }

    while (newParameterMappings.hasNext())
    {
      EntityDefinitionConstraintParameterMapping appliesToParameter = newParameterMappings.next();
      System.out.println("check parameter " + appliesToParameter.getParameterName() + " value:" + appliesToParameter.getValue());
      parameterMappingsMap.put(appliesToParameter.getParameterName(), appliesToParameter.getValue());
    }
  }

  @Override
  public String getDisplayValue(ConstraintContext newContext, String newParameterName, Object newValue)
  {
    // TODO Constraint.getDisplayValue is an auto-generated method stub
    return null;
  }

  @Override
  public void update(ConstraintContext newContext, String newParameterName, String newChangedParameterName)
  {
    //Update the value
  }

  @Override
  public boolean supportsArray()
  {
    // TODO Constraint.supportsArray is an auto-generated method stub
    return false;
  }
}