package org.openlmis.report.builder.helpers;

import org.apache.commons.lang.StringUtils;

import static org.apache.ibatis.jdbc.SqlBuilder.WHERE;

public class RequisitionPredicateHelper {

  public static void whereMultiProductFilter(String products){
    if(products.equals("-1")){
      WHERE("p.tracer = true");
    } else if(!products.equals("0") && !products.isEmpty()){
      WHERE(" p.id = ANY(array[" + products + " ]::INT[])");
    }
  }

  public static void whereProductCategory(Long productCategory){
    if(productCategory > 0){
      WHERE( "ppc.productCategoryId = #{filterCriteria.productCategory}");
    }
  }

  public static void whereFacilityPermission(){
    WHERE("f.id in (select facility_id from vw_user_facilities where user_id = #{userId} and program_id = #{filterCriteria.program}) " );
  }

  public static void whereGeographicZoneFilter(Long geographicZone){
    if(geographicZone != 0){
      WHERE( "(d.zone_id = #{filterCriteria.zone} or d.parent = #{filterCriteria.zone} or d.region_id = #{filterCriteria.zone} or d.district_id = #{filterCriteria.zone}) ") ;
    }
  }

  public static void whereProgramFilter(){
    WHERE("r.programId = #{filterCriteria.program}");
  }

  public static void wherePeriodFilter(){
    WHERE("r.periodId = #{filterCriteria.period}");
  }

}

@@ -82,16 +82,138 @@
 ngUtils;
+%0Aimport org.springframework.beans.factory.annotation.Autowired;%0Aimport org.springframework.beans.factory.annotation.Value;
 %0A%0Aimport
@@ -325,19 +325,16 @@
 tic 
-void whereM
+String m
 ulti
@@ -346,16 +346,18 @@
 ctFilter
+By
 (String 
@@ -368,336 +368,415 @@
 ucts
-)%7B%0A    if(products.equals(%22-1%22))%7B%0A      WHERE(%22p.tracer = true%22);%0A    %7D else if(!products.equals(%220%22) && !products.isEmpty())%7B%0A      WHERE(%22 p.id = ANY(array%5B%22 + products + %22 %5D::INT%5B%5D)%22);%0A    %7D%0A  %7D%0A%0A  public static void whereProductCategory(Long productCategory)%7B%0A    if(productCategory %3E 0)%7B%0A      WHERE( %22ppc.productCategoryId
+, String productIdField, String tracerField)%7B%0A    if(products.equals(%22-1%22))%7B%0A      return String.format(%22%25s = true%22, tracerField);%0A    %7D else if(!products.equals(%220%22) && !products.isEmpty())%7B%0A      return String.format(%22 %252$s = ANY(array%5B %251$s %5D::INT%5B%5D)%22, products, productIdField);%0A    %7D%0A    return null;%0A  %7D%0A%0A  public static String productCategoryIsFilteredBy(String field)%7B%0A      return String.format(%22%25s
  = #
@@ -811,17 +811,19 @@
 ory%7D
-%22);%0A    %7D
+ %22, field);
 %0A  %7D
@@ -844,55 +844,88 @@
 tic 
-void whereFacilityPermission()%7B%0A    WHERE(%22f.id
+String userHasPermissionOnFacilityBy(String field)%7B%0A    return String.format(%22%25s
  in 
@@ -1040,16 +1040,23 @@
 ram%7D) %22 
+, field
 );%0A  %7D%0A%0A
@@ -1075,106 +1075,87 @@
 tic 
-void whereGeographic
+String geo
 Zone
+Is
 Filter
-(Long geographicZone)%7B%0A    if(geographicZone != 0)%7B%0A      WHERE
+edBy( String viewAlias)%7B%0A      return String.format
 ( %22(
-d
+%25s
 .zon
@@ -1187,17 +1187,18 @@
 one%7D or 
-d
+%25s
 .parent 
@@ -1225,17 +1225,18 @@
 one%7D or 
-d
+%25s
 .region_
@@ -1266,17 +1266,18 @@
 one%7D or 
-d
+%25s
 .distric
@@ -1310,19 +1310,24 @@
 ne%7D)
- %22) ;%0A    %7D
+%22 , viewAlias) ;
 %0A  %7D
@@ -1348,57 +1348,78 @@
 tic 
-void whereP
+String p
 rogram
+Is
 Filter
-()%7B%0A    WHERE(%22r.programId
+edBy(String field)%7B%0A    return String.format(%22%25s
  = #
@@ -1443,16 +1443,23 @@
 rogram%7D%22
+, field
 );%0A  %7D%0A%0A
@@ -1478,84 +1478,261 @@
 tic 
-void whereP
+String p
 eriod
+Is
 Filter
-()%7B%0A    WHERE(%22r.periodId = #%7BfilterCriteria.period%7D%22)
+edBy(String field)%7B%0A    return String.format(%22%25s = #%7BfilterCriteria.period%7D%22, field);%0A  %7D%0A%0A  public static String rnrStatusFilteredBy(String field, String acceptedRnrStatuses)%7B%0A    return  field +%22 in ( %22 + acceptedRnrStatuses +%22 )%22
 ;%0A  

Make the requisition predicate helper fluent. -This is Experimental but the idea is to make the query building expriance more fluent.