@GET
 @Path("{id}")
 @Consumes({MediaType.APPLICATION_JSON})
 @Produces({MediaType.APPLICATION_JSON})
 public String retrieveAllDetailsForPayments(
     @PathParam("id") final Long id, @Context final UriInfo uriInfo) {
   this.context.authenticatedUser().validateHasReadPermission(resourceNameForPermissions);
   PaymentGatewayData paymentData = readPlatformService.retrievePaymentGatewayIdData(id);
   List<MediaEnumoptionData> data = readPlatformService.retrieveTemplateData();
   paymentData.setStatusData(data);
   final ApiRequestJsonSerializationSettings settings =
       apiRequestParameterHelper.process(uriInfo.getQueryParameters());
   return this.toApiJsonSerializer.serialize(settings, paymentData, RESPONSEPARAMETERS);
 }
  @GET
  @Consumes({MediaType.APPLICATION_JSON})
  @Produces({MediaType.APPLICATION_JSON})
  public String retrieveAllDetailsForPayments(
      @Context final UriInfo uriInfo,
      @QueryParam("sqlSearch") final String sqlSearch,
      @QueryParam("source") final String source,
      @QueryParam("limit") final Integer limit,
      @QueryParam("offset") final Integer offset,
      @QueryParam("tabType") final String type) {

    this.context.authenticatedUser().validateHasReadPermission(resourceNameForPermissions);
    final SearchSqlQuery searchItemDetails = SearchSqlQuery.forSearch(sqlSearch, offset, limit);
    Page<PaymentGatewayData> paymentData =
        readPlatformService.retrievePaymentGatewayData(searchItemDetails, type, source);
    return this.toApiJsonSerializer.serialize(paymentData);
  }
  @Path("download")
  @GET
  @Consumes({MediaType.APPLICATION_JSON})
  @Produces({MediaType.APPLICATION_JSON})
  public Response retriveDataForDownload(
      @Context final UriInfo uriInfo,
      @QueryParam("source") final String source,
      @QueryParam("status") final String status,
      @QueryParam("fromDate") final Long start,
      @QueryParam("toDate") final Long end)
      throws IOException {

    this.context.authenticatedUser().validateHasReadPermission(resourceNameForPermissions);
    /** have to convert from and to date to format like 2014-06-15 */
    Date fDate = new Date(start);
    Date tDate = new Date(end);
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd");

    String fromDate = df.format(fDate);
    String toDate = df.format(tDate);

    List<PaymentGatewayDownloadData> paymentData =
        readPlatformService.retriveDataForDownload(source, fromDate, toDate, status);

    /** receiptNo serialNumber paymentDate amountPaid PhoneMSISDN Remarks status */
    boolean statusSuccess = false;
    if (status.equalsIgnoreCase("Success")) statusSuccess = true;

    StringBuilder builder = new StringBuilder();
    if (statusSuccess) {
      builder.append(
          "Receipt No, Serial No, Payment Date, Amount Paid, Payment Id, Phone MSISDN, Remarks, Status \n");
    } else {
      builder.append(
          "Receipt No, Serial No, Payment Date, Amount Paid, Phone MSISDN, Remarks, Status \n");
    }

    for (PaymentGatewayDownloadData data : paymentData) {
      builder.append(data.getReceiptNo() + ",");
      builder.append(data.getSerialNo() + ",");
      builder.append(data.getPaymendDate() + ",");
      builder.append(data.getAmountPaid() + ",");
      if (statusSuccess) {
        builder.append(data.getPaymentId() + ",");
      }
      builder.append(data.getPhoneMSISDN() + ",");
      builder.append(data.getRemarks() + ",");
      builder.append(data.getStatus());
      builder.append("\n");
    }
    statusSuccess = false;
    String fileLocation =
        System.getProperty("java.io.tmpdir")
            + File.separator
            + "billing"
            + File.separator
            + ""
            + source
            + ""
            + System.currentTimeMillis()
            + status
            + ".csv";

    String dirLocation = System.getProperty("java.io.tmpdir") + File.separator + "billing";
    File dir = new File(dirLocation);
    if (!dir.exists()) {
      dir.mkdir();
    }

    File file = new File(fileLocation);
    if (!file.exists()) {
      file.createNewFile();
    }
    FileUtils.writeStringToFile(file, builder.toString());

    final ResponseBuilder response = Response.ok(file);
    response.header("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"");
    response.header("Content-Type", "application/csv");

    return response.build();

    /*String toJson = gson.toJson(paymentData);
    JSONArray arry  = null;
    try {
    	arry = new JSONArray(toJson);
    } catch (JSONException e) {
    	e.printStackTrace();
    }
    System.out.println(arry);
    String json = this.toApiJsonSerializer.serialize(paymentData);

    File file=new File("/home/rakesh/Desktop/demo.csv");
       String csv = null;
    try {
    	csv = CDL.toString(arry);
    } catch (JSONException e) {
    	// TODO Auto-generated catch block
    	e.printStackTrace();
    }
       FileUtils.writeStringToFile(file, csv);*/

    // return this.toApiJsonSerializer.serialize(paymentData);

  }