/* $This file is distributed under the terms of the license in /doc/license.txt$ */

package edu.cornell.mannlib.vitro.webapp.controller.json;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import edu.cornell.mannlib.vitro.webapp.beans.Individual;
import edu.cornell.mannlib.vitro.webapp.beans.VClass;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.dao.IndividualDao;
import edu.cornell.mannlib.vitro.webapp.services.shortview.ShortViewService;
import edu.cornell.mannlib.vitro.webapp.services.shortview.ShortViewService.ShortViewContext;
import edu.cornell.mannlib.vitro.webapp.services.shortview.ShortViewServiceSetup;
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.individual.IndividualTemplateModel;

/**
 * Does a Solr search for individuals, and uses the short view to render each of
 * the results.
 */
public class GetRenderedSolrIndividualsByVClass extends JsonObjectProducer {
	private static final Log log = LogFactory
			.getLog(GetRenderedSolrIndividualsByVClass.class);

	protected GetRenderedSolrIndividualsByVClass(VitroRequest vreq) {
		super(vreq);
	}

	/**
	 * Search for individuals by VClass. The class URI and the paging
	 * information are in the request parameters.
	 */
	@Override
	protected JSONObject process() throws Exception {
		JSONObject rObj = null;
		VClass vclass = getVclassParameter(vreq);
		String vclassId = vclass.getURI();

		vreq.setAttribute("displayType", vclassId);
		rObj = JsonServlet.getSolrIndividualsByVClass(vclassId, vreq, ctx);
		addShortViewRenderings(rObj);

		return rObj;
	}

	/**
	 * Look through the return object. For each individual, render the short
	 * view and insert the resulting HTML into the object.
	 */
	private void addShortViewRenderings(JSONObject rObj) throws JSONException {
		JSONArray individuals = rObj.getJSONArray("individuals");
		String vclassName = rObj.getJSONObject("vclass").getString("name");
		for (int i = 0; i < individuals.length(); i++) {
			JSONObject individual = individuals.getJSONObject(i);
			individual.put("shortViewHtml",
					renderShortView(individual.getString("URI"), vclassName));
		}
	}

	private String renderShortView(String individualUri, String vclassName) {
		IndividualDao iDao = vreq.getWebappDaoFactory().getIndividualDao();
		Individual individual = iDao.getIndividualByURI(individualUri);

		Map<String, Object> modelMap = new HashMap<String, Object>();
		modelMap.put("individual",
				new IndividualTemplateModel(individual, vreq));
		modelMap.put("vclass", vclassName);

		ShortViewService svs = ShortViewServiceSetup.getService(ctx);
		return svs.renderShortView(individual, ShortViewContext.BROWSE,
				modelMap, vreq);
	}
}

@@ -154,23 +154,71 @@
 va.util.
-HashMap
+Arrays;%0Aimport java.util.HashMap;%0Aimport java.util.List
 ;%0Aimport
@@ -1156,26 +1156,36 @@
 nds 
-JsonObjectProducer
+GetSolrIndividualsByVClasses
  %7B%0A%09
@@ -1408,16 +1408,63 @@
 y VClass
+ or VClasses in the case of multiple parameters
 . The cl
@@ -1470,16 +1470,19 @@
 lass URI
+(s)
  and the
@@ -1635,59 +1635,424 @@
 ;%0A%09%09
-VClass vclass = getVclassParameter(vreq);%0A%09%09String 
+%0A%09%09//This gets the first vclass value and sets that as display type%0A%09%09List%3CString%3E vclassIds = super.getVclassIds(vreq);%0A%09%09String vclassId = null;%0A%09%09if(vclassIds.size() %3E 1) %7B%0A%09%09%09//This ensures the second class instead of the first %0A%09%09%09//This is a temporary fix in cases where institutional internal classes are being sent in%0A%09%09%09//and thus the second class is the actual class with display information associated%0A%09%09%09
 vcla
@@ -2068,19 +2068,66 @@
 lass
+Ids
 .get
-URI();%0A
+(1);%0A%09%09%7D else %7B%0A%09%09%09vclassId = vclassIds.get(0);%0A%09%09%7D
 %0A%09%09v
@@ -2175,35 +2175,41 @@
 ;%0A%09%09
-rObj = JsonServlet.getSolrI
+%0A%09%09//This will get all the solr i
 ndiv
@@ -2218,36 +2218,157 @@
 uals
-By
+ by 
 VClass
-(vclassId, vreq, ctx
+ (if one value) or the intersection%0A%09%09//i.e. individuals that have all the types for the different vclasses entered%0A%09%09rObj = super.process(
 );%0A%09
@@ -2398,17 +2398,16 @@
 (rObj);%0A
-%0A
 %09%09return
@@ -2411,24 +2411,34 @@
 urn rObj;%0A%09%7D
+%0A%09%0A%09//Get 
 %0A%0A%09/**%0A%09 * L

Update to enable multiple VClasses to be searched