package databook.listener;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import databook.listener.service.IndexingService;
import databook.listener.service.MessagingService;
import databook.persistence.rule.PolymorphicDataEntityLinkMixin;
import databook.persistence.rule.PolymorphicDataEntityMixin;
import databook.persistence.rule.rdf.ruleset.DataEntity;
import databook.persistence.rule.rdf.ruleset.DataEntityLink;
import databook.persistence.rule.rdf.ruleset.Message;
import databook.persistence.rule.rdf.ruleset.Messages;

public class ModelUpdater implements MessagingService, IndexingService {
	// private final ModelUpdateListener modelUpdateListener;

	static final Log log = LogFactory.getLog(ModelUpdater.class);
	private List<Indexer> indexers = new ArrayList<Indexer>();
	private Scheduler s = new SimpleScheduler();

	public ModelUpdater() {
	}

	public void regIndexer(Indexer i) {
		if (!indexers.contains(i))
			indexers.add(i);
		i.setScheduler(s );
	}

	public void unregIndexer(Indexer i) {
		i.setScheduler(null);
		indexers.remove(i);
	}

	public static String genAVUId(String objId, String attribute, String value,
			String unit) {
		return URLEncoder.encode(objId + "/" + attribute + "/" + value + "/"
				+ unit);
	}

	/*
	 * message format: first line command:
	 * 
	 * add del move addMeta delMeta modMeta
	 */
	public void update(String messages) throws Exception {
		log.info("Executing update:\n===start===\n" + messages
				+ "\n===end===\n");
		ObjectMapper om = new ObjectMapper();
		om.addMixInAnnotations(DataEntity.class,
				PolymorphicDataEntityMixin.class);
		
		om.addMixInAnnotations(Message.class, PolymorphicDataEntityMixin.class);
		om.addMixInAnnotations(DataEntityLink.class, PolymorphicDataEntityLinkMixin.class);

		Messages ms;
		try {
			ms = om.readValue(messages, Messages.class);
		} catch (JsonParseException e) {
			log.error("error", e);
			return;
		} catch (JsonMappingException e) {
			log.error("error", e);
			return;
		} catch (IOException e) {
			log.error("error", e);
			return;
		}

		for (Indexer i : indexers) {
			i.messages(ms);
		}

	}

	@Override
	public void handle(String message) {
		System.out.println("Executing update:\n===start===\n" + message
				+ "\n===end===\n");
		log.info("Received AMQP message'" + message + "'");
		try {
			update(message);
			log.info("Model updated");
		} catch (Exception e) {
			log.error(e.getLocalizedMessage());
		}

	}
}

@@ -914,20 +914,18 @@
 e %7B%0A
+%0A
 %09
-// 
 private 
 fina
@@ -924,57 +924,8 @@
 ate 
-final ModelUpdateListener modelUpdateListener;%0A%0A%09
 stat
@@ -1093,37 +1093,8 @@
 );%0A%0A
-%09public ModelUpdater() %7B%0A%09%7D%0A%0A
 %09pub
@@ -1154,16 +1154,18 @@
 ains(i))
+ %7B
 %0A%09%09%09inde
@@ -1177,16 +1177,20 @@
 add(i);%0A
+%09%09%7D%0A
 %09%09i.setS
@@ -1199,17 +1199,16 @@
 eduler(s
- 
 );%0A%09%7D%0A%0A%09
@@ -1371,20 +1371,17 @@
 g value,
-%0A%09%09%09
+ 
 String u
@@ -1457,21 +1457,17 @@
 ue + %22/%22
-%0A%09%09%09%09
+ 
 + unit);
@@ -1475,103 +1475,8 @@
 %09%7D%0A%0A
-%09/*%0A%09 * message format: first line command:%0A%09 * %0A%09 * add del move addMeta delMeta modMeta%0A%09 */%0A
 %09pub
@@ -1511,25 +1511,8 @@
 ges)
- throws Exception
  %7B%0A%09
@@ -1566,21 +1566,17 @@
 messages
-%0A%09%09%09%09
+ 
 + %22%5Cn===
@@ -1587,16 +1587,17 @@
 ==%5Cn%22);%0A
+%0A
 %09%09Object
@@ -1670,21 +1670,16 @@
 y.class,
-%0A%09%09%09%09
 Polymorp
@@ -1709,11 +1709,8 @@
 s);%0A
-%09%09%0A
 %09%09om
@@ -2181,24 +2181,34 @@
 indexers) %7B%0A
+%09%09%09try %7B%0A%09
 %09%09%09i.message
@@ -2214,16 +2214,124 @@
 es(ms);%0A
+%09%09%09%09log.info(%22Model updated ( id = %22 + i + %22)%22);%0A%09%09%09%7D catch (Throwable e) %7B%0A%09%09%09%09log.error(%22error%22, e);%0A%09%09%09%7D%0A
 %09%09%7D%0A%0A%09%7D%0A
@@ -2445,21 +2445,17 @@
  message
-%0A%09%09%09%09
+ 
 + %22%5Cn===
@@ -2520,25 +2520,16 @@
 + %22'%22);%0A
-%09%09try %7B%0A%09
 %09%09update
@@ -2542,107 +2542,8 @@
 ge);
-%0A%09%09%09log.info(%22Model updated%22);%0A%09%09%7D catch (Exception e) %7B%0A%09%09%09log.error(e.getLocalizedMessage());%0A%09%09%7D
 %0A%0A%09%7D

make sure throwables from on indexer doesn't affect another indexer