/*
 * 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.datastore.internal.id.reservations;

import static com.google.common.base.Preconditions.checkNotNull;

import com.b2international.snowowl.datastore.BranchPathUtils;
import com.b2international.snowowl.snomed.datastore.SnomedTerminologyBrowser;
import com.b2international.snowowl.snomed.datastore.id.SnomedIdentifier;
import com.b2international.snowowl.snomed.datastore.id.reservations.Reservation;
import com.google.inject.Provider;

/**
 * Reserves all currently taken IDs by using the given {@link SnomedTerminologyBrowser#isUniqueId(com.b2international.snowowl.core.api.IBranchPath, String)}
 * 
 * @since 4.0
 */
public class UniqueInStoreReservation implements Reservation {

	private Provider<SnomedTerminologyBrowser> browser;

	public UniqueInStoreReservation(Provider<SnomedTerminologyBrowser> browser) {
		this.browser = checkNotNull(browser, "browser");
	}
	
	@Override
	public boolean includes(SnomedIdentifier identifier) {
		return !browser.get().isUniqueId(BranchPathUtils.createMainPath(), identifier.toString());
	}

}

@@ -712,64 +712,29 @@
 ort 
-static com.google.common.base.Preconditions.checkNotNull
+java.util.Collections
 ;%0A%0Ai
@@ -794,16 +794,71 @@
 hUtils;%0A
+import com.b2international.snowowl.eventbus.IEventBus;%0A
 import c
@@ -1081,16 +1081,92 @@
 vation;%0A
+import com.b2international.snowowl.snomed.datastore.request.SnomedRequests;%0A
 import c
@@ -1449,16 +1449,22 @@
 private 
+final 
 Provider
@@ -1468,41 +1468,22 @@
 der%3C
-SnomedTerminologyBrowser%3E browser
+IEventBus%3E bus
 ;%0A%0A%09
@@ -1527,259 +1527,355 @@
 der%3C
-SnomedTerminologyBrowser%3E browser) %7B%0A%09%09this.browser = checkNotNull(browser, %22browser%22);%0A%09%7D%0A%09%0A%09@Override%0A%09public boolean includes(SnomedIdentifier identifier) %7B%0A%09%09return !browser.get().isUniqueId(BranchPathUtils.createMainPath(), identifier.toString())
+IEventBus%3E bus) %7B%0A%09%09this.bus = bus;%0A%09%7D%0A%09%0A%09@Override%0A%09public boolean includes(SnomedIdentifier identifier) %7B%0A%09%09return SnomedRequests.prepareSearchConcept()%0A%09%09%09%09.setLimit(0)%0A%09%09%09%09.setComponentIds(Collections.singleton(identifier.toString()))%0A%09%09%09%09.build(BranchPathUtils.createMainPath().getPath())%0A%09%09%09%09.execute(bus.get())%0A%09%09%09%09.getSync().getTotal() %3E 0
 ;%0A%09%7D

use search request in UniqueInStoreReservation