/*
 * Waltz - Enterprise Architecture
 * Copyright (C) 2016  Khartec Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.khartec.waltz.data.measurable.search;

import com.khartec.waltz.data.DatabaseVendorSpecific;
import com.khartec.waltz.data.FullTextSearch;
import com.khartec.waltz.data.JooqUtilities;
import com.khartec.waltz.data.measurable.MeasurableDao;
import com.khartec.waltz.model.entity_search.EntitySearchOptions;
import com.khartec.waltz.model.measurable.Measurable;
import org.jooq.Condition;
import org.jooq.DSLContext;
import org.jooq.impl.DSL;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static com.khartec.waltz.common.SetUtilities.union;
import static com.khartec.waltz.common.StringUtilities.mkTerms;
import static com.khartec.waltz.schema.tables.Measurable.MEASURABLE;
import static java.util.Collections.emptyList;

public class SqlServerMeasurableSearch implements FullTextSearch<Measurable>, DatabaseVendorSpecific {

    @Override
    public List<Measurable> search(DSLContext dsl, String query, EntitySearchOptions options) {
        List<String> terms = mkTerms(query);

        if (terms.isEmpty()) {
            return emptyList();
        }

        Condition nameCondition = terms.stream()
                .map(term -> MEASURABLE.NAME.like("%" + term + "%"))
                .collect(Collectors.reducing(
                        DSL.trueCondition(),
                        (acc, frag) -> acc.and(frag)));

        List<Measurable> measurablesViaName = dsl.selectDistinct(MEASURABLE.fields())
                .from(MEASURABLE)
                .where(nameCondition)
                .orderBy(MEASURABLE.NAME)
                .limit(options.limit())
                .fetch(MeasurableDao.TO_DOMAIN_MAPPER);


        List<Measurable> measurablesViaFullText = dsl
                .selectFrom(MEASURABLE)
                .where(JooqUtilities.MSSQL.mkContainsPrefix(terms))
                .limit(options.limit())
                .fetch(MeasurableDao.TO_DOMAIN_MAPPER);

        return new ArrayList<>(union(measurablesViaName, measurablesViaFullText));
    }

}

@@ -1858,16 +1858,611 @@
      %7D%0A%0A
+        Condition externalIdCondition = terms.stream()%0A                .map(term -%3E MEASURABLE.EXTERNAL_ID.like(%22%25%22 + term + %22%25%22))%0A                .collect(Collectors.reducing(%0A                        DSL.trueCondition(),%0A                        (acc, frag) -%3E acc.and(frag)));%0A%0A        List%3CMeasurable%3E measurablesViaExternalId = dsl.selectDistinct(MEASURABLE.fields())%0A                .from(MEASURABLE)%0A                .where(externalIdCondition)%0A                .orderBy(MEASURABLE.EXTERNAL_ID)%0A                .limit(options.limit())%0A                .fetch(MeasurableDao.TO_DOMAIN_MAPPER);%0A%0A
         
@@ -2494,32 +2494,32 @@
  terms.stream()%0A
-
                 
@@ -3016,17 +3016,16 @@
 PPER);%0A%0A
-%0A
         
@@ -3267,32 +3267,32 @@
 OMAIN_MAPPER);%0A%0A
-
         return n
@@ -3312,16 +3312,42 @@
 %3E(union(
+measurablesViaExternalId, 
 measurab

Improve Current Search fucntion in Waltz UI - measurable by external id