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

import javax.inject.Inject;

import de.ivu.elect.business.konfiguration.entity.Konfiguration;

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

  @Inject
  protected KonfigurationDAO dao;

  @Inject
  protected KonfigurationExtendedDAO extendedDao;

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

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

    flushToSave();
  }

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

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

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

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

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