<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.api.impl.codesystem;

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

import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.eclipse.core.runtime.IStatus;

import com.b2international.snowowl.api.codesystem.ICodeSystemVersionService;
import com.b2international.snowowl.api.codesystem.domain.ICodeSystemVersion;
import com.b2international.snowowl.api.codesystem.domain.ICodeSystemVersionProperties;
import com.b2international.snowowl.api.impl.codesystem.domain.CodeSystemVersion;
import com.b2international.snowowl.core.ApplicationContext;
import com.b2international.snowowl.core.api.IBranchPath;
import com.b2international.snowowl.core.api.SnowowlRuntimeException;
import com.b2international.snowowl.core.api.SnowowlServiceException;
import com.b2international.snowowl.core.domain.exceptions.CodeSystemNotFoundException;
import com.b2international.snowowl.core.domain.exceptions.CodeSystemVersionNotFoundException;
import com.b2international.snowowl.core.exceptions.AlreadyExistsException;
import com.b2international.snowowl.core.exceptions.BadRequestException;
import com.b2international.snowowl.core.exceptions.ConflictException;
import com.b2international.snowowl.core.exceptions.LockedException;
import com.b2international.snowowl.core.exceptions.NotFoundException;
import com.b2international.snowowl.datastore.ICodeSystem;
import com.b2international.snowowl.datastore.TerminologyRegistryService;
import com.b2international.snowowl.datastore.UserBranchPathMap;
import com.b2international.snowowl.datastore.request.RepositoryRequests;
import com.b2international.snowowl.datastore.version.VersioningService;
import com.b2international.snowowl.eventbus.IEventBus;
import com.b2international.snowowl.terminologyregistry.core.request.CodeSystemRequests;
import com.google.common.base.Function;
import com.google.common.collect.Collections2;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.common.collect.Ordering;

public class CodeSystemVersionServiceImpl implements ICodeSystemVersionService {

	private static final Function<? super com.b2international.snowowl.datastore.ICodeSystemVersion, ICodeSystemVersion> CODE_SYSTEM_VERSION_CONVERTER = 
			new Function<com.b2international.snowowl.datastore.ICodeSystemVersion, ICodeSystemVersion>() {

		@Override
		public ICodeSystemVersion apply(final com.b2international.snowowl.datastore.ICodeSystemVersion input) {
			final CodeSystemVersion result = new CodeSystemVersion();
			result.setDescription(input.getDescription());
			result.setEffectiveDate(toDate(input.getEffectiveDate()));
			result.setImportDate(toDate(input.getImportDate()));
			result.setLastModificationDate(toDate(input.getLastUpdateDate()));
			result.setParentBranchPath(input.getParentBranchPath());
			result.setPatched(input.isPatched());
			result.setVersion(input.getVersionId());
			return result;
		}

		private Date toDate(final long timeStamp) {
			return timeStamp >= 0L ? new Date(timeStamp) : null;
		}
	};

	private static final Ordering<ICodeSystemVersion> VERSION_ID_ORDERING = Ordering.natural().onResultOf(new Function<ICodeSystemVersion, String>() {
		@Override
		public String apply(final ICodeSystemVersion input) {
			return input.getVersion();
		}
	});

	private static TerminologyRegistryService getRegistryService() {
		return ApplicationContext.getServiceForClass(TerminologyRegistryService.class);
	}

	@Override
	public List<ICodeSystemVersion> getCodeSystemVersions(final String shortName) {
		checkNotNull(shortName, "Short name may not be null.");
		
		final ICodeSystem codeSystem = getCodeSystem(shortName);
		if (codeSystem == null) {
			throw new CodeSystemNotFoundException(shortName);
		}
		
		final Collection<com.b2international.snowowl.datastore.ICodeSystemVersion> versions = new CodeSystemRequests(codeSystem.getRepositoryUuid())
				.prepareSearchCodeSystemVersion()
				.setCodeSystemShortName(shortName)
				.build(IBranchPath.MAIN_BRANCH)
				.executeSync(getEventBus())
				.getVersions();
		
		return toSortedCodeSystemVersionList(versions);
	}
	
	private ICodeSystem getCodeSystem(final String shortName) {
		return getRegistryService().getCodeSystemByShortName(new UserBranchPathMap(), shortName);
	}

