<commit_before>/*
 * This program was produced for the U.S. Agency for International Development. It was prepared by the USAID | DELIVER PROJECT, Task Order 4. It is part of a project which utilizes code originally licensed under the terms of the Mozilla Public License (MPL) v2 and therefore is licensed under MPL v2 or later.
 *
 * This program is free software: you can redistribute it and/or modify it under the terms of the Mozilla Public License as published by the Mozilla Foundation, either version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Mozilla Public License for more details.
 *
 * You should have received a copy of the Mozilla Public License along with this program. If not, see http://www.mozilla.org/MPL/
 */

package org.openlmis.report.builder;

import org.apache.ibatis.type.JdbcType;

import java.util.Map;
import static org.apache.ibatis.jdbc.SqlBuilder.*;

public class NonReportingFacilityQueryBuilder {



    public static String getQuery(Map params){

      Long userId = (Long) params.get("userId");
      params = (Map)( params.containsKey("filterCriteria")? params.get("filterCriteria") : params );


      String period           = ((String[])params.get("period"))[0];
      String zone   = params.containsKey("zone")? ((String[])params.get("zone"))[0]: "" ;
      String facilityType     = params.containsKey("facilityType")? ((String[])params.get("facilityType"))[0] : "" ;
      String program          = ((String[])params.get("program"))[0];
      String schedule         = ((String[])params.get("schedule"))[0];
      return getQueryString(params, program , period , zone, facilityType, schedule, userId);

    }

     private static String getQueryString(Map params, String program , String period, String zone, String facilityType, String schedule, Long userId) {
         BEGIN();
         SELECT_DISTINCT("facilities.code, facilities.name");
         SELECT_DISTINCT("gz.district_name as location");
         SELECT_DISTINCT("ft.name as facilityType");

         FROM("facilities");
         INNER_JOIN("requisition_group_members rgm on rgm.facilityid = facilities.id");
         INNER_JOIN("vw_districts gz on gz.district_id = facilities.geographiczoneid");
         INNER_JOIN("facility_types ft on ft.id = facilities.typeid");
         INNER_JOIN("programs_supported ps on ps.facilityid = facilities.id");
         INNER_JOIN("requisition_group_program_schedules rgps on rgps.requisitiongroupid = rgm.requisitiongroupid and ps.programid = rgps.programid");
         WHERE("facilities.id in (select facility_id from vw_user_facilities where user_id = cast(" + userId+ " as int4) and program_id = cast(" + program + " as int4))");
         WHERE("facilities.id not in (select r.facilityid from requisitions r where r.status in ('RELEASED','APPROVED') and r.periodid = cast (" + period + " as int4) and r.programid = cast(" + program + " as int4) )");
         writePredicates(program, period, zone, facilityType, schedule);
         ORDER_BY(QueryHelpers.getSortOrder(params, "name"));
         // cache the string query for debugging purposes
         String strQuery = SQL();
         return strQuery;
     }

     private static void writePredicates(String program, String period, String zone, String facilityType, String schedule) {

         if(zone != "" && !zone.endsWith( "undefined")){
             WHERE(" (gz.district_id = " + zone + " or gz.zone_id = " + zone + " or gz.region_id = " + zone +" or gz.parent = " + zone + " )");
         }
<commit_msg>ELMIS-1337: fix the non reporting report problem on National level.<commit_after>/*
 * This program was produced for the U.S. Agency for International Development. It was prepared by the USAID | DELIVER PROJECT, Task Order 4. It is part of a project which utilizes code originally licensed under the terms of the Mozilla Public License (MPL) v2 and therefore is licensed under MPL v2 or later.
 *
 * This program is free software: you can redistribute it and/or modify it under the terms of the Mozilla Public License as published by the Mozilla Foundation, either version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Mozilla Public License for more details.
 *
 * You should have received a copy of the Mozilla Public License along with this program. If not, see http://www.mozilla.org/MPL/
 */

package org.openlmis.report.builder;

import org.apache.ibatis.type.JdbcType;

import java.util.Map;
import static org.apache.ibatis.jdbc.SqlBuilder.*;

public class NonReportingFacilityQueryBuilder {



    public static String getQuery(Map params){

      Long userId = (Long) params.get("userId");
      params = (Map)( params.containsKey("filterCriteria")? params.get("filterCriteria") : params );


      String period           = ((String[])params.get("period"))[0];
      String zone   = params.containsKey("zone")? ((String[])params.get("zone"))[0]: "" ;
      String facilityType     = params.containsKey("facilityType")? ((String[])params.get("facilityType"))[0] : "" ;
      String program          = ((String[])params.get("program"))[0];
      String schedule         = ((String[])params.get("schedule"))[0];
      return getQueryString(params, program , period , zone, facilityType, schedule, userId);

    }

     private static String getQueryString(Map params, String program , String period, String zone, String facilityType, String schedule, Long userId) {
         BEGIN();
         SELECT_DISTINCT("facilities.code, facilities.name");
         SELECT_DISTINCT("gz.district_name as location");
         SELECT_DISTINCT("ft.name as facilityType");

         FROM("facilities");
         INNER_JOIN("requisition_group_members rgm on rgm.facilityid = facilities.id");
         INNER_JOIN("vw_districts gz on gz.district_id = facilities.geographiczoneid");
         INNER_JOIN("facility_types ft on ft.id = facilities.typeid");
         INNER_JOIN("programs_supported ps on ps.facilityid = facilities.id");
         INNER_JOIN("requisition_group_program_schedules rgps on rgps.requisitiongroupid = rgm.requisitiongroupid and ps.programid = rgps.programid");
         WHERE("facilities.id in (select facility_id from vw_user_facilities where user_id = cast(" + userId+ " as int4) and program_id = cast(" + program + " as int4))");
         WHERE("facilities.id not in (select r.facilityid from requisitions r where r.status in ('RELEASED','APPROVED') and r.periodid = cast (" + period + " as int4) and r.programid = cast(" + program + " as int4) )");
         writePredicates(program, period, zone, facilityType, schedule);
         ORDER_BY(QueryHelpers.getSortOrder(params, "name"));
         // cache the string query for debugging purposes
         String strQuery = SQL();
         return strQuery;
     }

     private static void writePredicates(String program, String period, String zone, String facilityType, String schedule) {

         if(!zone.equals("0") && !zone.isEmpty() && !zone.endsWith( "undefined")){
             WHERE(" (gz.district_id = " + zone + " or gz.zone_id = " + zone + " or gz.region_id = " + zone +" or gz.parent = " + zone + " )");
         }

{'fraction_non_alphanumeric': 0.11044497735145217, 'fraction_numerical': 0.004130029309885425, 'mean_word_length': 3.616851168511685, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4159131', 'n_tokens_mistral': 2119, 'n_tokens_neox': 2042, 'n_words': 696}