package org.icij.extract.cli;

import org.icij.extract.core.*;
import org.icij.extract.cli.options.*;
import org.icij.extract.http.PinnedHttpClientBuilder;
import org.icij.extract.solr.SolrDefaults;

import java.util.logging.Logger;

import java.io.IOException;

import org.apache.commons.cli.Option;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.ParseException;

import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.impl.HttpSolrClient;

/**
 * Extract
 *
 * @author Matthew Caruana Galizia <mcaruana@icij.org>
 * @version 1.0.0-beta
 * @since 1.0.0-beta
 */
public class SolrDeleteCli extends Cli {

	public SolrDeleteCli(Logger logger) {
		super(logger, new SolrOptionSet());

		options.addOption(Option.builder("i")
				.desc(String.format("The name of the unique ID field in the target Solr schema. Defaults to %s.", SolrDefaults.DEFAULT_ID_FIELD))
				.longOpt("id-field")
				.hasArg()
				.argName("name")
				.build())

			.addOption(Option.builder("c")
				.desc("Perform a commit when done.")
				.longOpt("commit")
				.build())

			.addOption(Option.builder()
				.desc("Perform a soft commit when done.")
				.longOpt("soft-commit")
				.build());
	}

	public CommandLine parse(String[] args) throws ParseException, RuntimeException {
		final CommandLine cmd = super.parse(args);

		final String idField = cmd.getOptionValue('i', SolrDefaults.DEFAULT_ID_FIELD);
		final String[] ids = cmd.getArgs();

		if (0 == ids.length) {
			throw new IllegalArgumentException("You must pass the IDs to delete on the command line.");
		}

		try (
			final CloseableHttpClient httpClient = PinnedHttpClientBuilder.createWithDefaults()
				.setVerifyHostname(cmd.getOptionValue("verify-host"))
				.pinCertificate(cmd.getOptionValue("pin-certificate"))
				.build();
			final SolrClient client = new HttpSolrClient(cmd.getOptionValue('s'), httpClient);
		) {
			for (String id : ids) {
				if (id.contains("*")) {
					logger.info(String.format("Deleting document matching ID pattern %s.", id));
					client.deleteByQuery(idField + ":" + id);
				} else {
					logger.info(String.format("Deleting document with ID %s.", id));
					client.deleteById(id);
				}
			}

			if (cmd.hasOption("soft-commit")) {
				client.commit(true, true, true);
			} else if (cmd.hasOption('c')) {
				client.commit(true, true, false);
			}
		} catch (SolrServerException e) {
			throw new RuntimeException("Unable to delete.", e);
		} catch (IOException e) {
			throw new RuntimeException("Unable to delete because of an error while communicating with Solr.", e);
		}

		return cmd;
	}

	public void printHelp() {
		super.printHelp(Command.SOLR_DELETE, "Delete documents from Solr.");
	}
}

@@ -1477,94 +1477,17 @@
 ring
- idField = cmd.getOptionValue('i', SolrDefaults.DEFAULT_ID_FIELD);%0A%09%09final String%5B%5D id
+%5B%5D querie
 s = 
@@ -1513,18 +1513,22 @@
 f (0 == 
-id
+querie
 s.length
@@ -1588,16 +1588,27 @@
 ass the 
+queries or 
 IDs to d
@@ -1979,15 +1979,22 @@
 ing 
-id : id
+query : querie
 s) %7B
@@ -2002,18 +2002,21 @@
 %09%09%09%09if (
-id
+query
 .contain
@@ -2018,17 +2018,17 @@
 ntains(%22
-*
+:
 %22)) %7B%0A%09%09
@@ -2074,16 +2074,17 @@
 document
+s
  matchin
@@ -2085,19 +2085,16 @@
 atching 
-ID 
 pattern 
@@ -2095,26 +2095,29 @@
 ttern %25s.%22, 
-id
+query
 ));%0A%09%09%09%09%09cli
@@ -2138,26 +2138,13 @@
 ery(
-idField + %22:%22 + id
+query
 );%0A%09
@@ -2219,18 +2219,21 @@
 D %25s.%22, 
-id
+query
 ));%0A%09%09%09%09
@@ -2251,18 +2251,21 @@
 eteById(
-id
+query
 );%0A%09%09%09%09%7D

Allow documents to be deleted by any query