
package de.ivu.elect.business.kandidatur.boundary;

import javax.inject.Inject;

import de.ivu.elect.business.kandidatur.entity.Kandidatur;

// abgeleitete Klasse mit "@javax.ejb.Stateless" annotieren!
public abstract class AbstractKandidaturEdit {

  @Inject
  protected KandidaturDAO dao;

  @Inject
  protected KandidaturExtendedDAO extendedDao;

  public void delete(Kandidatur entity) {
    dao.remove(getAttachedEntity(entity));
  }

  public void save(Kandidatur entity) {
    validate(entity);

    flushToSave();
  }

  protected void validate(Kandidatur entity) {
    // Do nothing by default.
  }

  protected void flushToSave() {
    if(usesMixedTransactionalContexts()) {
        extendedDao.flushNewTransaction();
    } else {
        extendedDao.flush();
    }
  }

  // ELECT-945
  protected boolean usesMixedTransactionalContexts() {
    return false;
  }

  protected Kandidatur getAttachedEntity(Kandidatur entity) {
    return dao.findByPrimaryKey(entity.getPk());
  }

  // Cf. ELECT-3187 (Änderungen an Entität werden nicht gespeichert)
  protected Kandidatur getAttachedAndRefreshedEntity(Kandidatur entity) {
    return dao.refresh(entity);
  }
}
