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

import javax.inject.Inject;

import de.ivu.elect.business.wahlvorschlag.entity.ReferendumOption;

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

  @Inject
  protected ReferendumOptionDAO dao;

  @Inject
  protected ReferendumOptionExtendedDAO extendedDao;

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

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

    flushToSave();
  }

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

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

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

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

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