package org.intermine.web.bag;

/* 
 * Copyright (C) 2002-2005 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.Iterator;
import java.util.Map;

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

import org.apache.commons.lang.StringUtils;
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;

/**
 * Controller for the bagUploadConfirm
 * @author Kim Rutherford
 */
public class BagUploadConfirmController extends TilesAction
{
    /**
     * Set up the bagUploadConfirm page.
     * @see TilesAction#execute(ActionMapping, ActionForm, HttpServletRequest, HttpServletResponse)
     */
    public ActionForward execute(ComponentContext context,
            ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        BagQueryResult bagQueryResult = (BagQueryResult) session.getAttribute("bagQueryResult");
        request.setAttribute("matches", bagQueryResult.getMatches());
        request.setAttribute("issues", bagQueryResult.getIssues());
        request.setAttribute("unresolved", bagQueryResult.getUnresolved());

        StringBuffer matchesStringBuffer = new StringBuffer();
        BagUploadConfirmForm bagUploadConfirmForm = ((BagUploadConfirmForm) form);
        Map matches = bagQueryResult.getMatches();
        // matches will be null if we get here if the form.validate() method fails
        if (matches != null) {
            Iterator matchIDIter = matches.keySet().iterator();
            while (matchIDIter.hasNext()) {
                matchesStringBuffer.append(matchIDIter.next()).append(' ');
            }
            bagUploadConfirmForm.setMatchIDs(matchesStringBuffer.toString().trim());
        }
        if (request.getAttribute("bagType") != null) {
            bagUploadConfirmForm.setBagType((String) request.getAttribute("bagType"));
        }
        String trimmedIds = bagUploadConfirmForm.getMatchIDs().trim();
        int matchCount;
        if (trimmedIds.length() > 0) {
            int spaceCount = StringUtils.countMatches(trimmedIds, " ");
            matchCount = spaceCount + 1;
        } else {
            matchCount = 0;
        }
        request.setAttribute("matchCount", new Integer(matchCount));
        return null;
    }
}

@@ -324,16 +324,42 @@
  *%0A */%0A%0A
+import java.util.HashMap;%0A
 import j
@@ -1524,24 +1524,73 @@
 Matches());%0A
+        Map issues = bagQueryResult.getIssues();%0A
         requ
@@ -1620,34 +1620,14 @@
 s%22, 
-bagQueryResult.getI
+i
 ssues
-()
 );%0A 
@@ -1702,16 +1702,739 @@
 ed());%0A%0A
+        // get all of the %22low quality%22 matches ie. those found by queries other than matching %0A        // class keys%0A        Map lowQualityMatches = new HashMap();%0A        Map otherMatchMap = (Map) bagQueryResult.getIssues().get(BagQueryResult.OTHER);%0A        if (otherMatchMap != null) %7B%0A            Iterator otherMatchesIter = otherMatchMap.values().iterator();%0A            while (otherMatchesIter.hasNext()) %7B%0A                Map inputToObjectsMap = (Map) otherMatchesIter.next();%0A                lowQualityMatches.putAll(inputToObjectsMap);%0A            %7D%0A        %7D%0A        request.setAttribute(%22lowQualityMatches%22, lowQualityMatches);%0A        %0A        // create a string containing the ids of the high-quality matches%0A
         

Set the lowQualityMatches request attribute in BagUploadConfirmController.