Merge branch 'master' into JAVA-3533
This commit is contained in:
commit
296fe08dde
4
pom.xml
4
pom.xml
@ -664,14 +664,12 @@
|
||||
<module>spring-mvc-java-2</module>
|
||||
|
||||
<module>spring-mvc-velocity</module>
|
||||
<module>spring-mvc-xml</module>
|
||||
|
||||
<module>spring-protobuf</module>
|
||||
<module>spring-quartz</module>
|
||||
|
||||
<module>spring-reactor</module>
|
||||
<module>spring-remoting</module>
|
||||
<module>spring-rest-query-language</module>
|
||||
<module>spring-rest-shell</module>
|
||||
<module>spring-rest-simple</module>
|
||||
<module>spring-resttemplate</module>
|
||||
@ -1129,14 +1127,12 @@
|
||||
<module>spring-mvc-java-2</module>
|
||||
|
||||
<module>spring-mvc-velocity</module>
|
||||
<module>spring-mvc-xml</module>
|
||||
|
||||
<module>spring-protobuf</module>
|
||||
<module>spring-quartz</module>
|
||||
|
||||
<module>spring-reactor</module>
|
||||
<module>spring-remoting</module>
|
||||
<module>spring-rest-query-language</module>
|
||||
<module>spring-rest-shell</module>
|
||||
<module>spring-rest-simple</module>
|
||||
<module>spring-resttemplate</module>
|
||||
|
@ -24,6 +24,7 @@
|
||||
<module>spring-boot-angular</module>
|
||||
<module>spring-boot-annotations</module>
|
||||
<module>spring-boot-artifacts</module>
|
||||
<module>spring-boot-artifacts-2</module>
|
||||
<module>spring-boot-autoconfiguration</module>
|
||||
<module>spring-boot-basic-customization</module>
|
||||
<module>spring-boot-basic-customization-2</module>
|
||||
|
7
spring-boot-modules/spring-boot-artifacts-2/README.md
Normal file
7
spring-boot-modules/spring-boot-artifacts-2/README.md
Normal file
@ -0,0 +1,7 @@
|
||||
## Spring Boot Artifacts 2
|
||||
|
||||
This module contains articles about configuring the Spring Boot build process 2.
|
||||
|
||||
### Relevant Articles:
|
||||
|
||||
TBD
|
48
spring-boot-modules/spring-boot-artifacts-2/pom.xml
Normal file
48
spring-boot-modules/spring-boot-artifacts-2/pom.xml
Normal file
@ -0,0 +1,48 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.spring-boot-modules</groupId>
|
||||
<artifactId>spring-boot-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<artifactId>spring-boot-artifacts-2</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>spring-boot-artifacts-2</name>
|
||||
<description>Demo project for Spring Boot</description>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>${project.artifactId}</finalName>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>repackage</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<!-- The main class to start by executing java -jar -->
|
||||
<start-class>com.baeldung.demo.DemoApplication</start-class>
|
||||
</properties>
|
||||
|
||||
</project>
|
@ -0,0 +1,11 @@
|
||||
package com.baeldung.demo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class DemoApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(DemoApplication.class, args);
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package com.baeldung.demo;
|
||||
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class DemoRestController {
|
||||
|
||||
@GetMapping(value = "/welcome")
|
||||
public ResponseEntity<String> welcomeEndpoint() {
|
||||
return ResponseEntity.ok("Welcome to Baeldung Spring Boot Demo!");
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
spring:
|
||||
application:
|
||||
name: Baeldung_SpringBoot_Demo
|
@ -1,2 +0,0 @@
|
||||
required.name = Name is required!
|
||||
NotEmpty.person.password = Password is required!
|
@ -22,9 +22,11 @@
|
||||
<module>spring-mvc-forms-jsp</module>
|
||||
<module>spring-mvc-views</module>
|
||||
<module>spring-mvc-webflow</module>
|
||||
<module>spring-mvc-xml</module>
|
||||
<module>spring-rest-angular</module>
|
||||
<module>spring-rest-http</module>
|
||||
<module>spring-rest-http-2</module>
|
||||
<module>spring-rest-query-language</module>
|
||||
<module>spring-resttemplate-2</module>
|
||||
<module>spring-thymeleaf</module>
|
||||
<module>spring-thymeleaf-2</module>
|
||||
|
@ -1,61 +1,61 @@
|
||||
package com.baeldung.spring.controller;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.http.*;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.context.support.ServletContextResource;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
@Controller
|
||||
public class ImageController {
|
||||
|
||||
@Autowired
|
||||
private ServletContext servletContext;
|
||||
|
||||
@RequestMapping(value = "/image-view", method = RequestMethod.GET)
|
||||
public String imageView() throws IOException {
|
||||
return "image-download";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/image-manual-response", method = RequestMethod.GET)
|
||||
public void getImageAsByteArray(HttpServletResponse response) throws IOException {
|
||||
final InputStream in = servletContext.getResourceAsStream("/WEB-INF/images/image-example.jpg");
|
||||
response.setContentType(MediaType.IMAGE_JPEG_VALUE);
|
||||
IOUtils.copy(in, response.getOutputStream());
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/image-byte-array", method = RequestMethod.GET)
|
||||
@ResponseBody
|
||||
public byte[] getImageAsByteArray() throws IOException {
|
||||
final InputStream in = servletContext.getResourceAsStream("/WEB-INF/images/image-example.jpg");
|
||||
return IOUtils.toByteArray(in);
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/image-response-entity", method = RequestMethod.GET)
|
||||
public ResponseEntity<byte[]> getImageAsResponseEntity() throws IOException {
|
||||
ResponseEntity<byte[]> responseEntity;
|
||||
final HttpHeaders headers = new HttpHeaders();
|
||||
final InputStream in = servletContext.getResourceAsStream("/WEB-INF/images/image-example.jpg");
|
||||
byte[] media = IOUtils.toByteArray(in);
|
||||
headers.setCacheControl(CacheControl.noCache().getHeaderValue());
|
||||
responseEntity = new ResponseEntity<>(media, headers, HttpStatus.OK);
|
||||
return responseEntity;
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/image-resource", method = RequestMethod.GET)
|
||||
@ResponseBody
|
||||
public ResponseEntity<Resource> getImageAsResource() {
|
||||
final HttpHeaders headers = new HttpHeaders();
|
||||
Resource resource = new ServletContextResource(servletContext, "/WEB-INF/images/image-example.jpg");
|
||||
return new ResponseEntity<>(resource, headers, HttpStatus.OK);
|
||||
}
|
||||
}
|
||||
package com.baeldung.spring.controller;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.http.*;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.context.support.ServletContextResource;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
@Controller
|
||||
public class ImageController {
|
||||
|
||||
@Autowired
|
||||
private ServletContext servletContext;
|
||||
|
||||
@RequestMapping(value = "/image-view", method = RequestMethod.GET)
|
||||
public String imageView() throws IOException {
|
||||
return "image-download";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/image-manual-response", method = RequestMethod.GET)
|
||||
public void getImageAsByteArray(HttpServletResponse response) throws IOException {
|
||||
final InputStream in = servletContext.getResourceAsStream("/WEB-INF/images/image-example.jpg");
|
||||
response.setContentType(MediaType.IMAGE_JPEG_VALUE);
|
||||
IOUtils.copy(in, response.getOutputStream());
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/image-byte-array", method = RequestMethod.GET)
|
||||
@ResponseBody
|
||||
public byte[] getImageAsByteArray() throws IOException {
|
||||
final InputStream in = servletContext.getResourceAsStream("/WEB-INF/images/image-example.jpg");
|
||||
return IOUtils.toByteArray(in);
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/image-response-entity", method = RequestMethod.GET)
|
||||
public ResponseEntity<byte[]> getImageAsResponseEntity() throws IOException {
|
||||
ResponseEntity<byte[]> responseEntity;
|
||||
final HttpHeaders headers = new HttpHeaders();
|
||||
final InputStream in = servletContext.getResourceAsStream("/WEB-INF/images/image-example.jpg");
|
||||
byte[] media = IOUtils.toByteArray(in);
|
||||
headers.setCacheControl(CacheControl.noCache().getHeaderValue());
|
||||
responseEntity = new ResponseEntity<>(media, headers, HttpStatus.OK);
|
||||
return responseEntity;
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/image-resource", method = RequestMethod.GET)
|
||||
@ResponseBody
|
||||
public ResponseEntity<Resource> getImageAsResource() {
|
||||
final HttpHeaders headers = new HttpHeaders();
|
||||
Resource resource = new ServletContextResource(servletContext, "/WEB-INF/images/image-example.jpg");
|
||||
return new ResponseEntity<>(resource, headers, HttpStatus.OK);
|
||||
}
|
||||
}
|
@ -1,22 +1,22 @@
|
||||
package com.baeldung.spring.validator;
|
||||
|
||||
import com.baeldung.spring.form.Person;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.validation.Errors;
|
||||
import org.springframework.validation.ValidationUtils;
|
||||
import org.springframework.validation.Validator;
|
||||
|
||||
@Component
|
||||
public class PersonValidator implements Validator {
|
||||
|
||||
@Override
|
||||
public boolean supports(final Class calzz) {
|
||||
return Person.class.isAssignableFrom(calzz);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void validate(final Object obj, final Errors errors) {
|
||||
|
||||
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "required.name");
|
||||
}
|
||||
package com.baeldung.spring.validator;
|
||||
|
||||
import com.baeldung.spring.form.Person;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.validation.Errors;
|
||||
import org.springframework.validation.ValidationUtils;
|
||||
import org.springframework.validation.Validator;
|
||||
|
||||
@Component
|
||||
public class PersonValidator implements Validator {
|
||||
|
||||
@Override
|
||||
public boolean supports(final Class calzz) {
|
||||
return Person.class.isAssignableFrom(calzz);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void validate(final Object obj, final Errors errors) {
|
||||
|
||||
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "required.name");
|
||||
}
|
||||
}
|
@ -0,0 +1,2 @@
|
||||
required.name = Name is required!
|
||||
NotEmpty.person.password = Password is required!
|
Before Width: | Height: | Size: 101 KiB After Width: | Height: | Size: 101 KiB |
@ -11,7 +11,7 @@
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-boot-2</relativePath>
|
||||
<relativePath>../../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
@ -1,100 +1,100 @@
|
||||
package com.baeldung.persistence.dao;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Deque;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
|
||||
import com.baeldung.web.util.SearchOperation;
|
||||
import com.baeldung.web.util.SpecSearchCriteria;
|
||||
|
||||
public class GenericSpecificationsBuilder<U> {
|
||||
|
||||
private final List<SpecSearchCriteria> params;
|
||||
|
||||
public GenericSpecificationsBuilder() {
|
||||
this.params = new ArrayList<>();
|
||||
}
|
||||
|
||||
public final GenericSpecificationsBuilder<U> with(final String key, final String operation, final Object value, final String prefix, final String suffix) {
|
||||
return with(null, key, operation, value, prefix, suffix);
|
||||
}
|
||||
|
||||
public final GenericSpecificationsBuilder<U> with(final String precedenceIndicator, final String key, final String operation, final Object value, final String prefix, final String suffix) {
|
||||
SearchOperation op = SearchOperation.getSimpleOperation(operation.charAt(0));
|
||||
if (op != null) {
|
||||
if (op == SearchOperation.EQUALITY) // the operation may be complex operation
|
||||
{
|
||||
final boolean startWithAsterisk = prefix != null && prefix.contains(SearchOperation.ZERO_OR_MORE_REGEX);
|
||||
final boolean endWithAsterisk = suffix != null && suffix.contains(SearchOperation.ZERO_OR_MORE_REGEX);
|
||||
|
||||
if (startWithAsterisk && endWithAsterisk) {
|
||||
op = SearchOperation.CONTAINS;
|
||||
} else if (startWithAsterisk) {
|
||||
op = SearchOperation.ENDS_WITH;
|
||||
} else if (endWithAsterisk) {
|
||||
op = SearchOperation.STARTS_WITH;
|
||||
}
|
||||
}
|
||||
params.add(new SpecSearchCriteria(precedenceIndicator, key, op, value));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public Specification<U> build(Function<SpecSearchCriteria, Specification<U>> converter) {
|
||||
|
||||
if (params.size() == 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final List<Specification<U>> specs = params.stream()
|
||||
.map(converter)
|
||||
.collect(Collectors.toCollection(ArrayList::new));
|
||||
|
||||
Specification<U> result = specs.get(0);
|
||||
|
||||
for (int idx = 1; idx < specs.size(); idx++) {
|
||||
result = params.get(idx)
|
||||
.isOrPredicate()
|
||||
? Specification.where(result)
|
||||
.or(specs.get(idx))
|
||||
: Specification.where(result)
|
||||
.and(specs.get(idx));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public Specification<U> build(Deque<?> postFixedExprStack, Function<SpecSearchCriteria, Specification<U>> converter) {
|
||||
|
||||
Deque<Specification<U>> specStack = new LinkedList<>();
|
||||
|
||||
Collections.reverse((List<?>) postFixedExprStack);
|
||||
|
||||
while (!postFixedExprStack.isEmpty()) {
|
||||
Object mayBeOperand = postFixedExprStack.pop();
|
||||
|
||||
if (!(mayBeOperand instanceof String)) {
|
||||
specStack.push(converter.apply((SpecSearchCriteria) mayBeOperand));
|
||||
} else {
|
||||
Specification<U> operand1 = specStack.pop();
|
||||
Specification<U> operand2 = specStack.pop();
|
||||
if (mayBeOperand.equals(SearchOperation.AND_OPERATOR))
|
||||
specStack.push(Specification.where(operand1)
|
||||
.and(operand2));
|
||||
else if (mayBeOperand.equals(SearchOperation.OR_OPERATOR))
|
||||
specStack.push(Specification.where(operand1)
|
||||
.or(operand2));
|
||||
}
|
||||
|
||||
}
|
||||
return specStack.pop();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
package com.baeldung.persistence.dao;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Deque;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
|
||||
import com.baeldung.web.util.SearchOperation;
|
||||
import com.baeldung.web.util.SpecSearchCriteria;
|
||||
|
||||
public class GenericSpecificationsBuilder<U> {
|
||||
|
||||
private final List<SpecSearchCriteria> params;
|
||||
|
||||
public GenericSpecificationsBuilder() {
|
||||
this.params = new ArrayList<>();
|
||||
}
|
||||
|
||||
public final GenericSpecificationsBuilder<U> with(final String key, final String operation, final Object value, final String prefix, final String suffix) {
|
||||
return with(null, key, operation, value, prefix, suffix);
|
||||
}
|
||||
|
||||
public final GenericSpecificationsBuilder<U> with(final String precedenceIndicator, final String key, final String operation, final Object value, final String prefix, final String suffix) {
|
||||
SearchOperation op = SearchOperation.getSimpleOperation(operation.charAt(0));
|
||||
if (op != null) {
|
||||
if (op == SearchOperation.EQUALITY) // the operation may be complex operation
|
||||
{
|
||||
final boolean startWithAsterisk = prefix != null && prefix.contains(SearchOperation.ZERO_OR_MORE_REGEX);
|
||||
final boolean endWithAsterisk = suffix != null && suffix.contains(SearchOperation.ZERO_OR_MORE_REGEX);
|
||||
|
||||
if (startWithAsterisk && endWithAsterisk) {
|
||||
op = SearchOperation.CONTAINS;
|
||||
} else if (startWithAsterisk) {
|
||||
op = SearchOperation.ENDS_WITH;
|
||||
} else if (endWithAsterisk) {
|
||||
op = SearchOperation.STARTS_WITH;
|
||||
}
|
||||
}
|
||||
params.add(new SpecSearchCriteria(precedenceIndicator, key, op, value));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public Specification<U> build(Function<SpecSearchCriteria, Specification<U>> converter) {
|
||||
|
||||
if (params.size() == 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final List<Specification<U>> specs = params.stream()
|
||||
.map(converter)
|
||||
.collect(Collectors.toCollection(ArrayList::new));
|
||||
|
||||
Specification<U> result = specs.get(0);
|
||||
|
||||
for (int idx = 1; idx < specs.size(); idx++) {
|
||||
result = params.get(idx)
|
||||
.isOrPredicate()
|
||||
? Specification.where(result)
|
||||
.or(specs.get(idx))
|
||||
: Specification.where(result)
|
||||
.and(specs.get(idx));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public Specification<U> build(Deque<?> postFixedExprStack, Function<SpecSearchCriteria, Specification<U>> converter) {
|
||||
|
||||
Deque<Specification<U>> specStack = new LinkedList<>();
|
||||
|
||||
Collections.reverse((List<?>) postFixedExprStack);
|
||||
|
||||
while (!postFixedExprStack.isEmpty()) {
|
||||
Object mayBeOperand = postFixedExprStack.pop();
|
||||
|
||||
if (!(mayBeOperand instanceof String)) {
|
||||
specStack.push(converter.apply((SpecSearchCriteria) mayBeOperand));
|
||||
} else {
|
||||
Specification<U> operand1 = specStack.pop();
|
||||
Specification<U> operand2 = specStack.pop();
|
||||
if (mayBeOperand.equals(SearchOperation.AND_OPERATOR))
|
||||
specStack.push(Specification.where(operand1)
|
||||
.and(operand2));
|
||||
else if (mayBeOperand.equals(SearchOperation.OR_OPERATOR))
|
||||
specStack.push(Specification.where(operand1)
|
||||
.or(operand2));
|
||||
}
|
||||
|
||||
}
|
||||
return specStack.pop();
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,70 +1,70 @@
|
||||
package com.baeldung.persistence.dao;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
|
||||
import com.baeldung.persistence.model.User;
|
||||
import com.baeldung.web.util.SearchOperation;
|
||||
import com.baeldung.web.util.SpecSearchCriteria;
|
||||
|
||||
public final class UserSpecificationsBuilder {
|
||||
|
||||
private final List<SpecSearchCriteria> params;
|
||||
|
||||
public UserSpecificationsBuilder() {
|
||||
params = new ArrayList<>();
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
public final UserSpecificationsBuilder with(final String key, final String operation, final Object value, final String prefix, final String suffix) {
|
||||
return with(null, key, operation, value, prefix, suffix);
|
||||
}
|
||||
|
||||
public final UserSpecificationsBuilder with(final String orPredicate, final String key, final String operation, final Object value, final String prefix, final String suffix) {
|
||||
SearchOperation op = SearchOperation.getSimpleOperation(operation.charAt(0));
|
||||
if (op != null) {
|
||||
if (op == SearchOperation.EQUALITY) { // the operation may be complex operation
|
||||
final boolean startWithAsterisk = prefix != null && prefix.contains(SearchOperation.ZERO_OR_MORE_REGEX);
|
||||
final boolean endWithAsterisk = suffix != null && suffix.contains(SearchOperation.ZERO_OR_MORE_REGEX);
|
||||
|
||||
if (startWithAsterisk && endWithAsterisk) {
|
||||
op = SearchOperation.CONTAINS;
|
||||
} else if (startWithAsterisk) {
|
||||
op = SearchOperation.ENDS_WITH;
|
||||
} else if (endWithAsterisk) {
|
||||
op = SearchOperation.STARTS_WITH;
|
||||
}
|
||||
}
|
||||
params.add(new SpecSearchCriteria(orPredicate, key, op, value));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public Specification<User> build() {
|
||||
if (params.size() == 0)
|
||||
return null;
|
||||
|
||||
Specification<User> result = new UserSpecification(params.get(0));
|
||||
|
||||
for (int i = 1; i < params.size(); i++) {
|
||||
result = params.get(i).isOrPredicate()
|
||||
? Specification.where(result).or(new UserSpecification(params.get(i)))
|
||||
: Specification.where(result).and(new UserSpecification(params.get(i)));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public final UserSpecificationsBuilder with(UserSpecification spec) {
|
||||
params.add(spec.getCriteria());
|
||||
return this;
|
||||
}
|
||||
|
||||
public final UserSpecificationsBuilder with(SpecSearchCriteria criteria) {
|
||||
params.add(criteria);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
package com.baeldung.persistence.dao;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
|
||||
import com.baeldung.persistence.model.User;
|
||||
import com.baeldung.web.util.SearchOperation;
|
||||
import com.baeldung.web.util.SpecSearchCriteria;
|
||||
|
||||
public final class UserSpecificationsBuilder {
|
||||
|
||||
private final List<SpecSearchCriteria> params;
|
||||
|
||||
public UserSpecificationsBuilder() {
|
||||
params = new ArrayList<>();
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
public final UserSpecificationsBuilder with(final String key, final String operation, final Object value, final String prefix, final String suffix) {
|
||||
return with(null, key, operation, value, prefix, suffix);
|
||||
}
|
||||
|
||||
public final UserSpecificationsBuilder with(final String orPredicate, final String key, final String operation, final Object value, final String prefix, final String suffix) {
|
||||
SearchOperation op = SearchOperation.getSimpleOperation(operation.charAt(0));
|
||||
if (op != null) {
|
||||
if (op == SearchOperation.EQUALITY) { // the operation may be complex operation
|
||||
final boolean startWithAsterisk = prefix != null && prefix.contains(SearchOperation.ZERO_OR_MORE_REGEX);
|
||||
final boolean endWithAsterisk = suffix != null && suffix.contains(SearchOperation.ZERO_OR_MORE_REGEX);
|
||||
|
||||
if (startWithAsterisk && endWithAsterisk) {
|
||||
op = SearchOperation.CONTAINS;
|
||||
} else if (startWithAsterisk) {
|
||||
op = SearchOperation.ENDS_WITH;
|
||||
} else if (endWithAsterisk) {
|
||||
op = SearchOperation.STARTS_WITH;
|
||||
}
|
||||
}
|
||||
params.add(new SpecSearchCriteria(orPredicate, key, op, value));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public Specification<User> build() {
|
||||
if (params.size() == 0)
|
||||
return null;
|
||||
|
||||
Specification<User> result = new UserSpecification(params.get(0));
|
||||
|
||||
for (int i = 1; i < params.size(); i++) {
|
||||
result = params.get(i).isOrPredicate()
|
||||
? Specification.where(result).or(new UserSpecification(params.get(i)))
|
||||
: Specification.where(result).and(new UserSpecification(params.get(i)));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public final UserSpecificationsBuilder with(UserSpecification spec) {
|
||||
params.add(spec.getCriteria());
|
||||
return this;
|
||||
}
|
||||
|
||||
public final UserSpecificationsBuilder with(SpecSearchCriteria criteria) {
|
||||
params.add(criteria);
|
||||
return this;
|
||||
}
|
||||
}
|
@ -1,171 +1,171 @@
|
||||
package com.baeldung.web.controller;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.data.querydsl.binding.QuerydslPredicate;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
|
||||
import com.baeldung.persistence.dao.GenericSpecificationsBuilder;
|
||||
import com.baeldung.persistence.dao.IUserDAO;
|
||||
import com.baeldung.persistence.dao.MyUserPredicatesBuilder;
|
||||
import com.baeldung.persistence.dao.MyUserRepository;
|
||||
import com.baeldung.persistence.dao.UserRepository;
|
||||
import com.baeldung.persistence.dao.UserSpecification;
|
||||
import com.baeldung.persistence.dao.UserSpecificationsBuilder;
|
||||
import com.baeldung.persistence.dao.rsql.CustomRsqlVisitor;
|
||||
import com.baeldung.persistence.model.MyUser;
|
||||
import com.baeldung.persistence.model.User;
|
||||
import com.baeldung.web.util.CriteriaParser;
|
||||
import com.baeldung.web.util.SearchCriteria;
|
||||
import com.baeldung.web.util.SearchOperation;
|
||||
import com.google.common.base.Joiner;
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.querydsl.core.types.Predicate;
|
||||
import com.querydsl.core.types.dsl.BooleanExpression;
|
||||
|
||||
import cz.jirutka.rsql.parser.RSQLParser;
|
||||
import cz.jirutka.rsql.parser.ast.Node;
|
||||
|
||||
//@EnableSpringDataWebSupport
|
||||
@Controller
|
||||
@RequestMapping(value = "/auth/")
|
||||
public class UserController {
|
||||
|
||||
@Autowired
|
||||
private IUserDAO service;
|
||||
|
||||
@Autowired
|
||||
private UserRepository dao;
|
||||
|
||||
@Autowired
|
||||
private MyUserRepository myUserRepository;
|
||||
|
||||
public UserController() {
|
||||
super();
|
||||
}
|
||||
|
||||
// API - READ
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/users")
|
||||
@ResponseBody
|
||||
public List<User> search(@RequestParam(value = "search", required = false) String search) {
|
||||
List<SearchCriteria> params = new ArrayList<SearchCriteria>();
|
||||
if (search != null) {
|
||||
Pattern pattern = Pattern.compile("(\\w+?)(:|<|>)(\\w+?),");
|
||||
Matcher matcher = pattern.matcher(search + ",");
|
||||
while (matcher.find()) {
|
||||
params.add(new SearchCriteria(matcher.group(1), matcher.group(2), matcher.group(3)));
|
||||
}
|
||||
}
|
||||
return service.searchUser(params);
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/users/spec")
|
||||
@ResponseBody
|
||||
public List<User> findAllBySpecification(@RequestParam(value = "search") String search) {
|
||||
UserSpecificationsBuilder builder = new UserSpecificationsBuilder();
|
||||
String operationSetExper = Joiner.on("|")
|
||||
.join(SearchOperation.SIMPLE_OPERATION_SET);
|
||||
Pattern pattern = Pattern.compile("(\\w+?)(" + operationSetExper + ")(\\p{Punct}?)(\\w+?)(\\p{Punct}?),");
|
||||
Matcher matcher = pattern.matcher(search + ",");
|
||||
while (matcher.find()) {
|
||||
builder.with(matcher.group(1), matcher.group(2), matcher.group(4), matcher.group(3), matcher.group(5));
|
||||
}
|
||||
|
||||
Specification<User> spec = builder.build();
|
||||
return dao.findAll(spec);
|
||||
}
|
||||
|
||||
@GetMapping(value = "/users/espec")
|
||||
@ResponseBody
|
||||
public List<User> findAllByOrPredicate(@RequestParam(value = "search") String search) {
|
||||
Specification<User> spec = resolveSpecification(search);
|
||||
return dao.findAll(spec);
|
||||
}
|
||||
|
||||
@GetMapping(value = "/users/spec/adv")
|
||||
@ResponseBody
|
||||
public List<User> findAllByAdvPredicate(@RequestParam(value = "search") String search) {
|
||||
Specification<User> spec = resolveSpecificationFromInfixExpr(search);
|
||||
return dao.findAll(spec);
|
||||
}
|
||||
|
||||
protected Specification<User> resolveSpecificationFromInfixExpr(String searchParameters) {
|
||||
CriteriaParser parser = new CriteriaParser();
|
||||
GenericSpecificationsBuilder<User> specBuilder = new GenericSpecificationsBuilder<>();
|
||||
return specBuilder.build(parser.parse(searchParameters), UserSpecification::new);
|
||||
}
|
||||
|
||||
protected Specification<User> resolveSpecification(String searchParameters) {
|
||||
|
||||
UserSpecificationsBuilder builder = new UserSpecificationsBuilder();
|
||||
String operationSetExper = Joiner.on("|")
|
||||
.join(SearchOperation.SIMPLE_OPERATION_SET);
|
||||
Pattern pattern = Pattern.compile("(\\p{Punct}?)(\\w+?)(" + operationSetExper + ")(\\p{Punct}?)(\\w+?)(\\p{Punct}?),");
|
||||
Matcher matcher = pattern.matcher(searchParameters + ",");
|
||||
while (matcher.find()) {
|
||||
builder.with(matcher.group(1), matcher.group(2), matcher.group(3), matcher.group(5), matcher.group(4), matcher.group(6));
|
||||
}
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/myusers")
|
||||
@ResponseBody
|
||||
public Iterable<MyUser> findAllByQuerydsl(@RequestParam(value = "search") String search) {
|
||||
MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder();
|
||||
if (search != null) {
|
||||
Pattern pattern = Pattern.compile("(\\w+?)(:|<|>)(\\w+?),");
|
||||
Matcher matcher = pattern.matcher(search + ",");
|
||||
while (matcher.find()) {
|
||||
builder.with(matcher.group(1), matcher.group(2), matcher.group(3));
|
||||
}
|
||||
}
|
||||
BooleanExpression exp = builder.build();
|
||||
return myUserRepository.findAll(exp);
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/users/rsql")
|
||||
@ResponseBody
|
||||
public List<User> findAllByRsql(@RequestParam(value = "search") String search) {
|
||||
Node rootNode = new RSQLParser().parse(search);
|
||||
Specification<User> spec = rootNode.accept(new CustomRsqlVisitor<User>());
|
||||
return dao.findAll(spec);
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/api/myusers")
|
||||
@ResponseBody
|
||||
public Iterable<MyUser> findAllByWebQuerydsl(@QuerydslPredicate(root = MyUser.class) Predicate predicate) {
|
||||
return myUserRepository.findAll(predicate);
|
||||
}
|
||||
|
||||
// API - WRITE
|
||||
|
||||
@RequestMapping(method = RequestMethod.POST, value = "/users")
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
public void create(@RequestBody User resource) {
|
||||
Preconditions.checkNotNull(resource);
|
||||
dao.save(resource);
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.POST, value = "/myusers")
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
public void addMyUser(@RequestBody MyUser resource) {
|
||||
Preconditions.checkNotNull(resource);
|
||||
myUserRepository.save(resource);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
package com.baeldung.web.controller;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.data.querydsl.binding.QuerydslPredicate;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
|
||||
import com.baeldung.persistence.dao.GenericSpecificationsBuilder;
|
||||
import com.baeldung.persistence.dao.IUserDAO;
|
||||
import com.baeldung.persistence.dao.MyUserPredicatesBuilder;
|
||||
import com.baeldung.persistence.dao.MyUserRepository;
|
||||
import com.baeldung.persistence.dao.UserRepository;
|
||||
import com.baeldung.persistence.dao.UserSpecification;
|
||||
import com.baeldung.persistence.dao.UserSpecificationsBuilder;
|
||||
import com.baeldung.persistence.dao.rsql.CustomRsqlVisitor;
|
||||
import com.baeldung.persistence.model.MyUser;
|
||||
import com.baeldung.persistence.model.User;
|
||||
import com.baeldung.web.util.CriteriaParser;
|
||||
import com.baeldung.web.util.SearchCriteria;
|
||||
import com.baeldung.web.util.SearchOperation;
|
||||
import com.google.common.base.Joiner;
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.querydsl.core.types.Predicate;
|
||||
import com.querydsl.core.types.dsl.BooleanExpression;
|
||||
|
||||
import cz.jirutka.rsql.parser.RSQLParser;
|
||||
import cz.jirutka.rsql.parser.ast.Node;
|
||||
|
||||
//@EnableSpringDataWebSupport
|
||||
@Controller
|
||||
@RequestMapping(value = "/auth/")
|
||||
public class UserController {
|
||||
|
||||
@Autowired
|
||||
private IUserDAO service;
|
||||
|
||||
@Autowired
|
||||
private UserRepository dao;
|
||||
|
||||
@Autowired
|
||||
private MyUserRepository myUserRepository;
|
||||
|
||||
public UserController() {
|
||||
super();
|
||||
}
|
||||
|
||||
// API - READ
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/users")
|
||||
@ResponseBody
|
||||
public List<User> search(@RequestParam(value = "search", required = false) String search) {
|
||||
List<SearchCriteria> params = new ArrayList<SearchCriteria>();
|
||||
if (search != null) {
|
||||
Pattern pattern = Pattern.compile("(\\w+?)(:|<|>)(\\w+?),");
|
||||
Matcher matcher = pattern.matcher(search + ",");
|
||||
while (matcher.find()) {
|
||||
params.add(new SearchCriteria(matcher.group(1), matcher.group(2), matcher.group(3)));
|
||||
}
|
||||
}
|
||||
return service.searchUser(params);
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/users/spec")
|
||||
@ResponseBody
|
||||
public List<User> findAllBySpecification(@RequestParam(value = "search") String search) {
|
||||
UserSpecificationsBuilder builder = new UserSpecificationsBuilder();
|
||||
String operationSetExper = Joiner.on("|")
|
||||
.join(SearchOperation.SIMPLE_OPERATION_SET);
|
||||
Pattern pattern = Pattern.compile("(\\w+?)(" + operationSetExper + ")(\\p{Punct}?)(\\w+?)(\\p{Punct}?),");
|
||||
Matcher matcher = pattern.matcher(search + ",");
|
||||
while (matcher.find()) {
|
||||
builder.with(matcher.group(1), matcher.group(2), matcher.group(4), matcher.group(3), matcher.group(5));
|
||||
}
|
||||
|
||||
Specification<User> spec = builder.build();
|
||||
return dao.findAll(spec);
|
||||
}
|
||||
|
||||
@GetMapping(value = "/users/espec")
|
||||
@ResponseBody
|
||||
public List<User> findAllByOrPredicate(@RequestParam(value = "search") String search) {
|
||||
Specification<User> spec = resolveSpecification(search);
|
||||
return dao.findAll(spec);
|
||||
}
|
||||
|
||||
@GetMapping(value = "/users/spec/adv")
|
||||
@ResponseBody
|
||||
public List<User> findAllByAdvPredicate(@RequestParam(value = "search") String search) {
|
||||
Specification<User> spec = resolveSpecificationFromInfixExpr(search);
|
||||
return dao.findAll(spec);
|
||||
}
|
||||
|
||||
protected Specification<User> resolveSpecificationFromInfixExpr(String searchParameters) {
|
||||
CriteriaParser parser = new CriteriaParser();
|
||||
GenericSpecificationsBuilder<User> specBuilder = new GenericSpecificationsBuilder<>();
|
||||
return specBuilder.build(parser.parse(searchParameters), UserSpecification::new);
|
||||
}
|
||||
|
||||
protected Specification<User> resolveSpecification(String searchParameters) {
|
||||
|
||||
UserSpecificationsBuilder builder = new UserSpecificationsBuilder();
|
||||
String operationSetExper = Joiner.on("|")
|
||||
.join(SearchOperation.SIMPLE_OPERATION_SET);
|
||||
Pattern pattern = Pattern.compile("(\\p{Punct}?)(\\w+?)(" + operationSetExper + ")(\\p{Punct}?)(\\w+?)(\\p{Punct}?),");
|
||||
Matcher matcher = pattern.matcher(searchParameters + ",");
|
||||
while (matcher.find()) {
|
||||
builder.with(matcher.group(1), matcher.group(2), matcher.group(3), matcher.group(5), matcher.group(4), matcher.group(6));
|
||||
}
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/myusers")
|
||||
@ResponseBody
|
||||
public Iterable<MyUser> findAllByQuerydsl(@RequestParam(value = "search") String search) {
|
||||
MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder();
|
||||
if (search != null) {
|
||||
Pattern pattern = Pattern.compile("(\\w+?)(:|<|>)(\\w+?),");
|
||||
Matcher matcher = pattern.matcher(search + ",");
|
||||
while (matcher.find()) {
|
||||
builder.with(matcher.group(1), matcher.group(2), matcher.group(3));
|
||||
}
|
||||
}
|
||||
BooleanExpression exp = builder.build();
|
||||
return myUserRepository.findAll(exp);
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/users/rsql")
|
||||
@ResponseBody
|
||||
public List<User> findAllByRsql(@RequestParam(value = "search") String search) {
|
||||
Node rootNode = new RSQLParser().parse(search);
|
||||
Specification<User> spec = rootNode.accept(new CustomRsqlVisitor<User>());
|
||||
return dao.findAll(spec);
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/api/myusers")
|
||||
@ResponseBody
|
||||
public Iterable<MyUser> findAllByWebQuerydsl(@QuerydslPredicate(root = MyUser.class) Predicate predicate) {
|
||||
return myUserRepository.findAll(predicate);
|
||||
}
|
||||
|
||||
// API - WRITE
|
||||
|
||||
@RequestMapping(method = RequestMethod.POST, value = "/users")
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
public void create(@RequestBody User resource) {
|
||||
Preconditions.checkNotNull(resource);
|
||||
dao.save(resource);
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.POST, value = "/myusers")
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
public void addMyUser(@RequestBody MyUser resource) {
|
||||
Preconditions.checkNotNull(resource);
|
||||
myUserRepository.save(resource);
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,36 +1,36 @@
|
||||
package com.baeldung.web.util;
|
||||
|
||||
public enum SearchOperation {
|
||||
EQUALITY, NEGATION, GREATER_THAN, LESS_THAN, LIKE, STARTS_WITH, ENDS_WITH, CONTAINS;
|
||||
|
||||
public static final String[] SIMPLE_OPERATION_SET = { ":", "!", ">", "<", "~" };
|
||||
|
||||
public static final String OR_PREDICATE_FLAG = "'";
|
||||
|
||||
public static final String ZERO_OR_MORE_REGEX = "*";
|
||||
|
||||
public static final String OR_OPERATOR = "OR";
|
||||
|
||||
public static final String AND_OPERATOR = "AND";
|
||||
|
||||
public static final String LEFT_PARANTHESIS = "(";
|
||||
|
||||
public static final String RIGHT_PARANTHESIS = ")";
|
||||
|
||||
public static SearchOperation getSimpleOperation(final char input) {
|
||||
switch (input) {
|
||||
case ':':
|
||||
return EQUALITY;
|
||||
case '!':
|
||||
return NEGATION;
|
||||
case '>':
|
||||
return GREATER_THAN;
|
||||
case '<':
|
||||
return LESS_THAN;
|
||||
case '~':
|
||||
return LIKE;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
package com.baeldung.web.util;
|
||||
|
||||
public enum SearchOperation {
|
||||
EQUALITY, NEGATION, GREATER_THAN, LESS_THAN, LIKE, STARTS_WITH, ENDS_WITH, CONTAINS;
|
||||
|
||||
public static final String[] SIMPLE_OPERATION_SET = { ":", "!", ">", "<", "~" };
|
||||
|
||||
public static final String OR_PREDICATE_FLAG = "'";
|
||||
|
||||
public static final String ZERO_OR_MORE_REGEX = "*";
|
||||
|
||||
public static final String OR_OPERATOR = "OR";
|
||||
|
||||
public static final String AND_OPERATOR = "AND";
|
||||
|
||||
public static final String LEFT_PARANTHESIS = "(";
|
||||
|
||||
public static final String RIGHT_PARANTHESIS = ")";
|
||||
|
||||
public static SearchOperation getSimpleOperation(final char input) {
|
||||
switch (input) {
|
||||
case ':':
|
||||
return EQUALITY;
|
||||
case '!':
|
||||
return NEGATION;
|
||||
case '>':
|
||||
return GREATER_THAN;
|
||||
case '<':
|
||||
return LESS_THAN;
|
||||
case '~':
|
||||
return LIKE;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,82 +1,82 @@
|
||||
package com.baeldung.web.util;
|
||||
|
||||
public class SpecSearchCriteria {
|
||||
|
||||
private String key;
|
||||
private SearchOperation operation;
|
||||
private Object value;
|
||||
private boolean orPredicate;
|
||||
|
||||
public SpecSearchCriteria() {
|
||||
|
||||
}
|
||||
|
||||
public SpecSearchCriteria(final String key, final SearchOperation operation, final Object value) {
|
||||
super();
|
||||
this.key = key;
|
||||
this.operation = operation;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public SpecSearchCriteria(final String orPredicate, final String key, final SearchOperation operation, final Object value) {
|
||||
super();
|
||||
this.orPredicate = orPredicate != null && orPredicate.equals(SearchOperation.OR_PREDICATE_FLAG);
|
||||
this.key = key;
|
||||
this.operation = operation;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public SpecSearchCriteria(String key, String operation, String prefix, String value, String suffix) {
|
||||
SearchOperation op = SearchOperation.getSimpleOperation(operation.charAt(0));
|
||||
if (op != null) {
|
||||
if (op == SearchOperation.EQUALITY) { // the operation may be complex operation
|
||||
final boolean startWithAsterisk = prefix != null && prefix.contains(SearchOperation.ZERO_OR_MORE_REGEX);
|
||||
final boolean endWithAsterisk = suffix != null && suffix.contains(SearchOperation.ZERO_OR_MORE_REGEX);
|
||||
|
||||
if (startWithAsterisk && endWithAsterisk) {
|
||||
op = SearchOperation.CONTAINS;
|
||||
} else if (startWithAsterisk) {
|
||||
op = SearchOperation.ENDS_WITH;
|
||||
} else if (endWithAsterisk) {
|
||||
op = SearchOperation.STARTS_WITH;
|
||||
}
|
||||
}
|
||||
}
|
||||
this.key = key;
|
||||
this.operation = op;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
public void setKey(final String key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
public SearchOperation getOperation() {
|
||||
return operation;
|
||||
}
|
||||
|
||||
public void setOperation(final SearchOperation operation) {
|
||||
this.operation = operation;
|
||||
}
|
||||
|
||||
public Object getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(final Object value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public boolean isOrPredicate() {
|
||||
return orPredicate;
|
||||
}
|
||||
|
||||
public void setOrPredicate(boolean orPredicate) {
|
||||
this.orPredicate = orPredicate;
|
||||
}
|
||||
|
||||
}
|
||||
package com.baeldung.web.util;
|
||||
|
||||
public class SpecSearchCriteria {
|
||||
|
||||
private String key;
|
||||
private SearchOperation operation;
|
||||
private Object value;
|
||||
private boolean orPredicate;
|
||||
|
||||
public SpecSearchCriteria() {
|
||||
|
||||
}
|
||||
|
||||
public SpecSearchCriteria(final String key, final SearchOperation operation, final Object value) {
|
||||
super();
|
||||
this.key = key;
|
||||
this.operation = operation;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public SpecSearchCriteria(final String orPredicate, final String key, final SearchOperation operation, final Object value) {
|
||||
super();
|
||||
this.orPredicate = orPredicate != null && orPredicate.equals(SearchOperation.OR_PREDICATE_FLAG);
|
||||
this.key = key;
|
||||
this.operation = operation;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public SpecSearchCriteria(String key, String operation, String prefix, String value, String suffix) {
|
||||
SearchOperation op = SearchOperation.getSimpleOperation(operation.charAt(0));
|
||||
if (op != null) {
|
||||
if (op == SearchOperation.EQUALITY) { // the operation may be complex operation
|
||||
final boolean startWithAsterisk = prefix != null && prefix.contains(SearchOperation.ZERO_OR_MORE_REGEX);
|
||||
final boolean endWithAsterisk = suffix != null && suffix.contains(SearchOperation.ZERO_OR_MORE_REGEX);
|
||||
|
||||
if (startWithAsterisk && endWithAsterisk) {
|
||||
op = SearchOperation.CONTAINS;
|
||||
} else if (startWithAsterisk) {
|
||||
op = SearchOperation.ENDS_WITH;
|
||||
} else if (endWithAsterisk) {
|
||||
op = SearchOperation.STARTS_WITH;
|
||||
}
|
||||
}
|
||||
}
|
||||
this.key = key;
|
||||
this.operation = op;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
public void setKey(final String key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
public SearchOperation getOperation() {
|
||||
return operation;
|
||||
}
|
||||
|
||||
public void setOperation(final SearchOperation operation) {
|
||||
this.operation = operation;
|
||||
}
|
||||
|
||||
public Object getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(final Object value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public boolean isOrPredicate() {
|
||||
return orPredicate;
|
||||
}
|
||||
|
||||
public void setOrPredicate(boolean orPredicate) {
|
||||
this.orPredicate = orPredicate;
|
||||
}
|
||||
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user