Skip to content

Instantly share code, notes, and snippets.

@Abhi-Codes
Abhi-Codes / pom.xml
Last active September 30, 2022 06:39
MySQL, QueryDSL dependencies
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- Query DSL -->
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
</dependency>
@Abhi-Codes
Abhi-Codes / SearchCriteria.java
Created September 30, 2022 05:37
Search Criteria
package com.abhicodes.querydsldynamicquery.utils;
import lombok.Builder;
import lombok.Getter;
import lombok.ToString;
@Getter
@ToString
@Builder
public class SearchCriteria {
@Abhi-Codes
Abhi-Codes / CommonPredicate.java
Last active September 30, 2022 05:45
Create Predicate dynamically based on type and operator
private BooleanExpression getPredicate(String key, String operator, String value, PathBuilder<?> entityPath,
Class<?> classType) {
boolean isMultiValue = value.contains(",");
Class<?> propertyType = getPropertyType(classType, key);
switch (propertyType.getSimpleName()) {
case "Integer":
if (isMultiValue) {
NumberPath<Integer> path = entityPath.getNumber(key, Integer.class);
Integer[] numValue = Stream.of(value.split(",")).map(Integer::parseInt).toArray(Integer[]::new);
return getNumberPredicate(path, operator, numValue);
@Abhi-Codes
Abhi-Codes / BaseService.java
Created September 30, 2022 05:44
Parse the filters passed by controller and build Search Criteria
/**
* Format search criteria.
*
* @param filter the filter
* @return the list
*/
public List<SearchCriteria> formatSearchCriteria(String[] filter) {
List<SearchCriteria> criterias = new ArrayList<>();
if (null != filter) {
Collection<SearchCriteria> collect = Arrays.asList(filter).parallelStream().map(this::validateFilterPattern)
@Abhi-Codes
Abhi-Codes / PostRepository.java
Created September 30, 2022 06:41
Repository Interface extending JPARepository and QueryDSL
package com.abhicodes.querydsldynamicquery.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.stereotype.Repository;
import com.abhicodes.querydsldynamicquery.entity.Post;
@Repository
public interface PostRepository extends JpaRepository<Post, Integer>, QuerydslPredicateExecutor<Post> {
@Abhi-Codes
Abhi-Codes / operators.csv
Last active September 30, 2022 08:13
Operators supported
Operator Meaning Applicable for Data Type
: In Integer,Long,Double,Date
> Greater than Integer,Long,Double,Date
>= Greater than equal to Integer,Long,Double,Date
< Less than Integer,Long,Double,Date
<= Less than equal to Integer,Long,Double,Date
: Equals Ignore Case String
% Starts With Ignore Case String
- Contains Ignore Case String
() Inclusive Range Date
@Abhi-Codes
Abhi-Codes / pom.xml
Created October 9, 2022 06:02
Pom xml file showing springfox 3 dependency
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
@Abhi-Codes
Abhi-Codes / SwaggerConfig.java
Created October 9, 2022 06:16
Add GlobalRequestParams in Docket builder
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
.securityContexts(Arrays.asList(securityContext())).securitySchemes(Arrays.asList(apiKey())).select()
.apis(RequestHandlerSelectors.basePackage("com.abhicodes.springfoxswagger3.controller"))
.paths(PathSelectors.ant("/api/**")).build()
.globalRequestParameters(Arrays.asList(
new RequestParameterBuilder().name("x-global-header-1").description("Remote User")
.in(ParameterType.HEADER).required(true)
@Abhi-Codes
Abhi-Codes / HelloWorld.java
Created October 9, 2022 06:34
Hello World endpoint to demonstrate Swagger 3 global request parameter
@RestController
@RequestMapping("/api")
@Api(value = "Demo APIs")
public class DemoController {
@GetMapping("/hello-world")
@ApiOperation("Hello peeps")
public ResponseEntity<String> helloWorld() {
return ResponseEntity.ok("Hello Medium");
}
@Abhi-Codes
Abhi-Codes / imap.java
Created October 18, 2022 15:58
Connect to mailbox using IMAP Oauth2
public String getAccessTokenByClientCredentialGrant() {
String accessToken = null;
String clientId = imapClientId;
String secret = imapSecret;
String authority = "https://login.microsoftonline.com/" + imapTenantId + "/oauth2/v2.0/token";
String scope = "https://outlook.office365.com/.default";
log.info("Client ID : " + clientId);
log.info("Client Secret : " + secret);
log.info("Auth Server: " + authority);