package org.waterforpeople.mapping.analytics;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.waterforpeople.mapping.analytics.dao.SurveyInstanceSummaryDao;
import org.waterforpeople.mapping.dao.CommunityDao;
import org.waterforpeople.mapping.dao.SurveyAttributeMappingDao;
import org.waterforpeople.mapping.dao.SurveyInstanceDAO;
import org.waterforpeople.mapping.domain.Community;
import org.waterforpeople.mapping.domain.QuestionAnswerStore;
import org.waterforpeople.mapping.domain.SurveyAttributeMapping;
import org.waterforpeople.mapping.domain.SurveyInstance;

import com.gallatinsystems.framework.analytics.summarization.DataSummarizer;

/**
 * Populates SurveyInstanceSummary objects (a roll-up that aggregates survey
 * instances by country/region/day)
 * 
 * @author Christopher Fagiani
 * 
 */
public class SurveyInstanceSummarizer implements DataSummarizer {

	private static Logger logger = Logger
			.getLogger(SurveyInstanceSummarizer.class.getName());

	private static final String COMMUNITY_QUESTION_ATTRIBUTE = "communityCode";
	private static final String ALT_COMMUNITY_QUESTION_ATTRIBUTE = "communityName";
	private SurveyInstanceDAO instanceDao;
	private SurveyAttributeMappingDao mappingDao;

	public SurveyInstanceSummarizer() {
		instanceDao = new SurveyInstanceDAO();
		mappingDao = new SurveyAttributeMappingDao();
	}

	/**
	 * looks up a survey instance then finds it's corresponding community (by
	 * looking at the questionAnswerStore until it finds the question with the
	 * ID that matches the configured value. If it finds the community, it will
	 * increment the count for the summary record.
	 */
	@Override
	public boolean performSummarization(String key, String type, String value,
			Integer offset, String cursor) {
		if (key != null) {

			SurveyInstance instance = instanceDao.getByKey(new Long(key));
			if (instance != null) {
				SurveyAttributeMapping mappings = mappingDao
						.findMappingForAttribute(instance.getSurveyId(),
								COMMUNITY_QUESTION_ATTRIBUTE);
				if (mappings == null ) {
					mappings = mappingDao.findMappingForAttribute(instance
							.getSurveyId(), ALT_COMMUNITY_QUESTION_ATTRIBUTE);
				}
				if (mappings != null ) {
					// if the survey has the attribute mapped, find the
					// appropriate question
					QuestionAnswerStore qas = null;
//					for (SurveyAttributeMapping mapping : mappings) {
						 qas = instanceDao
								.findQuestionAnswerStoreForQuestion(new Long(
										key), mappings.getSurveyQuestionId());
						 if(qas != null){
							// break;
						 }
//					}
					if (qas != null && qas.getValue() != null) {
						CommunityDao commDao = new CommunityDao();
						Community community = commDao.findCommunityByCode(qas
								.getValue());
						if (community != null) {
							SurveyInstanceSummaryDao.incrementCount(community
									.getCommunityCode(), community
									.getCountryCode(), instance
									.getCollectionDate());
						} else {
							logger
									.log(Level.SEVERE,
											"Couldn't find community for instance. Was the community saved correctly?");
						}
					}
				}
			}
		}
		return true;
	}

	@Override
	public String getCursor() {
		return null;
	}
}
@@ -1968,24 +1968,29 @@
 ull) %7B%0D%0A%09%09%09%09
+List%3C
 SurveyAttrib
@@ -1991,32 +1991,33 @@
 AttributeMapping
+%3E
  mappings = mapp
@@ -2034,32 +2034,33 @@
 %09%09%09%09.findMapping
+s
 ForAttribute(ins
@@ -2142,24 +2142,46 @@
 ngs == null 
+%7C%7C mappings.size() ==0
 ) %7B%0D%0A%09%09%09%09%09ma
@@ -2211,16 +2211,17 @@
 dMapping
+s
 ForAttri
@@ -2326,16 +2326,36 @@
 != null 
+&& mappings.size()%3E0
 ) %7B%0D%0A%09%09%09
@@ -2477,18 +2477,16 @@
  null;%0D%0A
-//
 %09%09%09%09%09for
@@ -2637,17 +2637,16 @@
  mapping
-s
 .getSurv
@@ -2695,18 +2695,16 @@
 %0A%09%09%09%09%09%09%09
-//
  break;%0D
@@ -2714,18 +2714,16 @@
 %09%09%09%09 %7D%0D%0A
-//
 %09%09%09%09%09%7D%0D%0A

put the handling of multiple mappings back into the summarizer since all the new survey have communityName mapped to 4 questions. Fixed a bug in the implementation too.