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

import javax.inject.Inject;

import de.ivu.elect.business.wahl.entity.Wahlart;

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

  @Inject
  protected WahlartDAO dao;

  @Inject
  protected WahlartExtendedDAO extendedDao;

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

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

    flushToSave();
  }

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

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

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

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

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