package org.intermine.bio.web;

/*
 * Copyright (C) 2002-2010 FlyMine
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  See the LICENSE file for more
 * information or http://www.gnu.org/copyleft/lesser.html.
 *
 */

import java.util.Collection;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.tiles.ComponentContext;
import org.apache.struts.tiles.actions.TilesAction;
import org.intermine.api.InterMineAPI;
import org.intermine.api.profile.InterMineBag;
import org.intermine.bio.logic.Mine;
import org.intermine.bio.logic.OrthologueLinkManager;
import org.intermine.bio.web.logic.BioUtil;
import org.intermine.util.TypeUtil;
import org.intermine.web.logic.bag.BagHelper;
import org.intermine.web.logic.session.SessionMethods;


/**
 * Class that generates links to other intermines
 * @author julie
 */
public class OrthologueLinkController  extends TilesAction
{
    private String identifierField = "primaryIdentifier";
    private String alternativeIdentifierField = "ensemblIdentifier";

    /**
     * {@inheritDoc}
     */
    @Override
    public ActionForward execute(@SuppressWarnings("unused") ComponentContext context,
                                 @SuppressWarnings("unused") ActionMapping mapping,
                                 @SuppressWarnings("unused") ActionForm form,
                                 HttpServletRequest request,
                                 @SuppressWarnings("unused") HttpServletResponse response) {

        final InterMineAPI im = SessionMethods.getInterMineAPI(request.getSession());
        InterMineBag bag = (InterMineBag) request.getAttribute("bag");
        Properties webProperties = SessionMethods.getWebProperties(request.getSession()
                .getServletContext());

        // if gene.ensemblId is a field, use that instead
        // FIXME this should come from config file instead

        Class c = null;
        try {
            c = Class.forName(bag.getQualifiedType());
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if (TypeUtil.getFieldInfo(c, alternativeIdentifierField) != null) {
            identifierField = alternativeIdentifierField;
        }


        String identifierList = BagHelper.getIdList(bag, im.getObjectStore(), "", identifierField);
        request.setAttribute("identifierList", identifierList);

        OrthologueLinkManager orthologueLinkManager
        = OrthologueLinkManager.getInstance(im, webProperties);
        Collection<String> organismNamesInBag = BioUtil.getOrganisms(im.getObjectStore(), bag,
                false, "shortName");
        Map<Mine, Map<String, Set[]>> mines
            = orthologueLinkManager.getMines(organismNamesInBag);
        if (!mines.isEmpty()) {
            request.setAttribute("mines", mines);
        }

        return null;
    }
}

@@ -2126,127 +2126,8 @@
 ));%0A
-%0A        // if gene.ensemblId is a field, use that instead%0A        // FIXME this should come from config file instead%0A%0A
     
@@ -2264,55 +2264,8 @@
 ) %7B%0A
-            // TODO Auto-generated catch block%0A
     
@@ -2293,16 +2293,41 @@
 race();%0A
+            return null;%0A
         
@@ -2462,16 +2462,16 @@
 rField;%0A
+
         
@@ -2472,18 +2472,16 @@
       %7D%0A
-%0A%0A
         

handle exception