/*
 * 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.api.impl.traceability;

import static com.google.common.collect.Maps.newHashMap;

import java.util.Map;

import com.b2international.snowowl.core.branch.Branch;
import com.b2international.snowowl.datastore.ICDOCommitChangeSet;
import com.b2international.snowowl.datastore.cdo.CDOCommitInfoUtils;
import com.b2international.snowowl.snomed.api.impl.domain.browser.SnomedBrowserConcept;

/**
 * Represents a traceability entry.
 */
class TraceabilityEntry {
	
	private final String userId;
	private final String commitComment;
	private final String branchPath;
	private final long commitTimestamp;
	private final Map<String, ConceptWithChanges> changes = newHashMap();
	
	public TraceabilityEntry(final ICDOCommitChangeSet changeSet) {
		this.userId = changeSet.getUserId();
		this.commitComment = CDOCommitInfoUtils.removeUuidPrefix(changeSet.getCommitComment());
		final String branchPathName = changeSet.getView().getBranch().getPathName();
		this.branchPath = Branch.BranchNameValidator.DEFAULT.getName(branchPathName);
		this.commitTimestamp = changeSet.getTimestamp();
	}

	public String getUserId() {
		return userId;
	}
	
	public String getCommitComment() {
		return commitComment;
	}

	public String getBranchPath() {
		return branchPath;
	}

	public long getCommitTimestamp() {
		return commitTimestamp;
	}

	public Map<String, ConceptWithChanges> getChanges() {
		return changes;
	}

	public void registerChange(final String conceptId, final TraceabilityChange componentChange) {
		if (!changes.containsKey(conceptId)) {
			changes.put(conceptId, new ConceptWithChanges());
		}
		
		changes.get(conceptId).addChange(componentChange);
	}

	public void setConcept(final String conceptId, final SnomedBrowserConcept convertedConcept) {
		changes.get(conceptId).setConcept(convertedConcept);
	}
}

@@ -21,9 +21,9 @@
 -201
-5
+8
  B2i
@@ -742,24 +742,47 @@
 ewHashMap;%0A%0A
+import java.util.List;%0A
 import java.
@@ -1069,16 +1069,56 @@
 Concept;
+%0Aimport com.google.common.base.Splitter;
 %0A%0A/**%0A *
@@ -1588,16 +1588,19 @@
 ent());%0A
+%09%09%0A
 %09%09final 
@@ -1610,199 +1610,907 @@
 ing 
-branchPathName = changeSet.getView().getBranch().getPathName();%0A%09%09this.branchPath = Branch.BranchNameValidator.DEFAULT.getName(branchPathName);%0A%09%09this.commitTimestamp = changeSet.getTimestamp
+changeSetBranchName = changeSet.getView().getBranch().getName();%0A%09%09final String branchPath = changeSet.getView().getBranch().getPathName();%0A%09%09if (changeSetBranchName.startsWith(Branch.TEMP_PREFIX)) %7B%0A%09%09%09final String name = Branch.BranchNameValidator.DEFAULT.getName(changeSetBranchName);%0A%09%09%09this.branchPath = buildBranchPath(name, branchPath);%0A%09%09%7D else %7B%0A%09%09%09this.branchPath = branchPath;%0A%09%09%7D%0A%09%09this.commitTimestamp = changeSet.getTimestamp();%0A%09%7D%0A%09%0A%09private String buildBranchPath(String name, String branchPathName) %7B%0A%09%09final List%3CString%3E splittedPath = Splitter.on(Branch.SEPARATOR).splitToList(branchPathName);%0A%09%09final StringBuilder builder = new StringBuilder();%0A%09%09final int splittedPathSize = splittedPath.size();%0A%09%09for (int i = 0; i %3C splittedPathSize - 1; i++) %7B%0A%09%09%09builder.append(splittedPath.get(i));%0A%09%09%09builder.append(Branch.SEPARATOR);%0A%09%09%7D%0A%09%09builder.append(name);%0A%09%09return builder.toString
 ();%0A

Build correct branchPath if the changeset's branch is a...