package edu.duke.cabig.c3pr.web.ajax;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.extremecomponents.table.bean.Column;
import org.extremecomponents.table.bean.Row;
import org.extremecomponents.table.bean.Table;
import org.extremecomponents.table.context.Context;
import org.extremecomponents.table.context.HttpServletRequestContext;
import org.extremecomponents.table.core.TableModel;
import org.extremecomponents.table.core.TableModelImpl;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.util.WebUtils;

import edu.duke.cabig.c3pr.dao.HealthcareSiteDao;
import edu.duke.cabig.c3pr.dao.ParticipantDao;
import edu.duke.cabig.c3pr.domain.Participant;
import edu.duke.cabig.c3pr.domain.SystemAssignedIdentifier;

public class SearchParticipantAjaxFacade {
	private static Log log = LogFactory.getLog(SearchParticipantAjaxFacade.class);
	
	public ParticipantDao participantDao ;

    public Object build(TableModel model, Collection participants) throws Exception {

        Table table = model.getTableInstance();
        table.setAutoIncludeParameters(false);
        table.setTableId("assembler");
        table.setItems(participants);
        table.setAction(model.getContext().getContextPath() + "/pages/personAndOrganization/participant/createParticipant");
        table.setTitle("Participants");
        table.setShowPagination(true);
        table.setRowsDisplayed(15);
        table.setOnInvokeAction("buildTable('assembler')");
        table.setImagePath(model.getContext().getContextPath() + "/images/table/*.gif");
        table.setShowExports(false);
        table.setSortable(true);
        model.addTable(table);

        Row row = model.getRowInstance();
        row.setHighlightRow(Boolean.TRUE);
        row.setInterceptor("edu.duke.cabig.c3pr.web.ajax.ParticipantRowInterceptor");
        model.addRow(row);

        Column name = model.getColumnInstance();
        name.setTitle("Name");
        name.setProperty("fullName");
        model.addColumn(name);
        
        Column primaryIdentifier = model.getColumnInstance();
        primaryIdentifier.setTitle("Primary identifier");
        primaryIdentifier.setProperty("primaryIdentifierValue");
        model.addColumn(primaryIdentifier);
        
        Column gender = model.getColumnInstance();
        gender.setTitle("Gender");
        gender.setProperty("administrativeGenderCode");
        model.addColumn(gender);
        
        Column races = model.getColumnInstance();
        races.setTitle("Race(s)");
        races.setProperty("raceCodes");
        model.addColumn(races);
        
        Column birthDate = model.getColumnInstance();
        birthDate.setTitle("Birth date");
        birthDate.setProperty("birthDateStr");
        model.addColumn(birthDate);
        
        return model.assemble();
    }

    public String getTable(Map<String, List> parameterMap, String[] params,
                    HttpServletRequest request) {
        Participant participant = new Participant();
        String text = "" ;
        String type = "" ;

        if (!StringUtils.isEmpty(params[0])) {
            type = params[0];
        }
        if (!StringUtils.isEmpty(params[1])) {
            text = params[1] ;
        }

        log.debug("search string = " + text + "; type = " + type);

        if ("N".equals(type)) {
            participant.setLastName(text);
        }
        if ("F".equals(type)) {
            participant.setFirstName(text);
        }
        if ("Identifier".equals(type)) {
            SystemAssignedIdentifier identifier = new SystemAssignedIdentifier();
            identifier.setValue(text);
            // FIXME:
            participant.addIdentifier(identifier);
        }

        List<Participant> participants = participantDao.searchByExample(participant);

        Context context = null;
        if (parameterMap == null) {
            context = new HttpServletRequestContext(request);
        }
        else {
            context = new HttpServletRequestContext(request, parameterMap);
        }

        TableModel model = new TableModelImpl(context);
        try {
            return build(model, participants).toString();
        }
        catch (Exception e) {
            log.error("Exception caught in SearchParticipantFacade");
            e.printStackTrace();
        }

        return "";
    }

	public ParticipantDao getParticipantDao() {
		return participantDao;
	}

	public void setParticipantDao(ParticipantDao participantDao) {
		this.participantDao = participantDao;
	}

}

@@ -38,37 +38,8 @@
 %0D%0A%0D%0A
-import java.util.ArrayList;%0D%0A
 impo
@@ -68,63 +68,8 @@
 n;%0D%0A
-import java.util.HashSet;%0D%0Aimport java.util.Iterator;%0D%0A
 impo
@@ -114,31 +114,8 @@
 ap;%0D
-%0Aimport java.util.Set;%0D
 %0A%0D%0Ai
@@ -675,160 +675,8 @@
 l;%0D%0A
-import org.springframework.web.servlet.ModelAndView;%0D%0Aimport org.springframework.web.util.WebUtils;%0D%0A%0D%0Aimport edu.duke.cabig.c3pr.dao.HealthcareSiteDao;
 %0D%0Aim

clean up