package org.fedorahosted.flies.core.action;

import org.fedorahosted.flies.core.model.HCommunity;
import org.fedorahosted.flies.core.model.HIterationProject;
import org.fedorahosted.flies.repository.model.HDocument;
import org.fedorahosted.flies.repository.model.HParentResource;
import org.fedorahosted.flies.repository.model.HTextFlow;
import org.hibernate.CacheMode;
import org.hibernate.FlushMode;
import org.hibernate.ScrollMode;
import org.hibernate.ScrollableResults;
import org.hibernate.Transaction;
import org.hibernate.search.FullTextSession;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.faces.FacesMessages;
import org.jboss.seam.log.Log;

@Scope(ScopeType.STATELESS)
@Name("adminAction")
public class AdminActionBean implements AdminAction {
	
	private static final int BATCH_SIZE = 500;
	
	@Logger 
	private Log log;
	
	@In
	FullTextSession fullTextSession;
	
	/* (non-Javadoc)
	 * @see org.fedorahosted.flies.core.action.AdminAction#reindexDatabase()
	 */
	public void reindexDatabase() {
		log.info("Re-indexing started");
		reindex(HCommunity.class);
		reindex(HIterationProject.class);
		reindex(HDocument.class);
		reindex(HParentResource.class);
		reindex(HTextFlow.class);
		log.info("Re-indexing finished");
		FacesMessages.instance().add("Re-indexing finished");
	}
	
	private void reindex (Class<?> clazz) {
		log.info("Re-indexing {0}", clazz);
		try {
			fullTextSession.setFlushMode(FlushMode.MANUAL);
			fullTextSession.setCacheMode(CacheMode.IGNORE);
			Transaction transaction = fullTextSession.beginTransaction();
			//Scrollable results will avoid loading too many objects in memory
			ScrollableResults results = fullTextSession.createCriteria( clazz )
			    .setFetchSize(BATCH_SIZE)
			    .scroll( ScrollMode.FORWARD_ONLY );
			int index = 0;
			while( results.next() ) {
			    index++;
			    fullTextSession.index( results.get(0) ); //index each element
			    if (index % BATCH_SIZE == 0) {
			        fullTextSession.flushToIndexes(); //apply changes to indexes
			        fullTextSession.clear(); //clear since the queue is processed
			    }
			}
			transaction.commit();
		} catch (IllegalArgumentException e) {
			log.warn("Unable to index object of {0}", clazz);
		}
	}
	
}

@@ -1019,25 +1019,17 @@
 Session 
-fullTextS
+s
 ession;%0A
@@ -1292,42 +1292,8 @@
 s);%0A
-%09%09reindex(HParentResource.class);%0A
 %09%09re
@@ -1499,33 +1499,25 @@
 %0A%09%09try %7B%0A%09%09%09
-fullTextS
+s
 ession.setFl
@@ -1546,25 +1546,17 @@
 AL);%0A%09%09%09
-fullTextS
+s
 ession.s
@@ -1615,25 +1615,17 @@
 ction = 
-fullTextS
+s
 ession.b
@@ -1744,25 +1744,17 @@
 sults = 
-fullTextS
+s
 ession.c
@@ -1918,33 +1918,25 @@
 x++;%0A%09%09%09    
-fullTextS
+s
 ession.index
@@ -2021,33 +2021,25 @@
 %0A%09%09%09        
-fullTextS
+s
 ession.flush
@@ -2093,17 +2093,9 @@
     
-fullTextS
+s
 essi
@@ -2157,16 +2157,36 @@
  %7D%0A%09%09%09%7D%0A
+%09%09%09results.close();%0A
 %09%09%09trans
@@ -2217,23 +2217,8 @@
 ch (
-IllegalArgument
 Exce
@@ -2268,24 +2268,27 @@
 ect of %7B0%7D%22,
+ e,
  clazz);%0A%09%09%7D

Fix re-indexing implementation