/*
 * Copyright (c) 2017 Public Library of Science
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

package org.ambraproject.rhino.config;

import com.google.common.base.Joiner;
import org.ambraproject.rhino.model.Comment;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.LongSerializer;
import org.apache.kafka.common.serialization.StringSerializer;
import org.hibernate.EmptyInterceptor;
import org.hibernate.type.Type;

import java.io.Serializable;
import java.util.Properties;
import java.util.Set;

public class HibernateLoggingInterceptor extends EmptyInterceptor {

  private Producer<String, Object> eventProducer;

  public HibernateLoggingInterceptor(RuntimeConfiguration runtimeConfiguration) {
    super();
    Properties props = new Properties();
    final Set<String> servers = runtimeConfiguration.getKafkaConfiguration().getServers();
    props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, Joiner.on(',').join(servers));
    props.put(ProducerConfig.CLIENT_ID_CONFIG, "RhinoEventProducerService");
    props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, LongSerializer.class.getName());
    props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
    eventProducer = new KafkaProducer<>(props);
  }

  @Override
  public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
    if (entity instanceof Comment) {
      Comment comment = (Comment) entity;
      final String commentCreationMessage = String.format("Comment created. URI: %s ", comment.getCommentUri());
      final ProducerRecord<String, Object> producerRecord = new ProducerRecord<>("ambra-comment-created",
          commentCreationMessage);
      eventProducer.send(producerRecord);
    }
    return super.onSave(entity, id, state, propertyNames, types);
  }
}

@@ -1849,17 +1849,22 @@
 Object%3E 
-e
+kafkaE
 ventProd
@@ -2010,22 +2010,16 @@
 ();%0A    
-final 
 Set%3CStri
@@ -2440,25 +2440,30 @@
 ame());%0A    
-e
+kafkaE
 ventProducer
@@ -2707,22 +2707,16 @@
 ;%0A      
-final 
 String c
@@ -2818,62 +2818,32 @@
     
-final ProducerRecord%3CString, Object%3E producerRecord = 
+kafkaEventProducer.send(
 new 
@@ -2887,41 +2887,418 @@
 ed%22,
+ commentCreationMessage));
 %0A    
+%7D%0A
     
-  commentCreationMessage
+return super.onSave(entity, id, state, propertyNames, types);%0A  %7D%0A%0A  @Override%0A  public void onDelete(Object entity, Serializable id, Object%5B%5D state, String%5B%5D propertyNames, Type%5B%5D types) %7B%0A    if (entity instanceof Comment) %7B%0A      Comment comment = (Comment) entity;%0A      String commentDeletionMessage = String.format(%22Comment deleted. URI: %25s %22, comment.getCommentUri()
 );%0A 
@@ -3302,17 +3302,22 @@
 ;%0A      
-e
+kafkaE
 ventProd
@@ -3326,17 +3326,21 @@
 er.send(
-p
+new P
 roducerR
@@ -3344,16 +3344,67 @@
 erRecord
+%3C%3E(%22ambra-comment-deleted%22, commentDeletionMessage)
 );%0A    %7D
@@ -3404,39 +3404,32 @@
 ;%0A    %7D%0A    
-return 
 super.on
 Save(entity,
@@ -3408,35 +3408,37 @@
   %7D%0A    super.on
-Sav
+Delet
 e(entity, id, st

add Kafka logging for comment deletion