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

import javax.inject.Inject;

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

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

  @Inject
  protected EmailLayoutDAO dao;

  @Inject
  protected EmailLayoutExtendedDAO extendedDao;

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

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

    flushToSave();
  }

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

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

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

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

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