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

import javax.inject.Inject;

import de.ivu.elect.business.email.entity.EmailVorlage;

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

  @Inject
  protected EmailVorlageDAO dao;

  @Inject
  protected EmailVorlageExtendedDAO extendedDao;

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

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

    flushToSave();
  }

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

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

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

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

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