package ca.uhn.fhir.cli;

/*-
 * #%L
 * HAPI FHIR - Command Line Client - API
 * %%
 * Copyright (C) 2014 - 2019 University Health Network
 * %%
 * 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.
 * #L%
 */

import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.context.FhirVersionEnum;
import ca.uhn.fhir.jpa.term.IHapiTerminologyLoaderSvc;
import ca.uhn.fhir.rest.client.api.IGenericClient;
import ca.uhn.fhir.rest.client.interceptor.LoggingInterceptor;
import ca.uhn.fhir.util.ParametersUtil;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.hl7.fhir.instance.model.api.IBaseParameters;
import org.hl7.fhir.r4.model.CodeSystem;

import static org.apache.commons.lang3.StringUtils.isBlank;

public class UploadTerminologyCommand extends BaseCommand {
	// TODO: Don't use qualified names for loggers in HAPI CLI.
	private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(UploadTerminologyCommand.class);
	private static final String UPLOAD_EXTERNAL_CODE_SYSTEM = "upload-external-code-system";

	@Override
	public String getCommandDescription() {
		return "Uploads a terminology package (e.g. a SNOMED CT ZIP file) to a server, using the $" + UPLOAD_EXTERNAL_CODE_SYSTEM + " operation.";
	}

	@Override
	public String getCommandName() {
		return "upload-terminology";
	}

	@Override
	public Options getOptions() {
		Options options = new Options();

		addFhirVersionOption(options);
		addBaseUrlOption(options);
		addRequiredOption(options, "u", "url", true, "The code system URL associated with this upload (e.g. " + IHapiTerminologyLoaderSvc.SCT_URI + ")");
		addOptionalOption(options, "d", "data", true, "Local file to use to upload (can be a raw file or a ZIP containing the raw file)");
		addOptionalOption(options, null, "custom", false, "Indicates that this upload uses the HAPI FHIR custom external terminology format");
		addBasicAuthOption(options);
		addVerboseLoggingOption(options);

		return options;
	}

	@Override
	public void run(CommandLine theCommandLine) throws ParseException {
		parseFhirContext(theCommandLine);
		FhirContext ctx = getFhirContext();

		String termUrl = theCommandLine.getOptionValue("u");
		if (isBlank(termUrl)) {
			throw new ParseException("No URL provided");
		}

		String[] datafile = theCommandLine.getOptionValues("d");
		if (datafile == null || datafile.length == 0) {
			throw new ParseException("No data file provided");
		}

		IGenericClient client = super.newClient(theCommandLine);
		IBaseParameters inputParameters = ParametersUtil.newInstance(myFhirCtx);
		ParametersUtil.addParameterToParametersUri(myFhirCtx, inputParameters, "url", termUrl);
		for (String next : datafile) {
			ParametersUtil.addParameterToParametersString(myFhirCtx, inputParameters, "localfile", next);
		}
		if (theCommandLine.hasOption("custom")) {
			ParametersUtil.addParameterToParametersCode(myFhirCtx, inputParameters, "contentMode", "custom");
		}

		if (theCommandLine.hasOption(VERBOSE_LOGGING_PARAM)) {
			client.registerInterceptor(new LoggingInterceptor(true));
		}

		ourLog.info("Beginning upload - This may take a while...");
		IBaseParameters response = client
			.operation()
			.onType(CodeSystem.class)
			.named(UPLOAD_EXTERNAL_CODE_SYSTEM)
			.withParameters(inputParameters)
			.execute();

		ourLog.info("Upload complete!");
		ourLog.info("Response:\n{}", ctx.newXmlParser().setPrettyPrint(true).encodeResourceToString(response));
	}

}

@@ -750,52 +750,8 @@
 xt;%0A
-import ca.uhn.fhir.context.FhirVersionEnum;%0A
 impo
@@ -3644,16 +3644,17 @@
 e...%22);%0A
+%0A
 %09%09IBaseP
@@ -3663,16 +3663,94 @@
 ameters 
+response;%0A%09%09switch (myFhirCtx.getVersion().getVersion()) %7B%0A%09%09%09case DSTU3:%0A%09%09%09%09
 response
@@ -3762,16 +3762,18 @@
 ient%0A%09%09%09
+%09
+%09
 .operati
@@ -3784,16 +3784,18 @@
 %0A%09%09%09
+%09%09
 .onType(
 Code
@@ -3790,16 +3790,41 @@
 .onType(
+org.hl7.fhir.dstu3.model.
 CodeSyst
@@ -3836,16 +3836,18 @@
 ass)%0A%09%09%09
+%09%09
 .named(U
@@ -3874,16 +3874,18 @@
 SYSTEM)%0A
+%09%09
 %09%09%09.with
@@ -3915,16 +3915,18 @@
 ers)%0A%09%09%09
+%09%09
 .execute
@@ -3928,16 +3928,360 @@
 ecute();
+%0A%09%09%09%09break;%0A%09%09%09case R4:%0A%09%09%09%09response = client%0A%09%09%09%09%09.operation()%0A%09%09%09%09%09.onType(CodeSystem.class)%0A%09%09%09%09%09.named(UPLOAD_EXTERNAL_CODE_SYSTEM)%0A%09%09%09%09%09.withParameters(inputParameters)%0A%09%09%09%09%09.execute();%0A%09%09%09%09break;%0A%09%09%09default:%0A%09%09%09%09throw new UnsupportedOperationException(%22This command does not currently support %22 + myFhirCtx.getVersion().getVersion());%0A%09%09%7D
 %0A%0A%09%09ourL

Enable support of DSTU3 for upload-terminology command.