/*
 * Copyright 2011-2015 B2i Healthcare Pte Ltd, http://b2i.sg
 * 
 * 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 com.b2international.snowowl.snomed.datastore.quicksearch;

import java.util.Map;

import com.b2international.snowowl.core.ApplicationContext;
import com.b2international.snowowl.core.api.IBranchPath;
import com.b2international.snowowl.core.quicksearch.CompactQuickSearchElement;
import com.b2international.snowowl.core.quicksearch.QuickSearchContentResult;
import com.b2international.snowowl.datastore.IBranchPathMap;
import com.b2international.snowowl.datastore.quicksearch.IQuickSearchContentProvider;
import com.b2international.snowowl.snomed.SnomedConstants.Concepts;
import com.b2international.snowowl.snomed.SnomedPackage;
import com.b2international.snowowl.snomed.datastore.SnomedTerminologyBrowser;
import com.b2international.snowowl.snomed.datastore.id.SnomedIdentifiers;
import com.google.common.collect.ImmutableList;

/**
 * @since 4.4
 */
public class MissingSnomedComponentQuickSearchContentProvider implements IQuickSearchContentProvider {

	@Override
	public QuickSearchContentResult getComponents(String queryExpression, IBranchPathMap branchPathMap, int limit, Map<String, Object> configuration) {
		try {
			SnomedIdentifiers.validate(queryExpression);
			final IBranchPath branch = branchPathMap.getBranchPath(SnomedPackage.eINSTANCE);
			if (!ApplicationContext.getInstance().getServiceChecked(SnomedTerminologyBrowser.class).exists(branch, queryExpression)) {
				return new QuickSearchContentResult(1, ImmutableList.of(new CompactQuickSearchElement(queryExpression, Concepts.ROOT_CONCEPT, queryExpression, false)));
			}
		} catch (IllegalArgumentException e) {
			// ignore invalid SNOMED CT IDs and return empty result
		}
		return new QuickSearchContentResult();
	}

}

@@ -880,23 +880,20 @@
 ksearch.
-Compact
+Full
 QuickSea
@@ -982,16 +982,87 @@
 Result;%0A
+import com.b2international.snowowl.core.terminology.ComponentCategory;%0A
 import c
@@ -1325,16 +1325,102 @@
 ackage;%0A
+import com.b2international.snowowl.snomed.common.SnomedTerminologyComponentConstants;%0A
 import c
@@ -1485,24 +1485,97 @@
 ogyBrowser;%0A
+import com.b2international.snowowl.snomed.datastore.id.SnomedIdentifier;%0A
 import com.b
@@ -2002,37 +2002,140 @@
 fier
-s.validate(queryExpression);%0A
+ identifier = SnomedIdentifiers.of(queryExpression);%0A%09%09%09if (ComponentCategory.CONCEPT.equals(identifier.getComponentCategory())) %7B%0A%09
 %09%09%09f
@@ -2214,16 +2214,17 @@
 TANCE);%0A
+%09
 %09%09%09if (!
@@ -2345,16 +2345,17 @@
 ) %7B%0A%09%09%09%09
+%09
 return n
@@ -2410,15 +2410,12 @@
 new 
-Compact
+Full
 Quic
@@ -2495,12 +2495,63 @@
 alse
-)));
+, SnomedTerminologyComponentConstants.CONCEPT)));%0A%09%09%09%09%7D
 %0A%09%09%09

Fix validation of missing component IDs