/*
 * 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.libgui;

import annis.resolver.SingleResolverRequest;
import com.vaadin.ui.UI;
import java.io.Serializable;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.corpus_tools.annis.api.model.CorpusConfiguration;
import org.corpus_tools.annis.api.model.VisualizerRule;
import org.corpus_tools.annis.api.model.VisualizerRule.ElementEnum;
import org.corpus_tools.salt.common.SDocument;
import org.corpus_tools.salt.core.SLayer;
import org.corpus_tools.salt.core.SNode;
import org.corpus_tools.salt.core.SRelation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Benjamin Weißenfels {@literal <b.pixeldrama@gmail.com>}
 */
public class ResolverProviderImpl implements ResolverProvider, Serializable {

  /**
   * 
   */
  private static final long serialVersionUID = 5170826828759716453L;

  private final static Logger log = LoggerFactory.getLogger(ResolverProviderImpl.class);

  private Map<HashSet<SingleResolverRequest>, LinkedHashSet<VisualizerRule>> cacheResolver;

  public ResolverProviderImpl(
      Map<HashSet<SingleResolverRequest>, LinkedHashSet<VisualizerRule>> cacheResolver) {
    this.cacheResolver = cacheResolver;
  }

  @Override
  public List<VisualizerRule> getResolverEntries(SDocument doc, UI ui) {
    

    // create a request for resolver entries
    HashSet<SingleResolverRequest> resolverRequests = new HashSet<SingleResolverRequest>();

    Set<String> nodeLayers = new HashSet<String>();
    Set<String> edgeLayers = new HashSet<String>();

    if (doc != null && doc.getDocumentGraph() != null) {
      for (SNode n : doc.getDocumentGraph().getNodes()) {
        for (SLayer layer : n.getLayers()) {
          nodeLayers.add(layer.getName());
        }
      }

      for (SRelation<SNode, SNode> e : doc.getDocumentGraph().getRelations()) {
        for (SLayer layer : e.getLayers()) {
          try {
            edgeLayers.add(layer.getName());
          } catch (NullPointerException ex) {
            log.warn("NullPointerException when using Salt, was trying to get layer name", ex);
          }
        }
      }

    }

    for (String ns : nodeLayers) {
      resolverRequests.add(new SingleResolverRequest(doc.getGraph().getRoots().get(0).getName(), ns,
          ElementEnum.NODE));
    }
    for (String ns : edgeLayers) {
      resolverRequests.add(new SingleResolverRequest(doc.getGraph().getRoots().get(0).getName(), ns,
          ElementEnum.EDGE));
    }
    LinkedHashSet<VisualizerRule> matchingRules = new LinkedHashSet<>();
    // query with this resolver request and make sure it is unique
    if (cacheResolver.containsKey(resolverRequests)) {
      matchingRules.addAll(cacheResolver.get(resolverRequests));
    } else {

      // Get all rules for the selected corpora
      Map<String, CorpusConfiguration> corpusConfigByName = new HashMap<>();
      for (SingleResolverRequest r : resolverRequests) {
        if (!corpusConfigByName.containsKey(r.getCorpusName())) {
          CorpusConfiguration corpusConfig = Helper.getCorpusConfig(r.getCorpusName(), ui);
          if (corpusConfig != null) {
            corpusConfigByName.put(r.getCorpusName(), corpusConfig);
          }
        }
      }

      for (SingleResolverRequest r : resolverRequests) {
        CorpusConfiguration corpusConfig = corpusConfigByName.get(r.getCorpusName());

        // Filter the visualizer entries that match this resolver request
        if (corpusConfig != null && corpusConfig.getVisualizers() != null) {
          for (VisualizerRule visRule : corpusConfig.getVisualizers()) {
            if (visRule.getMappings() == null) {
              visRule.setMappings(new LinkedHashMap<>());
            }
            if (visRule.getElement() != null && !visRule.getElement().equals(r.getType())) {
              continue;
            }
            if (visRule.getLayer() != null && !visRule.getLayer().equals(r.getNamespace())) {
              continue;
            }
            matchingRules.add(visRule);
          }
          cacheResolver.put(resolverRequests, matchingRules);
        }
      }
    }

    return new LinkedList<>(matchingRules);
  }
}

@@ -701,34 +701,8 @@
 le;%0A
-import java.util.HashMap;%0A
 impo
@@ -1980,20 +1980,16 @@
 I ui) %7B%0A
-    
 %0A%0A    //
@@ -2093,37 +2093,16 @@
 HashSet%3C
-SingleResolverRequest
 %3E();%0A%0A  
@@ -2788,24 +2788,93 @@
   %7D%0A%0A    %7D%0A%0A
+    String corpusName = doc.getGraph().getRoots().get(0).getName();%0A%0A
     for (Str
@@ -2953,65 +2953,23 @@
 est(
-doc.getGraph().getRoots().get(0).get
+corpus
 Name
-()
 , ns,
-%0A         
  Ele
@@ -3083,65 +3083,23 @@
 est(
-doc.getGraph().getRoots().get(0).get
+corpus
 Name
-()
 , ns,
-%0A         
  Ele
@@ -3430,229 +3430,35 @@
 the 
-selected corpora%0A      Map%3CString, CorpusConfiguration%3E corpusConfigByName = new HashMap%3C%3E();%0A      for (SingleResolverRequest r : resolverRequests) %7B%0A        if (!corpusConfigByName.containsKey(r.getCorpusName())) %7B%0A    
+corpus in the corpus graph%0A
     
@@ -3517,22 +3517,17 @@
 sConfig(
-r.getC
+c
 orpusNam
@@ -3531,21 +3531,17 @@
 Name
-()
 , ui);
-%0A    
+;%0A%0A
     
@@ -3570,365 +3570,90 @@
 null
-) %7B%0A            corpusConfigByName.put(r.getCorpusName(), corpusConfig);%0A          %7D%0A        %7D%0A      %7D%0A%0A      for (SingleResolverRequest r : resolverRequests) %7B%0A        CorpusConfiguration corpusConfig = corpusConfigByName.get(r.getCorpusName());%0A%0A        // Filter the visualizer entries that match this resolver request%0A        if (corpusConfig != null &&
+ && corpusConfig.getVisualizers() != null) %7B%0A        for (VisualizerRule visRule :
  cor
@@ -3674,32 +3674,24 @@
 isualizers()
- != null
 ) %7B%0A        
@@ -3701,62 +3701,50 @@
 or (
-VisualizerRule visRule : corpusConfig.getVisualizers()
+SingleResolverRequest r : resolverRequests
 ) %7B%0A

Make sure the order of the visualizer panels is the same as definied in the visualizer rules