/*
 * Copyright 2018-2021 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.reasoner;

import java.util.Collection;
import java.util.concurrent.TimeUnit;

import com.b2international.index.Index;
import com.b2international.snowowl.core.RepositoryManager;
import com.b2international.snowowl.core.config.SnowOwlConfiguration;
import com.b2international.snowowl.core.plugin.Component;
import com.b2international.snowowl.core.repository.ComponentDeletionPolicy;
import com.b2international.snowowl.core.repository.CompositeComponentDeletionPolicy;
import com.b2international.snowowl.core.repository.TerminologyRepositoryConfigurer;
import com.b2international.snowowl.core.setup.Environment;
import com.b2international.snowowl.core.setup.Plugin;
import com.b2international.snowowl.snomed.common.SnomedTerminologyComponentConstants;
import com.b2international.snowowl.snomed.datastore.config.SnomedCoreConfiguration;
import com.b2international.snowowl.snomed.datastore.index.constraint.SnomedConstraintDocument;
import com.b2international.snowowl.snomed.reasoner.classification.ClassificationTracker;
import com.b2international.snowowl.snomed.reasoner.index.*;
import com.google.common.collect.ImmutableList;

/**
 * @since 7.0
 */
@Component
public final class SnomedReasonerPlugin extends Plugin implements TerminologyRepositoryConfigurer {

	@Override
	public void run(final SnowOwlConfiguration configuration, final Environment env) throws Exception {
		if (env.isServer()) {
			final Index repositoryIndex = env.service(RepositoryManager.class).get(getToolingId()).service(Index.class);
			final SnomedCoreConfiguration snomedConfig = configuration.getModuleConfig(SnomedCoreConfiguration.class);
			final int maximumReasonerRuns = snomedConfig.getMaxReasonerRuns();
			final long cleanUpInterval = TimeUnit.MINUTES.toMillis(5L); // TODO: make this configurable
			final ClassificationTracker classificationTracker = new ClassificationTracker(repositoryIndex, maximumReasonerRuns, cleanUpInterval);
			
			env.services().registerService(ClassificationTracker.class, classificationTracker);
		}
	}
	
	@Override
	public Collection<Class<?>> getAdditionalMappings() {
		return ImmutableList.<Class<?>>of(
			ClassificationTaskDocument.class,
			EquivalentConceptSetDocument.class,
			ConceptChangeDocument.class,
			DescriptionChangeDocument.class,
			RelationshipChangeDocument.class,
			ConcreteDomainChangeDocument.class
		);
	}
	
	@Override
	public String getToolingId() {
		return SnomedTerminologyComponentConstants.TOOLING_ID;
	}
	
	@Override
	public CompositeComponentDeletionPolicy getComponentDeletionPolicy() {
		return CompositeComponentDeletionPolicy.of(SnomedConstraintDocument.class, ComponentDeletionPolicy.ALLOW);
	}
	
}

@@ -973,169 +973,8 @@
 nt;%0A
-import com.b2international.snowowl.core.repository.ComponentDeletionPolicy;%0Aimport com.b2international.snowowl.core.repository.CompositeComponentDeletionPolicy;%0A
 impo
@@ -1340,103 +1340,8 @@
 on;%0A
-import com.b2international.snowowl.snomed.datastore.index.constraint.SnomedConstraintDocument;%0A
 impo
@@ -2869,203 +2869,6 @@
 %7D%0A%09%0A
-%09@Override%0A%09public CompositeComponentDeletionPolicy getComponentDeletionPolicy() %7B%0A%09%09return CompositeComponentDeletionPolicy.of(SnomedConstraintDocument.class, ComponentDeletionPolicy.ALLOW);%0A%09%7D%0A%09%0A
 %7D%0A

Remove custom deletion policy for SnomedConstraints