package org.springframework.content.solr;

import java.io.IOException;
import java.io.InputStream;

import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.request.ContentStreamUpdateRequest;
import org.apache.solr.client.solrj.request.UpdateRequest;
import org.apache.solr.common.util.ContentStreamBase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.content.commons.annotations.ContentId;
import org.springframework.content.commons.annotations.ContentRepositoryEventHandler;
import org.springframework.content.commons.operations.ContentOperations;
import org.springframework.content.commons.repository.ContentAccessException;
import org.springframework.content.commons.repository.events.AbstractContentRepositoryEventListener;
import org.springframework.content.commons.utils.BeanUtils;
import org.springframework.util.Assert;

@ContentRepositoryEventHandler
public class SolrUpdateEventHandler extends AbstractContentRepositoryEventListener<Object> {

	private SolrClient solrClient;
	
	@Autowired private ContentOperations ops;
	@Autowired private SolrProperties properties;
	
	public SolrUpdateEventHandler(SolrClient solrCient) {
		this.solrClient = solrCient;
	}

	public SolrUpdateEventHandler(SolrClient solrCient, ContentOperations ops) {
		this.solrClient = solrCient;
		this.ops = ops;
	}

	@Override
	protected void onAfterSetContent(Object contentEntity) {
		if (BeanUtils.hasFieldWithAnnotation(contentEntity, ContentId.class) == false) {
			return;
		}

		if (BeanUtils.getFieldWithAnnotation(contentEntity, ContentId.class) == null) {
			return;
		}

	    ContentStreamUpdateRequest up = new ContentStreamUpdateRequest("/update/extract");
		if (properties.getUser() != "") {
			up.setBasicAuthCredentials(properties.getUser(), properties.getPassword());
		}
		up.addContentStream(new ContentEntityStream(ops, contentEntity));
		String id = BeanUtils.getFieldWithAnnotation(contentEntity, ContentId.class).toString();
	    up.setParam("literal.id", id);
	    up.setAction(org.apache.solr.client.solrj.request.AbstractUpdateRequest.ACTION.COMMIT, true, true);
	    try {
			/*NamedList<Object> request = */solrClient.request(up, null);
		} catch (SolrServerException e) {
			throw new ContentAccessException(String.format("Error updating entry in solr index %s", id), e);
		} catch (IOException e) {
			throw new ContentAccessException(String.format("Error updating entry in solr index %s", id), e);
		}
	}
	
	@Override
	protected void onBeforeUnsetContent(Object contentEntity) {
		if (BeanUtils.hasFieldWithAnnotation(contentEntity, ContentId.class) == false) {
			return;
		}
		
		Object id = BeanUtils.getFieldWithAnnotation(contentEntity, ContentId.class); 
		if (id == null) {
			return;
		}

		UpdateRequest request = new UpdateRequest();
		request.deleteById(id.toString());
		if (properties.getUser() != "") {
			request.setBasicAuthCredentials(properties.getUser(), properties.getPassword());
		}
		try {
			request.process(solrClient);
		} catch (SolrServerException e) {
			throw new ContentAccessException(String.format("Error deleting entry from solr index %s", id.toString()), e);
		} catch (IOException e) {
			throw new ContentAccessException(String.format("Error deleting entry from solr index %s", id.toString()), e);
		}
	}


	public class ContentEntityStream extends ContentStreamBase {

		private ContentOperations ops;
		private Object contentEntity;
		
		public ContentEntityStream(ContentOperations ops, Object contentEntity) {
			Assert.notNull(ops, "ConentOperations cannot be null");
			Assert.notNull(contentEntity, "ContentEntity cannot be null");
			this.ops = ops;
			this.contentEntity = contentEntity;
		}
		
		@Override
		public InputStream getStream() throws IOException {
			return ops.getContent(contentEntity);
		}
		
	}
	
}

@@ -2715,18 +2715,16 @@
 rn;%0A%09%09%7D%0A
-%09%09
 %0A%09%09Objec
@@ -2795,17 +2795,16 @@
 .class);
- 
 %0A%09%09if (i
@@ -2848,23 +2848,18 @@
 Request 
-request
+up
  = new U
@@ -2876,23 +2876,120 @@
 st();%0A%09%09
-request
+up.setAction(org.apache.solr.client.solrj.request.AbstractUpdateRequest.ACTION.COMMIT, true, true);%0A%09%09up
 .deleteB
@@ -3006,32 +3006,33 @@
 tring());%0A%09%09if (
+!
 properties.getUs
@@ -3039,28 +3039,27 @@
 er()
- != %22%22) %7B%0A%09%09%09request
+.isEmpty()) %7B%0A%09%09%09up
 .set
@@ -3147,34 +3147,29 @@
 %0A%09%09%09
-request.process(solrClient
+solrClient.request(up
 );%0A%09

change request from process to request [#137736229](https://www.pivotaltracker.com/story/show/137736229)