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

import java.security.PublicKey;

import javax.ejb.Stateless;
import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.ivu.elect.business.gebietsbaum.entity.Gebiet;
import de.ivu.elect.business.gebietsbaum.entity.NlGebietCustomData;
import de.ivu.elect.nl.x509.NlX509Metadata;

@Stateless
public class NlGebietPublicKeyEdit {

  private static final Logger LOG = LoggerFactory.getLogger(NlGebietPublicKeyEdit.class);

  @Inject
  private transient GebietDAO gebietDAO;

  public boolean saveGebietPublicKey(Gebiet gebietNotAttached, PublicKey publicKey, NlX509Metadata nlX509Metadata) {
    if (gebietNotAttached == null || publicKey == null || nlX509Metadata == null) {
      return false;
    }
    Gebiet gebiet = gebietDAO.attach(gebietNotAttached);
    LOG.info("Save public key: " + gebiet + ", " + publicKey + ", " + nlX509Metadata);
    gebiet.getCustomData(NlGebietCustomData.class).setPublicKey(publicKey, nlX509Metadata);
    return true;
  }
}