	@Override
	public ICodeSystemVersion getCodeSystemVersionById(final String shortName, final String versionId) {
		checkNotNull(shortName, "Short name may not be null.");
		checkNotNull(versionId, "Version identifier may not be null.");
		
		final ICodeSystem codeSystem = getCodeSystem(shortName);
		if (codeSystem == null) {
			throw new CodeSystemNotFoundException(shortName);
		}
		
		final Collection<com.b2international.snowowl.datastore.ICodeSystemVersion> versions = new CodeSystemRequests(codeSystem.getRepositoryUuid())
				.prepareSearchCodeSystemVersion()
				.setCodeSystemShortName(shortName)
				.setVersionId(versionId)
				.build(IBranchPath.MAIN_BRANCH)
				.executeSync(getEventBus())
				.getVersions();
		
		final com.b2international.snowowl.datastore.ICodeSystemVersion version = Iterables.getOnlyElement(versions, null);
		if (version == null) {
			throw new CodeSystemVersionNotFoundException(versionId);
		} else {
			return CODE_SYSTEM_VERSION_CONVERTER.apply(version);
		}<commit_msg>Remove duplicate, use private method instead.<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.api.impl.codesystem;

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

import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.eclipse.core.runtime.IStatus;

import com.b2international.snowowl.api.codesystem.ICodeSystemVersionService;
import com.b2international.snowowl.api.codesystem.domain.ICodeSystemVersion;
import com.b2international.snowowl.api.codesystem.domain.ICodeSystemVersionProperties;
import com.b2international.snowowl.api.impl.codesystem.domain.CodeSystemVersion;
import com.b2international.snowowl.core.ApplicationContext;
import com.b2international.snowowl.core.api.IBranchPath;
import com.b2international.snowowl.core.api.SnowowlRuntimeException;
import com.b2international.snowowl.core.api.SnowowlServiceException;
import com.b2international.snowowl.core.domain.exceptions.CodeSystemNotFoundException;
import com.b2international.snowowl.core.domain.exceptions.CodeSystemVersionNotFoundException;
import com.b2international.snowowl.core.exceptions.AlreadyExistsException;
import com.b2international.snowowl.core.exceptions.BadRequestException;
import com.b2international.snowowl.core.exceptions.ConflictException;
import com.b2international.snowowl.core.exceptions.LockedException;
import com.b2international.snowowl.core.exceptions.NotFoundException;
import com.b2international.snowowl.datastore.ICodeSystem;
import com.b2international.snowowl.datastore.TerminologyRegistryService;
import com.b2international.snowowl.datastore.UserBranchPathMap;
import com.b2international.snowowl.datastore.request.RepositoryRequests;
import com.b2international.snowowl.datastore.version.VersioningService;
import com.b2international.snowowl.eventbus.IEventBus;
import com.b2international.snowowl.terminologyregistry.core.request.CodeSystemRequests;
import com.google.common.base.Function;
import com.google.common.collect.Collections2;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.common.collect.Ordering;

public class CodeSystemVersionServiceImpl implements ICodeSystemVersionService {

	private static final Function<? super com.b2international.snowowl.datastore.ICodeSystemVersion, ICodeSystemVersion> CODE_SYSTEM_VERSION_CONVERTER = 
			new Function<com.b2international.snowowl.datastore.ICodeSystemVersion, ICodeSystemVersion>() {

		@Override
		public ICodeSystemVersion apply(final com.b2international.snowowl.datastore.ICodeSystemVersion input) {
			final CodeSystemVersion result = new CodeSystemVersion();
			result.setDescription(input.getDescription());
			result.setEffectiveDate(toDate(input.getEffectiveDate()));
			result.setImportDate(toDate(input.getImportDate()));
			result.setLastModificationDate(toDate(input.getLastUpdateDate()));
			result.setParentBranchPath(input.getParentBranchPath());
			result.setPatched(input.isPatched());
			result.setVersion(input.getVersionId());
			return result;
		}

		private Date toDate(final long timeStamp) {
			return timeStamp >= 0L ? new Date(timeStamp) : null;
		}
	};

	private static final Ordering<ICodeSystemVersion> VERSION_ID_ORDERING = Ordering.natural().onResultOf(new Function<ICodeSystemVersion, String>() {
		@Override
		public String apply(final ICodeSystemVersion input) {
			return input.getVersion();
		}
	});

	private static TerminologyRegistryService getRegistryService() {
		return ApplicationContext.getServiceForClass(TerminologyRegistryService.class);
	}

	@Override
	public List<ICodeSystemVersion> getCodeSystemVersions(final String shortName) {
		checkNotNull(shortName, "Short name may not be null.");
		
		final ICodeSystem codeSystem = getCodeSystem(shortName);
		if (codeSystem == null) {
			throw new CodeSystemNotFoundException(shortName);
		}
		
		final Collection<com.b2international.snowowl.datastore.ICodeSystemVersion> versions = getCodeSystemVersions(shortName,
				codeSystem.getRepositoryUuid()); 
		return toSortedCodeSystemVersionList(versions);
	}
	
	private ICodeSystem getCodeSystem(final String shortName) {
		return getRegistryService().getCodeSystemByShortName(new UserBranchPathMap(), shortName);
	}

	@Override
	public ICodeSystemVersion getCodeSystemVersionById(final String shortName, final String versionId) {
		checkNotNull(shortName, "Short name may not be null.");
		checkNotNull(versionId, "Version identifier may not be null.");
		
		final ICodeSystem codeSystem = getCodeSystem(shortName);
		if (codeSystem == null) {
			throw new CodeSystemNotFoundException(shortName);
		}
		
		final Collection<com.b2international.snowowl.datastore.ICodeSystemVersion> versions = new CodeSystemRequests(codeSystem.getRepositoryUuid())
				.prepareSearchCodeSystemVersion()
				.setCodeSystemShortName(shortName)
				.setVersionId(versionId)
				.build(IBranchPath.MAIN_BRANCH)
				.executeSync(getEventBus())
				.getVersions();
		
		final com.b2international.snowowl.datastore.ICodeSystemVersion version = Iterables.getOnlyElement(versions, null);
		if (version == null) {
			throw new CodeSystemVersionNotFoundException(versionId);
		} else {
			return CODE_SYSTEM_VERSION_CONVERTER.apply(version);
		}
{'fraction_non_alphanumeric': 0.08956359945872801, 'fraction_numerical': 0.0076116373477672535, 'mean_word_length': 6.299382716049383, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 19, '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': '5569441', 'n_tokens_mistral': 3747, 'n_tokens_neox': 3388, 'n_words': 648}