/*
 * 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.datastore.server.index.diff;

import static com.b2international.snowowl.datastore.BranchPathUtils.createPath;
import static com.b2international.snowowl.datastore.BranchPathUtils.isMain;
import static com.b2international.snowowl.datastore.server.index.IndexBranchService.getIndexCommit;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

import javax.annotation.Nullable;

import org.apache.lucene.index.IndexCommit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.b2international.snowowl.core.api.IBranchPath;
import com.b2international.snowowl.datastore.index.diff.IndexDifferService;
import com.b2international.snowowl.datastore.index.diff.VersionCompareConfiguration;
import com.b2international.snowowl.datastore.server.index.IndexBranchService;
import com.b2international.snowowl.datastore.server.index.IndexServerService;
import com.b2international.snowowl.datastore.server.index.IndexServerServiceManager;
import com.b2international.snowowl.index.diff.IndexDiff;
import com.b2international.snowowl.index.diff.IndexDiffer;
import com.b2international.snowowl.index.diff.IndexDifferFactory;

/**
 * Index differ service implementation.
 *
 */
public class IndexDifferServiceImpl implements IndexDifferService {

	private static final Logger LOGGER = LoggerFactory.getLogger(IndexDifferServiceImpl.class);
	
	@Override
	public IndexDiff calculateDiff(final VersionCompareConfiguration configuration) {
		return calculateDiff(getIndexService(checkNotNull(configuration, "configuration").getRepositoryUuid()), configuration);
	}

	private IndexDiff calculateDiff(final IndexServerService<?> indexService, final VersionCompareConfiguration configuration) {
		
		checkNotNull(indexService, "indexService");
		checkNotNull(configuration, "configuration");
		final IBranchPath sourceBranchPath = configuration.getSourcePath();
		checkArgument(!isMain(sourceBranchPath), "Source path argument cannot reference onto the MAIN branch.");

		final IndexBranchService targetBranchService = indexService.getBranchService(configuration.getTargetPath());
		
		log("Calculating index diff between '" + sourceBranchPath + "' and '" + configuration.getTargetPath() + "' for " + configuration.getToolingName() + "...");
		
		@Nullable final IndexCommit ancestorCommit;
		final IndexCommit sourceIndexCommit;
		
		if (configuration.isThreeWay()) {
			ancestorCommit = getIndexCommit(indexService.getBranchService(sourceBranchPath).getDirectory(), sourceBranchPath);
			sourceIndexCommit = indexService.getBranchService(createPath(sourceBranchPath)).getHeadIndexCommit();
		} else {
			final IndexBranchService sourceBranchService = indexService.getBranchService(sourceBranchPath);
			ancestorCommit = null;
			sourceIndexCommit = getIndexCommit(sourceBranchService.getDirectory(), sourceBranchPath);
		}
		
		final IndexCommit targetIndexCommit = targetBranchService.getHeadIndexCommit(); 
		
		final IndexDiffer differ = IndexDifferFactory.INSTANCE.createDiffer();
		final IndexDiff diff;
		
		if (configuration.isThreeWay()) {
			diff = differ.calculateDiff(ancestorCommit, sourceIndexCommit, targetIndexCommit);
		} else {
			diff = differ.calculateDiff(sourceIndexCommit, targetIndexCommit);
		}
		
		log(getDiffStatisticMessage(diff));
		
		return diff;
	}

	private String getDiffStatisticMessage(final IndexDiff diff) {
		final StringBuilder sb = new StringBuilder();
		sb.append("Index diff calculation was successful. New: ");
		sb.append(diff.getNewIds().size());
		sb.append(" changed: ");
		sb.append(diff.getChangedIds().size());
		sb.append(" detached: ");
		sb.append(diff.getDetachedIds().size());
		sb.append(".");
		return sb.toString();
	}

	private IndexServerService<?> getIndexService(final String repositoryUuid) {
		return (IndexServerService<?>) IndexServerServiceManager.INSTANCE.getByUuid(checkNotNull(repositoryUuid, "repositoryUuid"));
	}

	private void log(final String message) {
		LOGGER.info(message);
	}
	
}
@@ -847,108 +847,8 @@
 in;%0A
-import static com.b2international.snowowl.datastore.server.index.IndexBranchService.getIndexCommit;%0A
 impo
@@ -979,43 +979,8 @@
 l;%0A%0A
-import javax.annotation.Nullable;%0A%0A
 impo
@@ -2836,18 +2836,8 @@
 %09%0A%09%09
-@Nullable 
 fina
@@ -2944,16 +2944,72 @@
 ay()) %7B%0A
+%09%09%09// sourcePath is most likely an IBaseBranchPath here%0A
 %09%09%09ances
@@ -3020,31 +3020,16 @@
 ommit = 
-getIndexCommit(
 indexSer
@@ -3067,37 +3067,36 @@
 nchPath).get
-Directory(), 
+IndexCommit(
 sourceBranch
@@ -3184,28 +3184,28 @@
 chPath)).get
-Head
+Last
 IndexCommit(
@@ -3225,54 +3225,54 @@
 %0A%09%09%09
-final IndexBranchService sourceBranchService
+ancestorCommit = null;%0A%09%09%09sourceIndexCommit
  =
+ 
  ind
@@ -3319,110 +3319,24 @@
 ath)
-;%0A%09%09%09ancestorCommit = null;%0A%09%09%09sourceIndexCommit = getIndexCommit(sourceBranchService.getDirectory(), 
+.getIndexCommit(
 sour
@@ -3424,12 +3424,12 @@
 .get
-Head
+Last
 Inde

Use latest IndexBranchService for retrieving base index commit