/*
 * Copyright 2013 SFB 632.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package annis.sqlgen;

import annis.dao.AnnotatedMatch;
import annis.dao.AnnotatedSpan;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.springframework.jdbc.core.RowMapper;

/**
 * Implements an {@link Iterator} for a {@link AnnotatedMatch} from
 * a JDBC {@link ResultSet}.
 * @author Thomas Krause <thomas.krause@alumni.hu-berlin.de>
 */
public class AnnotatedMatchIterator implements Iterator<AnnotatedMatch>
{
  private ResultSetTypedIterator<AnnotatedSpan> itSpan;
  private AnnotatedSpan lastSpan;
  
  public AnnotatedMatchIterator(ResultSet rs, RowMapper<AnnotatedSpan> mapper)
  {
    this.itSpan = new ResultSetTypedIterator<AnnotatedSpan>(rs, mapper);
    this.lastSpan = null;
  }
  
  /**
   * Returns to the beginning of the iteration.
   */
  public void reset()
  {
    lastSpan = null;
    itSpan.reset();
  }

  @Override
  public boolean hasNext()
  {
    return lastSpan != null || itSpan.hasNext();
  }

  @Override
  public AnnotatedMatch next()
  {
    List<AnnotatedSpan> matchedSpans = new LinkedList<AnnotatedSpan>();
    List<Long> key = new ArrayList<Long>();
    
    if(lastSpan != null)
    {
      key = lastSpan.getKey();
      matchedSpans.add(lastSpan);
      lastSpan = null;
    }
    
    while(itSpan.hasNext())
    {
      AnnotatedSpan span = itSpan.next();
      List<Long> newKey = span.getKey();
      if(key.isEmpty() || newKey.equals(key))
      {
        matchedSpans.add(span);
        key = newKey;
        lastSpan = null;
      }
      else
      {
        // save reference to the already fetched span since we can't get back
        lastSpan = span;
        // we finished collecting all relevant spans
        break;
      }
    }

    return new AnnotatedMatch(matchedSpans);    
  }

  @Override
  public void remove()
  {
    throw new UnsupportedOperationException("Not supported yet.");
  }
  
}

@@ -1660,24 +1660,63 @@
  %7B%0A    List%3C
+Long%3E key = new ArrayList%3CLong%3E();%0A    
 AnnotatedSpa
@@ -1716,17 +1716,18 @@
 atedSpan
-%3E
+%5B%5D
  matched
@@ -1742,19 +1742,8 @@
 new 
-LinkedList%3C
 Anno
@@ -1755,55 +1755,11 @@
 Span
-%3E();%0A    List%3CLong%3E key = new ArrayList%3CLong%3E()
+%5B0%5D
 ;%0A  
@@ -1841,21 +1841,111 @@
 hedSpans
-.add(
+ = new AnnotatedSpan%5Bkey.size()%5D;%0A     %0A      setSpanForAllMatchedPositions(key, matchedSpans, 
 lastSpan
@@ -1943,24 +1943,31 @@
  lastSpan);%0A
+      %0A
       lastSp
@@ -2101,24 +2101,146 @@
 n.getKey();%0A
+      %0A      if(matchedSpans.length == 0)%0A      %7B%0A        matchedSpans = new AnnotatedSpan%5BnewKey.size()%5D;%0A      %7D%0A      %0A
       if(key
@@ -2280,32 +2280,70 @@
 %0A      %7B%0A       
+ setSpanForAllMatchedPositions(newKey,
  matchedSpans.ad
@@ -2343,20 +2343,26 @@
 pans
-.add(
+, 
 span);%0A
+        %0A
     
@@ -2616,17 +2616,16 @@
 %7D%0A    %7D%0A
-%0A
     retu
@@ -2775,18 +2775,382 @@
  yet.%22);%0A  %7D
+%0A%0A  private void setSpanForAllMatchedPositions(List%3CLong%3E key, %0A    AnnotatedSpan%5B%5D matchedSpans, AnnotatedSpan span)%0A  %7B%0A    // set annotation spans for *all* positions of the id%0A    // (node could have matched several times)%0A    int i=0;%0A    for(long l : key)%0A    %7B%0A      if(l == span.getId())%0A      %7B%0A        matchedSpans%5Bi%5D = span;%0A      %7D%0A      i++;%0A    %7D%0A  %7D
 %0A  %0A%7D%0A

handle the case that a span is matched several times in each match