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

import javax.inject.Inject;

import de.ivu.elect.business.wahl.entity.Wahl;
import de.ivu.elect.business.wertelisten.control.WertelistenUpdatedEvent;
import de.ivu.elect.business.wertelisten.entity.QWertelisten;
import de.ivu.elect.business.wertelisten.entity.Wertelisten;
import de.ivu.elect.business.wertelisten.entity.WertelistenArt;

@javax.ejb.Stateless
public class WertelistenEdit extends AbstractWertelistenEdit {

  @Inject
  private WertelistenUpdatedEvent wertelistenUpdatedEvent;

  @Override
  public void save(Wertelisten entity) {
    String oldValue = getOldValue(entity);
    String newValue = entity.getName();

    super.save(entity);

    wertelistenUpdatedEvent.sendChangeEvent(oldValue, newValue, entity.getWertelistenArt(), entity.getWahl());
  }

  private String getOldValue(Wertelisten entity) {
    return (String) extendedDao.getQueryDsl().from(QWertelisten.wertelisten)
        .eq(QWertelisten.wertelisten.id, entity.getPk().getId()).getQuery().select(QWertelisten.wertelisten.name).fetchOne();
  }

  @Override
  public void delete(Wertelisten entity) {
    String oldValue = getOldValue(entity);
    WertelistenArt wertelistenArt = entity.getWertelistenArt();
    Wahl wahl = entity.getWahl();

    super.delete(entity);

    wertelistenUpdatedEvent.sendChangeEvent(oldValue, null, wertelistenArt, wahl);
  }
}
