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

package edu.cornell.mannlib.vitro.webapp.web.templatemodels.individual;

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

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

import edu.cornell.mannlib.vitro.webapp.beans.DataPropertyStatement;
import edu.cornell.mannlib.vitro.webapp.beans.Individual;
import edu.cornell.mannlib.vitro.webapp.config.ConfigurationProperties;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder.ParamMap;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder.Route;
import edu.cornell.mannlib.vitro.webapp.controller.visualization.VisualizationFrameworkConstants;
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
import edu.cornell.mannlib.vitro.webapp.dao.jena.SimpleOntModelSelector;
import edu.cornell.mannlib.vitro.webapp.dao.jena.WebappDaoFactorySDB;

public class IndividualTemplateModel extends BaseIndividualTemplateModel {

    private static final Log log = LogFactory.getLog(IndividualTemplateModel.class);
    
    private static final String FOAF = "http://xmlns.com/foaf/0.1/";
    private static final String CORE = "http://vivoweb.org/ontology/core#";
    private static final String PERSON_CLASS = FOAF + "Person";
    private static final String ORGANIZATION_CLASS = FOAF + "Organization";
    
    private Map<String, String> qrData = null;
    
    public IndividualTemplateModel(Individual individual, VitroRequest vreq) {
        super(individual, vreq);
    }
    
    private String getBaseVisUrl() {
        return getUrl(Route.VISUALIZATION_SHORT.path());
    }
 
    private Map<String, String> generateQrData() {

        Map<String,String> qrData = new HashMap<String,String>();
        
        // BJL23 - Apparently this needs a special DAO factory because the 
        // normal one (usually) filters the contact info.
        // We don't want to use getAssertionsWebappDaoFactory() - that will 
        // reduce load scalability and miss out on data in different named 
        // graphs.  Instead, we will make a DAO factory from the main
        // model and dataset associated with the request.
        WebappDaoFactory wdf = new WebappDaoFactorySDB(
        		new SimpleOntModelSelector(
        				vreq.getJenaOntModel()),vreq.getDataset());
        
        Collection<DataPropertyStatement> firstNames = wdf.getDataPropertyStatementDao().getDataPropertyStatementsForIndividualByDataPropertyURI(individual, FOAF + "firstName");
        Collection<DataPropertyStatement> lastNames = wdf.getDataPropertyStatementDao().getDataPropertyStatementsForIndividualByDataPropertyURI(individual, FOAF + "lastName");
        Collection<DataPropertyStatement> preferredTitles = wdf.getDataPropertyStatementDao().getDataPropertyStatementsForIndividualByDataPropertyURI(individual, CORE + "preferredTitle");
        Collection<DataPropertyStatement> phoneNumbers = wdf.getDataPropertyStatementDao().getDataPropertyStatementsForIndividualByDataPropertyURI(individual, CORE + "phoneNumber");
        Collection<DataPropertyStatement> emails = wdf.getDataPropertyStatementDao().getDataPropertyStatementsForIndividualByDataPropertyURI(individual, CORE + "email");

        if(firstNames != null && ! firstNames.isEmpty())
            qrData.put("firstName", firstNames.toArray(new DataPropertyStatement[firstNames.size()])[0].getData());
        if(lastNames != null && ! lastNames.isEmpty())
            qrData.put("lastName", lastNames.toArray(new DataPropertyStatement[firstNames.size()])[0].getData());
        if(preferredTitles != null && ! preferredTitles.isEmpty())
            qrData.put("preferredTitle", preferredTitles.toArray(new DataPropertyStatement[firstNames.size()])[0].getData());
        if(phoneNumbers != null && ! phoneNumbers.isEmpty())
            qrData.put("phoneNumber", phoneNumbers.toArray(new DataPropertyStatement[firstNames.size()])[0].getData());
        if(emails != null && ! emails.isEmpty())
            qrData.put("email", emails.toArray(new DataPropertyStatement[firstNames.size()])[0].getData());

        String tempUrl = vreq.getRequestURL().toString();
        String prefix = "http://";
        tempUrl = tempUrl.substring(0, tempUrl.replace(prefix, "").indexOf("/") + prefix.length());
        String profileUrl = getProfileUrl();
        String externalUrl = tempUrl + profileUrl;
        qrData.put("externalUrl", externalUrl);

        String individualUri = individual.getURI();
        String contextPath = vreq.getContextPath();
        qrData.put("exportQrCodeUrl", contextPath + "/qrcode?uri=" + UrlBuilder.urlEncode(individualUri));
        
        qrData.put("aboutQrCodesUrl", contextPath + "/qrcode/about");
        
        return qrData;
    }
    
    
    /* Access methods for templates */

