/*
 * 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 static com.google.common.base.Preconditions.checkArgument;

import java.util.stream.Collectors;

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 static final int CONCEPT_LIMIT = 5000;
	
	private final CodeSystem codeSystem;
	
	public FhirCodeSystemPutRequest(CodeSystem codeSystem) {
		this.codeSystem = codeSystem;
	}

	@Override
	public Boolean execute(RepositoryContext context) {
		checkArgument(codeSystem.getConcepts() == null || codeSystem.getConcepts().size() < CONCEPT_LIMIT, "Maintenance of code systems with more than %d codes is not supported.", CONCEPT_LIMIT);
		
		FhirCodeSystemCUDSupport cudSupport =	context.service(RepositoryManager.class)
				.repositories()
				.stream()
				.filter(r -> r.optionalService(FhirCodeSystemCUDSupport.class).isPresent())
				.map(r -> r.service(FhirCodeSystemCUDSupport.class))
				.collect(Collectors.toList())
				.get(0);
		
		cudSupport.updateOrCreateCodeSystem(context, codeSystem);
		return Boolean.TRUE;			
	}
	
}

@@ -750,16 +750,39 @@
 ument;%0A%0A
+import java.util.List;%0A
 import j
@@ -1670,16 +1670,27 @@
 );%0A%09%09%0A%09%09
+final List%3C
 FhirCode
@@ -1705,16 +1705,17 @@
 DSupport
+%3E
  cudSupp
@@ -1719,17 +1719,17 @@
 upport =
-%09
+ 
 context.
@@ -1764,17 +1764,16 @@
 ass)%0A%09%09%09
-%09
 .reposit
@@ -1783,17 +1783,16 @@
 es()%0A%09%09%09
-%09
 .stream(
@@ -1797,19 +1797,19 @@
 m()%0A
-%09
 %09%09%09.f
-ilter
+latMap
 (r -
@@ -1864,79 +1864,31 @@
 ss).
-isPresent
+stream
 ())%0A
-%09
 %09%09%09.
-map(r -%3E r.service(FhirCodeSystemCUDSupport.class)
+limit(1
 )%0A
-%09
 %09%09%09.
@@ -1919,21 +1919,139 @@
 t())
+;
 %0A%09%09
+%0A
 %09%09
-.get(0);
+if (cudSupport.isEmpty()) %7B%0A%09%09%09throw new IllegalStateException(%22FHIR CodeSystem resource creation support is missing.%22);%0A%09%09%7D
 %0A%09%09%0A
@@ -2063,16 +2063,23 @@
 Support.
+get(0).
 updateOr
@@ -2146,12 +2146,10 @@
 ;%09%09%09%0A%09%7D%0A
-%09%0A
 %7D%0A

Throw exception if the required service for FHIR "write"...