package lemming.context.inbound;

import lemming.context.Context;
import lemming.context.ContextDao;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.model.IModel;

import java.util.List;
import java.util.SortedMap;

/**
 * A form for the verification of inbound contexts.
 */
class InboundContextVerificationForm extends Form<InboundContextPackage> {
    /**
     * Creates a context verify form.
     *
     * @param model model of a group of inbound contexts
     */
    public InboundContextVerificationForm(IModel<InboundContextPackage> model) {
        super("InboundContextVerificationForm", model);
        add(new SubmitButton(this));
    }

    private class SubmitButton extends AjaxButton {

        public SubmitButton(Form<InboundContextPackage> form) {
            super("submitButton", form);
        }

        /**
         * Match contexts by hash.
         */
        private void matchContextsByHash() {
            InboundContextPackage contextPackage = (InboundContextPackage) getForm().getModelObject();
            new InboundContextPackageDao().matchContextsByHash(contextPackage);
        }

        private void alignContextsStep2() {
            ContextDao contextDao = new ContextDao();
            InboundContextDao inboundContextDao = new InboundContextDao();
            InboundContextPackageDao inboundContextPackageDao = new InboundContextPackageDao();
            InboundContextPackage contextPackage = (InboundContextPackage) getForm().getModelObject();
            List<InboundContext> allContexts = inboundContextPackageDao.getContexts(contextPackage);
            List<InboundContext> unmatchedContexts = inboundContextPackageDao.findUnmatchedContexts(contextPackage);

            System.err.println(allContexts.size());
            System.err.println(unmatchedContexts.size());
        }

        @Override
        protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
            super.onSubmit(target, form);

            matchContextsByHash();
            alignContextsStep2();
        }
    }
}

@@ -63,43 +63,8 @@
 xt;%0A
-import lemming.context.ContextDao;%0A
 impo
@@ -266,22 +266,67 @@
 ort java
-.util.
+x.ws.rs.core.MultivaluedMap;%0Aimport java.util.Array
 List;%0Aim
@@ -344,17 +344,12 @@
 til.
-SortedMap
+List
 ;%0A%0A/
@@ -1260,97 +1260,176 @@
     
-private void alignContextsStep2() %7B%0A            ContextDao contextDao = new ContextDao();
+/**%0A         * Finds and sets locations with unmatched contexts.%0A         */%0A        private List%3CString%3E findUnmatchedLocations(InboundContextPackage contextPackage) %7B
 %0A   
@@ -1429,32 +1429,43 @@
 ) %7B%0A            
+return new 
 InboundContextDa
@@ -1466,256 +1466,614 @@
 text
-Dao inboundContextDao = new InboundContextDao();%0A            InboundContextPackageDao inboundContextPackageDao = new InboundContextPackageDao();%0A            InboundContextPackage contextPackage = (InboundContextPackage) getForm().getModelObject();%0A
+PackageDao().findUnmatchedContextLocations(contextPackage);%0A        %7D%0A%0A        private List%3CMatchHelper.Triple%3E computeMatchingTriplets(InboundContextPackage contextPackage, String location) %7B%0A            InboundContextPackageDao inboundContextPackageDao = new InboundContextPackageDao();%0A            MultivaluedMap%3CInteger, InboundContext%3E groupedContexts = inboundContextPackageDao%0A                    .groupUnmatchedContextsByLocation(contextPackage, location);%0A            List%3CMatchHelper.Triple%3E allMatchingTriples = new ArrayList%3C%3E();%0A%0A            for (Integer key : groupedContexts.keySet()) %7B%0A    
     
@@ -2101,20 +2101,17 @@
 ontext%3E 
-allC
+c
 ontexts 
@@ -2108,38 +2108,38 @@
  contexts = 
-inboun
+groupe
 dContext
 PackageDao.g
@@ -2130,48 +2130,24 @@
 text
-PackageDao.getContexts(contextPackage);%0A
+s.get(key);%0A    
     
@@ -2151,39 +2151,32 @@
            List%3C
-Inbound
 Context%3E unmatch
@@ -2172,29 +2172,27 @@
 xt%3E 
-unmatchedContexts = i
+complements = new I
 nbou
@@ -2204,38 +2204,27 @@
 text
-Package
 Dao
+()
 .find
-UnmatchedContex
+Complemen
 ts(c
@@ -2229,23 +2229,17 @@
 (context
-Package
+s
 );%0A%0A    
@@ -2250,320 +2250,1020 @@
     
-System.err.println(allContexts.size());%0A            System.err.println(unmatchedContexts.size());%0A        %7D%0A%0A        @Override%0A        protected void onSubmit(AjaxRequestTarget target, Form%3C?%3E form) %7B%0A            super.onSubmit(target, form);%0A%0A            matchContextsByHash();%0A            alignContextsStep2();
+    if (complements != null) %7B%0A                    MultivaluedMap%3CInteger, MatchHelper.Triple%3E tripleMap = MatchHelper%0A                            .getTriples(contexts, complements);%0A                    List%3CMatchHelper.Triple%3E matchingTriples = MatchHelper.computeMatchingTriples(tripleMap);%0A                    allMatchingTriples.addAll(matchingTriples);%0A                %7D%0A            %7D%0A%0A            return allMatchingTriples;%0A        %7D%0A%0A        @Override%0A        protected void onSubmit(AjaxRequestTarget target, Form%3C?%3E form) %7B%0A            super.onSubmit(target, form);%0A            InboundContextPackage contextPackage = (InboundContextPackage) getForm().getModelObject();%0A            List%3CString%3E unmatchedLocations;%0A%0A            matchContextsByHash();%0A            unmatchedLocations = findUnmatchedLocations(contextPackage);%0A%0A            for (String location : unmatchedLocations) %7B%0A                List%3CMatchHelper.Triple%3E matchingTriples = computeMatchingTriplets(contextPackage, location);%0A            %7D
 %0A   

Simplify code in InboundContextVerificationForm. Compute matching triples.