/*
 * Copyright 2017 Redlink GmbH
 *
 * 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 io.redlink.smarti.query.dbsearch;

import io.redlink.smarti.model.Slot;
import io.redlink.smarti.model.TemplateDefinition;
import io.redlink.smarti.model.Token;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.List;

@Component
public class DbSearchTemplateDefinition extends TemplateDefinition {

    public static final String DBSEARCH_TYPE = "dbsearch";
    
    public static final String ROLE_TERM = "Term";
    public static final String ROLE_ENTITY = "Entity";
    public static final String ROLE_KEYWORD = "Keyword";
    public static final String ROLE_TOPIC = "Topic";
    
    public DbSearchTemplateDefinition() {
        super(DBSEARCH_TYPE);
    }

    @Override
    protected Slot createSlotForName(String name) {
        switch (name) {
        case ROLE_TERM:
            return new Slot(ROLE_TERM, Token.Type.Term);
        case ROLE_KEYWORD:
            return new Slot(ROLE_KEYWORD, Token.Type.Keyword);
        case ROLE_ENTITY:
            return new Slot(ROLE_ENTITY, null);
        case ROLE_TOPIC:
            return new Slot(ROLE_TOPIC, Token.Type.Topic);
        default:
            log.warn("Unknown QuerySlot '{}' requested for {}", name, getClass().getSimpleName());
            return null;
        }
    }

    @Override
    protected boolean validate(Collection<Slot> slots, List<Token> tokens) {
        return slots.stream()
                .filter(s -> s.getRole().equals(ROLE_TERM))
                .filter(s -> s.getTokenIndex() >= 0).findAny().isPresent();
    }

}

@@ -812,16 +812,41 @@
 onent;%0A%0A
+import java.util.Arrays;%0A
 import j
@@ -883,19 +883,67 @@
 va.util.
-Lis
+HashSet;%0Aimport java.util.List;%0Aimport java.util.Se
 t;%0A%0A@Com
@@ -1296,32 +1296,150 @@
 = %22Topic%22;%0A    %0A
+    private final Set%3CString%3E REQUIRED_ROLES = new HashSet%3C%3E(Arrays.asList(ROLE_TERM,ROLE_ENTITY,ROLE_KEYWORD));%0A    %0A
     public DbSea
@@ -2232,36 +2232,43 @@
  -%3E 
-s.getRole().equals(ROLE_TERM
+REQUIRED_ROLES.contains(s.getRole()
 ))%0A 

fix for issue #10 - changed DbSearch template validation to also accept Keywords and Entities (in addition to terms)