<commit_before>/*
 * 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.converter;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

import com.b2international.snowowl.core.CoreTerminologyBroker;
import com.b2international.snowowl.core.date.EffectiveTimes;
import com.b2international.snowowl.core.domain.BranchContext;
import com.b2international.snowowl.core.domain.CollectionResource;
import com.b2international.snowowl.snomed.common.SnomedRf2Headers;
import com.b2international.snowowl.snomed.common.SnomedTerminologyComponentConstants;
import com.b2international.snowowl.snomed.core.domain.SnomedConcept;
import com.b2international.snowowl.snomed.core.domain.SnomedCoreComponent;
import com.b2international.snowowl.snomed.core.domain.SnomedDescription;
import com.b2international.snowowl.snomed.core.domain.SnomedRelationship;
import com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember;
import com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMemberImpl;
import com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMembers;
import com.b2international.snowowl.snomed.datastore.index.entry.SnomedRefSetMemberIndexEntry;
import com.b2international.snowowl.snomed.snomedrefset.SnomedQueryRefSetMember;
import com.b2international.snowowl.snomed.snomedrefset.SnomedRefSetMember;
import com.b2international.snowowl.snomed.snomedrefset.SnomedRefSetType;
import com.google.common.base.Function;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder;

/**
 * @since 4.5
 */
public class SnomedReferenceSetMemberConverter implements ResourceConverter<SnomedRefSetMemberIndexEntry, SnomedRefSetMember, SnomedReferenceSetMember, SnomedReferenceSetMembers> {

	private final BranchContext context;
	private final List<String> expand;

	SnomedReferenceSetMemberConverter(BranchContext context, List<String> expand) {
		this.context = context;
		this.expand = expand;
	}

	@Override
	public SnomedReferenceSetMember convert(SnomedRefSetMemberIndexEntry component) {
		return convert(Collections.singleton(component)).getItems().iterator().next();
	}
	
	@Override
	public SnomedReferenceSetMember convert(SnomedRefSetMember object) {
		final SnomedReferenceSetMemberImpl member = new SnomedReferenceSetMemberImpl();
		member.setId(object.getUuid());
		member.setEffectiveTime(object.getEffectiveTime());
		member.setReleased(object.isReleased());
		member.setActive(object.isActive());
		member.setModuleId(object.getModuleId());
		member.setReferenceSetId(object.getRefSetIdentifierId());
		if (object instanceof SnomedQueryRefSetMember) {
			final Builder<String, Object> props = ImmutableMap.builder();
			props.put(SnomedRf2Headers.FIELD_QUERY, ((SnomedQueryRefSetMember) object).getQuery());
			member.setProperties(props.build());
		}
		expandReferencedComponent(member, object.getReferencedComponentId(), object.getReferencedComponentType());
		return member;
	}
	
	@Override
	public SnomedReferenceSetMembers convert(Collection<SnomedRefSetMemberIndexEntry> components) {
		return new SnomedReferenceSetMembers(
				FluentIterable
				.from(components)
				.transform(new Function<SnomedRefSetMemberIndexEntry, SnomedReferenceSetMember>() {
					@Override
					public SnomedReferenceSetMember apply(SnomedRefSetMemberIndexEntry input) {
						return toResource(input);
					}
				})
				// TODO add expansion
				.toList());
	}

	private SnomedReferenceSetMember toResource(SnomedRefSetMemberIndexEntry entry) {
		final SnomedReferenceSetMemberImpl member = new SnomedReferenceSetMemberImpl();
		member.setId(entry.getId());
		member.setEffectiveTime(EffectiveTimes.toDate(entry.getEffectiveTimeAsLong()));
		member.setReleased(entry.isReleased());
		member.setActive(entry.isActive());
		member.setModuleId(entry.getModuleId());
		member.setReferenceSetId(entry.getRefSetIdentifierId());
		if (SnomedRefSetType.QUERY == entry.getRefSetType()) {
			// in case of query type refset the actual ESCG query is stored in the specialFieldId prop
			final Builder<String, Object> props = ImmutableMap.builder();
			props.put(SnomedRf2Headers.FIELD_QUERY, entry.getQuery());
			member.setProperties(props.build());
		}
		expandReferencedComponent(member, entry.getReferencedComponentId(), entry.getReferencedComponentType());
		return member;
	}
	
