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

import java.util.Map;

import javax.annotation.Nullable;

import com.b2international.snowowl.datastore.oplock.IOperationLockTarget;
import com.b2international.snowowl.datastore.oplock.OperationLockException;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;

/**
 * Thrown when reporting issues related to locking any terminology store.
 * 
 */
public class DatastoreOperationLockException extends OperationLockException {

	private static final long serialVersionUID = 1L;

	private final Map<IOperationLockTarget, DatastoreLockContext> targetMap;

	/**
	 * Creates a new instance with the specified detail message.
	 * 
	 * @param message the detail message
	 */
	public DatastoreOperationLockException(final @Nullable String message) {
		this(message, ImmutableMap.<IOperationLockTarget, DatastoreLockContext>of());
	}

	/**
	 * Creates a new instance with the specified detail message and lock targets which could not be acquired or released.
	 * 
	 * @param message the detail message
	 * @param targetMap a map keyed by lock targets with issues; the corresponding values are the lock contexts
	 * currently holding the requested lock, or a conflicting lock (may not be {@code null})
	 */
	public DatastoreOperationLockException(final @Nullable String message, final Map<IOperationLockTarget, DatastoreLockContext> targetMap) {
		super(message);
		Preconditions.checkNotNull(targetMap, "Lock target map may not be null.");
		this.targetMap = ImmutableMap.copyOf(targetMap);
	}
	
	/**
	 * Returns the current owner of the requested lock target, or {@code null} if no issue has been recorded for this target.
	 * 
	 * @param target the lock target to check (may not be {@code null})
	 * @return the current owner for the specified target, or {@code null}
	 */
	public DatastoreLockContext getContext(final IOperationLockTarget target) {
		Preconditions.checkNotNull(target, "Lock target to check may not be null.");
		return targetMap.get(target);
	}
}
@@ -919,29 +919,159 @@
 on.base.
-Preconditions
+Optional;%0Aimport com.google.common.base.Preconditions;%0Aimport com.google.common.base.Predicate;%0Aimport com.google.common.collect.FluentIterable
 ;%0Aimport
@@ -2807,9 +2807,787 @@
 et);%0A%09%7D%0A
+%09%0A%09@Override%0A%09public String getMessage() %7B%0A%09%09final FluentIterable%3CDatastoreLockContext%3E contexts = FluentIterable.from(targetMap.values());%0A%09%09final Optional%3CDatastoreLockContext%3E rootContext = contexts.firstMatch(new Predicate%3CDatastoreLockContext%3E() %7B%0A%09%09%09@Override%0A%09%09%09public boolean apply(DatastoreLockContext input) %7B%0A%09%09%09%09return DatastoreLockContextDescriptions.ROOT.equals(input.getParentDescription());%0A%09%09%09%7D%0A%09%09%7D);%0A%09%09%0A%09%09DatastoreLockContext context = null;%0A%09%09if (rootContext.isPresent()) %7B%0A%09%09%09context = rootContext.get();%0A%09%09%7D else %7B%0A%09%09%09if (contexts.first().isPresent()) %7B%0A%09%09%09%09context = contexts.first().get();%0A%09%09%09%7D else %7B%0A%09%09%09%09return super.getMessage();%0A%09%09%09%7D%0A%09%09%7D%0A%09%09%0A%09%09return String.format(%22%25s %25s is %25s.%22, super.getMessage(), context.getUserId(), context.getDescription());%0A%09%7D%0A
 %7D

Use a more meaningful message if possible.