package com.fns.grivet.service;

import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.stream.messaging.Processor;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;
import org.springframework.util.Assert;

import com.codahale.metrics.MetricRegistry;
import com.fns.grivet.model.Op;

public class PersistenceService {

	private final Logger log = LoggerFactory.getLogger(getClass());

	private final EntityService entityService;
	private final MetricRegistry metricRegistry;

	public PersistenceService(EntityService entityService, MetricRegistry metricRegistry) {
		this.entityService = entityService;
		this.metricRegistry = metricRegistry;
	}

	@ServiceActivator(inputChannel = Processor.INPUT, outputChannel = Processor.OUTPUT)
	public Object store(Message<JSONObject> message) {
		Object result = message;
		Assert.notNull(message.getHeaders(), "No message headers!");
		Assert.notNull(message.getPayload(), "Message must have non-null payload!");
		log.debug("Received message.  Headers - {}.  Payload - {}", message.getHeaders().toString(),
				message.getPayload().toString());

		Op op = message.getHeaders().get("op", Op.class);
		Assert.notNull(op, "Message header must contain an op code!");

		String type = null;
		Long oid = null;
		try {
			switch (op) {
				case CREATE:
					type = message.getHeaders().get("type", String.class);
					Assert.hasText(type, "Message header must contain a type for create requests!");
					entityService.create(type, message.getPayload());
					metricRegistry.counter(MetricRegistry.name("store", "create", type, "count")).inc();
					log.info("Successfully created type [{}]", type);
					result = MessageBuilder.fromMessage(message).setHeader("processed", true);
					break;
				case UPDATE:
					oid = message.getHeaders().get("oid", Long.class);
					Assert.notNull(oid, "Message header must contain an oid for update requests!");
					type = entityService.update(oid, message.getPayload());
					metricRegistry.counter(MetricRegistry.name("store", "update", type, "count")).inc();
					log.info("Successfully updated type [{}]", type);
					result = MessageBuilder.fromMessage(message).setHeader("processed", true);
					break;
				case DELETE:
					oid = message.getHeaders().get("oid", Long.class);
					Assert.notNull(oid, "Message header must contain an oid for delete requests!");
					type = entityService.delete(oid);
					metricRegistry.counter(MetricRegistry.name("store", "delete", type, "count")).inc();
					log.info("Successfully deleted type [{}]", type);
					result = MessageBuilder.fromMessage(message).setHeader("processed", true);
					break;
				default:
					log.error("Bad payload! Op not available.  Headers - {}.  Payload - {}", message.getHeaders().toString(),
							message.getPayload().toString());
					result = MessageBuilder.fromMessage(message).setHeader("processed", false);
			}
		} catch (Exception e) {
			result = MessageBuilder.fromMessage(message)
						.setHeader("processed", false)
						.setHeader("exception", e.getMessage());
		}
		return result;
	}
}

@@ -967,16 +967,149 @@
 sage) %7B%0A
+%09%09log.trace(%22Received message.  Headers - %7B%7D.  Payload - %7B%7D%22, message.getHeaders().toString(),%0A%09%09%09%09message.getPayload().toString());%0A
 %09%09Object
@@ -1127,16 +1127,25 @@
 essage;%0A
+%09%09try %7B%0A%09
 %09%09Assert
@@ -1197,24 +1197,25 @@
 aders!%22);%0A%09%09
+%09
 Assert.notNu
@@ -1284,141 +1284,10 @@
 );%0A%09
-%09log.debug(%22Received message.  Headers - %7B%7D.  Payload - %7B%7D%22, message.getHeaders().toString(),%0A%09%09%09%09message.getPayload().toString());%0A
 %0A
+%09
 %09%09Op
@@ -1334,16 +1334,17 @@
 class);%0A
+%09
 %09%09Assert
@@ -1400,17 +1400,19 @@
 ode!%22);%0A
-%0A
+%09%0A%09
 %09%09String
@@ -1427,16 +1427,17 @@
 null;%0A%09%09
+%09
 Long oid
@@ -1443,29 +1443,24 @@
 d = null;%0A%09%09
-try %7B
 %0A%09%09%09switch (
@@ -2832,32 +2832,105 @@
 %09%09%09%09
-log.error(%22Bad payload! 
+throw new RuntimeException(%22Bad message payload!%22);%0A%09%09%09%7D%0A%09%09%7D catch (Exception e) %7B%0A%09%09%09log.error(%22
 Op n
@@ -3012,18 +3012,16 @@
 ),%0A%09%09%09%09%09
-%09%09
 message.
@@ -3050,120 +3050,8 @@
 ));%0A
-%09%09%09%09%09result = MessageBuilder.fromMessage(message).setHeader(%22processed%22, false);%0A%09%09%09%7D%0A%09%09%7D catch (Exception e) %7B%0A
 %09%09%09r

Fix code booboo