	private void expandReferencedComponent(SnomedReferenceSetMemberImpl member, String referencedComponentId, String referencedComponentType) {
		final SnomedCoreComponent component;
		switch (referencedComponentType) {
		// TODO support query type refset refcomp expansion, currently it's a concept
		case SnomedTerminologyComponentConstants.REFSET:
		case SnomedTerminologyComponentConstants.CONCEPT:
			component = new SnomedConcept();<commit_msg>SO-1710: support referencedComponent expansion in member converter<commit_after>/*
 * 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.converter;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

import com.b2international.snowowl.core.CoreTerminologyBroker;
import com.b2international.snowowl.core.date.EffectiveTimes;
import com.b2international.snowowl.core.domain.BranchContext;
import com.b2international.snowowl.core.domain.CollectionResource;
import com.b2international.snowowl.core.terminology.ComponentCategory;
import com.b2international.snowowl.snomed.common.SnomedRf2Headers;
import com.b2international.snowowl.snomed.common.SnomedTerminologyComponentConstants;
import com.b2international.snowowl.snomed.core.domain.SnomedConcept;
import com.b2international.snowowl.snomed.core.domain.SnomedCoreComponent;
import com.b2international.snowowl.snomed.core.domain.SnomedDescription;
import com.b2international.snowowl.snomed.core.domain.SnomedRelationship;
import com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember;
import com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMemberImpl;
import com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMembers;
import com.b2international.snowowl.snomed.datastore.id.SnomedIdentifiers;
import com.b2international.snowowl.snomed.datastore.index.entry.SnomedRefSetMemberIndexEntry;
import com.b2international.snowowl.snomed.datastore.server.request.SearchRequestBuilder;
import com.b2international.snowowl.snomed.datastore.server.request.SnomedRequests;
import com.b2international.snowowl.snomed.snomedrefset.SnomedQueryRefSetMember;
import com.b2international.snowowl.snomed.snomedrefset.SnomedRefSetMember;
import com.b2international.snowowl.snomed.snomedrefset.SnomedRefSetType;
import com.google.common.base.Function;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder;
import com.google.common.collect.Multimap;

/**
 * @since 4.5
 */
public class SnomedReferenceSetMemberConverter implements ResourceConverter<SnomedRefSetMemberIndexEntry, SnomedRefSetMember, SnomedReferenceSetMember, SnomedReferenceSetMembers> {

	private final BranchContext context;
	private final List<String> expand;

	SnomedReferenceSetMemberConverter(BranchContext context, List<String> expand) {
		this.context = context;
		this.expand = expand == null ? Collections.<String>emptyList() : expand;
	}

	@Override
	public SnomedReferenceSetMember convert(SnomedRefSetMemberIndexEntry component) {
		return convert(Collections.singleton(component)).getItems().iterator().next();
	}
	
	@Override
	public SnomedReferenceSetMember convert(SnomedRefSetMember object) {
		final SnomedReferenceSetMemberImpl member = new SnomedReferenceSetMemberImpl();
		member.setId(object.getUuid());
		member.setEffectiveTime(object.getEffectiveTime());
		member.setReleased(object.isReleased());
		member.setActive(object.isActive());
		member.setModuleId(object.getModuleId());
		member.setReferenceSetId(object.getRefSetIdentifierId());
		if (object instanceof SnomedQueryRefSetMember) {
			final Builder<String, Object> props = ImmutableMap.builder();
			props.put(SnomedRf2Headers.FIELD_QUERY, ((SnomedQueryRefSetMember) object).getQuery());
			member.setProperties(props.build());
		}
		expandReferencedComponent(member, object.getReferencedComponentId(), object.getReferencedComponentType());
		return member;
	}
	
	@Override
	public SnomedReferenceSetMembers convert(Collection<SnomedRefSetMemberIndexEntry> components) {
		final List<SnomedReferenceSetMember> members = FluentIterable
			.from(components)
			.transform(new Function<SnomedRefSetMemberIndexEntry, SnomedReferenceSetMember>() {
				@Override
				public SnomedReferenceSetMember apply(SnomedRefSetMemberIndexEntry input) {
					return toResource(input);
				}
			}).toList();
		expand(members);
		return new SnomedReferenceSetMembers(members);
	}

	private void expand(List<SnomedReferenceSetMember> members) {
		if (expand.contains("referencedComponent")) {
			final Multimap<String, SnomedReferenceSetMember> refCompToMembers = FluentIterable.from(members).index(new Function<SnomedReferenceSetMember, String>() {
				@Override
				public String apply(SnomedReferenceSetMember input) {
					return input.getReferencedComponent().getId();
				}
			});
			final Multimap<ComponentCategory, String> typeToIds = FluentIterable.from(refCompToMembers.keySet()).index(new Function<String, ComponentCategory>() {
				@Override
				public ComponentCategory apply(String input) {
					return SnomedIdentifiers.getComponentCategory(input);
				}
			});
			// query components
			for (ComponentCategory category : typeToIds.keySet()) {
				final Collection<String> componentIds = typeToIds.get(category);
				final SearchRequestBuilder<?, ? extends CollectionResource<? extends SnomedCoreComponent>> search;
				switch (category) {
				case CONCEPT:
					search = SnomedRequests.prepareConceptSearch();
					break;
				case DESCRIPTION:
					search = SnomedRequests.prepareDescriptionSearch();
					break;
//				case RELATIONSHIP:
//					search = SnomedRequests.prepareRelationshipSearch();
//					break;
				default: throw new UnsupportedOperationException("Category is not supported in referenced component expansion");
				}
				// TODO paging in expansion
				// TODO async execution with Promise.all()
				for (SnomedCoreComponent component : search.setComponentIds(componentIds).setLimit(componentIds.size()).build().execute(context)) {
					for (SnomedReferenceSetMember member : refCompToMembers.get(component.getId())) {
						((SnomedReferenceSetMemberImpl) member).setReferencedComponent(component);
					}
				}
			}
		}
	}

	private SnomedReferenceSetMember toResource(SnomedRefSetMemberIndexEntry entry) {
		final SnomedReferenceSetMemberImpl member = new SnomedReferenceSetMemberImpl();
		member.setId(entry.getId());
		member.setEffectiveTime(EffectiveTimes.toDate(entry.getEffectiveTimeAsLong()));
		member.setReleased(entry.isReleased());
		member.setActive(entry.isActive());
		member.setModuleId(entry.getModuleId());
		member.setReferenceSetId(entry.getRefSetIdentifierId());
		if (SnomedRefSetType.QUERY == entry.getRefSetType()) {
			// in case of query type refset the actual ESCG query is stored in the specialFieldId prop
			final Builder<String, Object> props = ImmutableMap.builder();
			props.put(SnomedRf2Headers.FIELD_QUERY, entry.getQuery());
			member.setProperties(props.build());
		}
		expandReferencedComponent(member, entry.getReferencedComponentId(), entry.getReferencedComponentType());
		return member;
	}
	
	private void expandReferencedComponent(SnomedReferenceSetMemberImpl member, String referencedComponentId, String referencedComponentType) {
		final SnomedCoreComponent component;
		switch (referencedComponentType) {
		// TODO support query type refset refcomp expansion, currently it's a concept
		case SnomedTerminologyComponentConstants.REFSET:
		case SnomedTerminologyComponentConstants.CONCEPT:
			component = new SnomedConcept();
{'fraction_non_alphanumeric': 0.09339048048725468, 'fraction_numerical': 0.006165877133619069, 'mean_word_length': 6.224334600760456, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7398122', 'n_tokens_mistral': 4171, 'n_tokens_neox': 3767, 'n_words': 727}