/*
 * 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.importer.rf2.validation;

import static com.google.common.collect.Sets.newHashSet;

import java.net.URL;
import java.util.List;
import java.util.Set;

import org.eclipse.core.runtime.IProgressMonitor;

import com.b2international.snowowl.snomed.common.SnomedRf2Headers;
import com.b2international.snowowl.snomed.importer.net4j.DefectType;
import com.b2international.snowowl.snomed.importer.net4j.ImportConfiguration;
import com.b2international.snowowl.snomed.importer.release.ReleaseFileSet.ReleaseComponentType;
import com.b2international.snowowl.snomed.importer.rf2.model.ComponentImportType;
import com.google.common.collect.Iterables;

/**
 * RF2 file validator for SNOMED&nbsp;CT extended maps.
 *
 */
public class SnomedExtendedMapTypeRefSetValidator extends SnomedRefSetValidator {

	private Set<String> mapCategoryConceptNotExist = newHashSet();
	private Set<String> correlationConceptNotExist = newHashSet();
	
	public SnomedExtendedMapTypeRefSetValidator(final ImportConfiguration configuration, final URL releaseUrl, final SnomedValidationContext context) {
		super(configuration, releaseUrl, ComponentImportType.EXTENDED_MAP_TYPE_REFSET, context, SnomedRf2Headers.EXTENDED_MAP_TYPE_HEADER);
	}

	@Override
	protected void doValidate(final List<String> row) {
		super.doValidate(row);
		validateCorrelationConcept(row);
		validateMapCategory(row);
	}
	
	@Override
	protected void doValidate(String effectiveTime, IProgressMonitor monitor) {
		super.doValidate(effectiveTime, monitor);
		addDefect(DefectType.EXTENDED_MAP_REFERENCED_INVALID_CONCEPT, Iterables.concat(mapCategoryConceptNotExist, correlationConceptNotExist));
		mapCategoryConceptNotExist.clear();
		correlationConceptNotExist.clear();
	}

	@Override
	protected String getName() {
		return "extended map type";
	}
	
	private void validateMapCategory(final List<String> row) {
		final String uuid = row.get(0);
		final String effectiveTime = row.get(1);
		final String mapCategory = row.get(12);
		if (!isComponentExists(mapCategory, ReleaseComponentType.CONCEPT)) {
			mapCategoryConceptNotExist.add(getMissingComponentMessage(uuid, effectiveTime, "map category", mapCategory));
		}
	}
	
	private void validateCorrelationConcept(final List<String> row) {
		final String uuid = row.get(0);
		final String effectiveTime = row.get(1);
		final String correlation = row.get(11);
		if (!isComponentExists(correlation, ReleaseComponentType.CONCEPT)) {
			correlationConceptNotExist.add(getMissingComponentMessage(uuid, effectiveTime, "correlation", correlation));
		}
	}

	
}
@@ -905,35 +905,180 @@
 owl.
-snomed.common.SnomedRf2Head
+core.terminology.ComponentCategory;%0Aimport com.b2international.snowowl.snomed.common.SnomedRf2Headers;%0Aimport com.b2international.snowowl.snomed.datastore.id.SnomedIdentifi
 ers;
@@ -2776,32 +2776,98 @@
 .get(12);%0A%09%09if (
+getComponentCategory(mapCategory) == ComponentCategory.CONCEPT && 
 !isComponentExis
@@ -3415,10 +3415,201 @@
 %09%7D%0A%09%7D%0A%0A%09
+private ComponentCategory getComponentCategory(String id) %7B%0A%09%09try %7B%0A%09%09%09return SnomedIdentifiers.getComponentCategory(id);%0A%09%09%7D catch (final IllegalArgumentException e) %7B%0A%09%09%09return null;%0A%09%09%7D%0A%09%7D
 %0A%7D

Validate map category for existence only if it's a SNOMED CT id