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

import javax.inject.Inject;

import de.ivu.elect.business.mandant.entity.Mandant;

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

  @Inject
  protected MandantDAO dao;

  @Inject
  protected MandantExtendedDAO extendedDao;

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

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

    flushToSave();
  }

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

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

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

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

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