/*
 * 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.server.request;

import com.b2international.snowowl.core.api.IBranchPath;
import com.b2international.snowowl.core.domain.BranchContext;
import com.b2international.snowowl.core.exceptions.ComponentNotFoundException;
import com.b2international.snowowl.core.terminology.ComponentCategory;
import com.b2international.snowowl.snomed.core.domain.SnomedReferenceSet;
import com.b2international.snowowl.snomed.datastore.SnomedRefSetBrowser;
import com.b2international.snowowl.snomed.datastore.SnomedRefSetLookupService;
import com.b2international.snowowl.snomed.datastore.index.refset.SnomedRefSetIndexEntry;

/**
 * @since 4.5
 */
final class SnomedRefSetReadRequest extends SnomedRefSetRequest<BranchContext, SnomedReferenceSet> {

	private String referenceSetId;

	protected SnomedRefSetReadRequest(String referenceSetId) {
		this.referenceSetId = referenceSetId;
	}

	@Override
	public SnomedReferenceSet execute(BranchContext context) {
		final IBranchPath branch = context.branch().branchPath();
		final SnomedRefSetLookupService lookupService = new SnomedRefSetLookupService();
		if (!lookupService.exists(branch, referenceSetId)) {
			throw new ComponentNotFoundException(ComponentCategory.SET, referenceSetId);
		} else {
			final SnomedRefSetBrowser browser = context.service(SnomedRefSetBrowser.class);
			final SnomedRefSetIndexEntry entry = browser.getRefSet(branch, referenceSetId);
			return new SnomedReferenceSetConverter().apply(entry);
		}
	}
	
	@Override
	protected Class<SnomedReferenceSet> getReturnType() {
		return SnomedReferenceSet.class;
	}

}

@@ -927,189 +927,45 @@
 owl.
-core.terminology.ComponentCategory;%0Aimport com.b2international.snowowl.snomed.core.domain.SnomedReferenceSet;%0Aimport com.b2international.snowowl.snomed.datastore.SnomedRefSetBrowser
+snomed.core.domain.SnomedReferenceSet
 ;%0Aim
@@ -1612,13 +1612,45 @@
 ;%0A%09%09
-if (!
+final SnomedRefSetIndexEntry entry = 
 look
@@ -1663,14 +1663,20 @@
 ice.
-exists
+getComponent
 (bra
@@ -1695,16 +1695,37 @@
 ceSetId)
+;%0A%09%09if (entry == null
 ) %7B%0A%09%09%09t
@@ -1764,29 +1764,23 @@
 ion(
-ComponentCategory.SET
+%22Reference Set%22
 , re
@@ -1809,174 +1809,8 @@
 e %7B%0A
-%09%09%09final SnomedRefSetBrowser browser = context.service(SnomedRefSetBrowser.class);%0A%09%09%09final SnomedRefSetIndexEntry entry = browser.getRefSet(branch, referenceSetId);%0A
 %09%09%09r

use lookupservice only in SnomedRefSetReadRequest