package id.iptek.utms.agent.queue;

import id.iptek.utms.agent.dao.DiagnosticDao;
import id.iptek.utms.agent.model.Diagnostic;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author jakar
 */
public class DiagnosticInfoQueueMessageHandler implements QueueMessageHandler<Diagnostic> {

    public final static String NAME = "DIAGNOSTIC_QUEUE_HANDLER";
    private Logger logger = LoggerFactory.getLogger(getClass());
    private DiagnosticDao dao = new DiagnosticDao();
    
    public DiagnosticInfoQueueMessageHandler() {
    }

    @Override
    public boolean handleMessage(Diagnostic message) {
        try {
            logger.debug("Saving diagnostic: {}", message);
            long start = System.currentTimeMillis();
            boolean saved = dao.save(message);
            long end = System.currentTimeMillis();
            logger.debug("Saved in {}ms", (end-start));
            return saved;
        } catch(Exception ex) {
            logger.error("Error handling diagnostic: {}", ex.getMessage(), ex);
            return false;
        }
    }

    @Override
    public boolean handleMessages(List<Diagnostic> messages) {
        try {
            logger.debug("Saving list of diagnostics: {}", messages.size());
            long start = System.currentTimeMillis();
            boolean saved = dao.saveAll(messages);
            long end = System.currentTimeMillis();
            logger.debug("Saved in {}ms", (end-start));
            return saved;
        } catch(Exception ex) {
            logger.error("Error handling list of diagnostics: {}", ex.getMessage(), ex);
            return false;
        }
    }
    
}
