/*
 * Copyright 2011-2016 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.datastore.request;

import com.b2international.snowowl.core.domain.TransactionContext;
import com.b2international.snowowl.core.events.BaseRequest;
import com.b2international.snowowl.terminologymetadata.CodeSystem;
import com.b2international.snowowl.terminologymetadata.TerminologymetadataFactory;

/**
 * @since 4.7
 */
public final class CodeSystemCreateRequest extends BaseRequest<TransactionContext, String> {

	private static final long serialVersionUID = 1L;

	private String branchPath;
	private String citation;
	private String oid;
	private String iconPath;
	private String language;
	private String link;
	private String name;
	private String repositoryUuid;
	private String shortName;
	private String terminologyId;
	
	CodeSystemCreateRequest() {
	}

	void setBranchPath(String branchPath) {
		this.branchPath = branchPath;
	}

	void setCitation(String citation) {
		this.citation = citation;
	}

	void setOid(String oid) {
		this.oid = oid;
	}

	void setIconPath(String iconPath) {
		this.iconPath = iconPath;
	}

	void setLanguage(String language) {
		this.language = language;
	}

	void setLink(String link) {
		this.link = link;
	}

	void setName(String name) {
		this.name = name;
	}

	void setRepositoryUuid(String repositoryUuid) {
		this.repositoryUuid = repositoryUuid;
	}

	void setShortName(String shortName) {
		this.shortName = shortName;
	}

	void setTerminologyId(String terminologyId) {
		this.terminologyId = terminologyId;
	}

	@Override
	public String execute(final TransactionContext context) {
		// TODO check if code system exists
		final CodeSystem codeSystem = createCodeSystem();
		context.add(codeSystem);

		return codeSystem.getShortName();
	}

	private CodeSystem createCodeSystem() {
		final CodeSystem codeSystem = TerminologymetadataFactory.eINSTANCE.createCodeSystem();
		codeSystem.setBranchPath(branchPath);
		codeSystem.setCitation(citation);
		codeSystem.setCodeSystemOID(oid);
		codeSystem.setIconPath(iconPath);
		codeSystem.setLanguage(language);
		codeSystem.setMaintainingOrganizationLink(link);
		codeSystem.setName(name);
		codeSystem.setRepositoryUuid(repositoryUuid);
		codeSystem.setShortName(shortName);
		codeSystem.setTerminologyComponentId(terminologyId);

		return codeSystem;
	}

	@Override
	protected Class<String> getReturnType() {
		return String.class;
	}

}

@@ -804,16 +804,287 @@
 equest;%0A
+import com.b2international.snowowl.core.exceptions.BadRequestException;%0Aimport com.b2international.snowowl.datastore.BranchPathUtils;%0Aimport com.b2international.snowowl.datastore.TerminologyRegistryService;%0Aimport com.b2international.snowowl.datastore.UserBranchPathMap;%0A
 import c
@@ -1142,16 +1142,16 @@
 System;%0A
-
 import c
@@ -1658,10 +1658,9 @@
 Id;%0A
-%09
 %0A
+
 %09Cod
@@ -2458,43 +2458,45 @@
 %7B%0A%09%09
-// TODO check if code system exists
+ensureOidAndShortNameUnique(context);
 %0A%09%09f
@@ -2568,24 +2568,24 @@
 deSystem);%0A%0A
-
 %09%09return cod
@@ -2612,16 +2612,736 @@
 ();%0A%09%7D%0A%0A
+%09private void ensureOidAndShortNameUnique(final TransactionContext context) %7B%0A%09%09final UserBranchPathMap branchPathMap = new UserBranchPathMap();%0A%09%09branchPathMap.putBranchPath(repositoryUuid, BranchPathUtils.createMainPath());%0A%0A%09%09final TerminologyRegistryService registryService = context.service(TerminologyRegistryService.class);%0A%0A%09%09if (registryService.getCodeSystemByOid(branchPathMap, oid) != null) %7B%0A%09%09%09throw new BadRequestException(%22Couldn't create code system with OID %25s as it is not unique.%22, oid);%0A%09%09%7D%0A%0A%09%09if (registryService.getCodeSystemByShortName(branchPathMap, shortName) != null) %7B%0A%09%09%09throw new BadRequestException(%22Couldn't create code system with short name %25s as it is not unique.%22, shortName);%0A%09%09%7D%0A%09%7D%0A%0A
 %09private

Check if oid and short name attributes are unique.