/*
 * 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.CoreTerminologyBroker;
import com.b2international.snowowl.core.date.EffectiveTimes;
import com.b2international.snowowl.snomed.core.domain.ISnomedConcept;
import com.b2international.snowowl.snomed.core.domain.SnomedReferenceSet;
import com.b2international.snowowl.snomed.datastore.index.refset.SnomedRefSetIndexEntry;
import com.b2international.snowowl.snomed.datastore.server.domain.SnomedReferenceSetImpl;
import com.b2international.snowowl.snomed.snomedrefset.SnomedRefSet;
import com.google.common.base.Function;

/**
 * @since 4.5
 */
class SnomedReferenceSetConverter implements Function<SnomedRefSetIndexEntry, SnomedReferenceSet> {

	@Override
	public SnomedReferenceSet apply(SnomedRefSetIndexEntry entry) {
		final SnomedReferenceSetImpl refset = new SnomedReferenceSetImpl();
		refset.setId(entry.getId());
		refset.setEffectiveTime(EffectiveTimes.toDate(entry.getEffectiveTimeAsLong()));
		refset.setActive(entry.isActive());
		refset.setReleased(entry.isReleased());
		refset.setModuleId(entry.getModuleId());
		refset.setReferencedComponent(CoreTerminologyBroker.getInstance().getComponentInformation(entry.getReferencedComponentType()).getName());
		refset.setType(entry.getType());
		return refset;
	}
	
	public SnomedReferenceSet apply(SnomedRefSet refSet, ISnomedConcept concept) {
		final SnomedReferenceSetImpl refset = new SnomedReferenceSetImpl();
		refset.setId(concept.getId());
		refset.setEffectiveTime(concept.getEffectiveTime());
		refset.setActive(concept.isActive());
		refset.setReleased(concept.isReleased());
		refset.setModuleId(concept.getModuleId());
		refset.setReferencedComponent(CoreTerminologyBroker.getInstance().getComponentInformation(refSet.getReferencedComponentType()).getName());
		refset.setType(refSet.getType());
		return refset;
	}
	
}

@@ -1750,35 +1750,37 @@
 uleId());%0A%09%09
-refset.setR
+final short r
 eferencedCom
@@ -1789,75 +1789,83 @@
 nent
-(CoreTerminologyBroker.getInstance().getComponentInformati
+Type = entry.getReferencedComponentType();%0A%09%09refset.setReferencedComp
 on
-(
 ent
-ry.
+(
 getR
@@ -1883,35 +1883,47 @@
 mponentType(
-)).getName(
+referencedComponentType
 ));%0A%09%09refset
@@ -2336,35 +2336,37 @@
 uleId());%0A%09%09
-refset.setR
+final short r
 eferencedCom
@@ -2375,76 +2375,84 @@
 nent
-(CoreTerminologyBroker.getInstance().getComponentInformation(refSet.
+Type = refSet.getReferencedComponentType();%0A%09%09refset.setReferencedComponent(
 getR
@@ -2478,19 +2478,31 @@
 ype(
-)).getName(
+referencedComponentType
 ));%0A
@@ -2556,13 +2556,202 @@
 fset;%0A%09%7D
+%0A%0A%09private String getReferencedComponentType(final short referencedComponentType) %7B%0A%09%09return CoreTerminologyBroker.getInstance().getComponentInformation(referencedComponentType).getId();%0A%09%7D
 %0A%09%0A%7D%0A

use term comp ID instead of the name in refset representation