    public boolean isPerson() {
        return isVClass(PERSON_CLASS);
    }
    
    public boolean isOrganization() {
        return isVClass(ORGANIZATION_CLASS);        
    }
    
    public String getCoAuthorVisUrl() {   	
        String coauthorVisURL = getBaseVisUrl() + "/" + VisualizationFrameworkConstants.COAUTHORSHIP_VIS_SHORT_URL + "/";    	
    	return getVisUrl(coauthorVisURL);
    }

    public String getCoInvestigatorVisUrl() {    	
    	String coinvestigatorVisURL = getBaseVisUrl() + "/" + VisualizationFrameworkConstants.COINVESTIGATOR_VIS_SHORT_URL + "/";    	
    	return getVisUrl(coinvestigatorVisURL);
    }

	private String getVisUrl(String coinvestigatorVisURL) {
		boolean isUsingDefaultNameSpace = UrlBuilder.isUriInDefaultNamespace(
												getUri(),
												vreq);
        
        if (isUsingDefaultNameSpace) {        	
        	return coinvestigatorVisURL + getLocalName();        	
        } else {        	
        	return UrlBuilder.addParams(
        			coinvestigatorVisURL, 
        			new ParamMap(VisualizationFrameworkConstants.INDIVIDUAL_URI_KEY, getUri())); 
        }
	}

    public String getTemporalGraphUrl() {
        if (!isOrganization()) {
            return null;
        }        
        String temporalVisURL = getBaseVisUrl() + "/" + VisualizationFrameworkConstants.PUBLICATION_TEMPORAL_VIS_SHORT_URL + "/";    	
    	return getVisUrl(temporalVisURL);
    }

    public String getMapOfScienceUrl() {
    	String mapOfScienceVisURL = getBaseVisUrl() + "/" + VisualizationFrameworkConstants.MAP_OF_SCIENCE_VIS_SHORT_URL + "/";    	
    	return getVisUrl(mapOfScienceVisURL);
    }
    
    public String getSelfEditingId() {
        String id = null;
        String idMatchingProperty = ConfigurationProperties.getBean(getServletContext()).getProperty("selfEditing.idMatchingProperty");
        if (! StringUtils.isBlank(idMatchingProperty)) {
            // Use assertions model to side-step filtering. We need to get the value regardless of whether the property
            // is visible to the current user.
            WebappDaoFactory wdf = vreq.getAssertionsWebappDaoFactory();
            Collection<DataPropertyStatement> ids = 
                wdf.getDataPropertyStatementDao().getDataPropertyStatementsForIndividualByDataPropertyURI(individual, idMatchingProperty);
            if (ids.size() > 0) {
                id = ids.iterator().next().getData();
            }
        }
        return id;
    }
    
    public Map<String, String> doQrData() {
        if(qrData == null)
            qrData = generateQrData();
        return qrData;
    }


}

@@ -6739,840 +6739,8 @@
    %0A
-    public String getSelfEditingId() %7B%0A        String id = null;%0A        String idMatchingProperty = ConfigurationProperties.getBean(getServletContext()).getProperty(%22selfEditing.idMatchingProperty%22);%0A        if (! StringUtils.isBlank(idMatchingProperty)) %7B%0A            // Use assertions model to side-step filtering. We need to get the value regardless of whether the property%0A            // is visible to the current user.%0A            WebappDaoFactory wdf = vreq.getAssertionsWebappDaoFactory();%0A            Collection%3CDataPropertyStatement%3E ids = %0A                wdf.getDataPropertyStatementDao().getDataPropertyStatementsForIndividualByDataPropertyURI(individual, idMatchingProperty);%0A            if (ids.size() %3E 0) %7B%0A                id = ids.iterator().next().getData();%0A            %7D%0A        %7D%0A        return id;%0A    %7D%0A    %0A
     

Move getSelfEditingId() from vivo-specific IndividualTemplateModel to BaseIndividualTemplateModel, since the property is defined and managed at the vitro level