Tuesday, 25 February 2020

mongodb pagination with aggregate

https://stackoverflow.com/questions/48305624/how-to-use-mongodb-aggregation-for-pagination

http://blog.axant.it/2016/01/20/mongodb-aggregation-pipeline-and-pagination/

db.Order.aggregate([
    { '$match'    : { "company_id" : ObjectId("54c0...") } },
    { '$sort'     : { 'order_number' : -1 } },
    { '$facet'    : {
        metadata: [ { $count: "total" }, { $addFields: { page: NumberInt(3) } } ],
        data: [ { $skip: 20 }, { $limit: 10 } ] // add projection here wish you re-shape the docs
    } }
] )




pipeline = [
    {'$match': {'status': 'done'}},
    {'$group': {'_id': 'results', 'result': {'$push': '$$CURRENT'}}},
    {'$project': {'_id': 0, 'result': 1, 'pages': {'$divide': [{'$size': '$result'}, ITEMS_PER_PAGE]}}},
    {'$unwind': '$result'},
    {'$skip': 1 * ITEMS_PER_PAGE},
    {'$limit': ITEMS_PER_PAGE}
]

Friday, 14 February 2020

pdf to image

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.imageio.ImageIO;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;

public class Main {

    public static void saveFirstPageThumbnail() throws IOException {
        PDDocument document = PDDocument.load("./sample.pdf");
        List<PDPage> pages = document.getDocumentCatalog().getAllPages();
        PDPage page = pages.get(0); //first one
        BufferedImage bufferedImage = page.convertToImage();
        File outputFile = new File( "./image.jpg");
        ImageIO.write(bufferedImage, "jpg", outputFile);
    }

}

Thursday, 6 February 2020

mysql concat two columan

SELECT CONCAT(c.FIRSTNAME, ',', c.LASTNAME) AS FIRSTNAME,
       c.*
FROM   `customer` c;
search on two concat columan

WHERE CONCAT(customers.first_name, ' ', customers.last_name) LIKE '%John Smith%'

WHERE CONCAT(TRIM(customers.first_name), ' ', TRIM(customers.last_name)) LIKE '%John Smith%'

Wednesday, 5 February 2020

java async

https://www.callicoder.com/java-8-completablefuture-tutorial/

https://github.com/maoyunfei/CompletableFuture-Examples/blob/master/src/main/java/com/maoyunfei/study/CompletableFutureExample7.java

import java.util.Arrays; 
import java.util.List; 
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.*;
class Main {
  public static void main(String[] args) {
    CompletableFuture<Void> completableFuture =
    CompletableFuture.runAsync(()-> {
       TimeUnit.SECONDS.sleep(60);
      System.out.println("hare in the game");
  });
  System.out.println("lower function");
}
}




---------------------------------------------------------------------------------------------

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.*;

class Main {
  public static void main(String[] args) {
    String name="";
     CompletableFuture<String> completableFuture = new CompletableFuture<String>();
    try {
        completableFuture = CompletableFuture.supplyAsync(() -> {
        try {
        TimeUnit.SECONDS.sleep(60);
        System.out.println("hare in the game");
        return "pawan";
        }catch(Exception e) {
          System.out.println("handle exception");
          throw new IllegalStateException(e);
        }
      });
     CompletableFuture<String> greetingFuture = completableFuture.thenApply(namedata -> {
      return "Hello " + namedata;
    });
     // name=completableFuture.get();
      System.out.println("Name"+greetingFuture.get());
    } catch (Exception e) {
      System.out.println("error");
    }
    System.out.println("lower function"+name);
  }
} ---------------------------------------------------------------------------------

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.*;


class Main {
  public static void main(String[] args) {
    final String name="pawan";
     CompletableFuture<String> completableFuture = new CompletableFuture<String>();
    try {
        completableFuture = CompletableFuture.supplyAsync(() -> {
        try {
        TimeUnit.SECONDS.sleep(60);
        System.out.println("hare in the game");
        return "pawan";
        }catch(Exception e) {
          System.out.println("handle exception");
          throw new IllegalStateException(e);
        }
      }).thenApply(namedata -> {
      System.out.println("Name"+namedata);
      return namedata;
    });
     // name=completableFuture.get();
    
    } catch (Exception e) {
      System.out.println("error");
    }
    System.out.println("lower function"+name);
  }
}
----------------------------------------------------------------------------------- combine example ----------------------
import java.util.concurrent.CompletableFuture;
import java.util.*;
import java.util.concurrent.*;
class Main {
  public static void main(String[] args) {
    try{
    System.out.println("Hello world!");


    CompletableFuture<Integer> serviceOneValue = CompletableFuture.supplyAsync(() -> {
      System.out.println("pawan");
          try {
        TimeUnit.SECONDS.sleep(1);
    } catch (InterruptedException e) {
       throw new IllegalStateException(e);
    }
      return 10;
    });
    CompletableFuture<Integer> serviceTwoValue = CompletableFuture.supplyAsync(() -> {
          try {
        TimeUnit.SECONDS.sleep(1);
    } catch (InterruptedException e) {
       throw new IllegalStateException(e);
    }
      System.out.println("kumar");
      return 100;
    });


    CompletableFuture<Integer> sumOfTwoService = serviceOneValue.thenCombine(serviceTwoValue, (serviceOneValue1, serviceTwoValue1) -> {
      System.out.println("a+b");
      System.out.println("a+b");
      return serviceOneValue1+serviceTwoValue1;
    });
    System.out.println("result---data");
   
    System.out.println("result---"+sumOfTwoService.get());
    }catch(Exception e) {
      System.out.println(e);
    }
  }
}
completeable feature for comobine multipale async call --------------------------------------------------------------    CompletableFuture<Integer> contestInfromation = CompletableFuture.supplyAsync(() -> {
   return  contestService.getContestInfromationByContest(companyId, contestId);
   });
  
   CompletableFuture<Integer> contestGames = CompletableFuture.supplyAsync(() -> {
    return contestService.getContestGamesInfromationByContest(companyId, contestId);
   });
  
   CompletableFuture<Integer> contestAssignmentInfo = CompletableFuture.supplyAsync(() -> {
    return contestService.getContestAssignmentInfromationByContest(companyId, contestId);
   });
  
   CompletableFuture<Integer> contestTrophies = CompletableFuture.supplyAsync(() -> {
    return contestService.getContestTrophiesInfromationByContest(companyId, contestId);
   });
  
   CompletableFuture<Integer> contestRewards = CompletableFuture.supplyAsync(() -> {
    return contestService.getContestRewardsInfromationByContest(companyId, contestId);
   });
   CompletableFuture.allOf(contestInfromation,contestGames,contestAssignmentInfo,contestTrophies,contestRewards).join();
   System.out.println("contestInfromation"+contestInfromation.get());
   System.out.println("contestGames"+contestGames.get());
   System.out.println("contestAssignmentInfo"+contestAssignmentInfo.get());
   System.out.println("contestTrophies"+contestTrophies.get());
   System.out.println("contestRewards"+contestRewards.get());