/*
 * Copyright 2022 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.fhir.core.request.codesystem;

import java.util.Optional;

import com.b2international.snowowl.core.RepositoryManager;
import com.b2international.snowowl.core.domain.RepositoryContext;
import com.b2international.snowowl.core.events.Request;
import com.b2international.snowowl.fhir.core.model.codesystem.CodeSystem;

/**
 * @since 8.2.0
 */
final class FhirCodeSystemPutRequest implements Request<RepositoryContext, Boolean> {

	private static final long serialVersionUID = 1L;
	
	private final CodeSystem codeSystem;
	
	public FhirCodeSystemPutRequest(CodeSystem codeSystem) {
		this.codeSystem = codeSystem;
	}

	@Override
	public Boolean execute(RepositoryContext context) {
		
		Optional<FhirCodeSystemCUDSupport> cudSupport =	context.service(RepositoryManager.class)
				.repositories()
				.stream()
				.filter(r -> r.optionalService(FhirCodeSystemCUDSupport.class).isPresent())
				.map(r -> r.service(FhirCodeSystemCUDSupport.class))
				.findFirst();
		
		if (cudSupport.isPresent()) {
			cudSupport.get().updateOrCreateCodeSystem(context, codeSystem);
			return Boolean.TRUE;
		}
		
		return Boolean.FALSE;
	}
	
}

@@ -683,16 +683,83 @@
 ystem;%0A%0A
+import static com.google.common.base.Preconditions.checkArgument;%0A%0A
 import j
@@ -777,16 +777,16 @@
 tional;%0A
-
 %0Aimport 
@@ -1195,16 +1195,64 @@
 D = 1L;%0A
+%09private static final int CONCEPT_LIMIT = 5000;%0A
 %09%0A%09priva
@@ -1390,16 +1390,16 @@
 verride%0A
-
 %09public 
@@ -1443,16 +1443,206 @@
 text) %7B%0A
+%09%09checkArgument(codeSystem.getConcepts() == null %7C%7C codeSystem.getConcepts().size() %3C CONCEPT_LIMIT, %22Maintanence of code systems with more than %25d codes is not supported.%22, CONCEPT_LIMIT);%0A
 %09%09%0A%09%09Opt

Add a concept limit to put/create requests per code system