package jobs;

import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

import models.AnnotatedAssay;
import models.Reviewer;

import com.google.common.base.Stopwatch;

import play.Logger;
import play.jobs.Job;

public class AssignReviewersJob extends Job {

	public void doJob(){
		Logger.info("Simplification started...");
		Stopwatch stopwatch = Stopwatch.createUnstarted();
		stopwatch.start();

		List<AnnotatedAssay> assays = AnnotatedAssay.find("reviewer is null").fetch();

		int total = assays.size();
		int counter = 0;

		List<Reviewer> reviewers = Reviewer.find("email != 'super.cool.bot@gmail.com'").fetch();

		int totalReviewers = reviewers.size()-1;

		for (AnnotatedAssay assay : assays) {
			counter++;
			Logger.info("Assay: " + counter + "/" + total);
			//get random reviewer
			Random rand = new Random();
			int randomNum = rand.nextInt((totalReviewers - 0) + 1);
			assay.setReviewer(reviewers.get(randomNum));
		}

		stopwatch.stop();
		Logger.info("Assignement job done in " + stopwatch.elapsed(TimeUnit.MINUTES) + " minutes.");
	}

}

@@ -323,30 +323,24 @@
 fo(%22
-Simplification started
+Assigning assays
 ...%22
@@ -548,16 +548,59 @@
 r = 0;%0A%0A
+%09%09//Get all reviewer except super cool bot%0A
 %09%09List%3CR
@@ -726,17 +726,43 @@
 ze()-1;%0A
+%09%09int counterFlush = 0;%0A%09%09
 %0A
-
 %09%09for (A
@@ -973,16 +973,16 @@
 ) + 1);%0A
-
 %09%09%09assay
@@ -1021,16 +1021,143 @@
 mNum));%0A
+%09%09%09%0A%09%09%09counterFlush++;%0A%09%09%09if (counterFlush%25100 == 0) %7B%0A%09%09%09%09AnnotatedAssay.em().flush();%0A%09%09%09%09AnnotatedAssay.em().clear();%0A%09%09%09%7D%0A%0A
 %09%09%7D%0A%0A%09%09s

debug impl