/*
 * 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.validation.constraints.component;

import static com.b2international.snowowl.snomed.SnomedConstants.Concepts.FULLY_SPECIFIED_NAME;
import static com.b2international.snowowl.snomed.common.SnomedTerminologyComponentConstants.CONCEPT_NUMBER;

import org.apache.lucene.search.Query;

import com.b2international.snowowl.core.ApplicationContext;
import com.b2international.snowowl.core.api.IBranchPath;
import com.b2international.snowowl.core.validation.ComponentValidationConstraint;
import com.b2international.snowowl.core.validation.ComponentValidationDiagnostic;
import com.b2international.snowowl.core.validation.ComponentValidationDiagnosticImpl;
import com.b2international.snowowl.datastore.server.snomed.index.SnomedIndexServerService;
import com.b2international.snowowl.snomed.datastore.index.SnomedIndexService;
import com.b2international.snowowl.snomed.datastore.index.entry.SnomedConceptIndexEntry;
import com.b2international.snowowl.snomed.datastore.index.mapping.SnomedMappings;

/**
 * All concepts should have a fully-specified name of appropriate status.
 * <br/><br/><b>Note:</b> The SNOMED CT concept can be marked as valid if and only if the concept is <b>active</b> 
 * and has <b>exactly one active FSN</b>, but this constraint marks the concept invalid if it has not got any active 
 * FSN if the concept is active. The multiple active FSN validation is an other constraint's business.
 * 
 */
public class SnomedConceptFsnStatusConstraint extends ComponentValidationConstraint<SnomedConceptIndexEntry> {

	public static final String ID = "com.b2international.snowowl.snomed.validation.constraints.component.SnomedConceptFsnStatusConstraint";
	
	@Override
	public ComponentValidationDiagnostic validate(final IBranchPath branchPath, final SnomedConceptIndexEntry concept) {
		if (concept.isActive()) {
			final Query query = createQuery(concept.getId());
			if (getIndexService().getHitCount(branchPath, query, null) < 1) {
				final String errorMessage = String.format("%s has no active fully specified name.", concept.getLabel());
				return new ComponentValidationDiagnosticImpl(concept.getId(), errorMessage, ID, CONCEPT_NUMBER, error());
			}
		}
		return createOk(concept.getId(), ID, CONCEPT_NUMBER);
	}

	private SnomedIndexServerService getIndexService() {
		return (SnomedIndexServerService) ApplicationContext.getInstance().getService(SnomedIndexService.class);
	}

	private Query createQuery(final String conceptId) {
		return SnomedMappings.newQuery()
				.active()
				.descriptionConcept(conceptId)
				.descriptionType(FULLY_SPECIFIED_NAME)
				.matchAll();
	}

}
@@ -752,263 +752,67 @@
 med.
-SnomedConstants.Concepts.FULLY_SPECIFIED_NAME;%0Aimport static com.b2international.snowowl.snomed.common.SnomedTerminologyComponentConstants.CONCEPT_NUMBER;%0A%0Aimport org.apache.lucene.search.Query;%0A%0Aimport com.b2international.snowowl.core.ApplicationContext;
+common.SnomedTerminologyComponentConstants.CONCEPT_NUMBER;%0A
 %0Aimp
@@ -1154,62 +1154,39 @@
 owl.
-datastore.server.snomed.index.SnomedIndexServerService
+snomed.SnomedConstants.Concepts
 ;%0Aim
@@ -1229,42 +1229,38 @@
 med.
-datastore.index.SnomedIndexService
+core.domain.SnomedDescriptions
 ;%0Aim
@@ -1402,35 +1402,29 @@
 ore.
-index.mapping.SnomedMapping
+request.SnomedRequest
 s;%0A%0A
@@ -2267,33 +2267,143 @@
 nal 
-Query query = createQuery
+SnomedDescriptions descriptions = SnomedRequests.prepareSearchDescription()%0A%09%09%09%09.one()%0A%09%09%09%09.filterByActive(true)%0A%09%09%09%09.filterByConceptId
 (con
@@ -2419,70 +2419,159 @@
 d())
-;
 %0A%09%09%09
-if (getIndexService().getHitCount(branchPath, query, null
+%09.filterByType(Concepts.FULLY_SPECIFIED_NAME)%0A%09%09%09%09.build(branchPath.getPath())%0A%09%09%09%09.execute(getBus())%0A%09%09%09%09.getSync();%0A%09%09%09if (descriptions.getTotal(
 ) %3C 
@@ -2867,371 +2867,5 @@
 %09%7D%0A%0A
-%09private SnomedIndexServerService getIndexService() %7B%0A%09%09return (SnomedIndexServerService) ApplicationContext.getInstance().getService(SnomedIndexService.class);%0A%09%7D%0A%0A%09private Query createQuery(final String conceptId) %7B%0A%09%09return SnomedMappings.newQuery()%0A%09%09%09%09.active()%0A%09%09%09%09.descriptionConcept(conceptId)%0A%09%09%09%09.descriptionType(FULLY_SPECIFIED_NAME)%0A%09%09%09%09.matchAll();%0A%09%7D%0A%0A
 %7D

use requests in SnomedConceptFsnStatusConstraint