package de.ivu.elect.business.system.control;

import org.hibernate.CustomEntityDirtinessStrategy;
import org.hibernate.Session;
import org.hibernate.persister.entity.EntityPersister;

/**
 *  on 08.07.2016.
 */
public class ElectCustomEntityDirtinessStrategy implements CustomEntityDirtinessStrategy {

  @Override
  public boolean canDirtyCheck(Object o, EntityPersister entityPersister, Session session) {
    return true;
  }

  @Override
  public boolean isDirty(Object entity, EntityPersister entityPersister, Session session) {
    return ((EntityDirty)entity).isHibernateDirty();
  }

  @Override
  public void resetDirty(Object entity, EntityPersister entityPersister, Session session) {
    ((EntityDirty)entity).clearDirtyProperties();
  }

  @Override
  public void findDirty(Object entity, EntityPersister entityPersister, Session session, DirtyCheckContext dirtyCheckContext) {
    dirtyCheckContext.doDirtyChecking(
        attributeInformation -> ((EntityDirty) entity).getDirtyProperties().contains(attributeInformation.getName())
    );

  }
}
