Class RelationSetter

java.lang.Object
ch.tocco.nice2.entityoperation.api.util.RelationSetter
All Implemented Interfaces:
EntityUpdater

public class RelationSetter extends Object implements EntityUpdater
A class to adjust lookup entity relations depending on other fields pr relations. Example: If the relation "relEvent_status" is set to "archived" the relation "relRegistration.relRegistration_status" should be changed to "attended" but only if the status was "booked" Code: RelationSetter setter = RelationSetter.setRelation("relRegistration.relRegistration_status", "attended") .ifCondition(field("relEvent_status.unique_id").is("archived")) .expectTargetState("booked"); setter.applyOn(entity); Note: The class only support the changing of lookup entities atm
  • Method Details

    • setRelation

      public static RelationSetter setRelation(QueryBuilderFactory qbFactory, String path, String state)
      Note: if you apply the returned instance of this method to an Entity all of the relations will be adjusted
      Parameters:
      path - path to the relation
      state - the unique_id of the lookup entity which should be related to the given path
      Returns:
      a new instance of a RelationSetter
    • ifCondition

      public RelationSetter ifCondition(Condition condition)
      Pass the Condition which will be applied on the entity you want to adjust. If this method gets called multiple times it will connect the Conditions with Conditions.and(ch.tocco.nice2.persist.core.api.qb2.Condition...)
      Parameters:
      condition - the state when the relation should get adjusted
      Returns:
      this
    • expectTargetState

      public RelationSetter expectTargetState(String expectedTargetState)
      Can be used if you only want to adjust a relation if it has a certain state Note: You can add more than one state
      Parameters:
      expectedTargetState - the expected state
      Returns:
      this
    • expectTargetStates

      public RelationSetter expectTargetStates(String... expectedTargetStates)
    • applyOn

      public boolean applyOn(Entity entity)
      Specified by:
      applyOn in interface EntityUpdater
      Parameters:
      entity - the entity to adjust
      Returns:
      true if a relation got adjusted, false otherwise
    • applyOn

      public int applyOn(EntityList list)
      Specified by:
      applyOn in interface EntityUpdater
      Parameters:
      list - the entity list to adjust
      Returns:
      number of adjusted entities