/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package jobs;

import java.util.List;
import models.AnnotatedAssay;
import models.Annotation;
import models.AnnotationRule;
import play.Logger;
import play.db.jpa.JPA;
import play.jobs.Job;

/**
 *
 * @author loopasam
 */
public class FilterAssayJob extends Job {

    @Override
    public void doJob() throws Exception {
        Logger.info("Filtering job...");

        List<AnnotationRule> rules = AnnotationRule.find("select r from AnnotationRule r "
                + "where r.isFilter = true").fetch();

        int counter = 0;
        int total = rules.size();
        for (AnnotationRule annotationRule : rules) {
            counter++;

            String termMessage = "Rule: " + counter + "/" + total;
            Logger.info(termMessage);

            String rule = "SELECT DISTINCT assay_id, description FROM assays "
                    + "WHERE " + annotationRule.rule + ";";

            List<Object[]> results = JPA.em().createNativeQuery(rule).getResultList();

            String ruleMessage = "Rule: " + annotationRule.rule + " - Number of assays identified: " + results.size();
            Logger.info(ruleMessage);

            int counterFlush = 0;

            for (Object[] result : results) {

                int assayId = (int) result[0];
                AnnotatedAssay assay = AnnotatedAssay.find("byAssayId", assayId).first();

                //Check if the assay exists already, otherwise does nothing
                if (assay != null) {
                    Logger.info("removed: " + assay.chemblId);
                    assay.needReview = false;

                    for (Annotation annotation : assay.annotations) {
                        annotation.toRemove = true;
                        annotation.save();
                    }

                    assay.save();

                }

                counterFlush++;
                if (counterFlush % 100 == 0) {
                    AnnotatedAssay.em().flush();
                    AnnotatedAssay.em().clear();
                }
            }
        }

        Logger.info("Filtering done.");

    }

}

@@ -204,21 +204,100 @@
 ort 
-java.util.Lis
+com.google.common.base.Stopwatch;%0Aimport java.util.List;%0Aimport java.util.concurrent.TimeUni
 t;%0Ai
@@ -381,16 +381,37 @@
 onRule;%0A
+import models.Utils;%0A
 import p
@@ -636,24 +636,110 @@
 ng job...%22);
+%0A        Stopwatch stopwatch = Stopwatch.createUnstarted();%0A        stopwatch.start();
 %0A%0A        Li
@@ -1583,16 +1583,107 @@
 sults) %7B
+%0A                %0A                Logger.info(%22i: %22 + counterFlush + %22/%22 + results.size());
 %0A%0A      
@@ -2491,32 +2491,32 @@
    %7D%0A        %7D%0A%0A
-
         Logger.i
@@ -2538,16 +2538,164 @@
 done.%22);
+%0A        stopwatch.stop();%0A        Utils.emailAdmin(%22FilterAssayJob completed%22, %22Job done in %22 + stopwatch.elapsed(TimeUnit.MINUTES) + %22 minutes.%22);
 %0A%0A    %7D%0A

debug info for job